Skip to content


Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Latest commit


Git stats


Failed to load latest commit information.
Latest commit message
Commit time

Svelte Copy

Ever wanted to copy something to clipboard? Say hello to Svelte Copy



npm install svelte-copy -D


Let's make a button that when you click it copies Hello World to the clipboard:

    import { copy } from 'svelte-copy';

<button use:copy={'Hello World'}>
    Click me!


There are some custom events you can use on elements that have the copy action:

  • on:svelte-copy This will fire when text is copied, you have access to the copied text if needed with event.detail:

        use:copy={'Hello from alert'}
        on:svelte-copy={(event) => alert(event.detail)}>
        Click to cause alert on copy
  • on:svelte-copy:error This event will fire if there is an error in copying to clipboard, you have access to the error with event.detail:

        use:copy={'Some text'}
        on:svelte-copy:error="{(event) =>
            alert(`There was an error: ${event.detail.message}`)}">
        Click to cause alert on copy

Custom Triggers

By default, copy action is fired on click event. You can change this behavior by passing an object with the name or names of the events that you want to trigger the copy action.

    use:copy={{ text: 'Hello' , events: ['touchstart', 'mouseenter']}}
    on:svelte-copy={(event) => alert(event.detail)}>
    Move cursor over button or touch button to cause alert on copy

Copy Text

We also include a helper called copyText if you want to use the logic of this package without the action. Make sure you only call this in the browser!

    import { copyText } from 'svelte-copy';

<button on:click={() => copyText('Hello World!')}>
    Copy something