Skip to content

useCopyToClipboard

A React hook for copying content to the clipboard with feedback.

Usage

tsx
import { useCopyToClipboard } from '@samithahansaka/clipboard';

const { copy, copied, error, reset } = useCopyToClipboard(options);

Options

PropertyTypeDefaultDescription
successDurationnumber2000Duration in ms before copied resets to false. Set to 0 to disable auto-reset.
onSuccess(content) => void-Callback when copy succeeds
onError(error) => void-Callback when copy fails

Returns

PropertyTypeDescription
copy(content: CopyContent) => Promise<boolean>Function to copy content to clipboard. Returns true on success.
copiedbooleantrue if content was recently copied successfully
errorError | nullError object if copy failed
reset() => voidManually reset copied and error state

Types

CopyContent

Content that can be copied to the clipboard:

ts
type CopyContent = string | ClipboardItem;

interface ClipboardItem {
  text: string;   // Plain text (required)
  html?: string;  // Rich HTML content (optional)
}

Examples

Basic Usage

tsx
const { copy, copied } = useCopyToClipboard();

<button onClick={() => copy('Hello!')}>
  {copied ? 'Copied!' : 'Copy'}
</button>

With Options

tsx
const { copy, copied } = useCopyToClipboard({
  successDuration: 3000,
  onSuccess: (content) => console.log('Copied:', content),
  onError: (error) => console.error('Failed:', error),
});

Copy Rich Content

tsx
const { copy } = useCopyToClipboard();

copy({
  text: 'Hello',
  html: '<strong>Hello</strong>',
});

Released under the MIT License.