|
| 1 | +### TruncateList |
| 2 | + |
| 3 | +Responsive list that automatically collapses overflowing items and shows a +N counter. By default, the counter is rendered with the Tag component. |
| 4 | + |
| 5 | +Built on top of `react-responsive-overflow-list`. |
| 6 | + |
| 7 | +#### Import and basic usage |
| 8 | + |
| 9 | +```tsx |
| 10 | +import React from "react"; |
| 11 | +import {TruncateList, Tag, Tooltip} from "addon-ui"; |
| 12 | + |
| 13 | +type User = {id: string; name: string}; |
| 14 | + |
| 15 | +const users: User[] = [ |
| 16 | + {id: "1", name: "Alice"}, |
| 17 | + {id: "2", name: "Bob"}, |
| 18 | + {id: "3", name: "Charlie"}, |
| 19 | + {id: "4", name: "Diana"}, |
| 20 | + {id: "5", name: "Eve"}, |
| 21 | + {id: "6", name: "Frank"}, |
| 22 | +]; |
| 23 | + |
| 24 | +export function Example() { |
| 25 | + return ( |
| 26 | + <div style={{width: 260}}> |
| 27 | + {/* Default counter: <Tag>+N</Tag> */} |
| 28 | + <TruncateList |
| 29 | + items={users} |
| 30 | + collapseFrom="end" |
| 31 | + visibleItemCount={Infinity} |
| 32 | + itemRenderer={u => <Tag key={u.id}>{u.name}</Tag>} |
| 33 | + /> |
| 34 | + |
| 35 | + {/* Custom overflow renderer with tooltip */} |
| 36 | + <TruncateList |
| 37 | + items={users} |
| 38 | + collapseFrom="end" |
| 39 | + visibleItemCount={Infinity} |
| 40 | + itemRenderer={u => <Tag key={u.id}>{u.name}</Tag>} |
| 41 | + renderOverflow={hidden => ( |
| 42 | + <Tooltip content={hidden.map(u => u.name).join(", ")}> |
| 43 | + <Tag>{`+${hidden.length}`}</Tag> |
| 44 | + </Tooltip> |
| 45 | + )} |
| 46 | + /> |
| 47 | + </div> |
| 48 | + ); |
| 49 | +} |
| 50 | +``` |
| 51 | + |
| 52 | +--- |
| 53 | + |
| 54 | +#### Props |
| 55 | + |
| 56 | +Only the prop name, type, and default are listed below. |
| 57 | + |
| 58 | +| Prop | Type | Default | |
| 59 | +| ------------------ | --------------------------------------- | ------- | |
| 60 | +| `counterClassName` | `string` | — | |
| 61 | +| `renderOverflow` | `(hiddenItems: T[]) => React.ReactNode` | — | |
| 62 | +| OverflowList props | all `OverflowListProps<T>` | — | |
| 63 | + |
| 64 | +Notes: |
| 65 | + |
| 66 | +- If `renderOverflow` is not provided, the component renders `<Tag className="truncate-list__counter">+N</Tag>` by default. |
| 67 | +- Supports contextual defaults via the UI provider: `useComponentProps("truncateList")`. |
| 68 | +- The base props are inherited from `react-responsive-overflow-list` (e.g., `items`, `itemRenderer`, `visibleItemCount`, `collapseFrom`, etc.). See the package docs for the full API: https://www.npmjs.com/package/react-responsive-overflow-list |
| 69 | + |
| 70 | +--- |
| 71 | + |
| 72 | +### CSS variables for TruncateList |
| 73 | + |
| 74 | +Only variables actually referenced in `src/components/TruncateList/truncate-list.module.scss` are listed, with their exact fallback chains. |
| 75 | + |
| 76 | +| Variable | Fallback chain | |
| 77 | +| ----------------------------------- | ------------------------------------------------------------------ | |
| 78 | +| `--truncate-list-gap` | `var(--truncate-list-gap, 8px)` | |
| 79 | +| `--truncate-list-counter-br-radius` | `var(--truncate-list-counter-br-radius, 9999px)` | |
| 80 | +| `--truncate-list-counter-bg-color` | `var(--truncate-list-counter-bg-color, var(--bg-secondary-color))` | |
| 81 | +| `--truncate-list-counter-padding` | `var(--truncate-list-counter-padding, 0px 4px)` | |
| 82 | +| `--truncate-list-counter-color` | `var(--truncate-list-counter-color, var(--text-secondary-color))` | |
| 83 | +| `--truncate-list-counter-font-size` | `var(--truncate-list-counter-font-size, inherit)` | |
| 84 | + |
| 85 | +Notes: |
| 86 | + |
| 87 | +- RTL support: in right-to-left layouts, the list direction is reversed so that collapsing and counter placement remain intuitive. |
| 88 | + |
| 89 | +--- |
| 90 | + |
| 91 | +### Accessibility (A11y) |
| 92 | + |
| 93 | +- Ensure each rendered item has clear text or an accessible label. |
| 94 | +- Provide an accessible label for the counter (e.g., `aria-label="3 more"`), especially if the visual `+N` alone may not be descriptive. |
| 95 | +- If items are interactive (buttons/links), ensure they remain keyboard-accessible after truncation. |
| 96 | + |
| 97 | +--- |
| 98 | + |
| 99 | +### Usage notes |
| 100 | + |
| 101 | +- Spacing between items is controlled by `--truncate-list-gap`. |
| 102 | +- Use `visibleItemCount={Infinity}` to let the component determine how many items can fit based on available width. |
| 103 | +- `collapseFrom` can be used to collapse items from the `start` or the `end` of the list (depending on library behavior). |
| 104 | +- The default counter uses the Tag component; customize via `counterClassName` or provide a `renderOverflow` implementation. |
0 commit comments