Scroll Area
Gives a block of content a fixed size and lets it scroll inside that box.
Installation
pnpm dlx shadcn@latest add @silkware/scroll-areaTo copy the component by hand, install its dependency:
pnpm add cnThen copy the source into components/ui/scroll-area.tsx:
"use client"
import * as React from "react"import { cn } from "cn"
function ScrollArea({ className, children, ...props}: React.ComponentProps<"div">) { return ( <div tabIndex={0} data-slot="scroll-area" className={cn( "relative [scrollbar-width:thin] [scrollbar-color:var(--color-border)_transparent] overflow-auto outline-none focus-visible:ring-[3px] focus-visible:ring-ring/50 focus-visible:outline-1", className )} {...props} > {children} </div> )}
export { ScrollArea }Usage
import { ScrollArea } from "@/components/ui/scroll-area"<ScrollArea role="region" aria-label="Approved suppliers" className="h-56 w-64 rounded-md border"> {suppliers}</ScrollArea>The component is a native scrolling div with a thin scrollbar and a focus ring. It does not set a size: without a height or a width in className, it grows with its content and never scrolls. It sets tabIndex={0}, so keyboard users can focus it and scroll it in every browser. Pass tabIndex={-1} when the area holds focusable controls, which already make it reachable.
Examples
Vertical
A list taller than its box scrolls up and down. Set the height.
Horizontal
Set the width, and give the content w-max so it does not wrap. Use it for a row of cards, not for a wide table.
Accessibility
| Key | Action |
|---|---|
| Tab | Moves focus into the area. |
| ArrowDown | Scrolls down while the area has focus. |
| ArrowUp | Scrolls up while the area has focus. |
| PageDown | Scrolls down by one page of the area. |
| Space | Scrolls down by one page of the area. |
| Home | Scrolls to the start. |
| End | Scrolls to the end. |
The browser provides the scrolling and the keys above. There is no React Aria primitive behind this component.
You provide:
tabIndex={-1}when the area holds focusable controls. The component is focusable by default so that keyboard users can scroll it; a list of links does not need the extra tab stop.role="region"and anaria-labelthat name the area, so a screen reader announces what scrolls.- Content that does not rely on a hidden scrollbar: use the thin scrollbar as it is, and do not hide it.
Guidelines
When to use
- For a bounded panel with a long list inside: suppliers in a side panel, entries in a log.
- For a row of cards that is wider than its container.
When not to use
- For the page itself. Let the page scroll.
- For a table with many columns. Use the Table, which handles its own overflow.
- For content that fits. Remove the box.
- Inside another scrolling box. Nested scrolling traps the pointer and the keyboard.
Content
- Show part of the next item at the edge, so users see that there is more.
- Keep the first screen useful: put the most important items first.
Set a height, name the region and make it focusable.
Don’t leave out the height. The list grows to full size and nothing scrolls.
API reference
ScrollArea
Renders a div with overflow: auto. It accepts every div prop.
| Prop | Type | Default | Description |
|---|---|---|---|
className? | string | None | Classes merged after the base classes. Set the height or the width here. |
tabIndex? | number | 0 | Keyboard users focus the area to scroll it. Set -1 when the content holds focusable controls. |
role? | string | None | Use region together with aria-label to name the area. |
children? | ReactNode | None | The content to scroll. |