useChipList
useChipList measures a row of chips and decides which fit within maxRows and which fold into a +N count. It owns no markup. ChipList and Field.ChipInput are built on it; Field.ChipInput uses reserveWidth to hold room for its input.
On this page
Put listRef on the wrapping list and render the measure row into measureRef. Hide the list until measured, since the server always renders unmeasured.
const { listRef, measureRef, visible, hidden, measured } = useChipList({ chips, maxRows: 1 });
const { moreChips } = useStrings();
<div style={{ position: 'relative' }}>
<div ref={listRef} role="list" aria-label="Tags" className="chips" style={{ visibility: measured ? undefined : 'hidden' }}>
{visible.map((chip) => (
<Chip.Root key={chip.key} role="listitem" style={chip.ellipsis ? { maxWidth: chip.maxWidth } : undefined}>
{chip.value}
</Chip.Root>
))}
{hidden > 0 ? (
<span role="listitem">
<Chip.Root aria-hidden>{`+${hidden}`}</Chip.Root>
<VisuallyHidden.Root>{moreChips(hidden)}</VisuallyHidden.Root>
</span>
) : null}
</div>
{/* A hidden row laid out like the list: every chip, then a chip holding `…`, then the overflow count, in that order. */}
<div ref={measureRef} aria-hidden inert className="chips chips-measure">
{chips.map((value, index) => (
<Chip.Root key={index}>{value}</Chip.Root>
))}
<Chip.Root>…</Chip.Root>
<Chip.Root>{`+${chips.length || 1}`}</Chip.Root>
</div>
</div>
The live list, including remove and the folded chips, is on ChipList.
import { ChipList } from '@tounsoo/todae';
import { useState } from 'react';
const TAGS = ['design', 'tokens', 'accessibility', 'a-very-long-tag-that-stays-put', 'react', 'density', 'dark mode', 'docs'];
export default function ChipListOneRow() {
const [chips, setChips] = useState(TAGS);
return (
<div style={{ width: '100%', maxWidth: '18rem', minWidth: 0 }}>
<ChipList.Root
aria-label="Tags"
chips={chips}
maxRows={1}
overflowLabel={(n) => `+${n} more`}
panelLabel={(n) => `${n} more ${n === 1 ? 'tag' : 'tags'}`}
onRemove={(index) => setChips((current) => current.filter((_, i) => i !== index))}
/>
</div>
);
}
Accessible composition
Todae handles: Measurement only. The hook sets no role, no ARIA attribute and no focus. It reads the layout of the elements on listRef and measureRef and returns visible, hidden and measured.
You supply: What ChipList adds on top. A role="list" element, or a <ul>, with an accessible name, and a list item per chip. A measure row marked aria-hidden and inert, so its copies of the chips are neither read nor focusable. A name for +N: hide the count chip with aria-hidden and put the name, moreChips(hidden) from useStrings, in a VisuallyHidden.Root beside it. A way to reach the folded chips if people need them: hidden is only a count, and the folded chips are not rendered unless you render them. Where focus goes when a focused chip folds or is removed.
Watch for: A measure row left in the accessibility tree, so screen readers read every chip twice. A static +N with no control, which tells the count but never the values.
When to use
When: Your own markup for chips capped at maxRows with a +N count, where ChipList does not fit. useChipList gives the visible/hidden split; the list, its clamp CSS and its focus handling are yours.
When not: Applied filters, read-only tags or recipients → ChipList. Typing new chips → Field.ChipInput (already built on the hook). Picking from options → Combobox recipe. Selectable or toggle chips → Checkbox / Button.Root. One chip → Chip.Root. Chip add/remove state → useChipField.
Props
| Name | Type | Description |
|---|---|---|
chips Required | readonly string[] | Chip labels, in order. visible is always a prefix. |
maxRows | number | Fold chips that do not fit into +N. Unset shows every chip. |
reserveWidth | RefObject<HTMLElement | null> | Element whose computed min-width stays free on the last line. A ref so density changes stay current. |