-
-
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
5 changed files
with
114 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,5 @@ | ||
--- | ||
"runed": minor | ||
--- | ||
|
||
feat: `useClickOutside` |
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 @@ | ||
export * from "./useClickOutside.svelte.js"; |
52 changes: 52 additions & 0 deletions
52
packages/runed/src/lib/functions/useClickOutside/useClickOutside.svelte.ts
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,52 @@ | ||
import { box, type WritableBox } from "$lib/functions/box/box.svelte.js"; | ||
import { watch } from "$lib/functions/watch/watch.svelte.js"; | ||
|
||
type ClickOutside = { | ||
start: () => void; | ||
stop: () => void; | ||
}; | ||
|
||
/** | ||
* Accepts a box which holds a container element and callback function. | ||
* Invokes the callback function when the user clicks outside of the | ||
* container. | ||
* | ||
* @returns an object with start and stop functions | ||
* | ||
* @see {@link https://runed.dev/docs/functions/use-click-outside} | ||
*/ | ||
export function useClickOutside<T extends Element>( | ||
container: WritableBox<T | null>, | ||
fn: () => void | ||
): ClickOutside { | ||
const isEnabled = box<boolean>(true); | ||
|
||
function start() { | ||
isEnabled.value = true; | ||
} | ||
|
||
function stop() { | ||
isEnabled.value = false; | ||
} | ||
|
||
function handleClick(event: MouseEvent) { | ||
if (event.target && !container.value?.contains(event.target as Node)) { | ||
fn(); | ||
} | ||
} | ||
|
||
watch([container, isEnabled], ([currentContainer, currentIsEnabled]) => { | ||
if (currentContainer && currentIsEnabled) { | ||
window.addEventListener("click", handleClick); | ||
} | ||
|
||
return () => { | ||
window.removeEventListener("click", handleClick); | ||
}; | ||
}); | ||
|
||
return { | ||
start, | ||
stop, | ||
}; | ||
} |
55 changes: 55 additions & 0 deletions
55
packages/runed/src/lib/functions/useClickOutside/useClickOutside.test.svelte.ts
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 @@ | ||
import { describe, expect, vi } from "vitest"; | ||
import { tick } from "svelte"; | ||
import { testWithEffect } from "$lib/test/util.svelte.js"; | ||
import { box } from "$lib/functions/box/box.svelte.js"; | ||
import { useClickOutside } from "./useClickOutside.svelte.js"; | ||
|
||
describe("useClickOutside", () => { | ||
testWithEffect("calls a given callback when on an outside of container click", async () => { | ||
const container = document.createElement("div"); | ||
const innerButton = document.createElement("button"); | ||
const button = document.createElement("button"); | ||
|
||
document.body.appendChild(container); | ||
document.body.appendChild(button); | ||
container.appendChild(innerButton); | ||
|
||
const callbackFn = vi.fn(); | ||
|
||
useClickOutside(box.from(container), callbackFn); | ||
await tick(); | ||
|
||
button.dispatchEvent(new MouseEvent("click", { bubbles: true })); | ||
expect(callbackFn).toHaveBeenCalledOnce(); | ||
|
||
innerButton.dispatchEvent(new MouseEvent("click", { bubbles: true })); | ||
expect(callbackFn).toHaveBeenCalledOnce(); | ||
|
||
container.dispatchEvent(new MouseEvent("click", { bubbles: true })); | ||
expect(callbackFn).toHaveBeenCalledOnce(); | ||
}); | ||
|
||
testWithEffect("can be paused and resumed", async () => { | ||
const container = document.createElement("div"); | ||
const button = document.createElement("button"); | ||
|
||
document.body.appendChild(container); | ||
document.body.appendChild(button); | ||
|
||
const callbackFn = vi.fn(); | ||
|
||
const clickOutside = useClickOutside(box.from(container), callbackFn); | ||
|
||
clickOutside.stop(); | ||
await tick(); | ||
|
||
button.dispatchEvent(new MouseEvent("click", { bubbles: true })); | ||
expect(callbackFn).not.toHaveBeenCalled(); | ||
|
||
clickOutside.start(); | ||
await tick(); | ||
|
||
button.dispatchEvent(new MouseEvent("click", { bubbles: true })); | ||
expect(callbackFn).toHaveBeenCalledOnce(); | ||
}); | ||
}); |