useClipboard
Hook for copying textual content to the clipboard. Tries the modern
Clipboard API first and falls back to document.execCommand('copy').
Exposes a hasCopied signal that stays true for a few milliseconds after
a successful copy — ideal for temporarily switching a button label to
"Copied".
When to use
| ✅ Use when… | 🚫 Avoid when… |
|---|---|
|
|
Signature
const { onCopy, hasCopied } = useClipboard(text: string, timeout?: number);text: content to be copied.timeout: time (ms) thathasCopiedstaystrue. Default2000.
Example
import { Flex } from '@apollion-dsi/core/containers/flex';
import { Button } from '@apollion-dsi/core/elements/button';
import { TextArea } from '@apollion-dsi/core/form/text-area';
import { useClipboard } from '@apollion-dsi/core/hooks';
import { useState } from 'react';
export function UseClipboardExample() {
const [inputValue, setInputValue] = useState('Super important text too long to type manually');
const { onCopy, hasCopied } = useClipboard(inputValue);
return (
<Flex gap="large">
<TextArea size="compact" value={inputValue} onChange={(e) => setInputValue(e.target.value)} />
<Button size="extraSmall" variant="outlined" onClick={onCopy}>
{hasCopied ? 'Copied' : 'Copy'}
</Button>
<TextArea size="compact" placeholder="Paste here" />
</Flex>
);
}See also
- Full API:
useClipboard(reference generated from TSDoc).