跳到主要內容

IndexBar

通訊錄式索引欄:右側字母索引 + 分組錨點定位。scroll-spy 啟用換算(resolveActiveIndex)與觸控換算(hitIndexBar)全部消費 @lh-ui/share/core/index-bar 純邏輯;本端為 DOM 測量 + scrollTo 膠水。

基礎用法​

索引欄(點擊 / 觸控定位分組)

目前索引:—

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 開啟分組頭吸頂(預設開),stickyOffsetTop 為導航列佔位偏移,highlightColor 自訂啟用索引高亮色:

<LhIndexBar indexList={['甲', '乙', '丙']} stickyOffsetTop={0} highlightColor="#c0392b">
<LhIndexBarGroup index="甲"><LhIndexBarItem>甲一</LhIndexBarItem></LhIndexBarGroup>
<LhIndexBarGroup index="乙"><LhIndexBarItem>乙一</LhIndexBarItem></LhIndexBarGroup>
<LhIndexBarGroup index="丙"><LhIndexBarItem>丙一</LhIndexBarItem></LhIndexBarGroup>
</LhIndexBar>

item 自訂渲染​

itemSlot 替換預設索引項渲染(Vue 端的 #item 插槽):

<LhIndexBar indexList={['A', 'B']} itemSlot={(index) => <b>项{index}</b>} />

IndexBar API​

屬性​

屬性說明類型預設值
indexListstring[]string[]A-Z
stickybooleanbooleantrue
stickyOffsetTopnumbernumber0
highlightColorstringstring—
sxLhSxPropsLhSxProps—
childrenReactNodeReactNode—
itemSlot(index) => ReactNode(index: string) => ReactNode—
onSelect(index) => void(index: string) => void—

IndexBarGroup​

屬性說明類型
index分組索引鍵(與 indexList 項對齊)string
sxLhSxPropsLhSxProps
childrenLhIndexBarItemReactNode

IndexBarItem​

屬性說明類型預設值
disabled停用(降低對比度)booleanfalse
sxLhSxPropsLhSxProps—
childrenReactNodeReactNode—

無障礙​

  • 根元素帶 role="navigation" + aria-label="索引欄"。