import { useCallback, useEffect, useRef, useState, type ReactNode } from "react"; interface ScrollGradients { top: boolean; bottom: boolean; } interface ScrollableWithGradientsProps { children: ReactNode; className?: string; ariaLabel?: string; } const SCROLL_THRESHOLD = 5; // Pixels before showing gradient export function ScrollableWithGradients({ children, className, ariaLabel }: ScrollableWithGradientsProps) { const [gradients, setGradients] = useState({ top: false, bottom: false }); const scrollRef = useRef(null); const updateGradients = useCallback(() => { const node = scrollRef.current; if (!node) return; const { scrollTop, scrollHeight, clientHeight } = node; const top = scrollTop > SCROLL_THRESHOLD; const bottom = scrollTop + clientHeight < scrollHeight - SCROLL_THRESHOLD; setGradients((prev) => (prev.top === top && prev.bottom === bottom ? prev : { top, bottom })); }, []); const setRef = useCallback( (node: HTMLDivElement | null) => { scrollRef.current = node; updateGradients(); }, [updateGradients] ); // Handle resize events (window resize, font load, etc.) useEffect(() => { const node = scrollRef.current; if (!node) return; const resizeObserver = new ResizeObserver(updateGradients); resizeObserver.observe(node); return () => resizeObserver.disconnect(); }, [updateGradients]); return (
{children}
{gradients.top && (