# Scroll area

A native scroller with an overlay scrollbar that keeps the content clear.

Live variants: https://design.arcadia.is/components/scroll-area

```tsx
import {
  ScrollArea, useStickToBottom, useScrollKeys, springScroll, runningScroll,
  revealInView,
} from "@arcadia/ui";
```

```tsx
import { ScrollArea } from "@arcadia/ui";

function Files() {
  const files = Array.from({ length: 24 }, (_, i) => `src/parts/part-${i + 1}.tsx`);
  return (
    <ScrollArea rootStyle={cmp.scroller as never}>
      <div {...sx("cmp-scroller-content", cmp.scrollerContent)}>
        {files.map((f) => <div key={f}>{f}</div>)}
      </div>
    </ScrollArea>
  );
}
```

## Scrolls the app starts

```tsx
import { useRef } from "react";
import { ScrollArea, useStickToBottom } from "@arcadia/ui";

function JumpToLatest() {
  const viewport = useRef<HTMLDivElement>(null);
  const content = useRef<HTMLDivElement>(null);
  const stick = useStickToBottom(viewport, content);
  const lines = Array.from({ length: 60 }, (_, i) => `Message ${i + 1}`);
  return (
    <ScrollArea anchoring={false} rootStyle={cmp.scroller as never} viewportRef={viewport} contentRef={content}
      viewportProps={{ tabIndex: 0, "aria-label": "Messages" }}
      jumpToLatest={{ shown: !stick.atBottom, onJump: () => { stick.jumpToBottom(); viewport.current?.focus({ preventScroll: true }); } }}>
      <div {...sx("cmp-scroller-content", cmp.scrollerContent)}>{lines.map((l) => <div key={l}>{l}</div>)}</div>
    </ScrollArea>
  );
}
```

## Accessibility

The viewport is a normal scroller: wheel, touch and keyboard scrolling all work, and keyboard focus draws an inset ring on the root. While it overflows and holds nothing that takes focus it is a Tab stop of its own, so the keys scroll it, heard as a group named by `label` ("TypeScript code"); where it holds a link or a control, Tab reaches that instead and the keys scroll round it. Pass `viewportProps` for another role, such as `role="log"` on a transcript, so the region is announced for what it is.

The turn rail is a navigation named by its label ("Turns" by default) with one button a turn, each named "Turn 2 of 5: its first words"; the one at the reader's place carries `aria-current`="location". It is one Tab stop: the arrow keys move between its ticks, Home and End reach the first and last, and Enter or Space jumps. The preview is a picture for the eye and the button's name carries the turn's first words. Each tick is 24px square, and the scroller's gutter keeps it clear of the text. Reduced motion jumps at once, grows no tick and drops the fade of the card.

- ↓ + ↑: Scroll a line
- PageDown + PageUp: Scroll a page
- Home + End: Jump to the start or the end

## API reference

### ScrollArea

- `orientation` (`"vertical" | "horizontal" | "both"`, default `"vertical"`)
- `overscroll` (`"contain" | "auto"`, default `"contain"`)
- `scrollbar` (`"auto" | "shown" | "hidden"`, default `"auto"`)
- `anchoring` (`boolean`, default `true`)
- `scrolls` (`boolean`, default `true`)
- `stickyBar` (`boolean`, default `false`)
- `timeline` (``--${string}``)
- `viewportRef` (`Ref<HTMLDivElement>`)
- `contentRef` (`Ref<HTMLDivElement>`)
- `label` (`string`)
- `viewportProps` (`Omit<ComponentProps<"div">, "ref" | "children" | "style"> & { style?: Omit<CSSProperties, ViewportOverflow> }`)
- `jumpToLatest` (`{ readonly shown: boolean; readonly onJump: () => void; readonly label?: string }`)
- `dropTarget` (`{ readonly shown: boolean; readonly label: string; readonly icon?: IconName }`)
- `turnRail` (`{ items: readonly ScrollAreaTurn[]; label?: string; onJump?: (id: string, index: number) => void }`)
- `rootStyle` (`ScrollAreaStyle`)

Rest props go to `<div>` props.

### springScroll()

- `omega` (`number`, default `26`): Natural frequency in rad/s.
- `axis` (`"x" | "y"`, default `"y"`): The axis it moves: `y` springs `scrollTop`, `x` springs `scrollLeft` (negative right to left, as the browser counts it).
- `onFrame` (`(top: number) => void`): After every write, with the new `scrollTop`.
- `onEnd` (`(reason: SpringScrollEnd) => void`): Once, with why it stopped.

### useScrollKeys()

- `whenBodyFocused` (`boolean`): Also take the keys while nothing has focus (`<body>`): the app's main scroller.
- `end` (`() => void`): End jumps to the bottom through this (a transcript's `jumpToBottom(true)`, which re-pins).
- `onKey` (`() => void`): Called before a key moves the scroller, so a stick-to-bottom hook reads it as the reader's.
