import type { ComponentProps, ReactElement } from 'react' import { useCallback, useEffect, useState } from 'react' import { CheckIcon } from './check' import { CopyIcon } from './copy' import { Button } from './button' export const CopyToClipboard = ({ getValue, ...props }: { getValue: () => string } & ComponentProps<'button'>): ReactElement => { const [isCopied, setCopied] = useState(false) useEffect(() => { if (!isCopied) return const timerId = setTimeout(() => { setCopied(false) }, 2000) return () => { clearTimeout(timerId) } }, [isCopied]) const handleClick = useCallback< NonNullable['onClick']> >(async () => { setCopied(true) if (!navigator?.clipboard) { console.error('Access to clipboard rejected!') } try { await navigator.clipboard.writeText(getValue()) } catch { console.error('Failed to copy!') } }, [getValue]) const IconToUse = isCopied ? CheckIcon : CopyIcon return ( ) }