Skip to
Keyboard shortcuts

Use the up and down arrow keys to move between items. Press Alt+Shift+K to open this list again.

Alpha version out now. The API may still change.

ScrollArea

Holds content that may be wider or taller than its box, such as a wide table or a long code sample, and scrolls it with the browser’s own scrollbars. While it scrolls, a keyboard user can Tab to it and scroll it.

Package
@kvirn-ui/react
Testing
Automated. Core 0 of 2, Release 0 of 3

When to use it#

  • Use it around content that can’t wrap without losing its meaning: a wide table from your content system, or a long line of code. Let everything else wrap, so the page doesn’t scroll sideways at 320 pixels wide (1.4.10 Reflow).
  • When everything fits it is a plain <div>. While it scrolls it is a named region (a part of the page that a screen reader, software that reads the screen aloud, can list so users jump to it) and a Tab stop (a place the Tab key stops at). With region="always" it is a named region even when it fits, but it is a Tab stop only while it scrolls.
  • Name it and say what is inside: the name is what a screen reader reads.
  • Not for a table you build with Table: Table has its own scroll region. Not for a long page: let the page scroll.

Example#

PermitCategoryApplicantProcessing timeFeeAppeal periodDecision byContact
Building permitHousingProperty owner10 weeks12,400 kr3 weeksBuilding committeebuilding@kvirn.example
Demolition permitHousingProperty owner6 weeks4,200 kr3 weeksBuilding committeebuilding@kvirn.example
Change of useCommercialBusiness owner8 weeks9,800 kr3 weeksBuilding committeebuilding@kvirn.example
Sign permitCommercialBusiness owner4 weeks1,900 kr3 weeksPlanning officeplanning@kvirn.example
Outdoor servingHospitalityBusiness owner5 weeks2,700 kr3 weeksLicensing officelicensing@kvirn.example
Event permitCultureOrganiser3 weeks1,200 kr2 weeksLicensing officelicensing@kvirn.example

Information: Implement like this

The area sets no size. Add your own class with a height limit, such as className="permit-list", and it scrolls down as well as sideways.

.permit-list {
  max-block-size: 20rem;
}

Use cases#

A short fee list that fits

A short table fits, so nothing scrolls and the area is a plain div: no role, no name and no Tab stop. It becomes a named region by itself if the window narrows or the text grows.

Event sizeFee
Small event600 kr
Medium event1,200 kr
Large event2,400 kr

A table people can jump to

Set region to always when a table matters enough that screen reader users should find it in their list of regions, even when it fits. It is still a Tab stop only while it scrolls.

Event sizeFee
Small event600 kr
Medium event1,200 kr
Large event2,400 kr

Props in this example:

A code sample in an open-data guide

A long line of code keeps its meaning only when it isn’t wrapped. Let the area scroll sideways, and name it so people know what it holds.

curl "https://data.kvirn.example/api/permits?category=housing&year=2026&status=granted&format=json&sort=decision-date"

A table in your own element

When the markup is yours, spread the props of the hook on the element that scrolls. Add the name only while isRegion is true, because a name on a plain div is not allowed.

PermitCategoryApplicantProcessing timeFeeAppeal periodDecision byContact
Building permitHousingProperty owner10 weeks12,400 kr3 weeksBuilding committeebuilding@kvirn.example
Demolition permitHousingProperty owner6 weeks4,200 kr3 weeksBuilding committeebuilding@kvirn.example
Change of useCommercialBusiness owner8 weeks9,800 kr3 weeksBuilding committeebuilding@kvirn.example
Sign permitCommercialBusiness owner4 weeks1,900 kr3 weeksPlanning officeplanning@kvirn.example
Outdoor servingHospitalityBusiness owner5 weeks2,700 kr3 weeksLicensing officelicensing@kvirn.example
Event permitCultureOrganiser3 weeks1,200 kr2 weeksLicensing officelicensing@kvirn.example

Props in this example:

Accessibility#

What it does for you#

  • APG pattern: none. A scrollable region with native scrolling: the `region` role and a Tab stop only while it scrolls, so a keyboard user can scroll it (2.1.1 Keyboard, A).
  • Deviations: none. Native scrollbars only, never hidden or thinned; there is no orientation prop, because the native overflow already scrolls whichever axis overflows.
  • Native elements used: <div> (or <section> with as) that is a role="region" while it scrolls, or always with region="always".

A ScrollArea holds content that may be wider or taller than its box, such as a wide table, a long code sample or a pasted block, and scrolls it with the browser's own scrollbars. While the content doesn't fit it is a named region you can Tab to and scroll with the arrow keys. When everything fits it is a plain <div>. Safari doesn't make a scrollable <div> a Tab stop by itself, which is why the area sets tabindex="0" while it scrolls.

