Copy Text to Clipboard
Copies text to the user's clipboard using modern clipboard APIs when available, falling back to legacy methods for browser compatibility.
copyToClipboard
The copyToClipboard function copies a string of text to the clipboard. It uses modern clipboard APIs if available, and falls back to a legacy <textarea> method to ensure broad browser compatibility.
Function Signature
function copyToClipboard(text: string): Promise<void>Parameters
text(string): The text to copy to the clipboard.
Return Value
Promise<void>: A promise that resolves when the text is successfully copied.
Example Usage
import { copyToClipboard } from 'toolbox-x/dom';
await copyToClipboard('Hello, world!');
console.log('Copied to clipboard!');Notes
- Browser Context: This utility is only available in browser environments. It will fail in server environments (like Node.js or SSR frameworks).
- User Interactions: Browser security requires that clipboard operations occur inside user-initiated event handlers (e.g. click events).
- Fallback: On older browsers or insecure origins, the function automatically creates a temporary hidden
<textarea>element to perform the copy command.
Last updated: Mon, Jun 15, 2026 11:04:47AM (UTC)
Session Storage Utilities
Safely and conveniently interact with the browser's Session Storage API with full type safety and custom serialization.
Generate Query Parameters
Generates a URL-encoded query string from an object, flattening nested fields and supporting arrays, booleans, and various primitive types.
