Skip to content

Commit

Permalink
docs: add "Clean-up" example
Browse files Browse the repository at this point in the history
  • Loading branch information
sebkolind committed Jul 31, 2024
1 parent 8644bee commit 782fe6d
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 1 deletion.
3 changes: 2 additions & 1 deletion docs/.vitepress/config.mts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,12 @@ export default defineConfig({
items: [
{ text: "Custom Shadow", link: "/examples/custom-shadow" },
{ text: "Restrict a Drop", link: "/examples/restrict-a-drop" },
{ text: "Clean-up (destroy)", link: "/examples/clean-up" },
{
text: "Frameworks",
items: [
{ text: "React", link: "/examples/react" },
{ text: "Vue", link: "/examples/vue" },
{ text: "React", link: "/examples/react" },
],
},
],
Expand Down
72 changes: 72 additions & 0 deletions docs/examples/clean-up.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# Clean-up (`destroy`)

To ensure proper clean-up you should use the `destroy` method returned by `draggy()`.
This will help avoid memory leaks and other potential issues.

```ts
import { draggy } from "@sebkolind/draggy";

const { destroy } = draggy({ target: ".container" });

// Call when `draggy` is no longer needed
destroy();
```

## Vue

When using Vue you can manage the initialization and destruction of the Draggy instance using Vue's lifecycle hooks.

```vue
<template>
<div class="container">
<div>Draggable</div>
<div>Draggable</div>
<div>Draggable</div>
</div>
</template>
<script setup>
import { draggy } from "@sebkolind/draggy";
let instance;
onMounted(() => {
instance = draggy({ target: ".container" });
});
onUnmounted(() => {
instance?.destroy();
});
</script>
```

## React

If used with React you can use the `useEffect` hook to handle the
initialization and clean-up of the Draggy instance.

```tsx
import { useEffect } from "react";
import { draggy } from "@sebkolind/draggy";

const DraggableComponent = () => {
useEffect(() => {
const { destroy } = draggy({ target: ".container" });

// Clean up on component unmount
return () => {
destroy();
};
}, []);

return (
<div className="container">
<div>Draggable</div>
<div>Draggable</div>
<div>Draggable</div>
</div>
);
};

export { DraggableComponent };
```

0 comments on commit 782fe6d

Please sign in to comment.