fixed code
This commit is contained in:
20
src/hooks/useClickOutside.jsx
Normal file
20
src/hooks/useClickOutside.jsx
Normal file
@@ -0,0 +1,20 @@
|
||||
import { useEffect } from "react";
|
||||
|
||||
/**
|
||||
* Вызывает callback при клике вне элемента, на который повешен ref.
|
||||
* @param {React.RefObject} ref
|
||||
* @param {Function} onOutsideClick
|
||||
* @param {boolean} enabled — можно временно отключать слушатель
|
||||
*/
|
||||
export function useClickOutside(ref, onOutsideClick, enabled = true) {
|
||||
useEffect(() => {
|
||||
if (!enabled) return;
|
||||
const handleClick = (e) => {
|
||||
if (ref.current && !ref.current.contains(e.target)) {
|
||||
onOutsideClick();
|
||||
}
|
||||
};
|
||||
document.addEventListener("mousedown", handleClick);
|
||||
return () => document.removeEventListener("mousedown", handleClick);
|
||||
}, [ref, onOutsideClick, enabled]);
|
||||
}
|
||||
Reference in New Issue
Block a user