Skip to content

Commit

Permalink
docs: add svelte example
Browse files Browse the repository at this point in the history
  • Loading branch information
sebkolind committed Aug 13, 2024
1 parent 0ffa528 commit b8908e2
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/.vitepress/config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export default defineConfig({
items: [
{text: "Vue", link: "/examples/vue"},
{text: "React", link: "/examples/react"},
{text: "Svelte", link: "/examples/svelte"},
],
},
],
Expand Down
39 changes: 39 additions & 0 deletions docs/examples/svelte.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Svelte

This is how you initialize in Svelte.

In this example we are using the `onMount` and `onDestroy` lifecycle functions to initialize and destroy the draggy instance. We are also using the `bind:this` directive to get a reference to the target element.

:::tip NOTE
It is important to destroy the draggy instance when the component is destroyed to avoid memory leaks. This is done in the `onDestroy` lifecycle function.
:::

```svelte
<script>
import { onMount, onDestroy } from "svelte";
import { draggy } from "@sebkolind/draggy";
let drag;
let target;
let items = [
{ id: 1, title: "Draggable #1" },
{ id: 2, title: "Draggable #2" },
{ id: 3, title: "Draggable #3" },
];
onMount(() => {
drag = draggy({ target });
});
onDestroy(() => {
drag.destroy();
});
</script>
<div bind:this={target}>
{#each items as item (item.id)}
<div>{item.title}</div>
{/each}
</div>
```

0 comments on commit b8908e2

Please sign in to comment.