# Sidebar rows

A sidebar's rows as one list: rows under a row to any depth, a label that heads a group, reorderable by drag, each with its own menu, inside an app sidebar group.

Live variants: https://design.arcadia.is/components/sidebar-rows

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

## When to use

- The rows of a sidebar that a person puts in their own order
- The rows of an AppSidebar group: a group's `items` are drawn by this list
- A row that carries its own menu, opened from the row itself
- Rows under a row to any depth, folded by a caret after the name: a project, its agents and their chats
- A label row that heads a group of chats, with a count and one quiet action

## When not to use

- A list of things to choose from inside a page: use ListRow
- Labelled sections with a group action: give each section's rows to an AppSidebar group's `items`
- A menu of commands on one object: use ContextMenu alone
- A list of settings in a window: use SettingsGroup

## In context

In Crew, a person's projects stand in one list on the sidebar's ground, and a channel carries its threads under it, folded by the caret after its name. In Act, the same list holds projects, the agents inside them and their chats, to any depth. A dragged row shows the kit's drop line on an edge, or takes the drop itself on a row that holds others, and the row's own menu renames, copies or archives the project without opening it.

Choose a view to move the current row.

```tsx
import { useState } from "react";
import { SidebarRows } from "@arcadia/ui";

function Usage() {
  const [current, setCurrent] = useState("inbox");
  const views = [
    { id: "inbox", label: "Inbox", leading: "mail", end: 12 },
    { id: "today", label: "Today", leading: "calendar" },
    { id: "upcoming", label: "Upcoming", leading: "time" },
  ] as const;
  return (
    <SidebarRows
      aria-label="Views"
      items={views.map((view) => ({
        ...view, current: view.id === current, onSelect: () => setCurrent(view.id),
      }))}
    />
  );
}
```

## Usage

```tsx
import { useState } from "react";
import { SidebarRows } from "@arcadia/ui";

function Usage() {
  const [current, setCurrent] = useState("inbox");
  const views = [
    { id: "inbox", label: "Inbox", leading: "mail", end: 12 },
    { id: "today", label: "Today", leading: "calendar" },
    { id: "upcoming", label: "Upcoming", leading: "time" },
  ] as const;
  return (
    <SidebarRows
      aria-label="Views"
      items={views.map((view) => ({
        ...view, current: view.id === current, onSelect: () => setCurrent(view.id),
      }))}
    />
  );
}
```

Each row is a ListRow in a list of its own, so a link, the page you are on, the quiet away dot and the hover actions behave as they do everywhere in the kit. `href` makes a row a link, `onSelect` a button, and with neither the row is still. The list shares one leading column, so a row with a glyph and one without keep their names on one edge.

A row may hold rows under it to any depth: `children` and `childrenLabel`, each child a ListRow one indent step in, with `open`, `defaultOpen` or `onOpenChange` for its own fold. The indent stops after `indentCap` steps (default 3), so a name at depth keeps its room while its `aria-level` still says how deep it is. Folded, a row says nothing of the children itself, so their unread state goes in its `end`.

A child of `kind: "label"` is a quiet word above its own children, a project's Chats: it opens no page, a press folds it, and folded its end shows its `count` beside its one `action`. A row's `description` is its second line, cut to one. With `onMove`, a drag may end on a row that has `dropInto`, or between two rows at any level; a row never lands in itself or under itself.

A chat's row carries more. `mark` stands a state after the name, and an away row's dot follows it. `metaStays` keeps the `end` in view beside the actions, `actionsOpen` holds the actions while one of them has its menu open, and `touchActions: "menu"` moves them into the row's `menu` on a touch screen. With `descriptionYields`, the description stands after the name on its line: `true` gives way before the name loses a word, and `false` keeps its seat while the name steps down.

`selected` marks a row chosen with others: it uses a quieter fill than the current page. The list becomes a tree that holds many choices, the current row still the only page. A row's `data-*` marks land on its element, never over the kit's own. `onDragStart` hands the product the drag's `DataTransfer` as it starts, after the row's id, so a chat dragged out of the list carries the product's own data, and `draggable: false` holds one row still. `wrapRow` stands each row in a part of the product's, a hover card whose trigger is the row itself.

## Examples

### A row with its own menu

