Initial commit
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
import { useEffect, type RefObject } from "react";
|
||||
|
||||
export function useClickOutside<T extends HTMLElement>(
|
||||
ref: RefObject<T | null>,
|
||||
onOutside: () => void,
|
||||
enabled = true,
|
||||
) {
|
||||
useEffect(() => {
|
||||
if (!enabled) return;
|
||||
const handler = (e: MouseEvent) => {
|
||||
if (ref.current && !ref.current.contains(e.target as Node)) onOutside();
|
||||
};
|
||||
document.addEventListener("mousedown", handler);
|
||||
return () => document.removeEventListener("mousedown", handler);
|
||||
}, [ref, onOutside, enabled]);
|
||||
}
|
||||
Reference in New Issue
Block a user