Skip to main content

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​

NameDescriptionTypeDefault
indexListIndex entries (A-Z by default; empty array hides the sidebar)string[]A-Z
stickyPin group headersbooleantrue
stickyOffsetTopSticky offset px (navbar reservation)number0
highlightColorActive index color (falls back to indexBar.highlightColor token)string—
sxQuick layout propsLhSxProps—
childrenGroup content (LhIndexBarGroup / LhIndexBarItem)ReactNode—
itemSlotCustom index item renderer (Vue #item slot)(index: string) => ReactNode—
onSelectClick / touch a sidebar entry(index: string) => void—

IndexBarGroup API​

NameDescriptionTypeDefault
indexGroup key (aligned with indexList entries)string—
sxQuick layout propsLhSxProps—
childrenGroup items (LhIndexBarItem)ReactNode—

IndexBarItem API​

NameDescriptionTypeDefault
disabledDisabled (dimmed)booleanfalse
sxQuick layout propsLhSxProps—
childrenItem contentReactNode—

Accessibility​

  • The root element carries role="navigation" + aria-label="Index bar"