npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

react-copy-clipboard-hook

v1.1.0

Published

A modern, lightweight React hook to copy text to clipboard using the Clipboard API.

Readme

react-copy-clipboard-hook

The modern replacement for react-copy-to-clipboard. A lightweight, hook-based React library to copy text to the clipboard using the native Clipboard API. No component wrappers, no legacy APIs—just a simple useClipboard hook.

If you're migrating from react-copy-to-clipboard, this library gives you the same functionality with a modern, hook-first API, TypeScript support, and better control over success and error states.


Installation

npm install react-copy-clipboard-hook
yarn add react-copy-clipboard-hook
pnpm add react-copy-clipboard-hook

Usage

Basic example

import { useClipboard } from 'react-copy-clipboard-hook';

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

  return (
    <button
      onClick={() => copy('Hello, clipboard!')}
      disabled={!isSupported}
    >
      {isCopied ? 'Copied!' : 'Copy'}
    </button>
  );
}

With options: resetDuration and onError

import { useClipboard } from 'react-copy-clipboard-hook';

function CopyButton() {
  const { isCopied, copy, isSupported } = useClipboard({
    resetDuration: 3000,  // Reset "Copied" state after 3 seconds (default: 2000)
    onError: (error) => console.error('Copy failed:', error),
  });

  const handleCopy = async () => {
    const success = await copy('Some text');
    if (success) {
      // Optional: show toast, analytics, etc.
    }
  };

  return (
    <button onClick={handleCopy} disabled={!isSupported}>
      {isCopied ? 'Copied!' : 'Copy'}
    </button>
  );
}

Disable auto-reset

Set resetDuration to 0 to keep isCopied true until the next copy attempt or unmount:

const { isCopied, copy } = useClipboard({ resetDuration: 0 });

Check clipboard support (SSR / unsupported browsers)

The hook is SSR-safe and exposes isSupported so you can disable UI or show a fallback when the Clipboard API isn't available (e.g. non-HTTPS, older browsers):

const { isCopied, copy, isSupported } = useClipboard();

if (!isSupported) {
  return <span>Copy not supported in this browser.</span>;
}

return (
  <button onClick={() => copy(text)}>
    {isCopied ? 'Copied!' : 'Copy'}
  </button>
);

Copy dynamic or user input

Pass any string into copy()—from state, props, or refs:

function CopyInput() {
  const [value, setValue] = useState('');
  const { isCopied, copy, isSupported } = useClipboard();

  return (
    <div>
      <input
        value={value}
        onChange={(e) => setValue(e.target.value)}
        disabled={!isSupported}
      />
      <button onClick={() => copy(value)} disabled={!isSupported}>
        {isCopied ? 'Copied!' : 'Copy'}
      </button>
    </div>
  );
}

Handle errors explicitly

copy(text) returns Promise<boolean>. Use it with onError for logging or user feedback:

const [error, setError] = useState<string | null>(null);

const { isCopied, copy, isSupported } = useClipboard({
  onError: (err) => setError(err.message),
});

const handleCopy = async () => {
  setError(null);
  const ok = await copy(textToCopy);
  if (!ok) setError('Copy failed');
};

API Reference

useClipboard(options?)

Returns an object with:

| Property | Type | Description | |-------------|-------------------------------|-------------| | isCopied | boolean | true after a successful copy until reset (or next copy). | | copy | (text: string) => Promise<boolean> | Copies text to the clipboard. Resolves true on success, false on failure or unsupported environment. | | isSupported | boolean | true when the Clipboard API is available (and in a browser). Safe to use for SSR. |

Options

| Option | Type | Default | Description | |------------------|-------------------------|----------|-------------| | resetDuration | number | 2000 | Milliseconds after which isCopied is set back to false. Use 0 to disable auto-reset. | | onError | (error: Error) => void | — | Called when copy fails (unsupported, invalid input, or clipboard error). |

copy(text: string)

  • Returns: Promise<boolean>true if the copy succeeded, false otherwise.
  • Behavior:
    • If the environment doesn't support the Clipboard API, returns false and optionally calls onError.
    • If text is not a string, returns false and optionally calls onError.
    • On success, sets isCopied to true and schedules a reset after resetDuration ms (if > 0).
    • On clipboard failure (e.g. permission denied), sets isCopied to false, calls onError, and returns false.

Requirements

  • React >= 16.8.0 (hooks support)
  • Browser: Clipboard API support (HTTPS in production; see browser support)

Why use this instead of react-copy-to-clipboard?

| react-copy-to-clipboard (legacy) | react-copy-clipboard-hook (modern replacement) | |----------------------------------|-------------------------------------------------| | Component-based (<CopyToClipboard>) | Hook-based (useClipboard) — fits any UI | | Uses document.execCommand('copy') | Uses native navigator.clipboard (Clipboard API) | | Less control over success/error | copy() returns Promise<boolean>, plus onError and isSupported | | No built-in “copied” state timing | Configurable resetDuration (or disable with 0) | | — | SSR-safe and TypeScript-friendly |


License

MIT © Sameer Thite