import * as ScrollAreaPrimitive from "@radix-ui/react-scroll-area"; import React from "react"; import { cn } from "../../lib/utils"; interface ScrollAreaProps extends React.ComponentPropsWithoutRef { orientation?: "horizontal" | "vertical"; } // Add this custom hook above the ScrollArea component function useScrollRestoration( id: string | undefined, ref: React.RefObject, ) { React.useEffect(() => { const element = ref.current as any; const handleScroll = () => { (window as any).__scrollPositions[id as string] = { scrollTop: element.scrollTop, scrollLeft: element.scrollLeft, }; }; if (id && ref.current) { // Initialize window storage if it doesn't exist if (!(window as any).__scrollPositions) { (window as any).__scrollPositions = {}; } // Restore scroll position on mount const savedPosition = (window as any).__scrollPositions[id]; if (savedPosition) { const { scrollTop, scrollLeft } = savedPosition; element.scrollTop = scrollTop; element.scrollLeft = scrollLeft; } // Add scroll event listener to save position while scrolling element.addEventListener("scroll", handleScroll); } // Cleanup: remove event listener and save final position return () => { element.removeEventListener("scroll", handleScroll); }; }, [id, ref]); } const ScrollArea = React.forwardRef< React.ElementRef, ScrollAreaProps >(({ className, children, orientation = "vertical", id, ...props }, ref) => { const viewportRef = React.useRef(null); useScrollRestoration(id, viewportRef); return ( {children} ); }); ScrollArea.displayName = ScrollAreaPrimitive.Root.displayName; const ScrollBar = React.forwardRef< React.ElementRef, React.ComponentPropsWithoutRef >(({ className, orientation = "vertical", ...props }, ref) => ( )); ScrollBar.displayName = ScrollAreaPrimitive.ScrollAreaScrollbar.displayName; export { ScrollArea, ScrollBar };