47 — Layout
Scroll Area
A region that scrolls on its own, with a thin bar and edges that fade where there is more.
Installation
pnpm dlx @human-kit/design add scroll-areanpx @human-kit/design add scroll-areayarn dlx @human-kit/design add scroll-areabunx @human-kit/design add scroll-areaThe command copies the files to src/lib/components/ui/scroll-area. It also
installs the packages that the files import.
Usage
<script lang="ts">
import * as ScrollArea from '$lib/components/ui/scroll-area';
</script>
<ScrollArea.Root label="Visit notes" class="h-48">
<VisitNotes />
</ScrollArea.Root> Guidelines
- Use it for a part of the screen that holds more than its box: a list in a panel, a sidebar, the body of a dialog. Let the page itself scroll as the browser does.
- Give it a height, or put it in a flex box that sets one: it scrolls only when something holds it smaller than its content.
- Turn
fadeon where the cut would read as the end of the list, as a list with no edge of its own. It fades only an edge that the content goes on past. - Use
horizontalfor a row of items, as chips or days, andbothfor a wide table.fadedoes nothing withboth. - Keep one scrolling region per direction in a view. A region inside a region traps the wheel.
Accessibility
- It keeps the browser's own scrolling: the wheel, touch, the scrollbar and the keyboard work as they do on the page.
- While its content overflows it takes the focus, so the arrow keys and
Page UpandPage Downscroll it. With nothing to scroll it is no stop forTab. - Give
labeland it becomes arole="region"with that name, so a screen reader can find it and say what the focus landed on. - The focus ring is drawn inside its edge, where an outer ring would be cut by its own box.
API reference
| Prop | Type | Default |
|---|---|---|
orientation | 'vertical' | 'horizontal' | 'both' The direction it scrolls in. The other one is clipped. | 'vertical' |
fade | boolean Thins the content out at an edge it goes on past. Not for | false |
label | string The name of the region, read when the keyboard reaches it. | — |
element | HTMLDivElement | null The scrolling element, to scroll it from code. Bindable. | — |
class | string Classes for the scrolling element: its height and its padding. | — |
Plus every attribute of a <div>.