Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sindresorhus/copy-text-to-clipboard
Copy text to the clipboard in modern browsers (0.2 kB)
https://github.com/sindresorhus/copy-text-to-clipboard
Last synced: 27 days ago
JSON representation
Copy text to the clipboard in modern browsers (0.2 kB)
- Host: GitHub
- URL: https://github.com/sindresorhus/copy-text-to-clipboard
- Owner: sindresorhus
- License: mit
- Created: 2017-05-22T07:11:34.000Z (over 7 years ago)
- Default Branch: main
- Last Pushed: 2023-07-05T22:01:37.000Z (over 1 year ago)
- Last Synced: 2024-04-13T21:27:21.839Z (7 months ago)
- Language: JavaScript
- Homepage:
- Size: 26.4 KB
- Stars: 972
- Watchers: 8
- Forks: 234
- Open Issues: 2
-
Metadata Files:
- Readme: readme.md
- Funding: .github/funding.yml
- License: license
- Security: .github/security.md
Awesome Lists containing this project
- awesome-github-star - copy-text-to-clipboard
README
# copy-text-to-clipboard
> Copy text to the clipboard in modern browsers *(0.2 kB)*
[Try it out!](https://jsfiddle.net/sindresorhus/6406v3pf/)
## Comparison
- This module: **0.2 kB**
- [`clipboard.js`](https://github.com/zenorocha/clipboard.js): 3.4 kB## Install
```sh
npm install copy-text-to-clipboard
```## Usage
```js
import copy from 'copy-text-to-clipboard';button.addEventListener('click', () => {
copy('🦄🌈');
});
```## API
### copy(text, options?)
Copy `text` to the clipboard.
Returns a `boolean` of whether it succeeded to copy the text.
Must be called in response to a user gesture event, like `click` or `keyup`.
#### options
Type: `object`
##### target
Type: `HTMLElement`\
Default: `document.body`Specify a DOM element where the temporary, behind-the-scenes `textarea` should be appended, in cases where you need to stay within a focus trap, like in a modal.
## Related
- [clipboardy](https://github.com/sindresorhus/clipboardy) - Access the system clipboard (copy/paste) in Node.js