-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
40 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
``` |