diff options
-rw-r--r-- | thallium-frontend/src/utils/hooks.ts | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/thallium-frontend/src/utils/hooks.ts b/thallium-frontend/src/utils/hooks.ts new file mode 100644 index 0000000..9b478f7 --- /dev/null +++ b/thallium-frontend/src/utils/hooks.ts @@ -0,0 +1,15 @@ +import { useMemo, useState, useEffect } from "react"; + +export function useVisible(ref: RefObject<HTMLElement>) { + const [isVisible, setVisible] = useState(false) + + useEffect(() => { + const observer = new IntersectionObserver( + ([entry]) => setVisible(entry.isIntersecting) + ) + observer.observe(ref.current) + return () => observer.disconnect() + }, []) + + return isVisible +} |