Skip to content

Commit

Permalink
add docs page
Browse files Browse the repository at this point in the history
  • Loading branch information
sviripa committed May 8, 2024
1 parent d1ffadd commit b9300ee
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 0 deletions.
55 changes: 55 additions & 0 deletions sites/docs/content/functions/use-click-outside.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
---
title: useClickOutside
description:
A function that calls a callback when a click event is triggered outside of a given container
element.
category: Elements
---

<script>
import Demo from '$lib/components/demos/use-click-outside.svelte';
</script>

## Demo

<Demo />

## Usage

```svelte
<script lang="ts">
import { box, useClickOutside } from "runed";
const container = box<HTMLDivElement | null>(null);
useClickOutside(container, () => {
console.log("clicked outside of container");
});
</script>
<main>
<div bind:this={container.value}>Container</div>
<button>Click Me</button>
</main>
```

You can also programmatically pause and resume `useClickOutside` using the `start` and `stop`
functiosn returned by `useClickOutside`.

```svelte
<script lang="ts">
import { box, useClickOutside } from "runed";
const container = box<HTMLDivElement | null>(null);
const outsideClick = useClickOutside(container, () => {
console.log("clicked outside of container");
});
</script>
<main>
<button onclick={outsideClick.stop}>Stop listening for outside clicks</button>
<button onclick={outsideClick.start}>Start listening again</button>
<div bind:this={container.value}></div>
</main>
```
35 changes: 35 additions & 0 deletions sites/docs/src/lib/components/demos/use-click-outside.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<script lang="ts">
import { box, useClickOutside } from "runed";
let containerText = "Has not clicked yet";
const container = box<HTMLDivElement | null>(null);
useClickOutside(container, () => {
containerText = "Has clicked outside of container";
});
</script>

<main>
<div bind:this={container.value}>
<p>{containerText}</p>

<button onclick={() => (containerText = "Has clicked within container")}>
Button within container
</button>
</div>
<button>Button outside container</button>
</main>

<style>
div {
padding: 40px;
border: 1px solid;
margin-bottom: 16px;
}
button {
border: 1px solid;
padding-inline: 8px;
}
</style>

0 comments on commit b9300ee

Please sign in to comment.