clipboard

Reactive Clipboard API. Provides the ability to respond to clipboard commands (cut, copy, and paste) as well as to asynchronously read from and write to the system clipboard. Access to the contents of the clipboard is gated behind the Permissions API. Without user permission, reading or altering the clipboard contents is not permitted.

Demo

Your browser does not support Clipboard API

Usage

<script>
import { clipboard } from "svelte-legos";

let value = "";
const board = clipboard();
const readableBoard = clipboard({ read: true });

$: isSupported = $board.isSupported;
$: copy = $board.copy;
$: copied = $board.copied;

$: copiedText = $readableBoard.text; // or $board.text if not reading
</script>

<div>
    {#if isSupported}
        <div>
            <p>
                Current copied: {copiedText}
            </p>
            <input bind:value type="text" />
            <button on:click={() => copy(value)}>
                <!-- by default, `copied` will be reset in 1.5s -->
                {#if !copied}
                    <span>Copy</span>
                {:else}
                    <span>Copied!</span>
                {/if}
            </button>
        </div>
    {:else}
        <p>Your browser does not support Clipboard API</p>
    {/if}
</div>