useChipList
useChipList는 칩 한 줄을 측정해 어떤 칩이 maxRows 안에 들어가고 어떤 칩이 +N 개수로 접히는지 정합니다. 마크업은 갖지 않습니다. ChipList와 Field.ChipInput이 이 훅으로 만들어졌고, Field.ChipInput은 reserveWidth로 입력 자리를 확보합니다.
이 페이지의 내용
감싸는 목록에 listRef를 두고, 측정용 행은 measureRef에 렌더링하세요. 서버는 항상 측정 전 상태로 렌더링하므로, measured가 될 때까지 목록을 숨기세요.
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>
제거와 접힌 칩을 포함한 동작하는 목록은 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>
);
}
접근성을 지키는 조합
Todae가 맡는 것: 측정만 합니다. 이 훅은 role, ARIA 속성, 포커스를 설정하지 않습니다. listRef와 measureRef의 요소 레이아웃을 읽고 visible, hidden, measured를 돌려줍니다.
직접 제공할 것: ChipList가 그 위에 더하는 것을 직접 제공하세요. 접근 가능한 이름이 있는 role="list" 요소나 <ul>, 그리고 칩마다 목록 항목이 필요합니다. 측정용 줄에는 aria-hidden과 inert를 지정해, 그 안의 칩 복사본이 읽히거나 포커스를 받지 않게 하세요. +N에 이름을 주세요. 개수 칩은 aria-hidden으로 숨기고, useStrings의 moreChips(hidden)인 이름을 옆의 VisuallyHidden.Root에 넣으세요. 접힌 칩이 필요하다면 거기에 닿을 방법을 주세요. hidden은 개수일 뿐이고, 접힌 칩은 직접 렌더링하지 않으면 화면에 없습니다. 포커스된 칩이 접히거나 제거될 때 포커스가 갈 곳도 정하세요.
주의할 점: 측정용 줄을 접근성 트리에 남겨 두면 스크린 리더가 모든 칩을 두 번 읽습니다. 컨트롤이 없는 고정된 +N은 개수만 알려 주고 값은 알려 주지 않습니다.
언제 쓰나요
언제: ChipList가 맞지 않아, maxRows로 제한하고 +N 개수를 보이는 칩의 마크업을 직접 만들 때. 보이는/숨긴 칩 구분은 useChipList가 하고, 목록과 줄 제한 CSS, 포커스 처리는 직접 만듭니다.
쓰지 않을 때: 적용된 필터, 읽기 전용 태그, 수신자 → ChipList. 새 칩 입력 → Field.ChipInput(이미 이 훅으로 만들어져 있습니다). 옵션에서 고르기 → Combobox 레시피. 선택하거나 토글하는 칩 → Checkbox / Button.Root. 칩 하나 → Chip.Root. 칩 추가/제거 상태 → useChipField.
Props
| 이름 | 타입 | 설명 |
|---|---|---|
chips 필수 | readonly string[] | 순서대로 나열한 칩 레이블. visible은 항상 앞부분입니다. |
maxRows | number | 들어가지 않는 칩을 +N으로 접습니다. 설정하지 않으면 모든 칩을 보여 줍니다. |
reserveWidth | RefObject<HTMLElement | null> | 계산된 min-width만큼 마지막 줄에서 비워 둘 요소. 밀도가 바뀌어도 최신 값을 유지하도록 ref로 받습니다. |