useCopyToClipboard
Writes a string to the system clipboard and exposes a copied flag that auto-resets after resetAfter ms — perfect for “Copied!” UI affordances. Falls back to document.execCommand('copy') in non-secure contexts (e.g. plain HTTP, older browsers).
import { useCopyToClipboard } from '@wire-ui/react'
function CopyButton({ text }: { text: string }) {
const { copy, copied } = useCopyToClipboard({ resetAfter: 2000 })
return (
<button onClick={() => copy(text)} data-copied={copied}>
{copied ? 'Copied!' : 'Copy'}
</button>
)
}Options
| Option | Type | Default | Description |
|---|---|---|---|
resetAfter | number | 2000 | Milliseconds before copied resets to false. Pass 0 to disable auto-reset. |
Returns
| Key | Type | Description |
|---|---|---|
copy | (text: string) => Promise<boolean> | Write text to the clipboard. Resolves with true on success. |
copied | boolean | true for resetAfter ms after a successful copy. |
value | string | null | The last text written, or null if nothing has been copied yet. |
error | Error | null | The last copy error, if any. |
reset | () => void | Clear copied, value, and error. |
Last updated on