'use client' import { useCallback, useId, useRef, useState, type CSSProperties, type ReactNode, type Ref, type UIEvent, } from 'react' import { cn, ScrollBar, ScrollRoot, ScrollViewport } from 'ui' interface CurvedScrollAreaProps { children: ReactNode viewportRef?: Ref onScroll?: (event: UIEvent) => void bandHeight?: number maxShift?: number blur?: number className?: string } const svgDataUrl = (svg: string) => `data:image/svg+xml,${encodeURIComponent(svg)}` const DISPLACEMENT_MAP = svgDataUrl(` `) const BLUR_MASK = svgDataUrl(` `) export function CurvedScrollArea({ children, viewportRef, onScroll, bandHeight = 88, maxShift = 28, blur = 1.5, className, }: CurvedScrollAreaProps) { const filterId = useId() const [width, setWidth] = useState(0) const rootRef = useRef(null) const displacementRef = useRef(null) const blurRef = useRef(null) const bandRef = useRef(null) const blurMaskRef = useRef(null) // the filter works in pixels, so it needs the width of the area const measure = useCallback((root: HTMLDivElement | null) => { rootRef.current = root if (!root) return const observer = new ResizeObserver(() => setWidth(root.clientWidth)) observer.observe(root) return () => observer.disconnect() }, []) const handleScroll = (event: UIEvent) => { const { scrollTop } = event.currentTarget const progress = Math.min(scrollTop / bandHeight, 1) const strength = progress * progress * (3 - 2 * progress) rootRef.current?.toggleAttribute('data-scrolled', scrollTop > 0) bandRef.current?.setAttribute('y', String(scrollTop)) blurMaskRef.current?.setAttribute('y', String(scrollTop)) displacementRef.current?.setAttribute('scale', String(maxShift * 2 * strength)) blurRef.current?.setAttribute('stdDeviation', String(blur * strength)) onScroll?.(event) } return ( {children} ) }