1.0.0 • Published 4 years ago

copy-text-to-clipboard-async v1.0.0

Weekly downloads
2
License
MIT
Repository
github
Last release
4 years ago

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