|
| 1 | +import { useCallback, useEffect, useRef, useState } from 'react' |
| 2 | + |
| 3 | +type RootRef = HTMLElement | null | React.RefObject<HTMLElement> |
| 4 | + |
| 5 | +export interface UseIntersectionOptions { |
| 6 | + /** IntersectionObserver threshold */ |
| 7 | + threshold?: number |
| 8 | + /** Root element or ref to observe within */ |
| 9 | + root?: RootRef |
| 10 | + /** Root margin */ |
| 11 | + rootMargin?: string |
| 12 | + /** Whether to disconnect after first intersection (default: true) */ |
| 13 | + once?: boolean |
| 14 | +} |
| 15 | + |
| 16 | +export const useIntersection = (options: UseIntersectionOptions = {}) => { |
| 17 | + const { threshold = 0.15, root = null, rootMargin = '0px', once = true } = options |
| 18 | + |
| 19 | + // internal storage |
| 20 | + const internalRef = useRef<HTMLElement | null>(null) |
| 21 | + const [observedNode, setObservedNode] = useState<HTMLElement | null>(null) |
| 22 | + const [isVisible, setIsVisible] = useState(false) |
| 23 | + |
| 24 | + // callback ref so we react to node mount/unmount |
| 25 | + const ref = useCallback((node: HTMLElement | null) => { |
| 26 | + internalRef.current = node |
| 27 | + setObservedNode(node) |
| 28 | + }, []) |
| 29 | + |
| 30 | + // resolve root (may be a ref) |
| 31 | + const resolvedRoot = |
| 32 | + root && 'current' in (root as React.RefObject<HTMLElement>) |
| 33 | + ? (root as React.RefObject<HTMLElement>).current |
| 34 | + : (root as HTMLElement | null) |
| 35 | + |
| 36 | + useEffect(() => { |
| 37 | + if (typeof window === 'undefined' || typeof IntersectionObserver === 'undefined') return |
| 38 | + |
| 39 | + const node = observedNode |
| 40 | + if (!node) return |
| 41 | + |
| 42 | + // if no root provided or ref not ready, try to find common FixTabPanel container as fallback |
| 43 | + const rootCandidate = |
| 44 | + resolvedRoot ?? (document.querySelector('.fix-tab-panel-scroll-container') as HTMLElement | null) |
| 45 | + |
| 46 | + // debug helpers (enable by setting window.__DEBUG_USE_INTERSECTION = true) |
| 47 | + const isDebug = typeof window !== 'undefined' && (window as any).__DEBUG_USE_INTERSECTION |
| 48 | + if (isDebug) { |
| 49 | + console.debug( |
| 50 | + '[useIntersection] observed node:', |
| 51 | + node, |
| 52 | + 'resolvedRoot:', |
| 53 | + resolvedRoot, |
| 54 | + 'rootCandidate:', |
| 55 | + rootCandidate |
| 56 | + ) |
| 57 | + } |
| 58 | + |
| 59 | + let observer: IntersectionObserver | null = new IntersectionObserver( |
| 60 | + ([entry]) => { |
| 61 | + if (isDebug) console.debug('[useIntersection] observer cb, isIntersecting=', entry.isIntersecting) |
| 62 | + if (entry.isIntersecting) { |
| 63 | + setIsVisible(true) |
| 64 | + if (once && observer) observer.disconnect() |
| 65 | + } |
| 66 | + }, |
| 67 | + { threshold, root: rootCandidate ?? undefined, rootMargin } |
| 68 | + ) |
| 69 | + |
| 70 | + try { |
| 71 | + observer.observe(node) |
| 72 | + } catch (e) { |
| 73 | + if (isDebug) console.warn('[useIntersection] observer.observe failed:', e) |
| 74 | + } |
| 75 | + |
| 76 | + // Fallback: attach scroll/resize listener to manually check intersection in case |
| 77 | + // the observer doesn't trigger due to timing or root issues (robustness) |
| 78 | + const checkIntersect = () => { |
| 79 | + if (!node) return |
| 80 | + const nodeRect = node.getBoundingClientRect() |
| 81 | + const rootRect = rootCandidate |
| 82 | + ? (rootCandidate as HTMLElement).getBoundingClientRect() |
| 83 | + : { |
| 84 | + top: 0, |
| 85 | + left: 0, |
| 86 | + right: window.innerWidth, |
| 87 | + bottom: window.innerHeight, |
| 88 | + width: window.innerWidth, |
| 89 | + height: window.innerHeight, |
| 90 | + } |
| 91 | + |
| 92 | + const verticallyIn = nodeRect.bottom > rootRect.top && nodeRect.top < rootRect.bottom |
| 93 | + const horizontallyIn = nodeRect.right > rootRect.left && nodeRect.left < rootRect.right |
| 94 | + |
| 95 | + const inView = verticallyIn && horizontallyIn |
| 96 | + if (isDebug && inView) console.debug('[useIntersection] checkIntersect -> inView', { nodeRect, rootRect }) |
| 97 | + |
| 98 | + if (inView) { |
| 99 | + setIsVisible(true) |
| 100 | + if (once) { |
| 101 | + if (observer) { |
| 102 | + observer.disconnect() |
| 103 | + observer = null |
| 104 | + } |
| 105 | + removeListeners() |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + const removeListeners = () => { |
| 111 | + try { |
| 112 | + if (rootCandidate) { |
| 113 | + ;(rootCandidate as HTMLElement).removeEventListener('scroll', checkIntersect) |
| 114 | + } else { |
| 115 | + window.removeEventListener('scroll', checkIntersect) |
| 116 | + } |
| 117 | + window.removeEventListener('resize', checkIntersect) |
| 118 | + } catch (e) { |
| 119 | + /* swallow */ |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + if (rootCandidate) { |
| 124 | + ;(rootCandidate as HTMLElement).addEventListener('scroll', checkIntersect, { passive: true }) |
| 125 | + } else { |
| 126 | + window.addEventListener('scroll', checkIntersect, { passive: true }) |
| 127 | + } |
| 128 | + window.addEventListener('resize', checkIntersect) |
| 129 | + |
| 130 | + // initial check |
| 131 | + checkIntersect() |
| 132 | + |
| 133 | + return () => { |
| 134 | + if (observer) { |
| 135 | + observer.disconnect() |
| 136 | + observer = null |
| 137 | + } |
| 138 | + removeListeners() |
| 139 | + } |
| 140 | + }, [threshold, resolvedRoot, rootMargin, once, observedNode]) |
| 141 | + |
| 142 | + return [ref, isVisible] as const |
| 143 | +} |
0 commit comments