A right click, a long press, the menu key or Shift+F10 on the row opens it. The trigger is the row itself, which keeps its own press: opening the menu never chooses the row.

```tsx
import { MenuItem, SidebarRows } from "@arcadia/ui";

<SidebarRows
  aria-label="Projects"
  items={[
    {
      id: "first", label: "First project", onSelect: () => {},
      menu: (<><MenuItem>Rename</MenuItem><MenuItem>Copy link</MenuItem><MenuItem>Archive</MenuItem></>),
    },
    { id: "second", label: "Second project", onSelect: () => {} },
  ]}
/>
```

### Put in an order

With `onReorder`, rows are dragged with HTML5 drag and drop. The row under the pointer shows the kit's drop line, `--ring-width` of `--ring`, on the edge the drop will take, and no row moves until the product reorders its list.

```tsx
import { useState } from "react";
import { SidebarRows } from "@arcadia/ui";

function Reorderable() {
  const [order, setOrder] = useState(["inbox", "today", "upcoming"]);
  const move = (from: string, to: string, edge: "before" | "after") => setOrder((now) => {
    const next = now.filter((id) => id !== from);
    const at = next.indexOf(to);
    next.splice(edge === "before" ? at : at + 1, 0, from);
    return next;
  });
  return (
    <SidebarRows
      aria-label="Views"
      items={order.map((id) => ({ id, label: id === "inbox" ? "Inbox" : id === "today" ? "Today" : "Upcoming", leading: GLYPH[id] }))}
      onReorder={move}
    />
  );
}
```

### A tree

Rows under rows to any depth: two projects, the agents inside them and a project within a project. A label row heads a group with its count and folds on a press, and a row's description is its second line.

```tsx
import { useState } from "react";
import { Avatar, MenuItem, SidebarRows } from "@arcadia/ui";

function ATree() {
  const [chats, setChats] = useState(["Icon set", "Release notes for the September launch", "Onboarding"]);
  const newChat = () => setChats((now) => [...now, "New chat"]);
  return (
    <SidebarRows
      aria-label="Projects and chats"
      items={[
        {
          id: "launch", label: "Launch", leading: "folder", onSelect: () => {}, childrenLabel: "In Launch",
          children: [
            {
              id: "juniper", label: "Juniper", leading: <Avatar kind="agent" avatar={JUNIPER} name="Juniper" size="text" aria-hidden />,
              onSelect: () => {}, childrenLabel: "In Juniper",
              children: [{
                id: "chats", kind: "label", label: "Chats", count: chats.length, defaultOpen: false,
                childrenLabel: "Chats in Juniper",
                action: { label: "New chat in Juniper", icon: "plus", onPress: newChat },
                menu: <MenuItem onClick={newChat}>New chat in Juniper</MenuItem>,
                children: chats.map((chat, at) => ({
                  id: "chat-" + at, label: chat, href: "#chats-chosen-together",
                  description: at === 0 ? "Ada shared the new icon set" : undefined,
                })),
              }],
            },
            {
              id: "atlas", label: "Atlas", leading: <Avatar kind="agent" avatar={ATLAS} name="Atlas" size="text" aria-hidden />,
              onSelect: () => {},
            },
            {
              id: "website", label: "Website redesign for the September launch", leading: "folder", onSelect: () => {}, childrenLabel: "In Website",
              children: [{
                id: "sage", label: "Sage", leading: <Avatar kind="agent" avatar={SAGE} name="Sage" size="text" aria-hidden />,
                onSelect: () => {},
              }],
            },
          ],
        },
        { id: "keep", label: "Keep", leading: "archive", href: "#rows-under-a-row" },
      ]}
    />
  );
}
```

### A child row fills from its label

A tree's child row starts its fill at its label, so the clock in its leading column and the rail to its parent stand outside the highlight. Choose a routine to see where the highlight starts.

```tsx
import { useState } from "react";
import { Avatar, characterFromId, SidebarRows } from "@arcadia/ui";

function ChildRows() {
  const [current, setCurrent] = useState("hourly");
  return (
    <SidebarRows
      aria-label="Routines"
      items={[
        {
          id: "auditor", label: "Auditor", leading: <Avatar kind="agent" avatar={characterFromId("agt_auditor")} name="Auditor" size="text" aria-hidden />, onSelect: () => {}, childrenLabel: "Routines",
          children: [
            { id: "hourly", label: "Design audit", leading: "routine", onSelect: () => setCurrent("hourly"), current: current === "hourly", end: "Waiting" },
            { id: "nightly", label: "Nightly review", leading: "routine", onSelect: () => setCurrent("nightly"), current: current === "nightly", end: "Failed" },
            { id: "more", label: "Other routines", end: "2 working" },
          ],
        },
      ]}
    />
  );
}
```

