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 🙏

© 2025 – Pkg Stats / Ryan Hefner

use-copy-ts

v0.1.0

Published

Type-safe React hook for clipboard operations

Downloads

5

Readme

use-copy-ts

npm version License: MIT TypeScript

Type-safe React hook for clipboard operations with enhanced features.

Features

  • 🔒 Type-safe: Full TypeScript support with comprehensive type definitions
  • 📋 Modern Clipboard API: Uses the latest Clipboard API for secure operations
  • ⏱️ Auto-reset: Configurable timeout to automatically reset copied state
  • 🎯 Error Handling: Detailed error information with custom error callbacks
  • 🔍 Support Detection: Automatic detection of Clipboard API support
  • 🎨 Flexible: Success and error callbacks for custom handling
  • 🧹 Clean: Manual state reset functionality

Installation

npm install use-copy-ts

Quick Start

import { useCopy } from 'use-copy-ts';

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

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

API Reference

useCopy(options?)

Parameters

  • options (optional): Configuration object
interface UseCopyOptions {
  timeout?: number; // Auto-reset timeout in milliseconds (default: 2000)
  onSuccess?: (text: string) => void; // Success callback
  onError?: (error: Error) => void; // Error callback
}

Returns

interface UseCopyReturn {
  copy: (text: string) => Promise<boolean>; // Copy function
  copied: boolean; // Whether text was recently copied
  copiedText: string | null; // Most recently copied text
  error: Error | null; // Last error, if any
  isSupported: boolean; // Whether Clipboard API is supported
  reset: () => void; // Manual reset function
  clear: () => Promise<boolean>; // Clear clipboard contents
}

Examples

Basic Usage

import { useCopy } from 'use-copy-ts';

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

  const handleCopy = () => {
    copy('Text to copy');
  };

  if (error) {
    return <div>Error: {error.message}</div>;
  }

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

Advanced Usage with Callbacks

import { useCopy } from 'use-copy-ts';

function AdvancedCopyButton() {
  const { copy, copied, copiedText, isSupported } = useCopy({
    timeout: 3000, // Reset after 3 seconds
    onSuccess: (text) => {
      console.log('Successfully copied:', text);
    },
    onError: (error) => {
      console.error('Copy failed:', error);
    }
  });

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

  return (
    <div>
      <button onClick={() => copy('Hello World')}>
        Copy Text
      </button>
      {copied && <p>Copied: {copiedText}</p>}
    </div>
  );
}

Copy Code Snippet

import { useCopy } from 'use-copy-ts';

function CodeBlock({ code }: { code: string }) {
  const { copy, copied, reset } = useCopy({
    timeout: 2000,
    onSuccess: () => {
      // Show toast notification
    }
  });

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

Share URL

import { useCopy } from 'use-copy-ts';

function ShareButton() {
  const { copy, copied } = useCopy({
    onSuccess: () => {
      // Analytics tracking
      analytics.track('url_shared');
    }
  });

  const shareCurrentPage = () => {
    copy(window.location.href);
  };

  return (
    <button onClick={shareCurrentPage}>
      {copied ? 'URL Copied!' : 'Share Page'}
    </button>
  );
}

Clear Clipboard

import { useCopy } from 'use-copy-ts';

function SecureNoteEditor() {
  const { copy, clear, copied } = useCopy({
    onSuccess: () => {
      // Auto-clear clipboard after 30 seconds for security
      setTimeout(() => {
        clear();
      }, 30000);
    }
  });

  return (
    <div>
      <button onClick={() => copy('Sensitive information')}>
        Copy Note
      </button>
      <button onClick={clear}>
        Clear Clipboard
      </button>
      {copied && <p>Copied! Will auto-clear in 30s</p>}
    </div>
  );
}

Browser Support

This hook uses the modern Clipboard API which requires:

  • HTTPS (or localhost for development)
  • Modern browsers that support the Clipboard API

Supported Browsers

  • Chrome 66+
  • Firefox 63+
  • Safari 13.1+
  • Edge 79+

The hook automatically detects support via the isSupported property.

Requirements

  • React 16.8+ (hooks support)
  • TypeScript 4.0+ (if using TypeScript)

Contributing

Contributions are welcome! Please read our Contributing Guide for details.

License

MIT © nhaya1000

Changelog

See CHANGELOG.md for details about changes in each version.