security-checklist/components/Button/CopyLinkButton.js
2019-02-06 15:02:13 +01:00

37 lines
933 B
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// @flow
// $FlowIssue
import React, { useState } from 'react';
import { CopyLinkButton as StyledCopyLinkButton } from './style';
import Icon from '../Icon';
import type { ButtonProps } from './types';
type CopyLinkProps = {
...$Exact<ButtonProps>,
text: string,
};
export default function CopyLinkButton(props: CopyLinkProps) {
const { text, children } = props;
const [isClicked, handleClick] = useState(false);
const onClick = () => {
handleClick(true);
setTimeout(() => handleClick(false), 2000);
};
return (
<StyledCopyLinkButton
data-clipboard-text={text}
onSuccess={onClick}
component="button"
data-cy="copy-link-button"
isClicked={isClicked}
aria-label="Copy the websites address to your clipboard."
type="button"
{...props}
>
<Icon glyph="link" size={24} />
{isClicked ? 'Copied!' : children}
</StyledCopyLinkButton>
);
}