Skip to content

Scroll Area

Gives a block of content a fixed size and lets it scroll inside that box.

Approved suppliers

Tornival
Bullovia
Cartivolo
Lamivo Metalli
Plastrena
Imbaltra
Cuscinal
Rotalba Cavi
Vernisca
Trasvera
Gommarda
Vetrelia

Installation

pnpm dlx shadcn@latest add @silkware/scroll-area

To copy the component by hand, install its dependency:

pnpm add cn

Then copy the source into components/ui/scroll-area.tsx:

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.

Approved suppliers

Tornival
Bullovia
Cartivolo
Lamivo Metalli
Plastrena
Imbaltra
Cuscinal
Rotalba Cavi
Vernisca
Trasvera
Gommarda
Vetrelia

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.

Bergamo3 production lines
Brescia2 production lines
Cremona4 production lines
Lecco1 production lines
Mantova2 production lines
Pavia3 production lines

Accessibility

KeyAction
TabMoves focus into the area.
ArrowDownScrolls down while the area has focus.
ArrowUpScrolls up while the area has focus.
PageDownScrolls down by one page of the area.
SpaceScrolls down by one page of the area.
HomeScrolls to the start.
EndScrolls 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 an aria-label that 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.
  • Monday: steel brackets
  • Tuesday: fasteners
  • Wednesday: packaging
  • Thursday: lubricants
  • Friday: cables
  • Saturday: pallets
Do

Set a height, name the region and make it focusable.

  • Monday: steel brackets
  • Tuesday: fasteners
  • Wednesday: packaging
  • Thursday: lubricants
  • Friday: cables
  • Saturday: pallets
Don’t

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.

PropTypeDefaultDescription
className?stringNoneClasses merged after the base classes. Set the height or the width here.
tabIndex?number0Keyboard users focus the area to scroll it. Set -1 when the content holds focusable controls.
role?stringNoneUse region together with aria-label to name the area.
children?ReactNodeNoneThe content to scroll.