Skip to main content

Disclosure Nav

Site and app navigation that follows the WAI-ARIA disclosure navigation pattern, with panels of links and an optional mega menu surface. It is not a role="menu": each link stays in the Tab order, and each link is in the server HTML, also in a closed panel.

Desktop viewport
Tablet viewport
Mobile viewport

When to use

  • Show a site or app header with links and panels of more links, such as a marketing mega menu.
  • Prefer Sidebar for persistent vertical navigation to the top-level pages of an app.
  • Prefer DropdownMenu when the user picks an action, not a page to go to.
  • Prefer Tabs when the user switches between peer views of one page.

Mega menu

Add DisclosureNav.Viewport as the last child of DisclosureNav.Root for a mega menu. The panels then draw no surface of their own, and one surface moves and resizes from the open panel to the next. Lay out each panel with your own columns and headings, and use one DisclosureNav.List for each group of links. Name each group with a heading and aria-labelledby.

Desktop viewport
Tablet viewport
Mobile viewport

An item can hold a link and a panel of more links, as in the APG example with top-level links. Put a DisclosureNav.Trigger with no children after the link. It shows only its caret, and aria-label names it.

Desktop viewport
Tablet viewport
Mobile viewport

Mobile

Put DisclosureNav.ListTrigger right before the top-level DisclosureNav.List. Below the lg breakpoint, the list hides behind that menu button. A press opens the list as a full-height overlay under the nav, and the page stops scrolling. Each panel then opens inline under its trigger, and a hover opens nothing. The overlay is not modal: focus can leave it, and that closes it. Put a call-to-action button in the list as the last DisclosureNav.Item, so it shows in the overlay too. Lay out a mega menu panel for one column first, and add its columns at lg:.

Without a ListTrigger, the list stays in one row at each width, as in an app header.

Desktop viewport
Tablet viewport
Mobile viewport

Alignment

A panel lines up with the left edge of its item. Set align="center" on DisclosureNav.Content to center it under its item, or align="end" to line it up with the right edge, such as for the last item of a bar. A centered panel that is wider than its item reaches out on both sides.

Composition

DisclosureNav.Root
├── DisclosureNav.ListTrigger
├── DisclosureNav.List
│   └── DisclosureNav.Item
│       ├── DisclosureNav.Link
│       ├── DisclosureNav.Trigger
│       └── DisclosureNav.Content
│           └── DisclosureNav.List
│               └── DisclosureNav.Item
│                   └── DisclosureNav.Link
└── DisclosureNav.Viewport

Each DisclosureNav.Content comes right after its DisclosureNav.Trigger, in the same DisclosureNav.Item. That order puts the links of a panel after its trigger in the Tab order, as the APG pattern requires.

Polymorphism

Each part takes asChild.

  • DisclosureNav.Link: compose your router's link, so a press navigates on the client. Pass current for the active link, or compose React Router's NavLink, which sets aria-current="page" itself.
  • DisclosureNav.Root, DisclosureNav.List, DisclosureNav.Item, DisclosureNav.Content, and DisclosureNav.Viewport: swap the element, such as a <div> for a DisclosureNav.Content that holds no list.
  • DisclosureNav.Trigger: swap the <button> for your own button component. It keeps aria-expanded and aria-controls.
Desktop viewport
Tablet viewport
Mobile viewport

Accessibility

Disclosure Nav follows the WAI-ARIA disclosure navigation pattern, with the optional keys turned on.

KeyAction
Tab, Shift + TabMoves focus between the top-level links and triggers, and through the links of the open panel.
Enter, SpaceToggles the panel of the focused trigger. Follows the focused link.
EscapeCloses the open panel. When focus is in the nav, focus moves to the trigger of that panel. With no panel open, closes the mobile overlay, and focus moves to the menu button.
ArrowDownOn the trigger of an open panel, moves focus to its first link. Else moves focus to the next item or link.
ArrowRightOn the trigger of an open panel, moves focus to its first link. Else moves focus to the next top-level item, or to the next link in the open panel.
ArrowUp, ArrowLeftMoves focus to the previous top-level item, or to the previous link in the open panel.
Home, EndMoves focus to the first or the last top-level item, or to the first or the last link in the open panel.
  • Only one panel is open at a time.
  • Focus that leaves the nav, a press outside it, or a press on a link closes the open panel. A press with a modifier key, which opens the link in a new tab, keeps it open.
  • A mouse that rests on a trigger for 100ms opens its panel, and a mouse that moves to the next trigger switches the panel at once. A panel that a hover opened closes 300ms after the mouse leaves the nav. A press on its trigger keeps it open. Escape closes it when focus is anywhere on the page, so a panel that covers content never traps a mouse user (WCAG 1.4.13).
  • A find in the page opens the closed panel that holds the match, in a browser that supports hidden="until-found".
  • The menu button of the mobile overlay is a disclosure button too, with aria-expanded and aria-controls on the list. A press on a link, a press outside the nav, focus that leaves the nav, or a window that widens past lg closes the overlay.
  • The keys follow the keyboard table of the APG example. Space follows a focused link, as Enter does.
  • An open picker in a panel, such as a combobox, takes its own Escape first.

Performance