### Chats chosen together

Two chats chosen with the open one, a running state that stays beside the actions, and a draft's word that keeps its seat on the name's line.

```tsx
import { IconButton, MenuItem, SidebarRows, StatusDot } from "@arcadia/ui";

function ChosenTogether() {
  const archive = (name: string) => <IconButton icon="archive" label={"Archive " + name} size="xs" tone="muted" aria-hidden tabIndex={-1} />;
  const menu = <MenuItem>Archive</MenuItem>;
  return (
    <SidebarRows
      aria-label="Chats"
      items={[
        {
          id: "launch", label: "Launch", leading: "folder", onSelect: () => {}, childrenLabel: "Chats in Launch",
          children: [
            {
              id: "icons", label: "Icon set", href: "#chats-chosen-together", current: true, selected: true, actions: archive("Icon set"), menu,
              end: <StatusDot status="running" label="Running" />, metaStays: true,
            },
            {
              id: "notes", label: "Release notes", href: "#chats-chosen-together", selected: true,
              mark: <StatusDot status="unread" label="Unread" />, actions: archive("Release notes"), menu,
            },
            {
              id: "pricing", label: "Pricing page", href: "#chats-chosen-together", description: "Draft", descriptionYields: false,
              actions: archive("Pricing page"), touchActions: "menu", menu,
            },
          ],
        },
      ]}
    />
  );
}
```

### A card on hover

`wrapRow` stands each row in a hover card whose trigger is the row itself, so the row keeps its press, its menu and its place in the tree.

```tsx
import {
  HoverCard, HoverCardContent, HoverCardGroup, HoverCardRow, HoverCardTitle,
  HoverCardTrigger, SidebarRows,
} from "@arcadia/ui";

<HoverCardGroup>
  <SidebarRows
    aria-label="Chats"
    items={[
      { id: "report", label: "Quarterly report", href: "#chats-chosen-together", end: "13h" },
      { id: "copy", label: "Onboarding copy", href: "#chats-chosen-together", end: "2d" },
    ]}
    wrapRow={(row, item) => (
      <HoverCard>
        <HoverCardTrigger render={row} />
        <HoverCardContent>
          <HoverCardTitle>{item.label}</HoverCardTitle>
          <HoverCardRow icon="branch">{PLACES[item.id]?.branch}</HoverCardRow>
          <HoverCardRow icon="folder">{PLACES[item.id]?.folder}</HoverCardRow>
        </HoverCardContent>
      </HoverCard>
    )}
  />
</HoverCardGroup>
```

### Thread guides

`guides` draws each nested row's branch line, as a thread hangs under its chat: a hook from the parent's glyph to the row, bent at its foot, and a line on to the next sibling, which a last child does not draw. A nested row then stands a branch past its parent's glyph. A child's own children hang from its glyph the same way, their first hook rising to it. The lines are `--stroke-guide`, hairline in both themes.

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

<SidebarRows
  aria-label="Channels"
  guides
  items={[
    {
      id: "bugs", label: "Bugs", leading: "hash", onSelect: () => {}, childrenLabel: "Threads in Bugs",
      children: [
        { id: "staging", label: "Deploy failed on staging", leading: "chat", current: true, onSelect: () => {} },
        {
          id: "flaky", label: "The upload test is flaky", leading: "chat", onSelect: () => {}, childrenLabel: "Replies in The upload test is flaky",
          children: [
            { id: "port", label: "Pin the fixture server's port", leading: "chat", onSelect: () => {} },
            { id: "retry", label: "No retries in the runner", leading: "chat", onSelect: () => {} },
          ],
        },
        { id: "notes", label: "Waiting on the release notes", leading: "chat", onSelect: () => {} },
      ],
    },
    {
      id: "design", label: "Design", leading: "hash", onSelect: () => {}, childrenLabel: "Threads in Design",
      children: [{ id: "rows", label: "Caret after the name", leading: "chat", onSelect: () => {} }],
    },
    { id: "releases", label: "Releases", leading: "hash", onSelect: () => {} },
  ]}
