Skip to main content
Copies text to the clipboard and tracks the copy status.

Usage

import { useClipboard } from '@kivora/react';

function CopyButton() {
  const { copy, copied } = useClipboard();

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

Parameters

timeout
number
default:"2000"
Milliseconds before status resets to 'idle'.

Returns

copy
(text: string) => Promise<void>
Function to copy text to clipboard.
status
ClipboardStatus
Current clipboard status: 'idle' | 'copied' | 'error'.
copied
boolean
true when status is 'copied'.
reset
() => void
Manually resets status to 'idle'.

Examples

Basic copy button

const { copy, copied } = useClipboard();

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

With custom timeout

const { copy, status } = useClipboard(5000); // 5 second timeout

const handleCopy = async () => {
  await copy('https://example.com/share');
};

return (
  <div>
    <button onClick={handleCopy}>Share Link</button>
    {status === 'copied' && <span>Link copied!</span>}
    {status === 'error' && <span>Failed to copy</span>}
  </div>
);

Manual reset

const { copy, copied, reset } = useClipboard();

return (
  <>
    <button onClick={() => copy('Code snippet')}>
      {copied ? 'Copied!' : 'Copy Code'}
    </button>
    {copied && <button onClick={reset}>Reset</button>}
  </>
);

Type Definitions

type ClipboardStatus = 'idle' | 'copied' | 'error';

interface UseClipboardReturn {
  copy: (text: string) => Promise<void>;
  status: ClipboardStatus;
  copied: boolean;
  reset: () => void;
}

function useClipboard(timeout?: number): UseClipboardReturn;

Build docs developers (and LLMs) love