|Kit

ScrollArea

A scroll container with the same thin, overlaid scrollbars on every OS. Built on Ark's scroll-area machine, with visibility modes, a gutter option, edge fades and RTL support.

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.

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

ScrollArea

Loading preview

Props

Code

<ScrollArea
  type="hover"
  scrollbars="xy"
  size="md"
  offsetScrollbars={false}
  fade={false}
/>

Scrollbar visibility

type decides when a bar shows:

typeThe bar shows
hover (default)while the pointer is over the area, or it is scrolling
autowhenever the content overflows on that axis
scrollonly while it is scrolling, then fades out after a second
alwaysalways. 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

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

Code

<ScrollArea scrollbars="x" type="auto" offsetScrollbars>
  <div className="flex gap-3 p-3">
    {projects.map((p) => <ProjectCard key={p.id} project={p} />)}
  </div>
</ScrollArea>

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.

Code

<ScrollArea type="always" className="h-56">
  <table className="w-[44rem]">
    <thead>
      <tr>
        {columns.map((c) => (
          <th key={c} className="sticky top-0 z-10 bg-brand-50 dark:bg-brand-950">{c}</th>
        ))}
      </tr>
    </thead>
    <tbody>…</tbody>
  </table>
</ScrollArea>

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

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.

<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

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:

offsetScrollbarsGutter on
false (default)no axis
trueevery 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

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.

Code

<ScrollArea type="auto" className="max-h-56">
  <ProjectList count={count} />
</ScrollArea>

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.

y = 0

Code

const viewportRef = useRef<HTMLDivElement>(null);
const [position, setPosition] = useState({ x: 0, y: 0 });

<Button onClick={() => viewportRef.current?.scrollTo({ top: 0, behavior: 'smooth' })}>Top</Button>
<ScrollArea viewportRef={viewportRef} onScrollPositionChange={setPosition} className="h-56">
  <ProjectList />
</ScrollArea>

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

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.

Code

<ScrollArea fade type="scroll" className="h-56">
  <ProjectList />
</ScrollArea>

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

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

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.

Projects
18 projects

Code

<div className="grid h-72 grid-rows-[auto_1fr_auto]">
  <header>Projects</header>
  <ScrollArea type="auto" offsetScrollbars="present" aria-label="Projects">
    <ProjectList />
  </ScrollArea>
  <footer>18 projects</footer>
</div>

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

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

Code

<ScrollArea.Root type="always" size="lg" className="h-56">
  <ScrollArea.Viewport aria-label="Projects">
    <ScrollArea.Content>
      <ProjectList />
    </ScrollArea.Content>
  </ScrollArea.Viewport>
  <ScrollArea.Scrollbar orientation="vertical">
    <ScrollArea.Thumb className="bg-brand-500/60" />
  </ScrollArea.Scrollbar>
</ScrollArea.Root>

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. Flip the docs' direction toggle to see it.

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

The kit's own scroll regions are ScrollAreas, so they share these bars: the option lists of Select, Combobox, Autocomplete, MultiSelect, ComboboxList and Spotlight; the body of Modal and Drawer; AppShell's SidebarBody; the text pane of FilePreview; and the weeks of CalendarHeatmap.

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

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.

API

ScrollArea accepts every div prop of the root, plus:

Prop

Type

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.

Changelog

v0.30.2
  • With scrollbars="y", the content keeps the viewport's width, so a long row truncates instead of clipping every row; AppShell.SidebarBody rows truncate again

    #500
v0.30.0
  • New ScrollArea: thin overlaid scrollbars that look the same on every OS while scrolling stays native, with optional edge fades

    #480
  • A tabIndex on ScrollArea.Viewport overrides the automatic one

    #494
  • ScrollArea no longer draws the browser's own scrollbar beside the kit's

    #494

On this page