/>
```

### Rows under a row

A channel folded with its unread count in its `end`, beside one left open.

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

<SidebarRows
  aria-label="Channels"
  items={[
    {
      id: "bugs", label: "Bugs", leading: "hash", end: "3", defaultOpen: false, onSelect: () => {}, childrenLabel: "Threads in Bugs",
      children: [
        { id: "staging", label: "Deploy failed on staging" },
        { id: "notes", label: "Waiting on the release notes" },
      ],
    },
    {
      id: "design", label: "Design", leading: "hash", onSelect: () => {}, childrenLabel: "Threads in Design",
      children: [{ id: "rows", label: "Caret after the name" }],
    },
    { id: "releases", label: "Releases", leading: "hash", onSelect: () => {} },
  ]}
/>
```

## Accessibility

The list is named by `aria-label`, or by `aria-labelledby` where its name is on screen, as an AppSidebar group's label is. A list in which a row holds children, or a row says `selected`, is a tree: `role="tree"`, each row a `treeitem` with its `aria-level` and `aria-expanded`, its children a `group`. One tab stop covers the whole tree: Up and Down move over the visible rows, Right opens a folded row or moves to its first child, Left folds an open row or moves to its parent, Home and End reach the ends, a letter moves to the next row that starts with it, Enter opens the page and F2 asks the product to rename. The caret after a name and a label's action are pointer controls, hidden from a reader; the row's tree item carries its expanded state, and folding with the focus inside hands it to the row. A label row folds on its own press.

A row that acts is one stop, the current page says `aria-current="page"`, and the away dot is named Away. Once a row says `selected`, the tree says `aria-multiselectable`, a chosen row says `aria-selected="true"` and every other row `aria-selected="false"`. Dragging is a pointer gesture, so a product that reorders must also offer the move in the row's menu for a keyboard. A row's actions in a tree are pointer controls too, so their commands stand in its menu, which with `touchActions: "menu"` is where a touch screen finds them. With `onMove`, a row that takes a drop takes it over its whole height, and the line between rows stands at their level's indent.

A menu on a row is the kit's ContextMenu, so it opens on the menu key and Shift+F10 as it does on a right click, keeps the focus inside while it is open and gives it back to the row when it closes.

## API reference

### SidebarRows

- `items` (`readonly SidebarRowItem[]`, required): The rows, in the order they stand.
- `onReorder` (`(fromId: string, toId: string, edge: SidebarRowEdge) => void`): Given, the rows can be dragged into a new order; the product reorders its list on this call. A drag stays among siblings: two rows of the list, or two children of one row.
- `onMove` (`(id: string, intoId: string | null, beforeId: string | null) => void`): Given, a drag may cross levels: onto a row that has `dropInto`, or between two rows at any level. `intoId` is the row it lands in, null the top; `beforeId` the row it lands before, null the end. A row never lands in itself or under itself. Given with `onReorder`, it takes every drop.
- `onDragStart` (`(id: string, data: DataTransfer) => void`): Given, a row's drag carries the product's own data: called as a drag starts, with the row's id and the drag's `DataTransfer`, after the kit has set its own (`text/plain`, the id). Alone, it drags rows out of the list, and no row takes a drop.
- `onRename` (`(id: string) => void`): Given, F2 on a row of a tree calls this with its id.
- `wrapRow` (`(row: ReactElement<Record<string, unknown>>, item: SidebarRowItem) => ReactElement`): Given, each row stands in what it returns: it takes the row's `ListRow` and its item and returns the part that renders that row, a `HoverCard` whose `HoverCardTrigger` renders it.
- `guides` (`boolean`, default `false`): Draw each nested row's branch line (a thread under its chat): a hook from its parent's glyph to the row, and the line on to its next sibling. A nested row then stands a branch past its parent's glyph instead of one indent step in.
- `indentCap` (`number`, default `3`): How many indent steps a row moves in; deeper rows stop there, so a name keeps its room.
- `aria-label` (`string`): The list's name, for a reader.
- `aria-labelledby` (`string`): The element that names the list, where its name is already on screen (a group's label).
- `rootStyle` (`RootStyle`): Typed StyleX override: where the list sits.
