Olddevs UI4.14.0
NavigationMegaMenu

Overview

Open category: none

Opens on click or Enter, not on hover.

When to use

  • A catalog with ten or more sections, each with subsections in columns: a store, a marketplace, a help portal.
  • The sections should be visible at once and the subsections should appear on hover, without taking the user off the page.
  • The flyout needs content of its own next to the links: a promo card, an image, a selection.

When not to use

  • For commands on an object ("Rename", "Delete"), use Menu: it holds actions, not navigation.
  • For application navigation with a current section, use SideNav.
  • For a deep hierarchy that expands in place, use TreeView.
  • For two or three links with no subsections, use a plain list of links or Tabs.

Minimal example

import { MegaMenu, MegaMenuCategory, MegaMenuGroup, MegaMenuLink } from "@olddevs/ui";

<MegaMenu aria-label="Catalog">
  <MegaMenuCategory id="computers" label="Computers" href="/computers" active>
    <MegaMenuGroup title="Laptops" href="/computers/laptops">
      <MegaMenuLink href="/computers/laptops/gaming">Gaming</MegaMenuLink>
      <MegaMenuLink href="/computers/laptops/ultrabooks" active>
        Ultrabooks
      </MegaMenuLink>
    </MegaMenuGroup>
  </MegaMenuCategory>
  <MegaMenuCategory id="deals" label="Deals of the day" href="/deals" />
</MegaMenu>;

Props

MegaMenu:

PropDefaultMeaning
aria-label—The accessible name of the navigation. Required: on a page with several nav elements, the name tells them apart
value / defaultValue / onValueChangenullThe open category (its id) or null, controlled or uncontrolled
showcaseWidth"40rem"The width of the flyout, shared by all categories so that the catalog does not jump between them
className—Classes for the nav; the default width of the category column is w-64

MegaMenuCategory: id is the key for value; label is the text in the category column and the name of the flyout; icon; href or link is the address of the whole section; disabled; active means the current page is in this section; the children are the content of the flyout. For a category with a flyout, the address becomes a title link at the top of the flyout; for a category without children, the row in the category column is itself the link. link is a router link passed as an empty element (link={<Link href="/x" />}), and the label moves inside it. It is a separate prop rather than asChild because the children of a category are its flyout.

active on a category sets aria-current="true" and gives the label the accent color, the same one a current link gets. The mark stays whether or not the flyout is open: the route decides the current section, not hover. The open category is still marked with a background, so one row can carry both. While no flyout is open, Tab enters the category column on the current section. Your code decides which section is current, as with active on NavItem.

Categories don't have to be direct children of MegaMenu: you can split them across your own components and render them from a Next.js server component. Each category reports itself to the column, and the column doesn't inspect its children by type, because a server component's element arrives on the client with a different type. Before hydration, while the categories haven't reported themselves yet, every available category is a Tab stop; after hydration the column is a single stop again.

MegaMenuGroup: title is the title of the column and the name of its list; href or link turns the title into a link (link is a router link passed as an empty element, as on a category); the children are MegaMenuLink elements, and each one is placed in its own list item, including those passed in a fragment.

MegaMenuLink: the usual a props, icon, active and asChild. active sets aria-current="page" and the look of the current link. The kit does not work out the current page itself, because every router compares addresses by its own rules. asChild renders the link as a router element: <MegaMenuLink asChild><Link href="/x">Label</Link></MegaMenuLink>. The old name, current, works until 5.0 and is marked @deprecated: the kit calls the current navigation item active.

  • Implementation: src/components/navigation/MegaMenu.tsx; menu aim is in src/components/navigation/useMenuAim.ts.
  • Tests: tests/mega-menu.test.tsx covers roles and aria-controls, hover and mounting only the open flyout, closing with a delay, roving tabindex, Enter, Escape and the arrows, Tab past the last link, a category that is a link, a disabled category, active and the current alias, asChild and link (on a category and on a column), fragments in a column, the current section and the Tab stop, typeahead, categories inside your own components, the controlled value, a press outside the catalog and server rendering; tests/mega-menu-aim.test.tsx covers menu aim on mocked geometry and fake time, and the flip at the edge; tests/mega-menu-touch.test.tsx covers touch and pen, a mocked matchMedia, the drill-down in place of the category column, "Back" and focus, Escape, labels from the message catalog, the drill-down with categories inside your own components and server rendering; the browser test tests-visual/mega-menu.spec.ts uses a real mouse moving diagonally and down the category column, and checks the width of the dropdown on a phone.
  • Dropdown mode: tests/mega-menu-dropdown.test.tsx covers opening by click and keys with focus on the first category, every way of closing and returning focus, Escape in the flyout, open / defaultOpen / onOpenChange, the popover layer, the order of Escape inside a Dialog, the drill-down on a narrow screen and server rendering.
  • Story: src/components/navigation/MegaMenu.stories.tsx, including DropdownInTopBar, a mega menu in a top bar; at phone width, PhoneColumn, PhoneDrillDown, PhoneDropdown and PhoneDropdownDrillDown.

TypeScript types

MegaMenuProps

Prop

Type

Plus DOM attributes, such as onChange, onClick, className — 276 in total; the table does not repeat them.

MegaMenuCategoryProps

Prop

Type

MegaMenuGroupProps

Prop

Type

MegaMenuLinkProps

Prop

Type

Plus DOM attributes, such as onChange, onClick, className — 287 in total; the table does not repeat them.

MegaMenuTriggerProps

Prop

Type

MegaMenuButtonProps

Prop

Type

On this page