Skip to main content

Function: useClipboard()

useClipboard(options?): UseClipboardReturn

Defined in: use-clipboard.ts:74

Copies text to the clipboard with the async Clipboard API and exposes a temporary copied flag, handy for "Copied!" feedback.

copy never rejects: it resolves true on success and false on failure, and stores the failure in error. Failure covers an unavailable Clipboard API (unsupported browser, insecure context, or server rendering) and a rejected write (for example a denied permission). A newer copy call supersedes an older one that is still pending. The timeout is cleared on unmount, and no state is updated after unmount.

The Clipboard API requires a secure context (HTTPS or localhost), and most browsers only allow writes from a user gesture such as a click.

Parameters​

options?​

UseClipboardOptions

Optional behavior settings.

Returns​

UseClipboardReturn

The copy function, the copied and error state, and reset.

Examples​

const { copy, copied } = useClipboard();

return (
<button onClick={() => copy("npm i @altalyst/hookify")}>
{copied ? "Copied!" : "Copy"}
</button>
);
// Custom timeout and callbacks
const { copy, error } = useClipboard({
timeout: 1000,
onCopy: (text) => analytics.track("copy", { length: text.length }),
onError: (err) => toast.error(err.message),
});

For a live, editable example, see the useClipboard docs page.