copy-to-clipboard

Copy text to the clipboard from the browser with automatic fallbacks for older or non-secure contexts.

Library
npm
v4.0.2
1,397 stars
MIT License

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum. How we score it →
48 /100 Fair
Development Activity 20
Maintenance 20
Community 52
Maturity 60
Momentum 40

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation. How we score it →
80 /100 Excellent
Architecture 78
Code Quality 75
Innovation 82
Learning Curve 85

copy-to-clipboard is a small JavaScript module that copies text to the clipboard from the browser. It uses the modern Async Clipboard API (navigator.clipboard) when running in a secure context, and automatically falls back to document.execCommand('copy') for non-secure pages or older browsers, with an opt-in window.prompt() fallback as a last resort. Version 4 made the exported copy() function async, returning a Promise<boolean> that resolves to whether the copy succeeded.

The library ships as CommonJS, ESM, and an IIFE build for direct <script> tag usage, with bundled TypeScript declarations for both module formats. It supports copying rich content via a format option (e.g. text/html) and an onCopy hook for intercepting or replacing the clipboard payload before it’s written, making it useful beyond plain-text copy buttons.

What You Get

  • A single default-exported copy(text, options?) function returning Promise<boolean>
  • Automatic Async Clipboard API usage in secure contexts, with execCommand fallback elsewhere
  • Support for copying non-plain-text formats (e.g. text/html) via the format option
  • An onCopy hook to intercept and replace the ClipboardItem or DataTransfer payload before writing
  • CJS, ESM, and IIFE (CDN <script> tag) builds with bundled TypeScript declarations
  • An opt-in window.prompt() fallback (fallbackToPrompt) for environments where both clipboard paths fail

Common Use Cases

  • Adding a “copy” button next to a code snippet, API key, or share link in a web app
  • Copying rich HTML content (e.g. from a WYSIWYG editor) to the clipboard as both text/html and text/plain
  • Building UI copy affordances that need to work correctly on both HTTPS and non-secure/legacy pages
  • Copying custom MIME-typed data (like CSV) for pasting into spreadsheet or rich-text applications

Under The Hood

Architecture — index.ts implements a single tiered copy(text, options) async function. When window.isSecureContext and navigator.clipboard are available it tries copyViaClipboardAPI, which calls navigator.clipboard.writeText() for the plain-text case or builds a ClipboardItem (via buildClipboardItem) and calls navigator.clipboard.write() when a custom format or onCopy hook is supplied. On failure or unavailability it falls through to copyViaExecCommand, which creates a visually-hidden, screen-reader-hidden <span> (createHiddenSpan), selects its contents with the Selection/Range APIs, and triggers document.execCommand('copy'); a findOpenedModalDialog helper walks up from document.activeElement to append the span inside an open <dialog> (or the fullscreen element) so the copy works when the page is in a modal or fullscreen state. A toggle-selection dependency (deselectCurrent) saves and restores the user’s prior text selection around the operation. If both paths fail, an opt-in window.prompt() fallback is offered as a last resort.

Tech Stack — Written in TypeScript (index.ts), compiled to CommonJS, ESM, and an IIFE global build via tsup, with publint used to validate package.json export correctness. The only runtime dependency is toggle-selection; there are no framework dependencies. declarations.d.ts and generated .d.ts/.d.mts files ship types for both module systems.

Code Quality — There are no jsdom-style unit tests; instead the tests/ directory holds seven Nightwatch end-to-end specs (sanity check, basic/multiline/markup/rich text, dialog copy, fullscreen, tabbed text) that drive real Chrome/Firefox/Safari/Edge instances via native webdrivers — an appropriate strategy given the code’s entire surface area is browser Clipboard/Selection API interaction that jsdom cannot faithfully emulate. Each copy path is wrapped in try/catch with debug-gated console logging, and copyViaExecCommand uses a finally block to guarantee selection and DOM cleanup (removing the hidden span, restoring the previous selection) even when the copy fails. Function and variable naming is consistent and descriptive of the exact copy path taken.

API Design — The public surface is a single default export, copy(text: string, options?: Options): Promise<boolean>, requiring zero configuration for the common case (await copy('text')). The Options type is documented inline with JSDoc and covers debug, message (prompt text), format (MIME type), onCopy (payload interception), and fallbackToPrompt. The README documents a clear v4 breaking-change migration note (sync → async), install instructions for all three module formats, and worked recipes for HTML-copy-with-plain-text-fallback and custom MIME types, keeping the getting-started path to a single await call.

Used by 28 apps in this directory

TypeScript
87%
MIT

medusa

Ecommerce

36,897

The most flexible open-source commerce platform — build B2C, B2B, and marketplace applications with modular, composable commerce primitives.

View details
94
Repo Health
87
Technical
63
Dependency
Built with
TypeScript 87%
JavaScript 12%
Updated 1 weeks ago
Go
53%
MIT

memos

Note Taking

64,031

Open-source, self-hosted note-taking built for quick capture — Markdown-native, lightweight, and fully yours.

View details
91
Repo Health
78
Technical
65
Dependency
Built with
Go 53%
TypeScript 46%
Updated 6 days ago
TypeScript
99%
Other

NocoBase

Low Code Platforms · No Code Platforms

24,686

Open-source AI + no-code platform that lets coding agents and people collaborate to build business systems fast on proven infrastructure.

View details
94
Repo Health
81
Technical
63
Dependency
Built with
TypeScript 99%
Updated 6 days ago
Python
64%
Other

Onyx

AI Agents · AI Assistants · Knowledge Management

32,935

Self-hostable AI platform with agentic RAG, 50+ connectors, deep research, code execution, and support for every major LLM provider.

View details
92
Repo Health
83
Technical
68
Dependency
Built with
Python 64%
TypeScript 26%
Updated 6 days ago
TypeScript
95%
MIT

Open Lovable

AI Development

28,793

An open-source AI app builder that scrapes a live website and recreates it as a modern React app through chat — built by Firecrawl as a self-hostable example of the Lovable.dev workflow.

View details
50
Repo Health
66
Technical
69
Dependency
Built with
TypeScript 95%
Updated 3 weeks ago
TypeScript
96%

Open Scouts

AI Agents · Automation

1,370

Create automated AI scouts that continuously search the web and send email alerts when they find what you're looking for.

View details
33
Repo Health
65
Technical
69
Dependency
Built with
TypeScript 96%
Updated 5 months ago
TypeScript
54%
Other

OpenReplay

Analytics

13,042

Self-hosted session replay and product analytics suite that lets you see exactly what users do on your web app — without sending data to third parties.

View details
91
Repo Health
77
Technical
66
Dependency
Built with
TypeScript 54%
Go 13%
Updated 1 weeks ago
TypeScript
98%
Other

Outline

Collaboration · Knowledge Management

41,045

A fast, real-time collaborative knowledge base for growing teams built on React, Node.js, and ProseMirror.

View details
91
Repo Health
87
Technical
68
Dependency
Built with
TypeScript 98%
Updated 6 days ago
Python
49%
Other

Arize Phoenix

Analytics · Devops · Monitoring

11,976

Open-source AI observability platform for tracing, evaluating, and debugging LLM applications with built-in intelligence and MCP support.

View details
92
Repo Health
88
Technical
67
Dependency
Built with
Python 49%
TypeScript 43%
Updated 6 days ago

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers