copy-text-to-clipboard-async
Version:
Copy text to the clipboard in modern browsers
47 lines (37 loc) • 1.42 kB
TypeScript
interface Options {
/**
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.
@default document.body
@example
```
import copy from 'copy-text-to-clipboard';
const modalWithFocusTrap = document.getElementById('modal');
button.addEventListener('click', () => {
copy('🦄🌈', { target: modalWithFocusTrap })
.then(() => console.log('🎉'))
.catch(() => console.error('😢'));
});
```
*/
target?: HTMLElement;
}
/**
Copy text to the clipboard.
Must be called in response to a user gesture event, like `click` or `keyup`.
@param text - The text to copy to clipboard.
@param options - Copy options.
@returns Promise that resolves if the text was successfully copied or rejects
if the operation failed.
@example
```
import copy from 'copy-text-to-clipboard-async';
button.addEventListener('click', () => {
copy('🦄🌈')
.then(() => console.log('🎉'))
.catch(() => console.error('😢'));
});
```
*/
declare const copyTextToClipboard: (input: string, { target }?: Options) => Promise<void>;
export default copyTextToClipboard;