Skip to content

Instantly share code, notes, and snippets.

@surya76657
Forked from ryanflorence/copy-to-clipboard.js
Created October 25, 2020 03:12
Show Gist options
  • Select an option

  • Save surya76657/d190d34af1f9c0b398fbb3864fc7c309 to your computer and use it in GitHub Desktop.

Select an option

Save surya76657/d190d34af1f9c0b398fbb3864fc7c309 to your computer and use it in GitHub Desktop.
function CopyButton({ value }) {
let [copied, setCopied] = React.useState();
let hydrated = usePageIsHydrated();
React.useEffect(() => {
let id = setTimeout(() => setCopied(false), 2000);
return () => clearTimeout(id);
}, [copied]);
return (
<button
disabled={!hydrated}
onClick={() => {
navigator.clipboard.writeText(value);
setCopied(true);
}}
>
<ClipboardSvgIcon />
{copied ? "Copied!" : "Copy"}
</button>
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment