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.
When to use
- Show a site or app header with links and panels of more links, such as a marketing mega menu.
- Prefer
Sidebarfor persistent vertical navigation to the top-level pages of an app. - Prefer
DropdownMenuwhen the user picks an action, not a page to go to. - Prefer
Tabswhen 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.
Top-level links with panels
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.
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.
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
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. Passcurrentfor the active link, or compose React Router'sNavLink, which setsaria-current="page"itself.DisclosureNav.Root,DisclosureNav.List,DisclosureNav.Item,DisclosureNav.Content, andDisclosureNav.Viewport: swap the element, such as a<div>for aDisclosureNav.Contentthat holds no list.DisclosureNav.Trigger: swap the<button>for your own button component. It keepsaria-expandedandaria-controls.
Accessibility
Disclosure Nav follows the WAI-ARIA disclosure navigation pattern, with the optional keys turned on.
- 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.
Escapecloses 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-expandedandaria-controlson the list. A press on a link, a press outside the nav, focus that leaves the nav, or a window that widens pastlgcloses the overlay. - The keys follow the keyboard table of the APG example.
Spacefollows a focused link, asEnterdoes. - An open picker in a panel, such as a combobox, takes its own
Escapefirst.
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.
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.
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.
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.
DisclosureNav.Link
A link at the top level or in a panel. Renders an <a>. All props from a.
DisclosureNav.Trigger
The disclosure button of an item. Renders a <button> with aria-expanded, aria-controls, and a caret. All props from button.
DisclosureNav.Content
The panel of an item. Renders a <div> right after its trigger. All props from div.
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.