PartElement / roleARIA / stateNotes
ScrollArea<div>, <div role="region"> while it scrolls or region="always"role="region" only while the area is one: it overflows, or region="always". tabindex="0" and data-overflowing only while it overflows. Not a region (nothing overflows): no role, no name, no tabindex.The part's class names are listed in the API reference. Name it with your own aria-labelledby or aria-label, applied only once it is a region. A region with no name: a development warning
useScrollAreathe same attributes, for your own elementscrollAreaProps, isOverflowing, isRegion, elementOption region: 'overflow' (default) or 'always'. Spread scrollAreaProps on the element that scrolls

It announces nothing.

What you need to do#

  • Without the theme, set overflow: auto (the default theme does it). The component sets no style, so an unstyled area doesn't scroll.
  • Name it with aria-labelledby (a heading or caption) or aria-label, and say what is inside. A region without a name is read as an unlabelled landmark (4.1.2 Name, Role, Value, A).
  • Limit its height with CSS (max-block-size) to scroll down as well as sideways. The component sets no size.
  • Keep the content reachable: controls inside keep their own Tab stops, so don't hide the scrollbars or turn off native scrolling.
  • Show that the content continues. Add a short caption such as "The table scrolls sideways", or set a width that cuts a column in half. Sighted touch users on iOS, Android and macOS get no scrollbar until they scroll.
  • Don't scroll the page's main content inside a nested area without a reason: a scroll area inside a scroll area is hard to use.

Warning: Don't forget

Name the area and say what is inside. Without a name a screen reader says only “region”, and the user can’t tell what it holds (4.1.2 Name, Role, Value).

Visual and modes#

  • Focus indicator: the default theme draws a focus ring on the area (2.4.7 Focus Visible, AA; 2.4.11 Focus Not Obscured (Minimum), AA). In forced colours the ring uses the system's highlight colour.
  • Target size: n/a, native scrollbars.
  • forced-colors: the focus ring uses the system's highlight colour. Native scrollbars follow the system colours.
  • reduced-motion: no motion.
  • Reflow and zoom: the area scrolls its own content, so a wide table doesn't make the page scroll sideways at 320 px (1.4.10 Reflow, AA). The default theme keeps the area within the page width. If you restyle it, keep it within 320 CSS pixels.
  • RTL: native: the scroll origin and the arrow keys flip.

Warning: Don't forget

If you restyle the area, keep its focus ring and its native scrollbars. Keyboard users need the ring to see where they are (2.4.7 Focus Visible).

Focus management#

  • Initial focus: none. The area never moves focus.
  • Trap: no. Tab always leaves the area.
  • Restore to: n/a.
  • The area stays a region and a Tab stop while it holds focus, even if the content stops overflowing (a wider window, zooming out): taking the role and the tab stop from the focused element would drop focus to the page. It lets go once focus leaves.
  • Never obscured by: nothing. The theme draws a focus ring on the area, and a focused control inside it scrolls into view natively.

Allowed elements#

A tag outside the list changes the page's outline or semantics (1.3.1 Info and Relationships, A; 4.1.2 Name, Role, Value, A). as is a string, so it works from a Server Component.

Part`as`Why
ScrollAreadiv (default), sectionA scroll container is a region while it scrolls, so section adds nothing but is harmless. No landmark element (nav, aside, main) and no list: the area holds content, it is not content. A value outside the list is a type error and, in JS, warns once in development and renders the default element.

WCAG success criteria#

SCNameHow
1.3.1Info and Relationships (A)A named region only while it scrolls
1.4.10Reflow (AA)Content that is wider than the viewport scrolls inside the area
2.1.1Keyboard (A)A Tab stop while it scrolls, and the native scroll keys
2.4.7Focus Visible (AA)The area takes focus while it scrolls The theme draws the ring; the ring is checked by eye, and the forced-colours sweep checks it is drawn
2.4.11Focus Not Obscured (Minimum) (AA)The area adds no overlay, sticky part or custom scrollbar, so nothing of its own covers it. This is checked by eye
4.1.2Name, Role, Value (A)role="region" and a name from the consumer, with a development warning when it has none

Keyboard#

Focus
native: the area is one Tab stop while it overflows, and scrolls with the browser's own keys
Selection follows focus
n/a
Arrows wrap
n/a
Shortcuts
none
KeyWhereWhat happens
Tabbefore the areaMoves to the area when it overflows, and past it when nothing scrolls
Test: scroll-area.test.tsx › Tab focuses the area when it overflows, scroll-area.test.tsx › Tab skips the area when nothing scrolls
Shift+Tabafter the areaMoves back to the area when it overflows, and to the element before it otherwise
Test: scroll-area.test.tsx › Shift+Tab moves back to the area when it overflows
ArrowDown / ArrowUpon the focused areaScrolls the area down or up (native)
Test: scroll-area.test.tsx › ArrowDown scrolls the focused area, scroll-area.test.tsx › ArrowUp scrolls the focused area back up
ArrowRight / ArrowLefton the focused areaScrolls the area sideways (native, flips in RTL)
Test: scroll-area.test.tsx › ArrowRight scrolls the focused area sideways, scroll-area.test.tsx › ArrowLeft scrolls the area sideways in right-to-left, scroll-area.test.tsx › ArrowRight scrolls the area sideways in right-to-left
PageDown / PageUpon the focused areaScrolls the area a page down or up (native)
Test: scroll-area.test.tsx › PageDown scrolls the focused area a page, scroll-area.test.tsx › PageUp, Home and End scroll the focused area
Home / Endon the focused areaScrolls to the start or the end (native)
Test: scroll-area.test.tsx › PageUp, Home and End scroll the focused area
Spaceon the focused areaScrolls the area a page down (native)
Test: scroll-area.test.tsx › Space scrolls the focused area a page
Escape and letterson the focused areaNothing. There is no type-ahead and no shortcut
Test: scroll-area.test.tsx › Escape and letters do nothing

