# List

A vertical list of rows that each hold one record or setting: an optional leading mark, a title with a supporting line, trailing content and a chevron. grouped is one Card with a divider between rows; cards is one Card per row, the form a Table takes on a small screen. A row can be a link, a button or plain, and selectionMode turns every row into a checkbox.

**Dependencies:** npm: 1 direct, 5 total; registry: 4 direct, 6 total.

Native: iOS stable, Android stable.

## Usage

### When to use

- Show records on a small screen, where a Table would not fit: each row becomes a card.
- List settings rows, or the actions of a menu in a bottom Sheet.
- Let the user pick several rows, with a checkbox on each.

### When not to use

- Compare columns of figures. Use Table, and let it scroll sideways.
- The main navigation of an app. Use Sidebar or BottomNav.
- A list of conversations with unread counts. Use ConversationList.

### Native

One Card box with a divider between rows, for settings.

**Grouped**

```text
WerewolfList(label: "Account") {
    WerewolfListItem(action: { open(.profile) }, chevron: true, supporting: AnyView(Text("Name, photo and email"))) { Text("Profile") }
    WerewolfListItem(action: { open(.billing) }, chevron: true, supporting: AnyView(Text("Plan and invoices"))) { Text("Billing") }
    WerewolfListItem(trailing: AnyView(Text("English"))) { Text("Language") }
}
```

```text
WerewolfList(label = "Account") {
    WerewolfListItem(title = { Text("Profile") }, onClick = { open(Screen.Profile) }, chevron = true, supporting = { Text("Name, photo and email") })
    WerewolfListItem(title = { Text("Billing") }, onClick = { open(Screen.Billing) }, chevron = true, supporting = { Text("Plan and invoices") })
    WerewolfListItem(title = { Text("Language") }, trailing = { Text("English") })
}
```

One box per row, the form a Table takes on a phone. Pair swipe actions with a More button or a detail screen so swipe is never the only way.

**Cards with swipe actions**

```text
WerewolfList(label: "Customers", variant: .cards) {
    ForEach(customers) { customer in
        WerewolfListItem(
            action: { open(customer) },
            chevron: true,
            supporting: AnyView(Text("\(customer.plan), \(customer.mrr) MRR")),
            trailing: AnyView(WerewolfBadge(variant: .success) { Text("Active") }),
            trailingActions: [WerewolfListAction(label: "Delete", tone: .danger, action: { delete(customer) }, icon: { Image(systemName: "trash") })]
        ) { Text(customer.name) }
    }
}
```

```text
WerewolfList(label = "Customers", variant = WerewolfListVariant.Cards) {
    customers.forEach { customer ->
        WerewolfListItem(
            title = { Text(customer.name) },
            onClick = { open(customer) },
            chevron = true,
            supporting = { Text("${customer.plan}, ${customer.mrr} MRR") },
            trailing = { WerewolfBadge(variant = WerewolfBadgeVariant.Success) { Text("Active") } },
            trailingActions = listOf(WerewolfListAction(label = "Delete", onClick = { delete(customer) }, tone = WerewolfListActionTone.Danger) { Icon(Icons.Delete, contentDescription = null) }),
        )
    }
}
```

Every row is a checkbox named by its title. The screen owns the set of selected ids.

**Selection mode**

```text
WerewolfList(label: "Customers", selectionMode: true) {
    ForEach(customers) { customer in
        WerewolfListItem(
            onSelectedChange: { on in if on { picked.insert(customer.id) } else { picked.remove(customer.id) } },
            selected: picked.contains(customer.id)
        ) { Text(customer.name) }
    }
}
```

```text
WerewolfList(label = "Customers", selectionMode = true) {
    customers.forEach { customer ->
        WerewolfListItem(
            title = { Text(customer.name) },
            selected = customer.id in picked,
            onSelectedChange = { on -> picked = if (on) picked + customer.id else picked - customer.id },
        )
    }
}
```

A Compose row cannot cut your Text for you. Pass the row's line limit to it so a name is cut with an ellipsis at normal sizes and wraps at large text.

