Menu 導航選單
為網站提供導航功能的選單,支援水平 / 垂直兩種佈局模式、子選單內聯展開與飛出面板、摺疊為圖示條、手風琴單開與完整鍵盤導航。
基礎用法
已選:gallery
import { useState } from 'react'
import { LhMenu, LhMenuItem, LhSubMenu } from '@lh-ui/react'
export default function Demo() {
const [active, setActive] = useState('gallery')
return (
<LhMenu defaultValue="gallery" defaultOpeneds={['works']} onSelect={(key) => setActive(key)}>
<LhMenuItem index="gallery">畫廊首頁</LhMenuItem>
<LhSubMenu index="works" title="作品管理">
<LhMenuItem index="works-list">作品列表</LhMenuItem>
<LhMenuItem index="works-upload">上傳作品</LhMenuItem>
</LhSubMenu>
<LhMenuItem index="coming" disabled>
敬請期待
</LhMenuItem>
</LhMenu>
)
}
水平模式
mode="horizontal" 下根節點為頂欄選單(role="menubar"),子選單游標停留 / 點選後從觸發器下方飛出;巢狀子選單從右側飛出。
import { LhMenu, LhMenuItem, LhSubMenu } from '@lh-ui/react'
export default function Demo() {
return (
<LhMenu mode="horizontal" defaultValue="home">
<LhMenuItem index="home">首頁</LhMenuItem>
<LhSubMenu index="works" title="作品管理">
<LhMenuItem index="works-list">作品列表</LhMenuItem>
<LhMenuItem index="works-upload">上傳作品</LhMenuItem>
</LhSubMenu>
</LhMenu>
)
}
摺疊模式與手風琴
垂直模式傳 collapse 摺疊為圖示條(子選單改為飛出面板);uniqueOpened 開啟手風琴:同時最多展開一個子選單,再次點選收起(允許全部收起)。
import { LhMenu, LhMenuItem, LhSubMenu } from '@lh-ui/react'
export default function Demo() {
return (
<LhMenu collapse uniqueOpened defaultValue="works-list" defaultOpeneds={['works']}>
<LhSubMenu index="works" title="作品管理">
<LhMenuItem index="works-list">作品列表</LhMenuItem>
<LhMenuItem index="works-upload">上傳作品</LhMenuItem>
</LhSubMenu>
<LhSubMenu index="settings" title="帳戶設定">
<LhMenuItem index="profile">個人資料</LhMenuItem>
</LhSubMenu>
</LhMenu>
)
}
分組
LhMenuItemGroup 為子項渲染不可選取的分組標題。
import { LhMenu, LhMenuItem, LhMenuItemGroup } from '@lh-ui/react'
export default function Demo() {
return (
<LhMenu>
<LhMenuItemGroup title="分組一">
<LhMenuItem index="a">項 A</LhMenuItem>
<LhMenuItem index="b">項 B</LhMenuItem>
</LhMenuItemGroup>
<LhMenuItemGroup title="分組二">
<LhMenuItem index="c">項 C</LhMenuItem>
</LhMenuItemGroup>
</LhMenu>
)
}
Menu Props
| 屬性 | 說明 | 類型 | 可選值 | 預設值 |
|---|---|---|---|---|
| value | 當前啟用項(受控;對應 plus 的 modelValue) | string | — | — |
| defaultValue | 當前啟用項(非受控預設值) | string | — | — |
| mode | 佈局模式 | 'horizontal' | 'vertical' | — | 'vertical' |
| collapse | 垂直模式摺疊為圖示條(子選單改為飛出面板) | boolean | — | false |
| defaultOpeneds | 預設展開的子選單鍵(非受控) | string[] | — | [] |
| openeds | 受控展開的子選單鍵 | string[] | — | — |
| uniqueOpened | 手風琴:同時只展開一個子選單 | boolean | — | false |
| sx | 快速佈局屬性 | SxProps | — | — |
MenuItem Props
| 屬性 | 說明 | 類型 | 預設值 |
|---|---|---|---|
| index | 唯一標識(必填,作為啟用態的鍵) | string | — |
| disabled | 是否停用(tabindex -1 + aria-disabled) | boolean | false |
| children | 項內容 | ReactNode | — |
| sx | 快速佈局屬性 | SxProps | — |
LhSubMenu Props
| 屬性 | 說明 | 類型 | 預設值 |
|---|---|---|---|
| index | 唯一標識(必填,作為展開態的鍵) | string | — |
| title | 標題(ReactNode,對齊 plus 的 #title 插槽) | ReactNode | — |
| disabled | 是否停用(停用後無法展開) | boolean | false |
| children | 子級內容(LhMenuItem / LhSubMenu 可遞迴巢狀) | ReactNode | — |
| sx | 快速佈局屬性 | SxProps | — |
MenuItemGroup Props
| 屬性 | 說明 | 類型 | 預設值 |
|---|---|---|---|
| title | 分組標題(ReactNode,對齊 plus 的 #title 插槽) | ReactNode | — |
| children | 組內子項 | ReactNode | — |
| sx | 快速佈局屬性 | SxProps | — |
Menu 事件
| 事件 | 說明 | 回調簽名 | Vue 對照 |
|---|---|---|---|
| onSelect | 選取葉子項時觸發(indexPath 為子選單鍵鏈路,如 ['works', 'works-list']) | (index: string, indexPath: string[]) => void | @select |
| onOpen | 子選單展開時觸發 | (index: string, indexPath: string[]) => void | @open |
| onClose | 子選單收起時觸發 | (index: string, indexPath: string[]) => void | @close |
| onOpenedsChange | 展開的子選單鍵組變化時觸發 | (openeds: string[]) => void | @update:openeds |
傳 value 即受控,傳 defaultValue 走非受控內部態;展開的子選單鍵同理(openeds / defaultOpeneds / onOpenedsChange)。兩種模式下回呼都會觸發。巢狀子選單的 onSelect 會隨鏈路逐級冒泡(對齊 plus 的巢狀 re-provide),indexPath 由各級 index 前置累計。
鍵盤互動
垂直模式根節點委派 ↑/↓ 焦點遍歷:可見項 = 葉子項 + 子選單標題(DOM 序即視覺序),越過子選單標題 ↔ 面板內項邊界循環移動,跳過停用項與收起面板內的項;Home / End 直達首 / 末可見項。葉子項 Enter / Space 啟用;子選單標題 Enter / Space / → 展開(← / Escape 收起)。水平模式保留標題自身的 → / ← 語義,不走根節點的上下遍歷。
無障礙
根節點垂直模式為 role="menu"、水平模式為 role="menubar";葉子項為 role="menuitem",停用輸出 aria-disabled;子選單標題為 role="menuitem" + aria-expanded / aria-haspopup / aria-controls,面板為 role="menu" 並以 aria-label 標註(取 share 語言包 menu.submenuLabel),收起的內聯面板以 aria-hidden 移出無障礙樹。
ref
forwardRef<HTMLUListElement> —— 根列表元素。
樣式
import '@lh-ui/react/theme.css'
import '@lh-ui/react/style/menu'