From 27ae74af504f2c6155facf74d5ccc1da52572334 Mon Sep 17 00:00:00 2001 From: StyleZhang Date: Sat, 12 Oct 2024 11:03:00 +0800 Subject: [PATCH] hook --- .../components/plugins/marketplace/hooks.ts | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 web/app/components/plugins/marketplace/hooks.ts diff --git a/web/app/components/plugins/marketplace/hooks.ts b/web/app/components/plugins/marketplace/hooks.ts new file mode 100644 index 0000000000..3846bbac2f --- /dev/null +++ b/web/app/components/plugins/marketplace/hooks.ts @@ -0,0 +1,20 @@ +import { useEffect } from 'react' + +export const useScrollIntersection = ( + rootRef: React.RefObject, + anchorRef: React.RefObject, + callback: (isIntersecting: boolean) => void, +) => { + useEffect(() => { + let observer: IntersectionObserver | undefined + if (rootRef.current && anchorRef.current) { + observer = new IntersectionObserver((entries) => { + callback(entries[0].isIntersecting) + }, { + root: rootRef.current, + }) + observer.observe(anchorRef.current) + } + return () => observer?.disconnect() + }, [rootRef, anchorRef, callback]) +}