Scroll area
A box of fixed size whose content scrolls, with thin scrollbars in the theme's colours that appear only when they are needed. Scrolling stays native. For a page that scrolls as a whole, use the browser's own scrollbar; this is for panels, lists and code inside a page.
Install
npx shadcn@latest add https://thrumui.dev/r/scroll-area.jsonAdds components/ui/scroll-area.tsx and installs @base-ui/react@1.9.0. Nothing else in your project changes.
import { ScrollArea } from "@/components/ui/scroll-area";Props
| Prop | Type | Default | Description |
|---|---|---|---|
ScrollArea: orientation | "vertical" | "horizontal" | "both" | Default "vertical" | Which ways the content scrolls. Each way gets a 10 pixel scrollbar track with a pill-shaped thumb. |
ScrollArea: aria-label | string | – | Names the scrolling region. Set it when nothing inside can take the focus, as in a plain list: the viewport then becomes a named region a keyboard reader can Tab to and scroll. |
ScrollArea: aria-labelledby | string | – | Names the region by the id of a visible heading instead. |
ScrollArea: viewportClassName | string | – | Extra classes for the viewport, the element that scrolls. Padding belongs on the content inside it. |
ScrollArea: framed | boolean | Default false | Wrap the scroll area in a rounded bordered card panel that adapts to frosted glass. |
ScrollArea: fade | boolean | Default true | Softly fade overflowing edges with a CSS mask so readers can see more content waits beyond the fold. |
ScrollArea: scrollbars | "auto" | "always" | Default "auto" | auto reveals scrollbars on hover, scroll and focus; always keeps overflowing scrollbars softly visible at rest. |
ScrollArea: className | string | – | Sets the size and the frame: h-72, w-64, rounded-lg border. The box never grows with its content. |
ScrollArea: overflowEdgeThreshold | number | { xStart, xEnd, yStart, yEnd } | Default 0 | From the headless base: how many pixels from an edge count as being at that edge, for the data-overflow attributes. |
ScrollBar: orientation | "vertical" | "horizontal" | Default "vertical" | A second export, for a custom layout. ScrollArea already draws the bars its orientation asks for. |
ScrollBar: keepMounted | boolean | Default false | Keep the bar in the page while the content fits. By default it is not drawn then. |
children | ReactNode | – | The content. Give a horizontal row w-max so it can be wider than the box. |
Motion and sound
Motion
- The scrollbars fade in over 150 ms when the pointer enters the box, while it scrolls and while the box has the keyboard focus, and fade out after.
- Scrolling is the browser's own, including its smooth scrolling and momentum.
- Reduced motion: the bars appear and leave at once.
Sound
Silent. This component makes no sound.
Accessibility
- It is a native scrolling element with its scrollbar hidden and thin bars drawn over it, so scrolling by wheel, touch, keyboard and find in page all work as usual.
- When the content overflows, the viewport is in the tab order with a visible focus ring. Name it with aria-label or aria-labelledby when nothing inside can take the focus; it then has the region role.
- The bars are drawn in the muted text colour, 3 to 1 or more against the page in both themes, and are hidden from screen readers: the viewport already tells them it scrolls.
- Strict content security policy: the headless base adds a style element to hide the native scrollbar. Wrap the app in <CSPProvider disableStyleElements> from @base-ui/react/csp-provider and add the .base-ui-disable-scrollbar rule (scrollbar-width: none, and display: none on its ::-webkit-scrollbar) to the global stylesheet.
| Keys | Action |
|---|---|
| Tab | Focus the first control inside, or the viewport itself when nothing inside can take the focus. |
| ↓ / ↑ / ← / → | On the focused viewport: scroll a little, as in any scrolling element. |
| Page Down / Page Up | Scroll by one box height. |
| Home / End | Scroll to the start or the end. |
Source
"use client";
import { ScrollArea as ScrollAreaPrimitive } from "@base-ui/react/scroll-area";
import { useCallback, useEffect, useRef, useState, type ComponentProps } from "react";
import { cn } from "@/lib/utils";
export type ScrollAreaProps = ScrollAreaPrimitive.Root.Props & {
/** Which ways the content scrolls. Each way gets its own thin scrollbar. */
orientation?: "vertical" | "horizontal" | "both";
/** Names the scrolling region. Needed when nothing inside it can take the focus, so a keyboard reader can still reach it and scroll. */
"aria-label"?: string;
/** Names the scrolling region by the id of a visible heading. */
"aria-labelledby"?: string;
/** Extra classes for the viewport, the element that scrolls. */
viewportClassName?: string;
/** Wrap the scroll area in a rounded bordered panel surface that turns to glass with the page material. */
framed?: boolean;
/** Fade overflowing edges with a soft CSS mask to show more content lies beyond the fold. */
fade?: boolean;
/** `auto` reveals scrollbars on hover, scroll and focus; `always` keeps overflowing scrollbars softly visible at rest. */
scrollbars?: "auto" | "always";
};
/**
* The headless base marks the root, viewport and content with which edges overflow. Those marks arrive after the first
* paint, at a moment that varies, so they are left off: the markup then reads the same on every load.
*/
function Plain(props: ComponentProps<"div">) {
return <div {...Object.fromEntries(Object.entries(props).filter(([key]) => !/^data-(has-)?overflow-/.test(key)))} />;
}
/**
* A scrolling box with thin scrollbars drawn in the theme's colours. Scrolling itself stays native: the wheel, touch,
* the keyboard and the browser's find all work as usual. The bars appear while the pointer is over the box, while it
* scrolls and while it has the keyboard focus. Set its size with `className` (`h-72`, `w-64`).
*/
export function ScrollArea({
orientation = "vertical",
"aria-label": ariaLabel,
"aria-labelledby": ariaLabelledby,
viewportClassName,
framed = false,
fade = true,
scrollbars = "auto",
className,
children,
...props
}: ScrollAreaProps) {
const named = Boolean(ariaLabel || ariaLabelledby);
const viewportRef = useRef<HTMLDivElement>(null);
const contentRef = useRef<HTMLDivElement>(null);
const [edges, setEdges] = useState({ top: false, bottom: false, start: false, end: false });
const syncFade = useCallback(() => {
const el = viewportRef.current;
if (!el || !fade) {
setEdges((prev) => (prev.top || prev.bottom || prev.start || prev.end ? { top: false, bottom: false, start: false, end: false } : prev));
return;
}
const top = el.scrollTop > 2;
const bottom = el.scrollHeight - el.clientHeight - el.scrollTop > 2;
const scrolledX = Math.abs(el.scrollLeft);
const start = scrolledX > 2;
const end = el.scrollWidth - el.clientWidth - scrolledX > 2;
setEdges((prev) => (prev.top === top && prev.bottom === bottom && prev.start === start && prev.end === end ? prev : { top, bottom, start, end }));
}, [fade]);
useEffect(() => {
syncFade();
const viewport = viewportRef.current;
const content = contentRef.current;
if (!viewport || typeof ResizeObserver === "undefined") return;
const observer = new ResizeObserver(syncFade);
observer.observe(viewport);
if (content) observer.observe(content);
return () => observer.disconnect();
}, [syncFade, orientation]);
const fadeMask =
!fade
? undefined
: orientation === "horizontal"
? edges.start && edges.end
? "[mask-image:linear-gradient(to_right,transparent,black_20px,black_calc(100%-32px),transparent)]"
: edges.end
? "[mask-image:linear-gradient(to_right,black_calc(100%-32px),transparent)]"
: edges.start
? "[mask-image:linear-gradient(to_right,transparent,black_20px,black)]"
: undefined
: edges.top && edges.bottom
? "[mask-image:linear-gradient(to_bottom,transparent,black_20px,black_calc(100%-32px),transparent)]"
: edges.bottom
? "[mask-image:linear-gradient(to_bottom,black_calc(100%-32px),transparent)]"
: edges.top
? "[mask-image:linear-gradient(to_bottom,transparent,black_20px,black)]"
: undefined;
return (
<ScrollAreaPrimitive.Root
data-slot="scroll-area"
data-surface={framed ? "panel" : undefined}
// A style from the first render keeps the attribute order of server and client markup the same.
style={{ position: "relative" }}
render={Plain}
className={cn("group/scroll-area relative min-h-0 overflow-hidden", framed && "rounded-lg border bg-card text-card-foreground", className)}
{...props}
>
<ScrollAreaPrimitive.Viewport
ref={viewportRef}
data-slot="scroll-area-viewport"
// A named viewport is a region, so a screen reader can find it and say what it holds.
role={named ? "region" : undefined}
aria-label={ariaLabel}
aria-labelledby={ariaLabelledby}
style={{ overflow: "scroll" }}
render={Plain}
onScroll={syncFade}
className={cn(
"size-full overscroll-contain rounded-[inherit] outline-none",
"focus-visible:outline-2 focus-visible:-outline-offset-2 focus-visible:outline-ring",
fadeMask,
viewportClassName,
)}
>
{/* Watched for size changes, so the bars follow content that grows or shrinks. */}
<ScrollAreaPrimitive.Content
ref={contentRef}
data-slot="scroll-area-content"
render={Plain}
// Content as wide as its widest line scrolls sideways; a vertical area keeps it to the box, so text can wrap or truncate.
style={orientation === "vertical" ? { minWidth: 0 } : undefined}
>
{children}
</ScrollAreaPrimitive.Content>
</ScrollAreaPrimitive.Viewport>
{orientation !== "horizontal" && <ScrollBar orientation="vertical" className={scrollbars === "always" ? "opacity-70" : undefined} />}
{orientation !== "vertical" && <ScrollBar orientation="horizontal" className={scrollbars === "always" ? "opacity-70" : undefined} />}
<ScrollAreaPrimitive.Corner />
</ScrollAreaPrimitive.Root>
);
}
/**
* One scrollbar. `ScrollArea` draws the ones its `orientation` asks for; use this part only to build your own layout.
* It is not drawn at all while the content fits.
*/
export function ScrollBar({ orientation = "vertical", className, ...props }: ScrollAreaPrimitive.Scrollbar.Props) {
return (
<ScrollAreaPrimitive.Scrollbar
data-slot="scroll-area-scrollbar"
orientation={orientation}
className={cn(
"flex touch-none p-0.5 select-none",
"opacity-0 transition-opacity duration-150 ease-out motion-reduce:transition-none",
"data-hovering:opacity-100 data-scrolling:opacity-100 group-has-[[data-slot=scroll-area-viewport]:focus-visible]/scroll-area:opacity-100",
orientation === "vertical" ? "w-2.5" : "h-2.5 flex-col",
className,
)}
{...props}
>
<ScrollAreaPrimitive.Thumb
data-slot="scroll-area-thumb"
className={cn(
"relative rounded-[calc(var(--radius)*9999)] bg-muted-foreground transition-[background-color] duration-150 ease-out hover:bg-foreground motion-reduce:transition-none",
orientation === "vertical" ? "w-full" : "h-full",
)}
/>
</ScrollAreaPrimitive.Scrollbar>
);
}Files and dependencies
- Adds
components/ui/scroll-area.tsx- Packages
@base-ui/react@1.9.0- Brings along
- Nothing
Changelog
- 1.0.0 · 2026-12-03First release.