Skip to content

API Reference

Components

CopyButton

A button component that copies text to clipboard with visual feedback.

tsx
<CopyButton text="text to copy" onCopied={() => console.log("Copied!")}>
  Copy Text
</CopyButton>

Props:

PropTypeRequiredDefaultDescription
textstring-The text to copy to clipboard
childrenReactNode"Copy"Button content
onCopied() => void-Callback fired after successful copy

Behavior:

  • Shows "Copied!" for 2 seconds after successful copy
  • Handles errors gracefully with console logging
  • Uses native navigator.clipboard.writeText()

CopyOnClick

A wrapper component that copies text when the wrapped element is clicked.

tsx
<CopyOnClick text="text to copy">
  <code>Click to copy</code>
</CopyOnClick>

Props:

PropTypeRequiredDescription
textstringThe text to copy to clipboard
childrenReactNodeThe element to wrap (will receive click handler)

Behavior:

  • Adds click handler to wrapped element
  • Shows "✓ Copied" indicator for 2 seconds
  • Sets cursor to pointer
  • Adds title attribute with copy hint

Hooks

useClipboard

A React hook for programmatic clipboard operations.

tsx
const { copy, isCopying, error } = useClipboard({ timeout: 2000 });

Options:

OptionTypeDefaultDescription
timeoutnumber2000Duration (ms) to keep isCopying true

Returns:

PropertyTypeDescription
copy(text: string) => Promise<void>Function to copy text to clipboard
isCopyingbooleanWhether a copy operation is in progress
errorError | nullAny error that occurred during copy

Example:

tsx
function MyComponent() {
  const { copy, isCopying, error } = useClipboard();

  const handleCopy = async () => {
    await copy("Hello, World!");
  };

  return (
    <div>
      <button onClick={handleCopy} disabled={isCopying}>
        {isCopying ? "Copying..." : "Copy"}
      </button>
      {error && <p style={{ color: "red" }}>{error.message}</p>}
    </div>
  );
}

TypeScript Types

CopyButtonProps

tsx
interface CopyButtonProps {
  text: string;
  children?: ReactNode;
  onCopied?: () => void;
}

CopyOnClickProps

tsx
interface CopyOnClickProps {
  text: string;
  children: ReactNode;
}

UseClipboardOptions

tsx
interface UseClipboardOptions {
  timeout?: number;
}

Browser Compatibility

All components and hooks use the modern Clipboard API (navigator.clipboard):

  • ✅ Chrome/Edge 66+
  • ✅ Firefox 63+
  • ✅ Safari 13.1+
  • ✅ Opera 53+

Note: The Clipboard API requires a secure context (HTTPS or localhost).


📚 View interactive examples in Storybook.