ui/hooks: add clickOutside hook
This commit is contained in:
15
pkgs/clan-app/ui/src/hooks/useClickOutside.tsx
Normal file
15
pkgs/clan-app/ui/src/hooks/useClickOutside.tsx
Normal file
@@ -0,0 +1,15 @@
|
||||
import { onCleanup } from "solid-js";
|
||||
|
||||
export function useClickOutside(
|
||||
el: () => HTMLElement | undefined,
|
||||
handler: (e: MouseEvent) => void,
|
||||
) {
|
||||
const listener = (e: MouseEvent) => {
|
||||
const element = el();
|
||||
if (element && !element.contains(e.target as Node)) {
|
||||
handler(e);
|
||||
}
|
||||
};
|
||||
document.addEventListener("mousedown", listener);
|
||||
onCleanup(() => document.removeEventListener("mousedown", listener));
|
||||
}
|
||||
Reference in New Issue
Block a user