useClipboard
useClipboard copies a string to the clipboard and exposes hasCopied, which stays true for timeout ms after
a successful copy. It tries the async Clipboard API first and falls back to document.execCommand('copy').Hooks
Signature
const { onCopy, hasCopied } = useClipboard(text: string, timeout?: number);Name | Type | Description |
|---|---|---|
text | string | The text onCopy writes. |
timeout | number | Milliseconds hasCopied stays true. Default 2000. |
onCopy | () => void | Copies text. Call it from a user gesture, such as a click. |
hasCopied | boolean | true after a successful copy, until timeout elapses. |
Scrolls sideways inside the table. The page does not.
Example
import { Button } from '@apollion-dsi/core/elements/button';
import { useClipboard } from '@apollion-dsi/core/hooks';
function CopyId({ id }: { id: string }) {
const { onCopy, hasCopied } = useClipboard(id);
return <Button size="small" variant="outlined" onClick={onCopy} text={hasCopied ? 'Copied' : 'Copy ID'} />;
}Pitfalls
- Copies plain text only. For images or several formats, call the Clipboard API directly.
- When both strategies fail,
hasCopiedstaysfalseand the error is logged to the console; there is no error callback. - Changing the button label is not announced by every screen reader. Add a polite live region if the confirmation matters.