Gathering detailed insights and metrics for use-clipboard-copy
Gathering detailed insights and metrics for use-clipboard-copy
Gathering detailed insights and metrics for use-clipboard-copy
Gathering detailed insights and metrics for use-clipboard-copy
react-use-clipboard
React hook that provides copy to clipboard functionality.
@scalar/use-clipboard
tiny wrapper around the clipboard API
@coffeekraken/s-clipboard-copy-component
Simple copy to clipboard component that let you specify with ease a target to use for copy like an input, textarea, etc...
react-use-copy-to-clipboard
this hook is needed to copy text to the clipboard when clicking on any element
📋 Lightweight copy to clipboard hook for React
npm install use-clipboard-copy
Typescript
Module System
Node Version
NPM Version
97.8
Supply Chain
99.4
Quality
76
Maintenance
100
Vulnerability
100
License
TypeScript (100%)
Total Downloads
12,888,922
Last Day
3,395
Last Week
79,139
Last Month
353,752
Last Year
4,276,516
MIT License
388 Stars
34 Commits
16 Forks
2 Watchers
16 Branches
3 Contributors
Updated on Apr 08, 2025
Minified
Minified + Gzipped
Latest Version
0.2.0
Package Id
use-clipboard-copy@0.2.0
Size
5.58 kB
NPM Version
7.7.6
Node Version
15.14.0
Published on
Apr 16, 2021
Cumulative downloads
Total Downloads
Last Day
5.4%
3,395
Compared to previous day
Last Week
-10.5%
79,139
Compared to previous week
Last Month
-11.9%
353,752
Compared to previous month
Last Year
30.6%
4,276,516
Compared to previous year
1
1
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 (< 1KB) React hook that makes it possible to add a copy-to-clipboard functionality to your React application with very little code! A simple implementation looks like this:
1function CopyText() { 2 const clipboard = useClipboard(); 3 return ( 4 <div> 5 <input ref={clipboard.target} /> 6 <button onClick={clipboard.copy}>Copy</button> 7 </div> 8 ); 9}
P.S. You can do more than that with use-clipboard-copy
. Keep reading!
To get started, add use-clipboard-copy
to your project:
npm install --save use-clipboard-copy
Please note that use-clipboard-copy
requires react@^16.8.0
as a peer dependency.
A simple copy-to-clipboard interface consists of two parts:
target
, an element who holds the value to be copied, usually an input.copy
action.1import { useClipboard } from 'use-clipboard-copy'; 2 3export default function PublicUrl({ url }) { 4 const clipboard = useClipboard(); 5 return ( 6 <div> 7 <input ref={clipboard.target} value={url} readOnly /> 8 <button onClick={clipboard.copy}>Copy Link</button> 9 </div> 10 ); 11}
It is also possible to perform a copy action imperatively (programmatically). For example, a copy-to-clipboard interface may consist of a single copy button without any additional inputs or values displayed to the user. By passing a string to the clipboard.copy
action, the specified string will be copied to the clipboard.
1import { useClipboard } from 'use-clipboard-copy'; 2 3export default function PublicUrl({ id }) { 4 const clipboard = useClipboard(); 5 6 const handleClick = React.useCallback( 7 () => { 8 const url = Utils.formatUrl({ query: { id } }); 9 clipboard.copy(url); // programmatically copying a value 10 }, 11 [clipboard.copy, id] 12 ); 13 14 return ( 15 <button onClick={handleClick}>Copy Link</button> 16 ); 17}
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.
1import { useClipboard } from 'use-clipboard-copy'; 2 3export default function PublicUrl({ url }) { 4 const clipboard = useClipboard({ 5 copiedTimeout: 600, // timeout duration in milliseconds 6 }); 7 return ( 8 <div> 9 <input ref={clipboard.target} value={url} readOnly /> 10 <button onClick={clipboard.copy}> 11 {clipboard.copied ? 'Copied' : 'Copy Link'} 12 </button> 13 </div> 14 ); 15}
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.
1function CopyText() { 2 const clipboard = useClipboard({ 3 onSuccess() { 4 console.log('Text was copied successfully!') 5 }, 6 onError() { 7 console.log('Failed to copy text!') 8 } 9 }); 10 return ( 11 <div> 12 <input ref={clipboard.target} /> 13 <button onClick={clipboard.copy}>Copy</button> 14 </div> 15 ); 16}
In case the copy
action fails, useClipboard
will handle that gracefully by selecting the target
input instead so that users can copy the text manually. This behavior can be disabled by passing selectOnError: false
to the clipboard options.
use-clipboard-copy
is supported in all browsers that supports the native clipboard APIs, including major browsers such as Chrome, Firefox, Edge, Safari, IE11.
This hook provides an isSupported
method that you can use to check for browser support and update the UI accordingly:
1function ClipboardSupport() { 2 const clipboard = useClipboard(); 3 return ( 4 <div> 5 {clipboard.isSupported() 6 ? "yay! copy-to-clipboard is supported" 7 : "meh. copy-to-clipboard is not supported"} 8 </div> 9 ); 10}
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.
1import { useClipboard } from 'use-clipboard-copy'; 2 3function CopyText() { 4 const clipboard = useClipboard(); 5 return ( 6 // ... 7 ); 8}
UseClipboardOptions
useClipboard
takes an optional object with the following properties:
copiedTimeout?: number
onSuccess?: () => void
onError?: () => void
selectOnCopy?: boolean
selectOnError?: boolean
copiedTimeout?: number
The duration in milliseconds used to toggled the copied
state upon a successful copy action.
onSuccess?: () => void
A callback function that will be called upon a successful copy action.
onError?: () => void
A callback function that will be called when the copy action fails.
selectOnCopy?: boolean
Defaults to false
.
A boolean indicating whether the text of the target
element (if set) will be selected upon a successful copy action.
selectOnError?: boolean
Defaults to true
.
A boolean indicating whether the text of the target
element (if set) will be selected when the copy action fails.
ClipboardAPI
useClipboard
returns an object with the following properties:
copied: boolean
copy: (text?: string) => void
isSupported: () => boolean
target: React.RefObject<any>
copy: (text?: string) => void
A method that will be used to preform the copy action. If it's used without passing a string, the copy
action will use the text of the target
element (if set).
1<input ref={clipboard.target} value="a text to copy" />; 2<button onClick={clipboard.copy} />;
Optionally, copy
takes a string
to perform the copy action imperatively (programmatically). When this is used, the target
element (if set) will be ignored.
1<button onClick={() => clipboard.copy('a text to copy')} />;
target: React.RefObject<any>
A React Ref object used on input and textarea elements that holds the text to be copied.
1<input ref={clipboard.target} value="a text to copy" />;
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.
This hook is powered by clipboard-copy, the lightweight copy to clipboard for the web.
MIT
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 2/17 approved changesets -- score normalized to 1
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Reason
74 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-05-26
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More