{"id":13679454,"url":"https://github.com/wsmd/use-clipboard-copy","last_synced_at":"2025-05-16T07:06:39.069Z","repository":{"id":34504244,"uuid":"179410474","full_name":"wsmd/use-clipboard-copy","owner":"wsmd","description":"📋 Lightweight copy to clipboard hook for React","archived":false,"fork":false,"pushed_at":"2023-01-07T04:27:27.000Z","size":864,"stargazers_count":388,"open_issues_count":17,"forks_count":17,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-05-09T10:21:35.363Z","etag":null,"topics":["clipboard","clipboard-copy","clipboard-js","clipboardjs","copy-to-clipboard","hook","react","react-hooks","reactjs"],"latest_commit_sha":null,"homepage":"https://use-clipboard-copy.now.sh","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/wsmd.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-04-04T02:57:48.000Z","updated_at":"2025-04-08T07:35:29.000Z","dependencies_parsed_at":"2023-01-15T07:30:58.299Z","dependency_job_id":null,"html_url":"https://github.com/wsmd/use-clipboard-copy","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wsmd%2Fuse-clipboard-copy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wsmd%2Fuse-clipboard-copy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wsmd%2Fuse-clipboard-copy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/wsmd%2Fuse-clipboard-copy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/wsmd","download_url":"https://codeload.github.com/wsmd/use-clipboard-copy/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":254485063,"owners_count":22078767,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["clipboard","clipboard-copy","clipboard-js","clipboardjs","copy-to-clipboard","hook","react","react-hooks","reactjs"],"created_at":"2024-08-02T13:01:05.747Z","updated_at":"2025-05-16T07:06:34.056Z","avatar_url":"https://github.com/wsmd.png","language":"TypeScript","funding_links":[],"categories":["TypeScript"],"sub_categories":[],"readme":"\u003ch1 align=\"center\"\u003e\n  \u003cimg src=\"https://user-images.githubusercontent.com/2100222/55596467-9c054980-5716-11e9-8723-b8c286fcd9e3.png\" width=\"128\"\u003e\n  \u003cbr\u003e\n  use-clipboard-copy\n\u003c/h1\u003e\n\n\u003cp align=\"center\"\u003e\n  \u003ca href=\"https://www.npmjs.com/package/use-clipboard-copy\"\u003e\n    \u003cimg src=\"https://img.shields.io/npm/v/use-clipboard-copy.svg\" alt=\"Current Release\" /\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://www.npmjs.com/package/use-clipboard-copy\"\u003e\n    \u003cimg src=\"https://badgen.net/npm/dt/use-clipboard-copy\" alt=\"Downloads\" /\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://travis-ci.org/wsmd/use-clipboard-copy\"\u003e\n    \u003cimg src=\"https://travis-ci.org/wsmd/use-clipboard-copy.svg?branch=master\" alt=\"CI Build\"\u003e\n  \u003c/a\u003e\n  \u003ca href='https://coveralls.io/github/wsmd/use-clipboard-copy?branch=master'\u003e\n    \u003cimg src='https://coveralls.io/repos/github/wsmd/use-clipboard-copy/badge.svg?branch=master' alt='Coverage Status' /\u003e\n  \u003c/a\u003e\n  \u003ca href=\"https://github.com/wsmd/use-clipboard-copy/blob/master/LICENSE\"\u003e\n    \u003cimg src=\"https://img.shields.io/github/license/wsmd/use-clipboard-copy.svg\" alt=\"Licence\"\u003e\n  \u003c/a\u003e\n\u003c/p\u003e\n\n\u003cdetails\u003e\n\u003csummary\u003e📖 Table of Contents\u003c/summary\u003e\n\u003cp\u003e\n\n- [Motivation](#motivation)\n- [Getting Started](#getting-started)\n- [Usage](#usage)\n  - [Copying Text of Another Target Element](#copying-text-of-another-target-element)\n  - [Copying Text Imperatively (Without a Target Element)](#copying-text-imperatively-without-a-target-element)\n  - [Displaying a Temporary Success State](#displaying-a-temporary-success-state)\n  - [Handling Success and Errors](#handling-success-and-errors)\n- [Browser Support](#browser-support)\n- [API](#api)\n  - [`useClipboard(options?: UseClipboardOptions): ClipboardAPI`](#useclipboardoptions-useclipboardoptions-clipboardapi)\n  - [`UseClipboardOptions`](#useclipboardoptions)\n    - [`copiedTimeout?: number`](#copiedtimeout-number)\n    - [`onSuccess?: () =\u003e void`](#onsuccess---void)\n    - [`onError?: () =\u003e void`](#onerror---void)\n    - [`selectOnCopy?: boolean`](#selectoncopy-boolean)\n    - [`selectOnError?: boolean`](#selectonerror-boolean)\n  - [`ClipboardAPI`](#clipboardapi)\n    - [`copy: (text?: string) =\u003e void`](#copy-text-string--void)\n    - [`target: React.RefObject\u003cany\u003e`](#target-reactrefobjectany)\n    - [`isSupported: () =\u003e boolean`](#issupported---boolean)\n    - [`copied: boolean`](#copied-boolean)\n- [Acknowledgements](#acknowledgements)\n- [License](#license)\n\n\u003c/p\u003e\n\u003c/details\u003e\n\n## Motivation\n\nThere 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_.\n\n`use-clipboard-copy` is a **lightweight** (\u003c 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:\n\n```js\nfunction CopyText() {\n  const clipboard = useClipboard();\n  return (\n    \u003cdiv\u003e\n      \u003cinput ref={clipboard.target} /\u003e\n      \u003cbutton onClick={clipboard.copy}\u003eCopy\u003c/button\u003e\n    \u003c/div\u003e\n  );\n}\n```\n\nP.S. You can do more than that with `use-clipboard-copy`. Keep reading!\n\n## Getting Started\n\nTo get started, add `use-clipboard-copy` to your project:\n\n```\nnpm install --save use-clipboard-copy\n```\n\nPlease note that `use-clipboard-copy` requires `react@^16.8.0` as a peer dependency.\n\n## Usage\n\n### Copying Text of Another Target Element\n\nA simple copy-to-clipboard interface consists of two parts:\n\n- The `target`, an element who holds the value to be copied, usually an input.\n- The `copy` action.\n\n```jsx\nimport { useClipboard } from 'use-clipboard-copy';\n\nexport default function PublicUrl({ url }) {\n  const clipboard = useClipboard();\n  return (\n    \u003cdiv\u003e\n      \u003cinput ref={clipboard.target} value={url} readOnly /\u003e\n      \u003cbutton onClick={clipboard.copy}\u003eCopy Link\u003c/button\u003e\n    \u003c/div\u003e\n  );\n}\n```\n\n### Copying Text Imperatively (Without a Target Element)\n\nIt 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.\n\n```jsx\nimport { useClipboard } from 'use-clipboard-copy';\n\nexport default function PublicUrl({ id }) {\n  const clipboard = useClipboard();\n\n  const handleClick = React.useCallback(\n    () =\u003e {\n      const url = Utils.formatUrl({ query: { id } });\n      clipboard.copy(url); // programmatically copying a value\n    },\n    [clipboard.copy, id]\n  );\n\n  return (\n    \u003cbutton onClick={handleClick}\u003eCopy Link\u003c/button\u003e\n  );\n}\n```\n\n### Displaying a Temporary Success State\n\nSometimes 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.\n\nBy passing the `copiedTimeout` option to `useClipboard()`, you can use `clipboard.copied` as a way to toggle the copied state in the UI.\n\n```jsx\nimport { useClipboard } from 'use-clipboard-copy';\n\nexport default function PublicUrl({ url }) {\n  const clipboard = useClipboard({\n    copiedTimeout: 600, // timeout duration in milliseconds\n  });\n  return (\n    \u003cdiv\u003e\n      \u003cinput ref={clipboard.target} value={url} readOnly /\u003e\n      \u003cbutton onClick={clipboard.copy}\u003e\n        {clipboard.copied ? 'Copied' : 'Copy Link'}\n      \u003c/button\u003e\n    \u003c/div\u003e\n  );\n}\n```\n\n### Handling Success and Errors\n\nCopy 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.\n\nBy passing an `onSuccess` and `onError` callbacks to the `useClipboard` options, you will be able to handle whether the `copy` action was performed successfully.\n\n```js\nfunction CopyText() {\n  const clipboard = useClipboard({\n    onSuccess() {\n      console.log('Text was copied successfully!')\n    },\n    onError() {\n      console.log('Failed to copy text!')\n    }\n  });\n  return (\n    \u003cdiv\u003e\n      \u003cinput ref={clipboard.target} /\u003e\n      \u003cbutton onClick={clipboard.copy}\u003eCopy\u003c/button\u003e\n    \u003c/div\u003e\n  );\n}\n```\n\nIn 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.\n\n## Browser Support\n\n`use-clipboard-copy` is supported in all browsers that [supports the native clipboard APIs](https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Interact_with_the_clipboard), including major browsers such as **Chrome, Firefox, Edge, Safari, IE11**.\n\nThis hook provides an `isSupported` method that you can use to check for browser support and update the UI accordingly:\n\n```jsx\nfunction ClipboardSupport() {\n  const clipboard = useClipboard();\n  return (\n    \u003cdiv\u003e\n      {clipboard.isSupported()\n        ? \"yay! copy-to-clipboard is supported\"\n        : \"meh. copy-to-clipboard is not supported\"}\n    \u003c/div\u003e\n  );\n}\n```\n\n\n\n## API\n\n### `useClipboard(options?: UseClipboardOptions): ClipboardAPI`\n\n`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.\n\n```js\nimport { useClipboard } from 'use-clipboard-copy';\n\nfunction CopyText() {\n  const clipboard = useClipboard();\n  return (\n    // ...\n  );\n}\n```\n\n### `UseClipboardOptions`\n\n`useClipboard` takes an optional object with the following properties:\n\n- [`copiedTimeout?: number`](#copiedtimeout-number)\n- [`onSuccess?: () =\u003e void`](#onsuccess---void)\n- [`onError?: () =\u003e void`](#onerror---void)\n- [`selectOnCopy?: boolean`](#selectoncopy-boolean)\n- [`selectOnError?: boolean`](#selectonerror-boolean)\n\n#### `copiedTimeout?: number`\n\nThe duration in milliseconds used to toggled the [`copied`](#copied-boolean) state upon a successful copy action.\n\n#### `onSuccess?: () =\u003e void`\n\nA callback function that will be called upon a successful copy action.\n\n#### `onError?: () =\u003e void`\n\nA callback function that will be called when the copy action fails.\n\n#### `selectOnCopy?: boolean`\n\nDefaults to `false`.\n\nA boolean indicating whether the text of the [`target`](#target-reactrefobjectany) element (if set) will be selected upon a successful copy action.\n\n#### `selectOnError?: boolean`\n\nDefaults to `true`.\n\nA boolean indicating whether the text of the [`target`](#target-reactrefobjectany) element (if set) will be selected when the copy action fails.\n\n### `ClipboardAPI`\n\n`useClipboard` returns an object with the following properties:\n\n- [`copied: boolean`](#copied-boolean)\n- [`copy: (text?: string) =\u003e void`](#copy-text-string--void)\n- [`isSupported: () =\u003e boolean`](#issupported---boolean)\n- [`target: React.RefObject\u003cany\u003e`](#target-reactrefobjectany)\n\n#### `copy: (text?: string) =\u003e void`\n\nA 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`](#target-reactrefobjectany) element (if set).\n\n```jsx\n\u003cinput ref={clipboard.target} value=\"a text to copy\" /\u003e;\n\u003cbutton onClick={clipboard.copy} /\u003e;\n```\n\nOptionally, `copy` takes a `string` to perform the copy action imperatively (programmatically). When this is used, the [`target`](#target-reactrefobjectany) element (if set) will be ignored.\n\n```jsx\n\u003cbutton onClick={() =\u003e clipboard.copy('a text to copy')} /\u003e;\n```\n\n#### `target: React.RefObject\u003cany\u003e`\n\nA React Ref object used on input and textarea elements that holds the text to be copied.\n\n```jsx\n\u003cinput ref={clipboard.target} value=\"a text to copy\" /\u003e;\n```\n\n#### `isSupported: () =\u003e boolean`\n\nA 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.\n\n#### `copied: boolean`\n\nA boolean indicating whether the copy action was just performed. Must be used with the [`copiedTimeout`](#copiedtimeout-number) option. Useful to update the UI to display temporary success state.\n\n## Acknowledgements\n\nThis hook is powered by [clipboard-copy](https://github.com/feross/clipboard-copy), the lightweight copy to clipboard for the web.\n\n## License\n\nMIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwsmd%2Fuse-clipboard-copy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fwsmd%2Fuse-clipboard-copy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fwsmd%2Fuse-clipboard-copy/lists"}