Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
U

use-clipboard-copy

> 前端框架
Open source

Lightweight copy to clipboard hook for React

385 stars0 likes0 views
WebsiteGitHub

About

Lightweight copy to clipboard hook for React

use-clipboard-copy

Table of Contents

  • Motivation
  • Getting Started
  • Usage
    • Copying Text of Another Target Element
    • Copying Text Imperatively (Without a Target Element)
    • Displaying a Temporary Success State
    • Handling Success and Errors
  • Browser Support
  • API
    • useClipboard(options?: UseClipboardOptions): ClipboardAPI
    • UseClipboardOptions
      • copiedTimeout?: number
      • onSuccess?: () => void
      • onError?: () => void
      • selectOnCopy?: boolean
      • selectOnError?: boolean
    • ClipboardAPI
      • copy: (text?: string) => void
      • target: React.RefObject
      • isSupported: () => boolean
      • copied: boolean
  • Acknowledgements
  • License

Motivation

There are various copy-to-clipboard solutions for Javascript – really good ones, but getting them to work with React can feel a little odd... they don't feel very React-y.

use-clipboard-copy is a lightweight ( { const url = Utils.formatUrl({ query: { id } }); clipboard.copy(url); // programmatically copying a value }, [clipboard.copy, id] );

return ( Copy Link ); }

bash

### Displaying a Temporary Success State

Sometimes it can be helpful to notify the user that the text was successfully copied to the clipboard, usually by displaying a temporary "Copied" state after they trigger the copy action.

By passing the `copiedTimeout` option to `useClipboard()`, you can use `clipboard.copied` as a way to toggle the copied state in the UI.

```jsx
import { useClipboard } from 'use-clipboard-copy';

export default function PublicUrl({ url }) {
  const clipboard = useClipboard({
    copiedTimeout: 600, // timeout duration in milliseconds
  });
  return (
    

  );
}

Handling Success and Errors

Copy to clipboard in browsers can be tricky some times - there are various reasons that can contribute to preventing the copy action from working. Therefore, you should always handle such cases.

By passing an onSuccess and onError callbacks to the useClipboard options, you will be able to handle whether the copy action was performed successfully.

javascript
function CopyText() {
  const clipboard = useClipboard({
    onSuccess() {
      console.log('Text was copied successfully!')
    },
    onError() {
      console.log('Failed to copy text!')
    }
  });
  return (
    

  );
}

…

jsx
function ClipboardSupport() {
  const clipboard = useClipboard();
  return (
    

  );
}

API

useClipboard(options?: UseClipboardOptions): ClipboardAPI

use-clipboard-copy exposes a named export useClipboard which is the hook function itself. It takes an optional options object, and returns an object to control the clipboard.

javascript
import { useClipboard } from 'use-clipboard-copy';

function CopyText() {
  const clipboard = useClipboard();
  return (
    // ...
  );
}

…

jsx
;
;

Optionally, copy takes a string to perform the copy action imperatively (programmatically). When this is used, the target element (if set) will be ignored.

javascript
 clipboard.copy('a text to copy')} />;

target: React.RefObject

A React Ref object used on input and textarea elements that holds the text to be copied.

javascript
;

isSupported: () => boolean

A function that returns a boolean indicating whether the browser supports the clipboard APIs. Useful to deterministically update the UI if the browser does not support the clipboard functionality for whatever reason.

copied: boolean

A boolean indicating whether the copy action was just performed. Must be used with the copiedTimeout option. Useful to update the UI to display temporary success state.

Acknowledgements

This hook is powered by clipboard-copy, the lightweight copy to clipboard for the web.

License

MIT

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

TypeScriptclipboardclipboard-copyclipboard-jsclipboardjs

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 17, 2026
Category前端框架
PricingOpen source

> Related tools

R
React
用于构建用户界面的 JavaScript 库
V
Vue.js
渐进式 JavaScript 框架
N
Next.js
基于 React 的全栈 Web 框架