Skip to content

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:

  1. from the item's active property;
  2. from v-model, compared with the items' value (navigation handled by the application);
  3. 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 ​

PropTypeDefaultDescription
itemsNavMenuEntry[]-Items and groups
v-modelstring | number-Active item (value)
v-model:collapsedbooleanfalseIcons only
match'exact' | 'prefix''exact'Comparison with the current route
labelstring'Main navigation'Navigation name
PropertyTypeDescription
labelstringLabel
valuestring | numberIdentifier (v-model)
tostring | RouteLocationRawRouter link (RouterLink or the plugin linkComponent)
hrefstringLink
target, relstringLink target and relationship
iconComponentIcon
badgestring | numberCounter or short label
badgeVariantBadgeVariantCounter variant
badgeLabelstringText read instead of the counter ("4 unread messages")
activebooleanForces the active state
disabledbooleanDisabled: no link, no selection
itemsNavMenuItem[]Sub-items: the item becomes a button that expands them
defaultOpenbooleanSub-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 ​

EventPayloadDescription
select(item: NavMenuItem, event: MouseEvent)Chosen item
update:modelValuestring | numberActive item
update:collapsedbooleanA parent was chosen in collapsed mode

Accessibility ​

  • "Disclosure navigation" pattern: a named <nav> landmark with lists of links. No role="menu": these are navigation links, not actions.
  • Each group is a list named by its title (aria-labelledby).
  • A parent is an aria-expanded button tied to its sub-list (aria-controls); Enter or Space expands it, Tab moves through the links.
  • badgeLabel replaces 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.

Publié sous licence MIT.