Rooks
HooksBrowser APIs

useClipboard

Reads from and writes to the clipboard using the Clipboard API.

About

React hook for reading from and writing to the system clipboard using the Clipboard API.

Installation

npm install rooks

Examples

import { useState } from "react";
import { useClipboard } from "rooks";

function ClipboardDemo() {
  const { copy, paste, text, isSupported, error } = useClipboard();
  const [inputValue, setInputValue] = useState("");

  const handleCopy = async () => {
    try {
      await copy(inputValue);
      console.log("Copied to clipboard!");
    } catch (err) {
      console.error("Failed to copy:", err);
    }
  };

  const handlePaste = async () => {
    try {
      await paste();
      console.log("Pasted from clipboard!");
    } catch (err) {
      console.error("Failed to paste:", err);
    }
  };

  if (!isSupported) {
    return <div>Clipboard API not supported in this browser</div>;
  }

  return (
    <div>
      <h2>Clipboard Demo</h2>
      <div>
        <input
          value={inputValue}
          onChange={(e) => setInputValue(e.target.value)}
          placeholder="Enter text to copy"
        />
        <button onClick={handleCopy}>Copy</button>
        <button onClick={handlePaste}>Paste</button>
      </div>
      {text && (
        <div>
          <strong>Clipboard content:</strong> {text}
        </div>
      )}
      {error && (
        <div style={{ color: "red" }}>
          <strong>Error:</strong> {error.message}
        </div>
      )}
    </div>
  );
}

Another Example

import { useClipboard } from "rooks";

function ClipboardStatus() {
  const { copy, paste, text, error, isSupported } = useClipboard();

  if (!isSupported) {
    return <p>Clipboard access is unavailable in this browser.</p>;
  }

  return (
    <div>
      <button onClick={() => copy("rooks is on the clipboard")}>
        Copy preset text
      </button>
      <button onClick={paste}>Read clipboard</button>

      {text ? <p>Clipboard value: {text}</p> : <p>Clipboard is empty.</p>}
      {error ? <p>Error: {error.message}</p> : null}
    </div>
  );
}

Return value

Returns an object with the following properties:

PropertyTypeDescription
textstring | nullThe current text content from the clipboard
copy(value: string) => Promise<void>Copy text to the clipboard
paste() => Promise<void>Read text from the clipboard
isSupportedbooleanWhether the Clipboard API is supported
errorError | nullAny error that occurred during clipboard operations

Features

  • Copy text to clipboard - Write text using the Clipboard API
  • Read from clipboard - Read text from the system clipboard
  • Error handling - Comprehensive error handling for permission issues
  • Browser support detection - Detects if Clipboard API is available
  • TypeScript support - Full type definitions included

Browser Support

The Clipboard API requires:

  • HTTPS context (or localhost for development)
  • User permission for clipboard access
  • Modern browsers (Chrome 66+, Firefox 63+, Safari 13.1+, Edge 79+)

Notes

  • The clipboard read operation requires user permission in most browsers
  • Some browsers may show a permission prompt when accessing the clipboard
  • The hook automatically handles errors and permission denials
  • Works only in secure contexts (HTTPS or localhost)

Parameters

This hook takes no parameters. copy requires a string argument; paste takes no arguments.

Behavior and lifecycle

copy writes text, then stores that same value in text. paste reads text, then stores the result. Each successful operation clears the prior error; unsupported or failed operations store an Error and reject their promise. The hook owns no persistent browser resource and requires no unmount cleanup.

Compatibility and accessibility

SSR reports isSupported: false. Clipboard access generally requires HTTPS, a focused document, permission, and a user gesture; reading is more restricted than writing. Always keep normal input/select/copy behavior available, label action buttons, and show success or failure without exposing sensitive clipboard contents unnecessarily.

On this page