import React, { useEffect, useState } from 'react'; import Portal from '@/components/elements/Portal'; import Button from '@/components/elements/Button'; import { TextArea, TextInput } from '@/components/form/controls'; import copyToClipboard from '@/lib/clipboard'; import { cn } from '@/lib/cn'; interface CopyOnClickProps { text: string | number | null | undefined; showInNotification?: boolean; children: React.ReactNode; } interface ClickableChildProps { className?: string; role?: React.AriaRole; tabIndex?: number; onClick?: React.MouseEventHandler; onKeyDown?: React.KeyboardEventHandler; } type CopyStatus = 'copied' | 'failed'; const interactiveTypes = new Set([ 'a', 'button', 'input', 'select', 'textarea', Button, Button.Text, Button.Danger, TextInput, TextArea, ]); /** Copies `text` when its single child is clicked; a non-interactive child becomes a focusable button. */ const CopyOnClick = ({ text, showInNotification = false, children }: CopyOnClickProps) => { const [status, setStatus] = useState(null); useEffect(() => { if (!status) return; const timeout = setTimeout(() => { setStatus(null); }, 2500); return () => { clearTimeout(timeout); }; }, [status]); if (!React.isValidElement(children)) { throw new Error('Component passed to must be a valid React element.'); } const element = React.Children.only(children); const copy = () => { void copyToClipboard(String(text)).then((copied) => setStatus(copied ? 'copied' : 'failed')); }; const interactive = interactiveTypes.has(element.type); const onKeyDown = (e: React.KeyboardEvent) => { element.props.onKeyDown?.(e); if (!e.defaultPrevented && e.target === e.currentTarget && (e.key === 'Enter' || e.key === ' ')) { e.preventDefault(); copy(); } }; const child = !text ? element : React.cloneElement(element, { role: interactive ? element.props.role : (element.props.role ?? 'button'), tabIndex: interactive ? element.props.tabIndex : (element.props.tabIndex ?? 0), onKeyDown: interactive ? element.props.onKeyDown : onKeyDown, className: cn(element.props.className || '', 'cursor-pointer'), onClick: (e: React.MouseEvent) => { copy(); element.props.onClick?.(e); }, }); return ( <> {status && (

{status === 'failed' ? 'Unable to copy text to clipboard.' : showInNotification ? `Copied "${String(text)}" to clipboard.` : 'Copied text to clipboard.'}

)} {child} ); }; export default CopyOnClick;