mirror of
https://github.com/zkldi/Tachi.git
synced 2026-10-11 08:48:15 +03:00
22 lines
641 B
TypeScript
22 lines
641 B
TypeScript
import { useState, useEffect, useRef } from "react";
|
|
|
|
export default function useComponentVisible(initialIsVisible: boolean) {
|
|
const [isComponentVisible, setIsComponentVisible] = useState(initialIsVisible);
|
|
const ref = useRef<any>(null);
|
|
|
|
const handleClickOutside = (event: MouseEvent) => {
|
|
if (ref.current && !ref.current.contains(event.target)) {
|
|
setIsComponentVisible(false);
|
|
}
|
|
};
|
|
|
|
useEffect(() => {
|
|
document.addEventListener("click", handleClickOutside, true);
|
|
return () => {
|
|
document.removeEventListener("click", handleClickOutside, true);
|
|
};
|
|
}, []);
|
|
|
|
return { ref, isComponentVisible, setIsComponentVisible };
|
|
}
|