**Truncation on Compose**

```text
WerewolfListItem(title = { Text(customer.name, maxLines = LocalWerewolfLineLimit.current, overflow = TextOverflow.Ellipsis) })
```

## Variants

### variant

On List. grouped is one Card with a divider between rows. cards is one Card per row with spacing-sm between them, for records that were table rows.

Values: `grouped`, `cards`. Default: `grouped`.

## Examples

### Grouped

One Card with a divider between rows, for settings.

```tsx
<List label="Account" className="w-full max-w-sm">
  <ListItem href="#profile" title="Profile" supporting="Name, photo and email" chevron />
  <ListItem href="#billing" title="Billing" supporting="Plan and invoices" chevron />
  <ListItem title="Language" trailing="English" />
</List>
```

### Cards

One Card per row, the form a Table takes on a small screen.

```tsx
<List variant="cards" label="Customers" className="w-full max-w-sm">
  <ListItem href="#acme" title="Acme Inc" supporting="Pro plan" trailing="$1,240" />
  <ListItem href="#globex" title="Globex" supporting="Team plan" trailing="$890" />
</List>
```

### Records

Customer and status Badge on the first line, plan and MRR on the second.

```tsx
<List variant="cards" label="Customers" className="w-full max-w-sm">
  <ListItem href="#acme" title="Acme Inc" supporting="Pro plan, $1,240 MRR" trailing={<Badge variant="success">Active</Badge>} chevron />
  <ListItem href="#globex" title="Globex" supporting="Team plan, $890 MRR" trailing={<Badge variant="warning">Past due</Badge>} chevron />
</List>
```

### Selection mode

Every row is a checkbox named by its title. Hold selected in your state and update it from onSelectedChange.

```tsx
<List selectionMode label="Customers" className="w-full max-w-sm">
  <ListItem title="Acme Inc" selected />
  <ListItem title="Globex" />
</List>
```

## Do and don't

### Do

- Use List for records on a small screen: a Table never ships on a phone, so its rows become cards.
- Put the name in title and the main figure in trailing, then the details and the status Badge in supporting.
- Use grouped for settings rows and for action menus in a bottom Sheet.
- Add chevron only to rows that open something.

### Don't

- Do not use List to compare columns. Use Table and let it scroll sideways.
- Do not nest a button or link in trailing inside a row that is itself a link or button.
- Do not give a row a fixed height. It grows with a second line and never goes under the touch target.

## Props

| Name | Type | Values | Default | Description |
| --- | --- | --- | --- | --- |
| `variant` | `enum` | `grouped`, `cards` | `grouped` | On List. grouped is one Card with a divider between rows. cards is one Card per row with spacing-sm between them, for records that were table rows. |
| `label` (required) | `string` |  |  | On List: names the list for screen readers, such as Customers. |
| `selectionMode` | `boolean` |  | `false` | On List. Every row shows a checkbox named by its title. A press on a row toggles it, and href and onPress are ignored. |
| `leading` | `node` |  |  | On ListItem: an icon, an Avatar or another mark at the start. |
| `title` (required) | `node` |  |  | On ListItem: the main text, such as a customer name. |
| `supporting` | `node` |  |  | On ListItem: a second line under the title. |
| `trailing` | `node` |  |  | On ListItem: content at the end, such as a figure and a status Badge. |
| `href` | `string` |  |  | On ListItem. With it the row is a link. Only on web. Not generated for iOS and Android. |
| `onPress` | `(event: React.MouseEvent<HTMLButtonElement>) => void` |  |  | On ListItem. With it, and no href, the row is a button. |
| `selected` | `boolean` |  | `false` | On ListItem. In selection mode the checkbox is on. Otherwise the row is the current one: aria-current and the muted accent fill. |
| `onSelectedChange` | `(selected: boolean) => void` |  |  | On ListItem. Called with the next value when the row is pressed in selection mode. |
| `disabled` | `boolean` |  | `false` | On ListItem. The row takes no press, no link and no focus, and uses the disabled colors. |
| `chevron` | `boolean` |  | `false` | On ListItem. Shows the chevron at the end. Use it on rows that open another page or a sheet. Hidden in selection mode. |
| `leadingActions` | `{ label: string; icon: ReactNode; tone?: "default" \| "danger"; onPress: () => void }[]` |  |  | Native only, on ListItem: actions revealed by swiping toward the end. Not rendered on web. Only on iOS, Android. |
| `trailingActions` | `{ label: string; icon: ReactNode; tone?: "default" \| "danger"; onPress: () => void }[]` |  |  | Native only, on ListItem: actions revealed by swiping toward the start, such as Delete with tone danger. Not rendered on web. Only on iOS, Android. |

