Clipboard permission
A paste button that is blocked fails with permission-denied. That error comes too late to
help: the user already pressed the button. useClipboardPermission tells you before, and
again whenever the user changes it, so you can show the right message or hide the button.
It only asks the browser for the state. It never reads the clipboard, never shows a prompt, and works over plain HTTP in the browsers that have the permission at all.
Try it
Section titled “Try it”What the browser says
- Reading (paste buttons)
- Writing (copy buttons)
Press the button to let this page try to read the clipboard. Your browser may ask. Then watch the first line change, and change it again in the address bar.
import { useClipboardPermission } from "react-smart-copy";
function PasteButton({ onPaste }: { onPaste: () => void }) { const { state } = useClipboardPermission("read"); if (state === "denied") { return <p>Clipboard access is blocked. Allow it in the address bar, then try again.</p>; } return <button onClick={onPaste}>Paste</button>;}The four states
Section titled “The four states”| State | Meaning |
|---|---|
granted |
The permission is held. No prompt will show. |
prompt |
The browser will ask the user when the page next uses the clipboard. |
denied |
Refused by the user or a policy. A page cannot ask again; only the user can change it. |
unsupported |
This browser cannot say. Not a refusal: the clipboard may well work. Show your button. |
useClipboardPermission also returns checking until the browser answers, including on the
server and on the first client render, so hydration always matches.
Browser support. Chromium-based browsers can answer for both read and write. Firefox
and Safari have no such permission to query, so they always say unsupported. Do not hide a
paste button on unsupported: only hide it on denied.
Without React
Section titled “Without React”import { readClipboardPermission, watchClipboardPermission } from "react-smart-copy/core";
const state = await readClipboardPermission("read"); // 'granted' | 'prompt' | 'denied' | 'unsupported'
const stop = watchClipboardPermission("read", (state) => { // Called now, asynchronously, and again each time the user changes it.});stop(); // Stopping before the first answer arrives leaves nothing behind.readClipboardPermission never rejects for a browser that cannot tell. Both functions throw a
CopyFailure of type invalid-payload for a name other than 'read' or 'write', and nothing
else ever reaches the browser. An error thrown by your callback is reported, not swallowed.
- A permission that is
grantedcan still fail: a page that is not focused, or a paste with nothing on the clipboard. Handle the result of the paste as before. - Keyboard paste (Ctrl/⌘+V) needs no permission at all. See usePaste.