NavMenu
Side navigation of an application: titled groups, icons, counters, one level of expandable sub-items, active item from the route, icons-only collapsed mode. Put it in the sidebar of AppShell.
Usage
Basic usage
vue
<script setup>
import { HomeIcon, InboxIcon, FolderIcon } from '@heroicons/vue/24/outline'
const items = [
{ label: 'Dashboard', to: '/', icon: HomeIcon },
{ label: 'Messages', to: '/messages', icon: InboxIcon, badge: 4, badgeLabel: '4 unread messages' },
{
type: 'group',
label: 'Workspace',
items: [
{
label: 'Projects',
icon: FolderIcon,
items: [
{ label: 'Active', to: '/projects' },
{ label: 'Archived', to: '/projects/archived' }
]
}
]
}
]
</script>
<template>
<SuNavMenu :items="items" />
</template>Active item
The active item has aria-current="page". It comes, in this order:
- from the item's
activeproperty; - from
v-model, compared with the items'value(navigation handled by the application); - from the current Vue Router (or Nuxt) route, compared with the items'
to.
match="prefix" keeps an item active on every page of its section: /projects stays active on /projects/42. The parent of the active item opens by itself.
vue
<SuNavMenu :items="items" match="prefix" />Collapsed to icons
vue
<SuNavMenu v-model:collapsed="collapsed" :items="items" />Labels are still read by screen readers and show in a tooltip on hover and focus. An item without an icon shows its initial. A parent expands the menu (update:collapsed) and opens.
API
Props
| Prop | Type | Default | Description |
|---|---|---|---|
items | NavMenuEntry[] | - | Items and groups |
v-model | string | number | - | Active item (value) |
v-model:collapsed | boolean | false | Icons only |
match | 'exact' | 'prefix' | 'exact' | Comparison with the current route |
label | string | 'Main navigation' | Navigation name |
NavMenuItem
| Property | Type | Description |
|---|---|---|
label | string | Label |
value | string | number | Identifier (v-model) |
to | string | RouteLocationRaw | Router link (RouterLink or the plugin linkComponent) |
href | string | Link |
target, rel | string | Link target and relationship |
icon | Component | Icon |
badge | string | number | Counter or short label |
badgeVariant | BadgeVariant | Counter variant |
badgeLabel | string | Text read instead of the counter ("4 unread messages") |
active | boolean | Forces the active state |
disabled | boolean | Disabled: no link, no selection |
items | NavMenuItem[] | Sub-items: the item becomes a button that expands them |
defaultOpen | boolean | Sub-items expanded on first render |
A group is written { type: 'group', label, items }. Without a link (to, href), an item is a button: navigation then goes through v-model or the select event.
Events
| Event | Payload | Description |
|---|---|---|
select | (item: NavMenuItem, event: MouseEvent) | Chosen item |
update:modelValue | string | number | Active item |
update:collapsed | boolean | A parent was chosen in collapsed mode |
Accessibility
- "Disclosure navigation" pattern: a named
<nav>landmark with lists of links. Norole="menu": these are navigation links, not actions. - Each group is a list named by its title (
aria-labelledby). - A parent is an
aria-expandedbutton tied to its sub-list (aria-controls); Enter or Space expands it, Tab moves through the links. badgeLabelreplaces the counter for screen readers: "4 unread messages" rather than "4".- Collapsed mode: the label stays in the link (visually hidden); the tooltip is visual only and does not repeat the name.