import { ScrollArea } from "@registry/components/ui/scroll-area";
export function ScrollAreaExample() {
return (
<div className="flex items-start justify-center">
<ScrollArea className="h-48 w-72 rounded-md bg-muted">
<div className="flex flex-col gap-3 p-4">{longContent}</div>
</ScrollArea>
</div>
);
} Examples
Horizontal scrolling
Use the same ScrollArea component for horizontally scrollable content. Scrollbars automatically adapt to the content direction.
import { ScrollArea } from "@registry/components/ui/scroll-area";
export function ScrollAreaHorizontalExample() {
return (
<div className="flex items-start justify-center">
<ScrollArea className="w-72 whitespace-nowrap rounded-md bg-muted">
<div className="flex w-max gap-4 p-4">
{Array.from({ length: 10 }, (_, i) => (
<div
key={i}
className="flex h-16 w-32 shrink-0 items-center justify-center rounded-md bg-secondary text-sm font-medium"
>
Item {i + 1}
</div>
))}
</div>
</ScrollArea>
</div>
);
} Installation
Copy the source code below into your project:
import { ScrollArea as BaseScrollArea } from "@base-ui/react/scroll-area";
import { cn } from "@registry/lib/utils";
function ScrollArea({
className,
children,
viewportProps,
...props
}: { viewportProps?: BaseScrollArea.Viewport.Props } & BaseScrollArea.Root.Props) {
return (
<BaseScrollArea.Root className={cn("size-full min-h-0", className)} {...props}>
<BaseScrollArea.Viewport
{...viewportProps}
className={cn(
"size-full overscroll-contain rounded-[inherit] outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-1 focus-visible:ring-offset-background",
viewportProps?.className,
)}
data-slot="scroll-area-viewport"
>
{children}
</BaseScrollArea.Viewport>
<ScrollBar orientation="vertical" />
<ScrollBar orientation="horizontal" />
<BaseScrollArea.Corner data-slot="scroll-area-corner" />
</BaseScrollArea.Root>
);
}
function ScrollBar({
className,
orientation = "vertical",
...props
}: BaseScrollArea.Scrollbar.Props) {
return (
<BaseScrollArea.Scrollbar
className={cn(
"pointer-events-none flex justify-center rounded opacity-0 transition-opacity delay-300 data-hovering:pointer-events-auto data-hovering:opacity-100 data-hovering:delay-0 data-hovering:duration-75 data-scrolling:pointer-events-auto data-scrolling:opacity-100 data-scrolling:delay-0 data-scrolling:duration-75",
"m-1 data-[orientation=horizontal]:h-1 data-[orientation=horizontal]:flex-col data-[orientation=vertical]:w-1",
className,
)}
data-slot="scroll-area-scrollbar"
orientation={orientation}
{...props}
>
<BaseScrollArea.Thumb
className="relative flex-1 rounded-full bg-muted-foreground/30"
data-slot="scroll-area-thumb"
/>
</BaseScrollArea.Scrollbar>
);
}
export { ScrollArea, ScrollBar }; API Reference
ScrollArea
The ScrollArea component extends the Base UI ScrollArea.Root
props and adds the following:
| Prop | Type | Default | Description |
|---|---|---|---|
| viewportProps | BaseScrollArea.Viewport.Props | — | Props forwarded to the underlying Viewport component. |
ScrollBar
The ScrollBar component extends the Base UI ScrollArea.Scrollbar
props and adds the following:
| Prop | Type | Default | Description |
|---|---|---|---|
| orientation | "vertical" | "horizontal" | "vertical" | Orientation of the scrollbar. |