Open History to see the last commit
1import { useCallback, useEffect, useRef, useState } from "react";
2
3export interface UseScrollShadowReturn {
4 scrollRef: React.RefObject<HTMLDivElement | null>;
5 canScrollUp: boolean;
6 canScrollDown: boolean;
7}
8
9/**
10 * Hook that tracks scroll position to determine if scroll shadows should be visible
11 * at the top and bottom of a scrollable container.
12 *
13 * @param externalRef - Optional external ref to use instead of creating a new one
14 * @param deps - Optional dependency array that triggers shadow recalculation when changed
15 * @returns Object containing the scroll ref and boolean flags for shadow visibility
16 */
17export function useScrollShadow(
18 externalRef?: React.RefObject<HTMLDivElement | null>,
19 deps: React.DependencyList = [],
20): UseScrollShadowReturn {
21 const internalRef = useRef<HTMLDivElement>(null);
22 const scrollRef = externalRef ?? internalRef;
23 const [canScrollUp, setCanScrollUp] = useState(false);
24 const [canScrollDown, setCanScrollDown] = useState(false);
25
26 /**
27 The biome-ignores are necessary because:
28 - Adding scrollRef.current to the dependency array won't work as expected (React doesn't track ref mutations)
29 - We've already solved the "stale ref" problem by adding the deps parameter that consumers can use to trigger recalculation
30 */
31 // biome-ignore lint/correctness/useExhaustiveDependencies: scrollRef.current is intentionally not a dependency - refs don't trigger re-renders
32 const updateScrollShadows = useCallback(() => {
33 const element = scrollRef.current;
34 if (!element) return;
35
36 const { scrollTop, scrollHeight, clientHeight } = element;
37
38 // Can scroll up if we're not at the top
39 setCanScrollUp(scrollTop > 0);
40
41 // Can scroll down if we're not at the bottom
42 // Add a small threshold (1px) to account for sub-pixel rendering
43 setCanScrollDown(scrollTop + clientHeight < scrollHeight - 1);
44 }, []);
45
46 // biome-ignore lint/correctness/useExhaustiveDependencies: scrollRef.current is intentionally not a dependency - refs don't trigger re-renders
47 useEffect(() => {
48 const element = scrollRef.current;
49 if (!element) return;
50
51 // Initial check
52 updateScrollShadows();
53
54 // Update on scroll
55 element.addEventListener("scroll", updateScrollShadows, { passive: true });
56
57 // Update on resize using ResizeObserver
58 const resizeObserver = new ResizeObserver(() => {
59 updateScrollShadows();
60 });
61
62 resizeObserver.observe(element);
63
64 // Also observe changes to the content inside
65 const mutationObserver = new MutationObserver(() => {
66 updateScrollShadows();
67 });
68
69 mutationObserver.observe(element, {
70 childList: true,
71 subtree: true,
72 attributes: false,
73 });
74
75 return () => {
76 element.removeEventListener("scroll", updateScrollShadows);
77 resizeObserver.disconnect();
78 mutationObserver.disconnect();
79 };
80 // eslint-disable-next-line react-hooks/exhaustive-deps
81 }, [updateScrollShadows, ...deps]);
82
83 return { scrollRef, canScrollUp, canScrollDown };
84}