Skip to content

Introduction

react-smart-copy handles the whole interaction around the clipboard, not just the call to it: copying, copied, failed, retry and pasting, with honest errors, hover, focus and touch reveal, and screen-reader announcements built in. It is headless, so it fits any design system.

Email shubh@example.com [Copy] -> Copied
Phone +91 98xxx xxxxx [Copy]
ID PLT-29018 [Copy]

navigator.clipboard.writeText() is one line. Everything around it is not. A button has to show that it copied, and then go back. A failed copy needs a reason the person can act on, not a silent no-op. A screen reader has to hear “Copied”. Rich content needs a plain-text fallback. Pasted HTML from Word or Google Docs is a mess, and pasted photos carry the location they were taken at. react-smart-copy does all of it, and does it the same way in every browser.

  • Small. About 4.4 kB for useCopy and 6.5 kB for everything copy (min + brotli). No runtime dependencies, and every opt-in entry point costs nothing until you import it.
  • Typed, honest errors. Every failure is a classified, typed error. Nothing silently no-ops.
  • Accessible. Announcements, focus handling and keyboard support built in; WCAG 2.2 AA for everything the docs render, checked with axe in real browsers.
  • Unstyled. Works with Tailwind, CSS modules, CSS-in-JS, shadcn, MUI and Ant Design.
  • Safe on servers. SSR and Next.js App Router safe, React 18 and 19, StrictMode-proof.
  • Tested in real engines. Chromium, Firefox and WebKit on every change, plus 100% test coverage and property tests for every parser and serialiser.
You want to Use
Copy text, HTML, images or JSON from a button useCopy, CopyField
Paste text, HTML or screenshots usePaste, PasteField
Decide what Ctrl/Cmd+C copies from your page useCopyInterceptor
Fill the data when people drag content out useDragCopy
Copy an SVG or part of your UI as a PNG react-smart-copy/capture
Copy and paste spreadsheet ranges react-smart-copy/table
Copy your own typed objects react-smart-copy/structured
Paste rich content as Markdown react-smart-copy/markdown
Copy and paste code safely react-smart-copy/code
Make pasted images private and small react-smart-copy/image
Take pasted and dropped files and folders safely react-smart-copy/files
Keep keys and passwords out of pastes and copies react-smart-copy/secrets
Read pasted one-time codes, and build a code field react-smart-copy/otp, useOtpField
Paste and copy numbers and amounts in any locale react-smart-copy/number
Paste lists of e-mail recipients, checked react-smart-copy/email
Replace react-copy-to-clipboard by changing the import react-smart-copy/compat
Copy links that paste well react-smart-copy/link
Copy styled content, or paste into an editor react-smart-copy/rich
Copy and paste colours react-smart-copy/color
A Share button that falls back to copy react-smart-copy/share

This package only promises what browsers actually support.

Content Supported Notes
Plain text ✅ Everywhere; on plain HTTP and in webviews without the async API, through the browser’s copy command
Rich HTML ✅ Always paired with a required plain-text fallback. Degrades to text when ClipboardItem is missing
Images ✅ PNG only image/png is the only format with reliable cross-browser write support. Convert with canvas.toBlob(cb, 'image/png')
JSON ✅ Serialised to text. JSON is not a native clipboard format
Tables (Excel, Sheets, Docs) ✅ react-smart-copy/table: an HTML table plus tab-separated text in one entry. Pasting reads both back
Several formats at once ✅ kind: 'multi'; each MIME type is checked with ClipboardItem.supports() first
Your app’s own data (typed) ✅ react-smart-copy/structured: a web custom format, a custom type and an HTML envelope, so every browser and paste path gets one
Rich text as Markdown ✅ paste react-smart-copy/markdown: CommonMark + GFM from Docs, Word, web pages and mail; plain-text pastes stay untouched
Code (blocks, sessions, diffs) ✅ react-smart-copy/code: plain text plus monospaced, highlighted HTML; commands without prompts or output; Trojan Source checks on paste
Pasted images, made safe ✅ paste react-smart-copy/image: removes location and camera metadata losslessly; resizes and compresses under a byte budget
Links (titled, to a passage) ✅ react-smart-copy/link: a titled <a> plus the URL; text/uri-list when dragged; text fragment links; never javascript:
Styled content, portable ✅ react-smart-copy/rich: sanitized HTML with inline styles and email-safe colours, plus plain text; for buttons and Ctrl/⌘+C
Rich text pasted into an editor ✅ react-smart-copy/rich: Docs, Word and mail into TipTap, Lexical or contenteditable as clean, semantic HTML
Colours (every CSS format) ✅ react-smart-copy/color: the value as text, plus a swatch for rich-text apps; any CSS colour read back on paste
Share sheet (Web Share API) ✅ with copy fallback react-smart-copy/share: the share sheet on phones and tablets, a titled link copied elsewhere; files where the share sheet takes them
SVG (icons, charts, logos) ✅ as PNG svgToPngBlob() from react-smart-copy/capture rasterises natively, no dependency. Raw SVG is not a clipboard image format
A section of your UI as an image ✅ bring a rasteriser Browsers have no “screenshot this element” API. captureElement() wraps one you choose (e.g. modern-screenshot) with limits and checks
Files (PDF, DOCX, ZIP) ❌ Not writable to the system clipboard by any browser API
Anything over plain HTTP ❌ The Clipboard API requires HTTPS or localhost. You get an insecure-context error, not a silent failure

Next: install it, then follow the quick start.