# ScrollArea (/docs/scroll-area)





`ScrollArea` is a box whose content scrolls, with the kit's own scrollbars instead of the OS's. Native bars look different on macOS, Windows and Linux, and on Windows they take up layout space. These are thin, drawn over the content and the same everywhere. Scrolling itself stays native: wheel, touch, keyboard and find-in-page all work as usual.

```tsx
import { ScrollArea } from '@42-central/ui-react/scroll-area';

<ScrollArea className="h-64">
  <LongList />
</ScrollArea>;
```

Give it a height (`h-64`), or a cap (`max-h-64`) so it grows with its content first. Content beyond that scrolls.

## Playground [#playground]

<story.Playground />

## Scrollbar visibility [#scrollbar-visibility]

`type` decides when a bar shows:

| `type`            | The bar shows                                                             |
| ----------------- | ------------------------------------------------------------------------- |
| `hover` (default) | while the pointer is over the area, or it is scrolling                    |
| `auto`            | whenever the content overflows on that axis                               |
| `scroll`          | only while it is scrolling, then fades out after a second                 |
| `always`          | always. When nothing overflows, the empty track stays and the thumb hides |

A hidden bar fades out and lets clicks through to the content beneath it.

## Axes [#axes]

`scrollbars` picks the axes that scroll: `"xy"` (default), `"y"` or `"x"`. The other axis clips instead of scrolling, and gets no bar.

<Preview align="block" code="&#x22;<ScrollArea scrollbars=\&#x22;x\&#x22; type=\&#x22;auto\&#x22; offsetScrollbars>\n  <div className=\&#x22;flex gap-3 p-3\&#x22;>\n    {projects.map((p) => <ProjectCard key={p.id} project={p} />)}\n  </div>\n</ScrollArea>&#x22;">
  <ScrollAreaHorizontalDemo />
</Preview>

When both axes overflow, a corner fills the square where the two bars meet, so neither runs under the other. Sticky content works inside, because the viewport is the scroller: this table's header sticks with nothing more than `sticky top-0`.

<Preview align="block" code="&#x22;<ScrollArea type=\&#x22;always\&#x22; className=\&#x22;h-56\&#x22;>\n  <table className=\&#x22;w-[44rem]\&#x22;>\n    <thead>\n      <tr>\n        {columns.map((c) => (\n          <th key={c} className=\&#x22;sticky top-0 z-10 bg-brand-50 dark:bg-brand-950\&#x22;>{c}</th>\n        ))}\n      </tr>\n    </thead>\n    <tbody>…</tbody>\n  </table>\n</ScrollArea>&#x22;">
  <ScrollAreaBothAxesDemo />
</Preview>

The viewport isolates its content's stacking (`isolate`), so a sticky header's `z-10` stays under the bars.

## Size [#size]

`size` sets the bar's thickness: a kit size (`xs` to `xl`, default `md`) or a number of pixels. The track keeps 2px of padding around the thumb, so `md` draws a 6px thumb in a 10px track.

```tsx
<ScrollArea size="lg" className="h-64">…</ScrollArea>
<ScrollArea size={16} className="h-64">…</ScrollArea>
```

The thickness is the `--scroll-area-size` custom property on the root, if you need it in your own styles.

## Offset scrollbars [#offset-scrollbars]

The bars are drawn over the content, so by default a row's trailing text can sit under the vertical bar. `offsetScrollbars` keeps a gutter the width of the bar instead:

| `offsetScrollbars` | Gutter on                         |
| ------------------ | --------------------------------- |
| `false` (default)  | no axis                           |
| `true`             | every axis in `scrollbars`        |
| `"y"` / `"x"`      | that axis only                    |
| `"present"`        | each axis only while it overflows |

The vertical gutter is on the inline end, like the bar: on the right in LTR, on the left in RTL.

## Autosize [#autosize]

Use `max-h-*` instead of `h-*` and the area takes its content's height until it reaches the cap, then scrolls. This is what Mantine calls `ScrollArea.Autosize`; here it needs no separate component.

<Preview align="block" code="&#x22;<ScrollArea type=\&#x22;auto\&#x22; className=\&#x22;max-h-56\&#x22;>\n  <ProjectList count={count} />\n</ScrollArea>&#x22;">
  <ScrollAreaAutosizeDemo />
</Preview>

## Scrolling from code [#scrolling-from-code]

`viewportRef` points at the scroller, so the DOM's own `scrollTo`, `scrollTop` and `scrollIntoView` work on it. `onScrollPositionChange` reports `{ x, y }` (the viewport's `scrollLeft` and `scrollTop`) on every scroll.

