IndexBar
Contacts-style index bar: an alphabetical sidebar plus anchored groups.
The scroll-spy activation (resolveActiveIndex) and touch hit testing
(hitIndexBar) are all pure functions consumed from
@lh-ui/share/core/index-bar; this side is DOM measurement + scrollTo glue.
Basic usage
Index bar (click / touch to jump to a group)
Current index: —
import { useState } from 'react'
import { LhIndexBar, LhIndexBarGroup, LhIndexBarItem } from '@lh-ui/react'
export default function Demo() {
const [current, setCurrent] = useState('')
const groups: Record<string, string[]> = {
A: ['阿白', '阿黄'],
B: ['柏舟', '薄荷'],
C: ['春日', '椿'],
}
return (
<LhIndexBar indexList={['A', 'B', 'C']} onSelect={setCurrent}>
{(['A', 'B', 'C'] as const).map((key) => (
<LhIndexBarGroup key={key} index={key}>
{groups[key].map((name) => (
<LhIndexBarItem key={name}>{name}</LhIndexBarItem>
))}
</LhIndexBarGroup>
))}
</LhIndexBar>
)
}
Sticky headers & custom highlight
sticky keeps group headers pinned (on by default), stickyOffsetTop
reserves space for a navbar, and highlightColor overrides the active
index color:
<LhIndexBar indexList={['甲', '乙', '丙']} stickyOffsetTop={0} highlightColor="#c0392b">
<LhIndexBarGroup index="甲"><LhIndexBarItem>Item 1</LhIndexBarItem></LhIndexBarGroup>
<LhIndexBarGroup index="乙"><LhIndexBarItem>Item 2</LhIndexBarItem></LhIndexBarGroup>
<LhIndexBarGroup index="丙"><LhIndexBarItem>Item 3</LhIndexBarItem></LhIndexBarGroup>
</LhIndexBar>
Custom item rendering
itemSlot replaces the default index item (Vue side: #item slot):
<LhIndexBar indexList={['A', 'B']} itemSlot={(index) => <b>Item {index}</b>} />
IndexBar API
Props
| Name | Description | Type | Default |
|---|---|---|---|
| indexList | Index entries (A-Z by default; empty array hides the sidebar) | string[] | A-Z |
| sticky | Pin group headers | boolean | true |
| stickyOffsetTop | Sticky offset px (navbar reservation) | number | 0 |
| highlightColor | Active index color (falls back to indexBar.highlightColor token) | string | — |
| sx | Quick layout props | LhSxProps | — |
| children | Group content (LhIndexBarGroup / LhIndexBarItem) | ReactNode | — |
| itemSlot | Custom index item renderer (Vue #item slot) | (index: string) => ReactNode | — |
| onSelect | Click / touch a sidebar entry | (index: string) => void | — |
IndexBarGroup API
| Name | Description | Type | Default |
|---|---|---|---|
| index | Group key (aligned with indexList entries) | string | — |
| sx | Quick layout props | LhSxProps | — |
| children | Group items (LhIndexBarItem) | ReactNode | — |
IndexBarItem API
| Name | Description | Type | Default |
|---|---|---|---|
| disabled | Disabled (dimmed) | boolean | false |
| sx | Quick layout props | LhSxProps | — |
| children | Item content | ReactNode | — |
Accessibility
- The root element carries
role="navigation"+aria-label="Index bar"