Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 41 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Svelte Copy

A svelte action to copy text to clipboard. It uses the `navigator.clipboard` api, with a fallback to the legacy method.
A svelte attachment/action to copy text to clipboard. It uses the `navigator.clipboard` api, with a fallback to the legacy method.

# Installing

Expand All @@ -10,6 +10,8 @@ npm install svelte-copy -D

This library only works with Svelte 5, checkout [svelte-copy@1](https://www.npmjs.com/package/svelte-copy/v/1.4.2) for Svelte 3/4 support.

[Actions (use:)](https://svelte.dev/docs/svelte/use) and [attachments (@attach)](https://svelte.dev/docs/svelte/@attach) are both supported.

# Usage

The simplest use is to just pass the text you want to copy:
Expand All @@ -19,7 +21,17 @@ The simplest use is to just pass the text you want to copy:
import { copy } from 'svelte-copy';
</script>

<button use:copy={'Hello World'}>
<button {@attach copy('Hello World')}>
Click me!
</button>
```

```svelte
<script>
import { copyAction } from 'svelte-copy';
</script>

<button use:copyAction={'Hello World'}>
Click me!
</button>
```
Expand All @@ -32,7 +44,28 @@ You can expand that with an options object:
</script>

<button
use:copy={{
{@attach copy({
text,
events: ['click'],
onCopy({ text, event }) {
alert(`Text copied: "${text}". Triggered by "${event}"`);
},
onError({ error, event }) {
alert(error.message)
}
})}
>
Copy
</button>
```

```svelte
<script>
import { copyAction } from 'svelte-copy';
</script>

<button
use:copyAction={{
text,
events: ['click'],
onCopy({ text, event }) {
Expand All @@ -49,6 +82,11 @@ You can expand that with an options object:

[Read the full docs here](https://svelte-copy.willow.codes).

# Migrating from v2 to v3

- `copy` now refers to the attachment.
- To use the action, use `copyAction`.

# Migrating from v1 to v2

- The `on:svelte-copy` event is now a `onCopy` param to the action options.
Expand Down
2 changes: 2 additions & 0 deletions mise.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[tools]
node = "lts"
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"runed": "^0.15.3",
"sass": "^1.80.3",
"shiki": "^1.22.0",
"svelte": "^5.0.2",
"svelte": "^5.29.0",
"typescript": "^5.6.3",
"vite": "^5.4.9",
"vitest": "^2.1.3"
Expand Down
Loading