<Preview align="block" code="&#x22;const viewportRef = useRef<HTMLDivElement>(null);\nconst [position, setPosition] = useState({ x: 0, y: 0 });\n\n<Button onClick={() => viewportRef.current?.scrollTo({ top: 0, behavior: 'smooth' })}>Top</Button>\n<ScrollArea viewportRef={viewportRef} onScrollPositionChange={setPosition} className=\&#x22;h-56\&#x22;>\n  <ProjectList />\n</ScrollArea>&#x22;">
  <ScrollAreaProgrammaticDemo />
</Preview>

In RTL, `x` runs from `0` down to negative values, as the browser reports `scrollLeft`.

## Edge fade [#edge-fade]

`fade` fades the content out at each edge that has more to scroll to. An edge you rest against stays sharp, and its fade grows in as you scroll away from it, up to `--scroll-area-fade` (default `2rem`). Set that custom property on the root to change the length.

<Preview align="block" code="&#x22;<ScrollArea fade type=\&#x22;scroll\&#x22; className=\&#x22;h-56\&#x22;>\n  <ProjectList />\n</ScrollArea>&#x22;">
  <ScrollAreaFadeDemo />
</Preview>

The fade is a `mask-image` on the viewport, so it covers the viewport's content but never the bars.

## Scroll chaining [#scroll-chaining]

Once the area reaches its end, the wheel carries on and scrolls the page, as a native scroller does. To keep the scroll inside, add `overscroll-contain` to the viewport: `classNames={{ viewport: 'overscroll-contain' }}`.

## In a flex or grid layout [#in-a-flex-or-grid-layout]

A scroller in a column needs a definite height to scroll. In a flex column or a grid row, give the area its track (`flex-1`, or the `1fr` row) and it fills it. The root clips (`overflow-hidden`), and a clipping flex or grid item may shrink below its content, so it needs no `min-h-0` of its own.

<Preview align="block" code="&#x22;<div className=\&#x22;grid h-72 grid-rows-[auto_1fr_auto]\&#x22;>\n  <header>Projects</header>\n  <ScrollArea type=\&#x22;auto\&#x22; offsetScrollbars=\&#x22;present\&#x22; aria-label=\&#x22;Projects\&#x22;>\n    <ProjectList />\n  </ScrollArea>\n  <footer>18 projects</footer>\n</div>&#x22;">
  <ScrollAreaInGridDemo />
</Preview>

Two things to know when you put layout inside it:

* **Container queries.** A query measures its container's content box. `offsetScrollbars` pads the viewport, so with a gutter the viewport's content box is the width the rows really get. To make the content respond to that width, put `@container` on the viewport (`classNames={{ viewport: '@container' }}`), not on the root.
* **Content width.** The content is at least as wide as what it holds (`min-width: fit-content`), so an unbreakable line widens it and scrolls sideways. With `scrollbars="y"` nothing scrolls sideways, so the content keeps the viewport's width: its rows shrink to fit, and a `truncate` label ends in an ellipsis instead of pushing the row past the edge.

## Composition [#composition]

`ScrollArea` renders the whole anatomy for you. For a custom layout, compose the parts:

<Preview align="block" code="&#x22;<ScrollArea.Root type=\&#x22;always\&#x22; size=\&#x22;lg\&#x22; className=\&#x22;h-56\&#x22;>\n  <ScrollArea.Viewport aria-label=\&#x22;Projects\&#x22;>\n    <ScrollArea.Content>\n      <ProjectList />\n    </ScrollArea.Content>\n  </ScrollArea.Viewport>\n  <ScrollArea.Scrollbar orientation=\&#x22;vertical\&#x22;>\n    <ScrollArea.Thumb className=\&#x22;bg-brand-500/60\&#x22; />\n  </ScrollArea.Scrollbar>\n</ScrollArea.Root>&#x22;">
  <ScrollAreaCompositionDemo />
</Preview>

## RTL [#rtl]

The vertical bar sits on the inline end: on the right in LTR and on the left in RTL, with its gutter. The horizontal bar's thumb starts on the right in RTL, as the content does. The direction comes from the locale, like every Ark-backed part: see [Text direction](/docs/intl). Flip the docs' direction toggle to see it.

## Accessibility [#accessibility]

* The viewport is the real scroller, so wheel, touch, the keyboard and find-in-page all scroll it natively.
* Whenever it overflows on a scrolling axis, the viewport joins the tab order (`tabIndex={0}`), so a keyboard user can focus it and scroll with the arrow keys, Page Up/Down, Home and End. Its focus ring is drawn on an overlay that doesn't scroll, so content can't cover it.
* Pass `aria-label` or `aria-labelledby` and they go to the viewport, which then becomes a `region` landmark. Without a name it stays a plain focusable scroller rather than an unnamed region.
* The scrollbars are pointer-only, like native ones. The content stays in the DOM, so a screen reader reads it in order, whatever is scrolled into view.
* Ark gives the viewport `role="presentation"` and only makes it focusable when *both* axes overflow. The kit drops the role, since a focusable element can't be presentational, and focuses it when *either* axis overflows.
* A `tabIndex` passed to `ScrollArea.Viewport` wins over the automatic one. Pass `tabIndex={-1}` when the area sits inside a widget that already owns focus and scrolls it: a listbox moved by its input, or a grid with a roving cell. An extra tab stop there would only be in the way.

