-
-
Notifications
You must be signed in to change notification settings - Fork 29
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
90 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
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
35
sites/docs/src/lib/components/demos/use-click-outside.svelte
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,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> |