List item
One row of a List, its content spread to both ends, in two sizes.
Demo
Usage
import { Avatar, Badge, List, ListItem, Menu, Row, Switch, Text } from '@tuttn/slant'
<List>
{/* Two children: what the row is at the start, its state or action at the end. */}
<ListItem><Text>Persist checkpoints</Text><Badge tone="success">Done</Badge></ListItem>
{/* More at one end: group it in a div or a Row. Name the action from the row's text. */}
<ListItem>
<div><Text id="sync" variant="title">Sync checkpoints</Text><Text variant="hint">Push each one as it lands.</Text></div>
<Switch defaultChecked aria-labelledby="sync" />
</ListItem>
{/* Dense: one control tall, for one-line rows. */}
<ListItem size="sm"><Text variant="mono">handoff.md</Text><Text variant="mono" muted>4 KB</Text></ListItem>
{/* Line both ends up at the top when the start runs to several lines. */}
<ListItem align="start">…</ListItem>
{/* Slots: media leads, actions trail, and the children between them truncate. */}
<ListItem media={<Avatar initials="AL" name="Ada Lovelace" />}
actions={<Menu label="Actions for Ada" trigger="More" items={actions} onSelect={run} />}>
Ada Lovelace
</ListItem>
{/* With slots, align="start" wraps the content instead. */}
<ListItem align="start" media={…} actions={…}>…</ListItem>
{/* In a list narrower than 20rem, stack puts the value under its label. */}
<ListItem stack><Text>Anthropic API</Text><Text variant="mono">$1,284.10</Text></ListItem>
</List>Props
| Prop | Type | Default | Description |
|---|---|---|---|
as | 'li' | 'div' | 'li' | The element to render: `li` inside a ul or ol, `div` inside a div list. |
align | 'start' | 'center' | 'center' | Cross-axis alignment of the two ends. `start` also lets the content wrap when `media` or `actions` is set. |
size | 'sm' | 'md' | 'md' | `sm` rows are one control tall (34px at regular density) with 13px text, for dense lists. |
media | ReactNode | — | A leading slot: an avatar, an icon or a thumbnail. It keeps its size. |
actions | ReactNode | — | A trailing slot: buttons or a Menu. It keeps its size, and the content truncates first. |
stack | boolean | — | Below 20rem of the row's width, puts the second end under the first, start-aligned, instead of squeezing both onto one line: a label over its value. Without slots the children are wrapped in a `div.sl-list-item-body`; with `media` or `actions`, the content column's children stack, and its text wraps rather than truncates. The item becomes a size container, so it takes its width from the list, never from its content. |
…rest | HTMLAttributes<HTMLElement> | — | className, style, id, aria-* and handlers go on the li or div, and so does the ref. |
Accessibility
- A list item renders an
li, which belongs in aulorol: List renders aulby default. InsideList as="div", passas="div"too. - The row has no role and no tab stop of its own. Put the action inside it, a Switch, a Button or a link, and name it from the row's text with
aria-labelledby, as the settings rows above do. - Keep the start content first in the DOM and the action last. The row spreads them apart but never reorders them, so the reading order and the Tab order match the screen, in either direction. Slots keep that order too: media, content, actions.
- Name the actions after the row, as the Message buttons above do with
aria-label="Message Ada Lovelace": the visible word first, then whom it is for. Give a Menu inactionsalabellike "Actions for Ada". - Truncated text is still all there for a screen reader, but sighted users lose the end of it. If that part matters, use
align="start"so it wraps. - A stacked row keeps its DOM order, label then value, so it reads the same at every width; only the layout moves.
A list item is a flex row that spreads its children with space-between: two children go to the two ends, and a third lands in the middle, so group what belongs together in a Row or a div. A regular row has 10px above and below it and a hairline on top; the first row has no hairline and no space above it. A sm row has no padding and is one control tall instead, 34px at regular density, so it suits one-line rows: taller content grows the row but touches the hairlines. In a divided={false} list every row, whatever its size, gets 4px above and below and no hairline.
Give it media or actions and it lays out three parts instead: the media, a content column that holds the children (.sl-list-item-content), and the actions. Media and actions keep their size; the content takes the rest and cuts a long line off with an ellipsis, or wraps when the item has align="start". The ellipsis shows on text and inline elements, like the spans above; a block child such as a Text paragraph is clipped without one. An empty slot (undefined, null, false or '') counts as not set, so cond && <Button /> works, and with neither slot set the markup is the plain row.
With stack, a row narrower than 20rem puts its second end under the first, both from the start edge, instead of squeezing a label and its value onto one line; wider, it looks like any other row. It measures the row, not the window, so a list in a narrow pane stacks on a wide screen. The item is the container query, and an element can't query its own width, so without slots stack wraps the children in a div.sl-list-item-body that does the spreading; with media or actions the content column's children stack instead, and its text wraps rather than truncating. The slots stay beside it. A container takes its width from its parent, never from its content, so a stacking item fills its list; give a list that sits in something shrink-to-fit, such as an inline-block or a flex item without a width, a width of its own. Without stack the markup and the layout are unchanged.