## In the kit [#in-the-kit]

The kit's own scroll regions are ScrollAreas, so they share these bars: the option lists of [`Select`](/docs/select), [`Combobox`](/docs/combobox), [`Autocomplete`](/docs/autocomplete), [`MultiSelect`](/docs/multi-select), [`ComboboxList`](/docs/combobox-list) and [`Spotlight`](/docs/spotlight); the body of [`Modal`](/docs/modal) and [`Drawer`](/docs/drawer); [`AppShell`](/docs/app-shell)'s `SidebarBody`; the text pane of [`FilePreview`](/docs/file-preview); and the weeks of [`CalendarHeatmap`](/docs/calendar-heatmap).

## Anatomy [#anatomy]

* **`ScrollArea.Root`**: the frame. It holds the scroll machine, positions the bars and takes `type`, `scrollbars`, `size`, `offsetScrollbars` and `fade`. Size it here.
* **`ScrollArea.Viewport`**: the scroller, with its focus overlay. Takes `aria-label` / `aria-labelledby`. Typed `NoAsChild`, because it renders the overlay beside the scroller.
* **`ScrollArea.Content`**: wraps what scrolls, so the machine can watch its size.
* **`ScrollArea.Scrollbar`**: one track, with `orientation="vertical" | "horizontal"`. Carries `data-state="visible" | "hidden"` from `type`, and `data-empty` when its axis doesn't overflow.
* **`ScrollArea.Thumb`**: the draggable thumb, inside a `Scrollbar`.
* **`ScrollArea.Corner`**: the square where the two bars meet, shown only while both overflow.

### `testId` [#testid]

On the ready-made `ScrollArea`, `testId` lands on the root and every part takes it with a suffix: `-viewport`, `-content`, `-scrollbar-y`, `-thumb-y`, `-scrollbar-x`, `-thumb-x` and `-corner`. Each composed part takes its own `testId`. See [Testing](/docs/testing).

## API [#api]

`ScrollArea` accepts every `div` prop of the root, plus:

<TypeTable
  type="{
  type: { type: '&#x22;hover&#x22; | &#x22;auto&#x22; | &#x22;scroll&#x22; | &#x22;always&#x22;', default: '&#x22;hover&#x22;', description: 'When the scrollbars show. See Scrollbar visibility.' },
  scrollbars: { type: '&#x22;xy&#x22; | &#x22;y&#x22; | &#x22;x&#x22;', default: '&#x22;xy&#x22;', description: 'Which axes scroll. The other axis clips and gets no bar.' },
  size: { type: 'Size | number', default: '&#x22;md&#x22;', description: 'The bar thickness: xs to xl (6 to 14px), or pixels. Exposed as --scroll-area-size.' },
  offsetScrollbars: { type: 'boolean | &#x22;x&#x22; | &#x22;y&#x22; | &#x22;present&#x22;', default: 'false', description: 'Keep a gutter the width of the bar, so content never sits under it. present keeps it only on an axis that overflows.' },
  fade: { type: 'boolean', default: 'false', description: 'Fade the content out at each edge with more to scroll to, over up to --scroll-area-fade (2rem).' },
  viewportRef: { type: 'Ref<HTMLDivElement>', description: 'A ref to the scroller, for scrollTo, scrollTop, scrollIntoView.' },
  onScrollPositionChange: { type: '(position: { x: number; y: number }) => void', description: &#x22;Called on every scroll with the viewport's scrollLeft (x) and scrollTop (y).&#x22; },
  'aria-label': { type: 'string', description: 'Names the viewport, which then becomes a region landmark.' },
  'aria-labelledby': { type: 'string', description: 'Names the viewport by an element id, which then becomes a region landmark.' },
  classNames: { type: '{ viewport?, content?, scrollbar?, thumb?, corner? }', description: 'A class per part, merged with the kit’s. className styles the root.' },
  children: { type: 'ReactNode', description: 'The scrolled content.' },
  testId: { type: 'string', description: 'Renders as data-testid on the root, and suffixed on each part. See Testing.' },
}"
/>

`ScrollArea.Root` takes `type`, `scrollbars`, `size`, `offsetScrollbars`, `fade` and `testId`, plus every `div` prop and `asChild`. `ScrollArea.Scrollbar` takes `orientation` (default `"vertical"`). The other parts take `className`, `testId` and their element's props.
