Menu
Navigation menus for page headers or sidebars, supporting nested submenus and collapsed layouts. Build them with HuiMenu, HuiSubMenu and HuiMenuItem.
Import
import { HuiMenu, HuiSubMenu, HuiMenuItem } from '@nscode/handy-ui'
Basic Usage
Menus are vertical by default. Every HuiMenuItem and HuiSubMenu requires a unique index. Default-active sets the initial selection. Clicking an item emits select with its index and the indices of its enclosing submenus.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiMenu, HuiSubMenu, HuiMenuItem } from '@nscode/handy-ui'
const selected = ref('1')
const path = ref<string[]>([])
const handleSelect = (index: string, indexPath: string[]) => {
selected.value = index
path.value = indexPath
}
</script>
<template>
<HuiMenu default-active="1" style="width: 240px" @select="handleSelect">
<HuiMenuItem index="1">Dashboard</HuiMenuItem>
<HuiSubMenu index="2" title="System Management">
<HuiMenuItem index="2-1">User Management</HuiMenuItem>
<HuiMenuItem index="2-2">Role Management</HuiMenuItem>
</HuiSubMenu>
<HuiMenuItem index="3">Message Center</HuiMenuItem>
</HuiMenu>
<p style="margin-top: 12px">
Selected: {{ selected }}; enclosing submenus: {{ path.length ? path.join(' / ') : 'None' }}
</p>
</template>
Direction
Mode accepts vertical (default) or horizontal.
- Vertical menus stack items and expand submenus inline below their headings, suitable for sidebars.
- Horizontal menus arrange items in a 60px-high row, suitable for headers. Submenus open below their headings after a 150ms hover delay and close 150ms after leaving. Nested popups open to the right. Selecting an item closes all popups. If items do not fit, the menu scrolls horizontally without compressing submenu headings.
<template>
<HuiSpace direction="vertical" fill size="large">
<div>
<p class="row-label">horizontal</p>
<HuiMenu mode="horizontal" default-active="1">
<HuiMenuItem index="1">Home</HuiMenuItem>
<HuiSubMenu index="2" title="Products">
<HuiMenuItem index="2-1">Product List</HuiMenuItem>
<HuiMenuItem index="2-2">Categories</HuiMenuItem>
<HuiSubMenu index="2-3" title="More">
<HuiMenuItem index="2-3-1">Product Statistics</HuiMenuItem>
<HuiMenuItem index="2-3-2">Product Log</HuiMenuItem>
</HuiSubMenu>
</HuiSubMenu>
<HuiMenuItem index="3">About Us</HuiMenuItem>
</HuiMenu>
</div>
<div>
<p class="row-label">vertical</p>
<HuiMenu mode="vertical" default-active="1" style="width: 240px">
<HuiMenuItem index="1">Home</HuiMenuItem>
<HuiSubMenu index="2" title="Products">
<HuiMenuItem index="2-1">Product List</HuiMenuItem>
<HuiMenuItem index="2-2">Categories</HuiMenuItem>
<HuiSubMenu index="2-3" title="More">
<HuiMenuItem index="2-3-1">Product Statistics</HuiMenuItem>
<HuiMenuItem index="2-3-2">Product Log</HuiMenuItem>
</HuiSubMenu>
</HuiSubMenu>
<HuiMenuItem index="3">About Us</HuiMenuItem>
</HuiMenu>
</div>
</HuiSpace>
</template>
<style scoped>
.row-label {
margin: 0 0 8px;
color: var(--hui-text-color-secondary);
font-size: var(--hui-font-size-sm);
}
</style>
Horizontal menus also support icons and disabled items:
<template>
<HuiMenu mode="horizontal" default-active="1">
<HuiMenuItem index="1" icon="home">Home</HuiMenuItem>
<HuiSubMenu index="2" title="Products" icon="apps">
<HuiMenuItem index="2-1" icon="file">Product List</HuiMenuItem>
<HuiMenuItem index="2-2" icon="folder">Categories</HuiMenuItem>
</HuiSubMenu>
<HuiMenuItem index="3" icon="bell">Messages</HuiMenuItem>
<HuiMenuItem index="4" icon="lock" disabled>Administration</HuiMenuItem>
</HuiMenu>
</template>
Nested Submenus
In vertical menus, clicking a submenu heading expands or collapses its inline content. Children are indented by depth. Selecting a nested item also highlights the headings of its enclosing submenus.
<template>
<HuiMenu default-active="2-2-1" style="width: 240px">
<HuiMenuItem index="1">Dashboard</HuiMenuItem>
<HuiSubMenu index="2" title="System Management">
<HuiMenuItem index="2-1">User Management</HuiMenuItem>
<HuiSubMenu index="2-2" title="Permissions">
<HuiMenuItem index="2-2-1">Role Management</HuiMenuItem>
<HuiMenuItem index="2-2-2">Menu Management</HuiMenuItem>
</HuiSubMenu>
</HuiSubMenu>
</HuiMenu>
</template>
Initial Expansion and Selection
When default-active points to a nested item, its ancestor submenus expand after mounting, but only in an uncollapsed vertical menu. Default-openeds lists the submenus initially expanded.
Both props also respond to runtime changes. Changing default-active selects the new item and opens its ancestors. Changing the contents of default-openeds resets expansion to the supplied list.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiMenu, HuiSubMenu, HuiMenuItem, HuiButton, HuiSpace } from '@nscode/handy-ui'
const active = ref('2-1')
const openeds = ref(['3'])
</script>
<template>
<HuiSpace wrap style="margin-bottom: 12px">
<HuiButton @click="active = '3-2'">Select Activity Log</HuiButton>
<HuiButton @click="active = '2-2'">Select Role Management</HuiButton>
<HuiButton @click="openeds = ['2', '3']">Expand all</HuiButton>
<HuiButton @click="openeds = []">Collapse all</HuiButton>
</HuiSpace>
<HuiMenu :default-active="active" :default-openeds="openeds" style="width: 240px">
<HuiMenuItem index="1">Dashboard</HuiMenuItem>
<HuiSubMenu index="2" title="System Management">
<HuiMenuItem index="2-1">User Management</HuiMenuItem>
<HuiMenuItem index="2-2">Role Management</HuiMenuItem>
</HuiSubMenu>
<HuiSubMenu index="3" title="Logs">
<HuiMenuItem index="3-1">Login Log</HuiMenuItem>
<HuiMenuItem index="3-2">Activity Log</HuiMenuItem>
</HuiSubMenu>
</HuiMenu>
</template>
Accordion Expansion
With unique-opened enabled, opening a submenu closes unrelated submenus while preserving its ancestors. The left example permits multiple open submenus; the right enables unique-opened:
<template>
<HuiSpace align="start" size="large" wrap>
<HuiMenu style="width: 240px">
<HuiSubMenu index="1" title="System Management">
<HuiMenuItem index="1-1">User Management</HuiMenuItem>
<HuiMenuItem index="1-2">Role Management</HuiMenuItem>
</HuiSubMenu>
<HuiSubMenu index="2" title="Logs">
<HuiMenuItem index="2-1">Login Log</HuiMenuItem>
<HuiMenuItem index="2-2">Activity Log</HuiMenuItem>
</HuiSubMenu>
</HuiMenu>
<HuiMenu unique-opened style="width: 240px">
<HuiSubMenu index="1" title="System Management">
<HuiMenuItem index="1-1">User Management</HuiMenuItem>
<HuiMenuItem index="1-2">Role Management</HuiMenuItem>
</HuiSubMenu>
<HuiSubMenu index="2" title="Logs">
<HuiMenuItem index="2-1">Login Log</HuiMenuItem>
<HuiSubMenu index="2-2" title="Activity Log">
<HuiMenuItem index="2-2-1">Created Records</HuiMenuItem>
<HuiMenuItem index="2-2-2">Deleted Records</HuiMenuItem>
</HuiSubMenu>
</HuiSubMenu>
</HuiMenu>
</HuiSpace>
</template>
Icons and Collapsing
Icon on HuiMenuItem and HuiSubMenu shows an HuiIcon before the text. With collapse enabled, the menu becomes 64px wide and top-level items and submenu headings display only icons. Submenus become right-side popups opened by hover or click. Switching to collapsed mode closes existing expanded submenus.
Provide icons for top-level items in collapsed mode; otherwise their visible area is blank.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiMenu, HuiSubMenu, HuiMenuItem, HuiSwitch } from '@nscode/handy-ui'
const collapsed = ref(false)
</script>
<template>
<div style="margin-bottom: 12px">
<HuiSwitch v-model="collapsed" active-text="Collapse" />
</div>
<HuiMenu :collapse="collapsed" default-active="1">
<HuiMenuItem index="1" icon="home">Dashboard</HuiMenuItem>
<HuiSubMenu index="2" title="System Management" icon="setting">
<HuiMenuItem index="2-1" icon="user">User Management</HuiMenuItem>
<HuiMenuItem index="2-2" icon="users">Role Management</HuiMenuItem>
</HuiSubMenu>
<HuiMenuItem index="3" icon="bell">Message Center</HuiMenuItem>
</HuiMenu>
</template>
Disabled Items
Disabled HuiMenuItem and HuiSubMenu components ignore clicks, hover and keyboard interaction, and Tab skips them. Individual items inside a submenu can also be disabled.
<template>
<HuiMenu style="width: 240px" :default-openeds="['4']">
<HuiMenuItem index="1">Dashboard</HuiMenuItem>
<HuiMenuItem index="2" disabled>Analytics Dashboard (under construction)</HuiMenuItem>
<HuiSubMenu index="3" title="System Management" disabled>
<HuiMenuItem index="3-1">User Management</HuiMenuItem>
</HuiSubMenu>
<HuiSubMenu index="4" title="Logs">
<HuiMenuItem index="4-1">Login Log</HuiMenuItem>
<HuiMenuItem index="4-2" disabled>Audit Log (no permission)</HuiMenuItem>
</HuiSubMenu>
</HuiMenu>
</template>
Custom Colors
Background-color, text-color and active-text-color set the menu background, text and active text colors. Horizontal and collapsed popups use the same colors. Values become inline styles and may use theme variables.
<template>
<HuiSpace direction="vertical" fill>
<HuiMenu
mode="horizontal"
default-active="1"
background-color="var(--hui-color-primary-darker)"
text-color="var(--hui-color-white)"
active-text-color="var(--hui-color-warning)"
>
<HuiMenuItem index="1" icon="home">Dashboard</HuiMenuItem>
<HuiSubMenu index="2" title="System Management" icon="setting">
<HuiMenuItem index="2-1">User Management</HuiMenuItem>
<HuiMenuItem index="2-2">Role Management</HuiMenuItem>
</HuiSubMenu>
<HuiMenuItem index="3" icon="bell">Message Center</HuiMenuItem>
</HuiMenu>
<HuiMenu
default-active="1"
background-color="var(--hui-color-primary-darker)"
text-color="var(--hui-color-white)"
active-text-color="var(--hui-color-warning)"
style="width: 240px"
>
<HuiMenuItem index="1" icon="home">Dashboard</HuiMenuItem>
<HuiSubMenu index="2" title="System Management" icon="setting">
<HuiMenuItem index="2-1">User Management</HuiMenuItem>
<HuiMenuItem index="2-2">Role Management</HuiMenuItem>
</HuiSubMenu>
</HuiMenu>
</HuiSpace>
</template>
Custom Headings
HuiMenuItem text can use the default slot or title slot. When both are provided, only title is shown. Collapsing hides this top-level text while retaining the icon.
HuiSubMenu's title slot replaces both the icon and title text, retaining the trailing arrow. The heading uses space-between layout, so wrap multiple slot elements together to prevent them spreading apart. In collapsed mode, the component cannot distinguish icons from text within a custom slot and does not hide it. Use icon and title props for a menu intended to collapse.
<script setup lang="ts">
import { HuiMenu, HuiSubMenu, HuiMenuItem, HuiIcon } from '@nscode/handy-ui'
</script>
<template>
<HuiMenu style="width: 240px">
<HuiMenuItem index="1">
<template #title>Dashboard</template>
</HuiMenuItem>
<HuiSubMenu index="2">
<template #title>
<span>
<HuiIcon name="mail" />
Message Center
<span style="color: var(--hui-text-color-secondary)">(12 unread)</span>
</span>
</template>
<HuiMenuItem index="2-1">Inbox</HuiMenuItem>
<HuiMenuItem index="2-2">System Notices</HuiMenuItem>
</HuiSubMenu>
</HuiMenu>
</template>
Router Mode
With router enabled, clicking an item emits select first, then calls app.config.globalProperties.$router.push(route ?? index). Route accepts a path string or route object; when omitted, index is the target.
The current highlight remains while navigation is pending. On success, the actual destination, including its query, is matched to a menu item. Redirects highlight the matching destination; no match clears highlighting. If several items share a target, the clicked item takes precedence. Cancelled guards, duplicate navigation and navigation errors retain the previous highlight. Errors are handled internally with an [HandyUI] diagnostic. Older navigations, navigations completed after unmounting, and navigations superseded by an explicit default-active change cannot overwrite a newer highlight.
Select reports the click immediately and does not indicate navigation success. For browser back/forward or navigation initiated by application code, keep binding the application's computed active item to default-active; the menu does not watch every route change. It uses $router provided by app.use(router). Without a registered router, selection behaves synchronously like an ordinary menu.
<script setup lang="ts">
import { useRoute } from 'vue-router'
import { HuiMenu, HuiMenuItem } from '@nscode/handy-ui'
const route = useRoute()
</script>
<template>
<HuiMenu router :default-active="route.path" style="width: 240px">
<HuiMenuItem index="/dashboard" route="/dashboard">Dashboard</HuiMenuItem>
<HuiMenuItem index="/users" :route="{ path: '/users', query: { page: 1 } }"
>User Management</HuiMenuItem
>
</HuiMenu>
</template>
Custom Popup Classes
HuiSubMenu's popper-class applies to the submenu ul, whose class is hui-sub-menu__menu. In horizontal or collapsed mode, this list is teleported under body:
- Use global styles, without scoped.
- Use .my-popup directly rather than a descendant selector such as .hui-menu .my-popup, because the popup is not inside the menu.
<template>
<HuiMenu mode="horizontal">
<HuiSubMenu index="1" title="More" popper-class="my-menu-popup">
<HuiMenuItem index="1-1">Documentation</HuiMenuItem>
<HuiMenuItem index="1-2">Feedback</HuiMenuItem>
</HuiSubMenu>
</HuiMenu>
</template>
<style>
.my-menu-popup {
min-width: 260px;
border: 1px solid var(--hui-border-color);
}
</style>
Expansion Events
Opening a submenu emits open; closing emits close. Arguments are the submenu's index and the indices of its ancestors.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiMenu, HuiSubMenu, HuiMenuItem } from '@nscode/handy-ui'
const log = ref('No expansion or closing yet')
const handleOpen = (index: string, indexPath: string[]) => {
log.value = `open: ${index}; ancestors: ${indexPath.length ? indexPath.join(' / ') : 'None'}`
}
const handleClose = (index: string, indexPath: string[]) => {
log.value = `close: ${index}; ancestors: ${indexPath.length ? indexPath.join(' / ') : 'None'}`
}
</script>
<template>
<HuiMenu style="width: 240px" @open="handleOpen" @close="handleClose">
<HuiSubMenu index="1" title="System Management">
<HuiMenuItem index="1-1">User Management</HuiMenuItem>
<HuiSubMenu index="1-2" title="Permissions">
<HuiMenuItem index="1-2-1">Role Management</HuiMenuItem>
<HuiMenuItem index="1-2-2">Menu Management</HuiMenuItem>
</HuiSubMenu>
</HuiSubMenu>
<HuiSubMenu index="2" title="Logs">
<HuiMenuItem index="2-1">Login Log</HuiMenuItem>
</HuiSubMenu>
</HuiMenu>
<p style="margin-top: 12px">{{ log }}</p>
</template>
Expanding and Closing Through Methods
Use an HuiMenu ref to call open(index, indexPath) and close(index, indexPath). IndexPath lists the submenu's ancestors; use [] for a top-level submenu. Unique-opened uses this path to decide which submenus to retain. The ref also exposes the selected activeIndex and the openedMenus set.
<script setup lang="ts">
import { ref } from 'vue'
import { HuiMenu, HuiSubMenu, HuiMenuItem, HuiButton, HuiSpace } from '@nscode/handy-ui'
const menuRef = ref<InstanceType<typeof HuiMenu>>()
const state = ref('')
const readState = () => {
const menu = menuRef.value
if (!menu) return
const opened = Array.from(menu.openedMenus)
state.value = `activeIndex: ${menu.activeIndex || 'None'}; openedMenus: ${opened.length ? opened.join(', ') : 'None'}`
}
</script>
<template>
<div style="margin-bottom: 12px">
<HuiSpace wrap>
<HuiButton @click="menuRef?.open('2', [])">Open System Management</HuiButton>
<HuiButton @click="menuRef?.close('2', [])">Close System Management</HuiButton>
<HuiButton @click="readState">Read state</HuiButton>
<span>{{ state }}</span>
</HuiSpace>
</div>
<HuiMenu ref="menuRef" default-active="1" style="width: 240px">
<HuiMenuItem index="1">Dashboard</HuiMenuItem>
<HuiSubMenu index="2" title="System Management">
<HuiMenuItem index="2-1">User Management</HuiMenuItem>
<HuiMenuItem index="2-2">Role Management</HuiMenuItem>
</HuiSubMenu>
</HuiMenu>
</template>
Keyboard Interaction
A menu has one Tab stop. Vertical inline navigation includes visible descendants of expanded branches; each popup level navigates independently. Disabled items and hidden branches are excluded. Data changes do not pull focus back from outside. Composition, key code229, Ctrl/Meta/Alt combinations, and consumed events are left to input or system behavior.
| Key | Action |
|---|---|
| Up / Down | Move through available vertical items, including expanded inline descendants |
| Left / Right | Move among horizontal top-level items; open or close popup branches according to actual placement |
| Home / End | Focus the first/last available item |
| Enter / Space | Select an item or toggle/open its submenu |
| Escape | Close the current popup level and return to its heading |
Stable item identity preserves focus after reordering; removing or disabling an item restores a nearby available stop without stealing external focus.
HuiMenu Props
| Prop | Type | Default | Description |
|---|---|---|---|
| mode | 'horizontal' | 'vertical' | 'vertical' | Menu direction |
| defaultActive | string | '' | Selected item index. Runtime changes select a new item and open its ancestor submenus |
| defaultOpeneds | string[] | [] | Initially expanded submenu indices. Content changes reset expansion to this list |
| collapse | boolean | false | Collapsed state for vertical menus. Width becomes 64px and top-level items show only icons |
| backgroundColor | string | - | Menu and popup background color |
| textColor | string | - | Menu and popup text color |
| activeTextColor | string | - | Text color for the active item and its ancestor headings. Defaults to the primary color |
| router | boolean | false | Calls $router.push(route ?? index) and updates highlighting after successful navigation |
| uniqueOpened | boolean | false | Whether only one unrelated submenu remains expanded |
HuiSubMenu Props
| Prop | Type | Default | Description |
|---|---|---|---|
| index | string | - | Required unique identifier |
| title | string | '' | Heading text, replaced by the title slot |
| icon | string | - | HuiIcon name before the title; the only top-level content shown when collapsed |
| disabled | boolean | false | Whether the submenu is disabled |
| popperClass | string | '' | Class on the submenu ul. Popups are under body, so styles must be global |
HuiMenuItem Props
| Prop | Type | Default | Description |
|---|---|---|---|
| index | string | - | Required unique identifier |
| route | string | Record<string, unknown> | - | Router target passed to $router.push when HuiMenu enables router |
| icon | string | - | HuiIcon name before the text; the only top-level content shown when collapsed |
| disabled | boolean | false | Whether the item is disabled |
HuiMenu Events
| Event | Callback Arguments | Description |
|---|---|---|
| select | (index: string, indexPath: string[], item: MenuItemContext) | Emitted immediately for clicks, Enter or Space, including the selected item. Does not indicate navigation success. IndexPath lists enclosing submenu indices, excluding the item itself |
| open | (index: string, indexPath: string[]) | Submenu opens. IndexPath lists its ancestors |
| close | (index: string, indexPath: string[]) | Submenu closes, with the same arguments as open |
HuiMenu Slots
| Slot | Scope Arguments | Description |
|---|---|---|
| default | - | HuiMenuItem and HuiSubMenu content |
HuiSubMenu Slots
| Slot | Scope Arguments | Description |
|---|---|---|
| default | - | Nested HuiMenuItem and HuiSubMenu content |
| title | - | Heading replacing the icon and title text, while retaining the trailing arrow |
HuiMenuItem Slots
| Slot | Scope Arguments | Description |
|---|---|---|
| default | - | Item text |
| title | - | Item text, replacing the default slot. Hidden for top-level items when collapsed |
Exposed HuiMenu Properties and Methods
Read or call through a component ref.
| Name | Type | Description |
|---|---|---|
| activeIndex | string | Selected item index |
| openedMenus | Set<string> | Indices of expanded submenus |
| open | (index: string, indexPath: string[]) => void | Opens and emits open; no effect if already open. Does not check disabled state or open ancestors |
| close | (index: string, indexPath: string[]) => void | Closes and emits close; no effect if already closed |
Types
import type {
MenuMode,
MenuRoute,
MenuProps,
SubMenuProps,
MenuItemProps,
MenuItemContext
} from '@nscode/handy-ui'
import type { Ref } from 'vue'
type MenuMode = 'horizontal' | 'vertical'
type MenuRoute = string | Record<string, unknown>
interface MenuProps {
mode?: MenuMode
defaultActive?: string
defaultOpeneds?: string[]
collapse?: boolean
backgroundColor?: string
textColor?: string
activeTextColor?: string
router?: boolean
uniqueOpened?: boolean
}
interface SubMenuProps {
index: string
title?: string
disabled?: boolean
popperClass?: string
icon?: string
}
interface MenuItemProps {
index: string
route?: MenuRoute
disabled?: boolean
icon?: string
}
/** Third argument of the select event */
interface MenuItemContext {
index: string
rootMenu: MenuContext
parentMenu: SubMenuContext | null
active: Ref<boolean>
disabled: Ref<boolean>
/** Enclosing submenu indices, excluding the item itself */
indexPath: string[]
route?: MenuRoute
}
MenuEmits, MenuContext, SubMenuContext, MENU_INJECTION_KEY and SUB_MENU_INJECTION_KEY are also exported for custom child components integrating with HuiMenu.