### Slots

| Name | Required | Description |
| --- | --- | --- |
| `content` | yes | The ListItem rows of the list. On native the app supplies them as the content closure. |
| `leading` | no | Optional mark at the start of a row. |
| `title` | yes | The main text of a row. |
| `supporting` | no | Optional second line. |
| `trailing` | no | Optional content at the end of a row. |

## Anatomy

| Part | Description |
| --- | --- |
| `root` | The ul, named by label. With grouped it draws the Card box (surface, border, radius-container); with cards it is a column with spacing-sm between the rows. |
| `listItem` | One row inside its li: a link, a button or a plain div. At least size-target-min tall, it grows with a second line. With cards each row draws its own Card box; with grouped a Separator line above it divides it from the previous row. |
| `separator` | With grouped, the Separator line above every row but the first, in the border color. |
| `leading` | Optional slot at the start: an icon, an Avatar or a Checkbox. It keeps its own size and does not shrink. |
| `title` | The main text of the row, on one line, medium weight, cut with an ellipsis. |
| `supporting` | Optional second line under the title: a detail, a date or a plan, in secondary text. |
| `trailing` | Optional slot at the end: a value, a Badge or a Switch. It does not shrink and is aligned to the title line. |
| `chevron` | Optional Tabler chevron (16px) at the very end that says the row opens something. It points to the end of the reading direction. |

## Accessibility

Role: `list`

| Keys | Action |
| --- | --- |
| `Tab` | Moves to the next row that is a link or a button, or to the next checkbox in selection mode. |
| `Enter` | Follows the focused link, or presses the focused button. |
| `Space` | Presses the focused button, or toggles the focused checkbox in selection mode. |

- The root is a ul named by label (aria-label) and every row is wrapped in an li, so a screen reader announces the list and the number of items.
- A row with href is a link and a row with onPress is a button. A row with neither is plain text in the li and takes no focus. Its whole content is the name, so title comes first in the reading order.
- In selection mode a row holds a checkbox (role checkbox, aria-checked) named by the row's title. A press anywhere on the row toggles it; href and onPress are not used in this mode.
- selected outside selection mode marks the current row with aria-current="true" and the muted accent fill. The fill is never the only sign: the title also steps to the on-muted text color.
- A disabled row has aria-disabled="true", no href and no press, and takes the disabled colors.
- The chevron and the leading mark are decorative. Name an icon-only leading mark through the title, not through the icon.
- Native only: leadingActions and trailingActions are swipe actions. Each one has a label that a screen reader reads as a custom action on the row, so a swipe is never the only way to reach it.

## Install

```bash
npx werewolf-ui add list
```

Run `npx werewolf-ui init` once in your app before adding components. Init sets up tokens, their CSS import and agent rules. Add installs this component and its dependencies while preserving your theme. See [Installation](https://www.werewolf-ui.dev/docs/installation.md) for requirements and the alternative registry setup.

```bash
npx werewolf-ui add list --platform ios
npx werewolf-ui add list --platform android
```

For iOS and Android, run `npx werewolf-ui init --platform ios` or `npx werewolf-ui init --platform android` once, then add with `--platform`. See [Installation](https://www.werewolf-ui.dev/docs/installation.md).
