kitzo

React utilities library

copy

Copy a string or serializable value to the system clipboard safely.

Usage

copy.ts
await copy(value);

Example

CopyExample.tsx
import { copy } from 'kitzo/fns';
            
async function copyExample() {
  try {
    await copy('Hello, world!');
    console.log('Copied successfully!');
  } catch (err) {
    console.error('Copy failed:', err);
  }
}

copyExample();

API

ParameterTypeDescription
doc
string

The value to copy. Strings and numbers are used as-is; objects are JSON-stringified.

Returns a Promise that resolves after the text is copied to the clipboard.

Behavior

  • Automatically falls back to a legacy <textarea /> copy method if `navigator.clipboard` is unavailable.
  • Serializes non-string/number values using JSON.stringify.
  • Returns a Promise to allow async handling and error catching.
  • Circular objects are replaced with a placeholder string to prevent errors.