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

@samithahansaka/clipboard

v1.0.4

Published

A modern, lightweight React hook and component for copying to clipboard with feedback

Readme

@samithahansaka/clipboard

A modern, lightweight React hook and component for copying to clipboard with feedback.

CI npm version bundle size codecov TypeScript license docs demo

Features

  • Modern Clipboard API - Uses navigator.clipboard with smart fallbacks
  • Hook + Component APIs - Use whichever fits your needs
  • Rich content support - Copy HTML alongside plain text
  • TypeScript-first - Full type definitions included
  • Zero dependencies - Just React as a peer dependency
  • Tiny bundle - ~1KB gzipped
  • SSR-safe - Works with Next.js, Remix, etc.

Installation

npm install @samithahansaka/clipboard
yarn add @samithahansaka/clipboard
pnpm add @samithahansaka/clipboard

Usage

Hook API

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

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

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

With Options

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

function MyComponent() {
  const { copy, copied } = useCopyToClipboard({
    successDuration: 3000, // Reset after 3 seconds (default: 2000)
    onSuccess: (content) => console.log('Copied:', content),
    onError: (error) => console.error('Failed:', error),
  });

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

Component API

import { CopyButton } from '@samithahansaka/clipboard';

function MyComponent() {
  return (
    <CopyButton content="Hello, World!">
      {({ copy, copied, error }) => (
        <button onClick={copy} disabled={!!error}>
          {error ? 'Failed!' : copied ? 'Copied!' : 'Copy'}
        </button>
      )}
    </CopyButton>
  );
}

Copy Rich Content (HTML + Text)

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

function MyComponent() {
  const { copy, copied } = useCopyToClipboard();

  const handleCopy = () => {
    copy({
      text: 'Hello, World!', // Plain text fallback
      html: '<strong>Hello, World!</strong>', // Rich HTML
    });
  };

  return (
    <button onClick={handleCopy}>
      {copied ? 'Copied!' : 'Copy formatted text'}
    </button>
  );
}

With Toast Notifications

import { useCopyToClipboard } from '@samithahansaka/clipboard';
import { toast } from 'your-toast-library';

function CopyableCode({ code }: { code: string }) {
  const { copy, copied } = useCopyToClipboard({
    onSuccess: () => toast.success('Copied to clipboard!'),
    onError: () => toast.error('Failed to copy'),
  });

  return (
    <div className="code-block">
      <pre>{code}</pre>
      <button onClick={() => copy(code)}>
        {copied ? '✓' : 'Copy'}
      </button>
    </div>
  );
}

API Reference

useCopyToClipboard(options?)

Options

| Property | Type | Default | Description | |----------|------|---------|-------------| | successDuration | number | 2000 | Duration 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

| Property | Type | Description | |----------|------|-------------| | copy | (content) => Promise<boolean> | Copy content to clipboard. Returns true on success. | | copied | boolean | true if content was recently copied | | error | Error \| null | Error object if copy failed | | reset | () => void | Manually reset copied and error state |

<CopyButton>

Props

| Prop | Type | Default | Description | |------|------|---------|-------------| | content | string \| ClipboardItem | - | Content to copy | | successDuration | number | 2000 | Duration before copied state resets | | onSuccess | (content) => void | - | Callback on success | | onError | (error) => void | - | Callback on error | | children | (state) => ReactNode | - | Render prop for custom UI |

Render Props

The children function receives:

{
  copy: () => void;      // Trigger copy
  copied: boolean;       // Success state
  error: Error | null;   // Error state
  reset: () => void;     // Reset state
}

Browser Support

  • Modern browsers: Uses the Clipboard API (navigator.clipboard)
  • Older browsers: Falls back to document.execCommand('copy')
  • Rich content: HTML copying requires Clipboard API support (Chrome 76+, Firefox 63+, Safari 13.1+)

License

MIT