diff --git a/src/components/Clipboard.tsx b/src/components/Clipboard.tsx new file mode 100644 index 0000000..691124a --- /dev/null +++ b/src/components/Clipboard.tsx @@ -0,0 +1,49 @@ +import { useCopyToClipboard } from '@wordpress/compose'; +import { useState, useEffect } from '@wordpress/element'; +import { Button } from '@wordpress/components'; + +type CSSStyle = Record; + +const Clipboard = ({ + text, + style, +}: { + text: string; + style: CSSStyle; +}): JSX.Element => { + const [hasCopied, setHasCopied] = useState(false); + + useEffect(() => { + let clipTimeout: NodeJS.Timeout | null = null; + + if (hasCopied) { + clipTimeout = setTimeout(() => { + setHasCopied(false); + }, 4000); + } + + return () => { + if (clipTimeout) { + window.clearTimeout(clipTimeout); + } + }; + }, [hasCopied]); + + const onCopy = () => setHasCopied(true); + + const clipRef = useCopyToClipboard(text, onCopy); + + return ( + <> +