# View stack

Views that replace one another in one place: a push in from the end, a pop back, a tab rising in.

Live variants: https://design.arcadia.is/components/view-stack

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

```tsx
import { useState, useLayoutEffect, useRef } from "react";
import { Button, ListRow, ViewStack } from "@arcadia/ui";

function Drill() {
  const [file, setFile] = useState<string | null>(null);
  const lastOpened = useRef<string | null>(null);
  const back = useRef<HTMLButtonElement>(null);
  const returnRow = useRef<HTMLButtonElement>(null);

  useLayoutEffect(() => {
    const target = file ? back.current : lastOpened.current ? returnRow.current : null;
    target?.focus({ preventScroll: true });
  }, [file]);

  return (
    <div {...sx("cmp-stack-pane", ViewStackStyles.pane)}>
      <ViewStack view={file ?? "list"} depth={file ? 1 : 0} rootStyle={ViewStackStyles.stack}>
        {file ? (
          <div {...sx("cmp-stack-view", ViewStackStyles.view)}>
            <div {...sx("cmp-bar", cmp.bar)}><Button ref={back} variant="ghost" size="sm" icon="chevron-left" onClick={() => setFile(null)}>Back</Button></div>
            <h3 data-toc-skip {...sx("cmp-stack-title", ViewStackStyles.title)}>{file}</h3>
            <p {...sx("cmp-stack-body", ViewStackStyles.body)}>The file, opened from the list.</p>
          </div>
        ) : (
          <div {...sx("cmp-stack-view", ViewStackStyles.view)}>
            {FILES.map(f => <ListRow key={f} ref={f === lastOpened.current ? returnRow : undefined} leading="file" label={f} labelWraps onClick={() => { lastOpened.current = f; setFile(f); }} />)}
          </div>
        )}
      </ViewStack>
    </div>
  );
}
```

## Accessibility

The leaving view is `inert` and hidden from assistive tech the moment the move starts, so focus and the pointer land only on the new view and no input waits on the animation. Move focus into the new view yourself (its heading or its first control, then back to the row that opened it). Reduced motion swaps the views at once.

## API reference

### ViewStack

- `view` (`string`, required): Which view shows: a new value is a move to another view.
- `depth` (`number`, default `0`): How deep the view is (a list 0, the item it opened 1). Deeper than the last is a push: the new view comes in from the end as the old one sets back. Shallower is a pop, the mirror. The same depth is a swap: the new view rises in.
- `rootStyle` (`RootStyle<ViewStackKnobs>`): Typed StyleX override: where the stack sits (it fills a flex or grid parent like a block) and its surface.
- `children` (`ReactNode`): The view.

Rest props go to `HTMLAttributes<HTMLDivElement>`.
