$ show zenorocha/clipboard.js
Clipboard.js is a lightweight JavaScript library that provides a modern, no-Flash way to copy or cut text to the clipboard. It operates as a browser library, using event delegation via the `good-listener` package and the deprecated `document.execCommand` API. The core class `ClipboardJS` can be instantiated with a DOM selector, element, or NodeList, and it automatically listens for click events to trigger copy/cut actions based on HTML data attributes (`data-clipboard-text`, `data-clipboard-target`, `data-clipboard-action`). It also exposes static `copy()` and `cut()` methods for programmatic use, and emits custom `success` and `error` events. The library includes a fallback mechanism using a temporary textarea element for browsers that do not support selecting hidden elements. It is well-tested, has TypeScript definitions, and is distributed as a UMD bundle. The architecture is a simple monolith with a single entry point and isolated action modules.
The LLM identified 2 files worth highlighting for reuse:
Implements the copy action, including a fallback for non-input elements by creating a temporary hidden textarea (`fakeCopyAction`). This pattern is reusable for any browser-based copy-to-clipboard implementation, as it handles both text strings and DOM elements with correct selection logic. The function is self-contained, only depending on `select` and `createFakeElement`, making it easy to extract.
Creates a hidden textarea element with specific styles to prevent iOS zoom and ensure it is out of the viewport. It also respects RTL layout and positions the element at the current scroll position. This is a standalone utility that can be reused in any project needing to programmatically select and copy text via a hidden input.