npm i svelte-copy-to-clipboard
// OR
yarn add svelte-copy-to-clipboard
Note: to use this library in sapper, install as devDependency. See the link.
Local demo:
git clone https://github.com/henriquecaraujo/svelte-copy-to-clipboard.git
cd svelte-copy-to-clipboard
yarn && yarn dev
<script>
import CopyToClipboard from "svelte-copy-to-clipboard";
let exampleText = 'Copy me!';
const handleSuccessfullyCopied = (e) => {
alert(`successfully copied to clipboard! ${e}`);
}
const handleFailedCopy = () => {
alert('failed to copy :(');
}
</script>
<CopyToClipboard text={exampleText} on:copy={handleSuccessfullyCopied} on:fail={handleFailedCopy} let:copy>
<button on:click={copy}>An element to trigger the copy</button>
</CopyToClipboard>
Component props:
Prop | Type | Description |
---|---|---|
text | string | Value to copy |
copy | func | Call when text's copied |
fail | func | Call when copy fails |
Prop | Type | Description |
---|---|---|
onCopy | string | Call foi copy |
Download stats for this NPM package
Svelte Copy to Clipboard is open source software licensed as MIT.