The open state lives on the DOM. A press writes aria-expanded on the trigger, and hidden and data-open on its panel, so a panel opens and closes with no React render. One set of delegated listeners serves each trigger. The click and arrow-key listeners run after React, so your onClick or onKeyDown on a part runs first and can call preventDefault(). The panel takes its place from its item with plain CSS, and only DisclosureNav.Viewport reads the open panel through CSS anchor positioning.

A press to open or close a panel of 12 links runs about 2ms of script in Chromium at 4× CPU slowdown. The same nav built on Popover runs about 24ms. The page shows the change in about 19ms, against about 13ms for a hand-written nav with the same motion and no framework, and about 32ms for the Popover nav. Each number is the median of three rounds that rotate the order of the builds, from the keydown to the next frame. With all eight parts, Disclosure Nav adds 5.5KB gzip to a page that already uses Slot.

API Reference

DisclosureNav.Root

The navigation landmark. Renders a <nav> that owns the open panel. All props from nav.

PropTypeDefaultDescription
aria-labelstring-The name of the landmark. Set it or aria-labelledby.
aria-labelledbystring-The id of the element that names the landmark. Set it or aria-label.
asChildbooleanfalseRender the child element instead of a <nav>.
Data AttributeValueDescription
data-slot"disclosure-nav"Always present.
data-openpresent while a panel is openPresence-only. Style with group-data-open/disclosure-nav:.
data-closingpresent during the exit fadePresence-only. The last panel fades out for 150ms after it closes.
data-viewportpresent with a viewportPresence-only. Set after hydration and on each open when a DisclosureNav.Viewport is in the nav.

DisclosureNav.List

A list of items. Renders a <ul>: a row at the top level, and a column inside a DisclosureNav.Content. All props from ul.

PropTypeDefaultDescription
asChildbooleanfalseRender the child element instead of a <ul>.
Data AttributeValueDescription
data-slot"disclosure-nav-list"Always present.

DisclosureNav.ListTrigger

The menu button that shows the whole list below the lg breakpoint. Renders a <button> with a menu icon that turns into a close icon. It shows only below lg. Put it right before the top-level DisclosureNav.List. All props from button, except children.

PropTypeDefaultDescription
aria-labelstring-Required. The name of the button, such as "Site navigation".
asChildbooleanfalseRender the child element instead of a <button>.
Data AttributeValueDescription
data-slot"disclosure-nav-list-trigger"Always present.
aria-expanded"true" | "false"Whether the overlay shows. Style with aria-expanded:.
data-mantle-scroll-lockpresent while the overlay showsPresence-only. On <html>. The page stops scrolling.

When the overlay opens, the list gets --disclosure-nav-list-top and --disclosure-nav-list-left in its style: the bottom and the left of the nav in the viewport, so the overlay fills the screen under the nav.

DisclosureNav.Item

One entry of a list. Renders an <li>. It holds a link, a trigger and its panel, or a link, a trigger, and its panel. All props from li.

PropTypeDefaultDescription
asChildbooleanfalseRender the child element instead of an <li>.
Data AttributeValueDescription
data-slot"disclosure-nav-item"Always present.

A link at the top level or in a panel. Renders an <a>. All props from a.

PropTypeDefaultDescription
currentbooleanfalseWhether the link goes to the page that shows now. Sets aria-current="page".
asChildbooleanfalseRender the child element, such as your router's link, instead of an <a>.
Data AttributeValueDescription
data-slot"disclosure-nav-link"Always present.
aria-current"page"Present with current. Style with aria-current:.

DisclosureNav.Trigger

The disclosure button of an item. Renders a <button> with aria-expanded, aria-controls, and a caret. All props from button.

PropTypeDefaultDescription
childrenReactNode-The visible label. Omit it for a trigger that shows only its caret beside a link.
aria-labelstring-Required without children. The name of a trigger that shows only its caret.
asChildbooleanfalseRender the child element instead of a <button>.
Data AttributeValueDescription
data-slot"disclosure-nav-trigger"On the button.
data-slot"disclosure-nav-trigger-label"On the display: contents <span> that wraps children. Absent without children and under asChild.
data-slot"disclosure-nav-trigger-caret"On the caret icon. Absent under asChild.
aria-expanded"true" | "false"Whether its panel shows. Style with aria-expanded:.

DisclosureNav.Content

The panel of an item. Renders a <div> right after its trigger. All props from div.

PropTypeDefaultDescription
align"start" | "center" | "end""start"Which edge of its item the panel lines up with. A panel in the mobile overlay opens inline instead.
asChildbooleanfalseRender the child element instead of a <div>.
Data AttributeValueDescription
data-slot"disclosure-nav-content"Always present.
hidden"until-found" | ""Present while closed. "until-found" where the browser supports it, so a find in the page opens the panel that holds the match.
data-openpresent while openPresence-only. Style with data-open:.
data-closingpresent during the exit fadePresence-only. The panel fades out for 150ms and is inert.
data-direction"forward" | "backward"Set on each closed panel while another panel is open: the side it slides in from if it opens next.

DisclosureNav.Viewport

The one surface that every panel shares. Renders an aria-hidden <div>. Put it last in DisclosureNav.Root. All props from div, except children.

PropTypeDefaultDescription
asChildbooleanfalseRender the child element instead of a <div>.
Data AttributeValueDescription
data-slot"disclosure-nav-viewport"Always present.