Native copy (Ctrl/Cmd+C)
The other copy pages copy from a button. useCopyInterceptor handles the other way people
copy: selecting text and pressing Ctrl/⌘+C, Ctrl/⌘+X or using the context menu. Inside the
element you attach it to, you decide what reaches the clipboard.
Try it
Section titled “Try it”Attribution Copies keep a link back to the source.
react-smart-copy is a headless clipboard library for React. It copies text, rich HTML and images, reads pastes and drops, and tells you exactly why a copy failed.
Redaction Secrets are masked, even when your selection starts above. Priority 10.
- Live key
- demo-live-9f2c41ab77e0
- Webhook secret
- demo-hook-5d18c0e2a9
Spreadsheet-ready Select whole rows; they paste into Excel or Sheets as cells.
| Invoice | Client | Amount |
|---|---|---|
| INV-29018 | DesignCo | ₹12,400 |
| INV-29019 | Northwind | ₹8,950 |
| INV-29020 | Acme Labs | ₹21,000 |
No-copy zone Copying and cutting do nothing here.
Internal draft: pricing for the enterprise tier is not final. Please don't share it yet.
What reached the clipboard
Nothing copied yet.
Control Ctrl/⌘+C, not just your buttons
Most copying is select-and-copy, which normally bypasses your app. useCopyInterceptor
rewrites, redacts or blocks it inside one element. It writes through the copy event, so there is
no permission prompt and it works over plain HTTP.
Select from the first paragraph down into the credentials: exactly one scope handles a selection,
the highest priority first, and it receives the whole selection. A transform that
throws blocks the copy, so a redaction bug never leaks.
const { ref } = useCopyInterceptor<HTMLElement>({
transform: ({ text }) => `${text}\n\nSource: ${location.href}`,
});
<article ref={ref}>…</article>
// Redact, and win overlapping selections
useCopyInterceptor({ priority: 10, transform: mask });
// Block: return false
useCopyInterceptor({ transform: () => false });
This prevents accidents, not determined users: screenshots and dev tools still work.
import { useCopyInterceptor } from "react-smart-copy";
function Article({ children }: { children: React.ReactNode }) { const { ref } = useCopyInterceptor<HTMLElement>({ transform: ({ text }) => `${text}\n\nSource: ${location.href}`, }); return <article ref={ref}>{children}</article>;}The transform receives the selection (text, html, a detached fragment you may
mutate, the field for inputs, and whether it was a copy or cut) and returns:
| Return | Effect |
|---|---|
| a string or a text/HTML/JSON/multi payload | written instead of the selection |
false |
nothing is copied; a blocked cut deletes nothing |
null / undefined |
the browser’s normal copy, untouched |
It writes through the copy event itself, so there is no permission prompt, it works over
plain HTTP, and in every browser. The trade-off: only strings can be written, so the types
reject image payloads and async transforms (the clipboard closes when the event returns).
Use useCopy for images.
Redaction. Mutate the fragment and return HTML with a text fallback:
const { ref } = useCopyInterceptor<HTMLDivElement>({ priority: 10, // see "Overlapping scopes" transform: ({ fragment, text }) => { if (!fragment) return text.replace(/sk-live-\w+/g, "••••"); // inside an <input> fragment.querySelectorAll("[data-redact]").forEach((node) => (node.textContent = "••••")); const box = document.createElement("div"); box.append(fragment); return { kind: "html", html: box.innerHTML, text: box.textContent }; },});Tables to Excel. Return a multi payload with tab-separated text/plain and an HTML
<table>; spreadsheets paste either into cells.
Cuts in inputs and contenteditable still remove the selection after your content is
written, through the browser’s own editing path: undo works and React sees an input event.
outcome.deleted tells you whether that happened.
Failures fail closed. If the transform throws or returns something unwritable, nothing
is copied and onError fires. That is the safe default for redaction; pass
failureMode: "native" to let the original copy through instead (e.g. for attribution).
Overlapping scopes. One selection can touch several intercepted elements. Exactly one
handles it: the highest priority, then the innermost, then the first in the document. The
winner receives the whole selection, so give redaction scopes a higher priority than
attribution scopes and copying across both is still masked. A scope counts as touched only
when the selection includes something of it (text, an image or another embedded element); a
selection that just ends where the scope begins, as a triple-click on the paragraph above does, leaves it out.
Web components. A scope also covers the web components inside it: text selected inside a
component’s open shadow root (Shoelace, Ionic, Lightning and similar build their cards and
inputs that way) and a field inside one, such as an API-key input. The copy event reaches the
page retargeted to the component’s host; the interceptor follows its composed path to the real
field, and reads selections that Safari and Chromium fold onto the host through
getComposedRanges, nested components included. A scope can also be registered inside a
component’s own shadow root. Drag-to-copy registered on a component’s host works for drags
that start inside it too. Two limits: a closed shadow root stays private, as it is meant
to, and a selection that starts outside a component and ends inside it is left to the browser. A React onCopy handler that
calls preventDefault() always wins.
Other options: events (["copy"], ["cut"] or both, the default), enabled, and
onIntercept(outcome) for telemetry. Without React, call registerCopyInterceptor(element, options) from react-smart-copy/core; it returns an unsubscribe function. For a single
element’s own onCopy handler, interceptCopyEvent(event, { transform }) applies a
transform to one event.
This guards against accidents, not adversaries. Anyone can still screenshot, open dev tools or disable JavaScript. Never send data to the page that the user must not see.