Announcements#

What a screen reader reads#

State or actionRead aloudPolitenessTest
The content fitsthe content only, no regionnonenone
The content overflowsregion, <name>nonenone
region="always", the content fitsregion, <name>nonenone
Tab onto the overflowing area<name>, regionnonenone

API reference#

import { ScrollArea, useScrollArea } from '@kvirn-ui/react'

ScrollArea

Renders <div>, which is a region only while it scrolls (or with region="always"). It takes every attribute of a <div> and passes ref to it. The default theme is described on Theming.

PropTypeDefaultDescription
region'overflow' | 'always''overflow'overflow: a named region only while the content doesn’t fit. always: a named region whether it scrolls or not. It is a Tab stop only while it scrolls, in both.
classNamestring–Your own classes. They join kv-scroll-region kv-scroll-area and never replace them, so the theme keeps styling the area. Use one to limit its height.
as'div' | 'section''div'Changes the element. It is a region while it scrolls either way, so section adds nothing. No landmark element and no list. A string, so it works from a Server Component.
refRef<HTMLElement>–Reaches the element, whichever it is.
Class or attributeValuesMeaning
kv-scroll-region kv-scroll-areaalwaysThe part classes. The default theme sets overflow, the focus ring and the rounded corners.
roleregion, while it scrolls or with region="always"Not a region when everything fits: no role, no name.
tabindex0, only while it overflowsMakes the area a Tab stop so a keyboard user can scroll it. Your own tabIndex wins.
data-overflowingpresent while the content doesn’t fitThe area scrolls. It stays while the area holds focus.
aria-label | aria-labelledbyyoursApplied once the area is a region. A name on a plain <div> is not allowed.

useScrollArea

Options

PropTypeDefaultDescription
region'overflow' | 'always''overflow'overflow: a named region only while the content doesn’t fit. always: a named region whether it scrolls or not. It is a Tab stop only while it scrolls, in both.

Result

PropTypeDefaultDescription
scrollAreaPropsScrollAreaPartProps–Spread on the element that scrolls: the classes, role, tabIndex, data-overflowing and a ref. Name it yourself with aria-labelledby or aria-label.
isOverflowingboolean–The content is wider or taller than the area. It stays true while the area holds focus.
isRegionboolean–The area has role="region" now: it overflows, or region is always.
elementHTMLElement | null–The scroll element, once it is mounted.

Testing#

The automated tests prove the keyboard behaviour, the names and states, that axe finds no violations in every story and theme, and, for the components they cover, that the stories hold up in forced colours, reduced motion, text spacing and a 320 pixel width. An Automated row passes on these tests alone. A Core or Release row passes only when an automated agent check and a person on the real assistive technology have both checked it. "agent-verified" means automated tests in the repository (with a virtual screen reader for the screen reader rows), not the real assistive technology.

AT + browser + OSAgent checkHuman checkResultNotes
Automated (re-runnable tests)
axe, WCAG 2.2 A and AA, every story, 4 themespassn/apassscroll-area.stories.tsx, every story, 4 themes
Markup and ARIA (roles, names, ids, states)passn/apassscroll-area.test.tsx › content that fits is a plain div: no role, no name, no tabindex, no data-overflowing; expectNoA11yViolations
Keyboard contract rows (Vitest browser)passn/apass8 rows, each names a test (scroll-area.test.tsx › Tab focuses the area when it overflows)
Focus order and managementpassn/apassscroll-area.test.tsx asserts focus (toHaveFocus); first rule: Tab focuses the area when it overflows
Display modes (forced colours, 320px reflow, text spacing, reduced motion)passn/apassthe four sweep projects (storybook-forced-colors, -reduced-motion, -reflow, -text-spacing), run in CI
Core (required for beta)
NVDA + Chrome + Windowspendingpendingpending
VoiceOver + Safari + iOSpendingpendingpending
Release (before 1.0 and each minor)
JAWS + Chrome + Windowspendingpendingpending
TalkBack + Chrome + Androidpendingpendingpending
VoiceOver + Safari + macOSpendingpendingpending

What to check#

  • With content wider than the box, Tab to the area: it is announced as a region with its name.
  • Arrow keys, Home, End and Page Down scroll it.
  • Widen the window until the content fits: the region is gone and Tab skips it.
  • At 400% zoom or 320px, only the area scrolls, not the page.
  • In forced colours the area's focus ring is visible.