"use client" import * as React from 'react'; import * as ScrollAreaPrimitive from '@radix-ui/react-scroll-area'; import { cn } from '../../../lib/utils'; /** * ScrollArea imperative handle for programmatic control */ export interface ScrollAreaHandle { /** Scroll to bottom of the content */ scrollToBottom: (behavior?: ScrollBehavior) => void; /** Scroll to top of the content */ scrollToTop: (behavior?: ScrollBehavior) => void; /** Scroll to a specific position */ scrollTo: (options: ScrollToOptions) => void; /** Get the viewport element */ getViewport: () => HTMLDivElement | null; } export type ScrollAreaOrientation = 'vertical' | 'horizontal' | 'both'; export interface ScrollAreaProps extends React.ComponentPropsWithoutRef { /** Ref to access the viewport element directly */ viewportRef?: React.RefObject; /** Additional className for the viewport */ viewportClassName?: string; /** Scroll orientation: vertical (default), horizontal, or both */ orientation?: ScrollAreaOrientation; /** Control horizontal overflow on the viewport (default: auto) */ overflowX?: 'auto' | 'hidden' | 'scroll'; } const ScrollArea = React.forwardRef( ({ className, children, viewportRef, viewportClassName, orientation = 'vertical', overflowX, ...props }, ref) => { const internalViewportRef = React.useRef(null); const actualViewportRef = viewportRef || internalViewportRef; // Expose imperative methods React.useImperativeHandle(ref, () => ({ scrollToBottom: (behavior: ScrollBehavior = 'smooth') => { const viewport = actualViewportRef.current; if (viewport) { viewport.scrollTo({ top: viewport.scrollHeight, behavior, }); } }, scrollToTop: (behavior: ScrollBehavior = 'smooth') => { const viewport = actualViewportRef.current; if (viewport) { viewport.scrollTo({ top: 0, behavior, }); } }, scrollTo: (options: ScrollToOptions) => { actualViewportRef.current?.scrollTo(options); }, getViewport: () => actualViewportRef.current, }), [actualViewportRef]); const showVerticalBar = orientation === 'vertical' || orientation === 'both'; const showHorizontalBar = orientation === 'horizontal' || orientation === 'both'; return ( } className={cn( "h-full w-full rounded-[inherit]", overflowX === 'hidden' && "!overflow-x-hidden", overflowX === 'scroll' && "!overflow-x-scroll", viewportClassName, )} > {children} {showVerticalBar && } {showHorizontalBar && } ); } ); ScrollArea.displayName = "ScrollArea" const ScrollBar = React.forwardRef< React.ElementRef, React.ComponentPropsWithoutRef >(({ className, orientation = "vertical", ...props }, ref) => ( )) ScrollBar.displayName = ScrollAreaPrimitive.ScrollAreaScrollbar.displayName export { ScrollArea, ScrollBar }