copy-text-to-clipboard-async
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

Copy text to the clipboard in modern browsers

This library extends copy-text-to-clipboard, adding promise and Clipboard API support. If the current browser environment supports the Clipboard API then that will be used, otherwise it will fall back to using document.execCommand('copy').

Install

$ npm install copy-text-to-clipboard-async

Usage

import copy from 'copy-text-to-clipboard-async';
 
button.addEventListener('click', () => {
  copy('🦄🌈')
    .then(() => console.log('🎉'))
    .catch(() => console.error('😢'));
});

API

copy(text, options?)

Copy text to the clipboard.

Returns a promise that resolves if the text was successfully copied or rejects if the operation failed.

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.

Note: This option will only have an effect if the browser environment does not support the Clipboard API.

Related

  • copy-text-to-clipboard - The original library this is based on
  • clipboardy - Access the system clipboard (copy/paste) in Node.js (from the same, original author)

Dependencies (0)

    Dev Dependencies (6)

    Package Sidebar

    Install

    npm i copy-text-to-clipboard-async

    Weekly Downloads

    9

    Version

    1.0.0

    License

    MIT

    Unpacked Size

    9.97 kB

    Total Files

    6

    Last publish

    Collaborators

    • mapref41