Behavior
Hover and the flyout
Hovering a category with the mouse opens its flyout at once, with no delay. Exactly one category is highlighted,
the one whose flyout is open: the highlight follows aria-expanded, not :hover. Hovering a category without a
flyout closes the open one, with the same caveat about menu aim as below.
The flyout closes 150 ms after the pointer has left both the category column and the flyout, so a brief excursion past the edge does not reset the catalog. It also closes on a pointer press outside the catalog and when focus leaves the catalog.
The content of a flyout is mounted only for the open category, so a catalog of a hundred links and images does
not load everything at once. The region itself is always there, hidden: the button's aria-controls points to it.
The flyout is a DOM descendant of its category, not a portal. That is why the Tab order runs "category, its
links, then on down the page", and why the flyout sits flush against the category column without any position
calculation. An ancestor with overflow: hidden clips it, so put the catalog where there is room beside it.
The flyout sits on the end side of the line: on the right, or on the left in right-to-left writing. If there is not enough room there and there is enough on the other side, it moves to the other side. The kit measures the side ahead of time and again when the window is resized, so the arrow key that points "toward the flyout" already points to where the flyout will appear.
Touch and narrow screens
Touch has no hover, so the touch itself (click) opens a category, without menu aim. A pointer whose pointerType is
"touch" or "pen" neither opens a category on hover nor closes the flyout when it leaves: a pen behaves like a
finger. Touching an open category again neither closes it nor goes anywhere. The flyout closes on a touch outside
the catalog or on Escape.
The mouse behaves as before: hover, menu aim and closing after 150 ms all remain while the screen is wide.
On a narrow screen, under 640px (the same breakpoint as Menu submenus and Editor submenus), there is no room beside
the category column for the flyout: the edge of the window would cut it off on the right. So the catalog works as a
drill-down:
- The category column takes the full width, and category labels wrap instead of being truncated.
- An open category (by touch, click, Enter or Space) replaces the category column with its flyout. The flyout starts with a "Back" row that shows the category name. If the category has a link of its own, the link shows the name, on the row below, instead of the "Back" row. Below that, the columns of links follow, one under another.
- "Back" returns the category column and puts focus on the category the person came from. Escape inside the flyout works like "Back".
- When you drill down, focus moves to "Back": the category button is hidden along with the category column, and focus left on it would be lost. In the flyout on a narrow screen, the arrow key toward the category column does nothing, because the column is not beside it.
- There is no menu aim on a narrow screen: hovering with the mouse does not open a category, and moving the mouse away does not cancel the drill-down. Replacing the column under the cursor would pull it out from under the person's hand.
- The markup stays on the disclosure pattern: the category button keeps
aria-expandedandaria-controlspointing to the region on screen (the button itself is hidden during the drill-down), and the region is named after the category.
The "Back" label comes from the message catalog: the key is megaMenu.back, with the default "Back"; the Russian
catalog supplies "Назад".
The kit reads the width through matchMedia, not through CSS, because the width determines the contents of the
markup (the category column is hidden), not just the look. Before hydration and on the server, the hook reports a wide
screen (ADR-0018): the server does not know the width, and the markup it produces is always the regular one. On a phone,
after hydration, a flyout opened by defaultValue is re-rendered once as a drill-down. On a phone the nav takes
the full width through the max-sm:w-full class; a w-* class from className applies on a wide screen.
Keyboard and names
- The category column is a single Tab stop. The Up and Down arrows, Home and End move through the categories;
disabled ones are skipped. When the user comes back with Tab, they land on the category they were on; the first
time, on the open one, otherwise on the current section (
active), otherwise on the first one. - Typing a letter on a category moves focus to the next category whose label starts with it, wrapping around; the same letter again moves to the one after. Letters typed in quick succession match the start of a label, and a half-second pause clears them. A Space in the middle of typing is part of the label ("Home and garden"), not a key that opens the category. A letter with Ctrl, Cmd or Alt is a keyboard shortcut, and the catalog leaves it alone.
- Enter, Space and the arrow toward the flyout open it and put focus on the first link. On a category that is a link, Enter follows it.
- Inside the flyout, plain Tab works. The arrow toward the category column returns focus to the category, and so does Escape, which also closes the flyout. Tab past the last link moves on down the page and closes the flyout.
- While the flyout is open, it follows focus moved by arrows and letters the way it follows hover.
- The arrow "toward the flyout" is the one that points to where the flyout sits: in right-to-left writing and at the edge of the screen, the arrows swap places.
- A screen reader hears a navigation with the name from
aria-label, the categories as expand/collapse buttons, the flyout as a region named after the category, and a column as a list named after its title.
Dropdown mode
A catalog in a page header is opened with a "Catalog" button rather than kept on the page. The markup of the
categories is the same as in the embedded mode; only the wrapper changes: put MegaMenu into MegaMenuTrigger
as the second child, after the MegaMenuButton.
import {
MegaMenu,
MegaMenuButton,
MegaMenuCategory,
MegaMenuGroup,
MegaMenuLink,
MegaMenuTrigger,
} from "@olddevs/ui";
import { ListIcon } from "@olddevs/ui/icons";
<MegaMenuTrigger>
<MegaMenuButton icon={<ListIcon size={16} />}>Catalog</MegaMenuButton>
<MegaMenu aria-label="Catalog">
<MegaMenuCategory id="computers" label="Computers" href="/computers">
<MegaMenuGroup title="Laptops">
<MegaMenuLink href="/computers/laptops/gaming">Gaming</MegaMenuLink>
</MegaMenuGroup>
</MegaMenuCategory>
</MegaMenu>
</MegaMenuTrigger>;MegaMenuTrigger: open, defaultOpen and onOpenChange control whether the overlay is open, controlled or
uncontrolled, as with other overlays of the kit. The first child is the button, and all the others go into the
overlay. Children are sorted by position, so you can wrap the button in a component of your own and move MegaMenu
into one. MegaMenuButton accepts the kit's button look (variant, tone, size, iconOnly, selected), icon,
disabled, aria-label and className; the look is the same as for MenuButton.
- Opening. Click, Enter, Space and the Down arrow on the button. Hover does not open the catalog: it should not jump out at a stray mouse movement.
- Focus. On opening, focus goes to the first available category (or, if one is already open through
valueordefaultValue, to that one), and the flyout stays closed until a category is chosen. After that the category column and the flyout work as in the embedded mode. - Closing. Escape on a category, a press outside the catalog, a second click on the button, following any link (a
category, a column title, a
MegaMenuLink, router links included) and Tab past the last element. The first Escape inside a flyout closes only the flyout and returns focus to its category; the next one closes the overlay. - Returning focus. Focus returns to the button on Escape and on a second click. After Tab past the last element, focus moves on down the page, past the button; Shift+Tab before the first element returns it to the button. After a press outside the catalog, focus stays where the user pressed.
- Screen reader. The button is a disclosure with
aria-expandedandaria-controlsand noaria-haspopup: the overlay is neither a menu nor a dialog, but the same navigation with links. The overlay has nodialogrole. - Layers. The overlay sits on the
popoverlayer: above the scrim of an openDialog, below the confirmation. Inside a dialog, the first Escape closes the catalog and the next one closes the dialog. If focus is in an open flyout, the very first Escape closes only the flyout and returns focus to its category. - Narrow screens. Under 640px the overlay spans the window, minus 8px at each edge, instead of shrinking to the longest label: labels wrap, a long one can't push the catalog off screen, and the width stays put when you drill into a category. The drill-down, "Back" and Escape work inside the overlay just as in the embedded mode: the first Escape in a flyout brings the column back, the next one closes the catalog.
- Height. The overlay is no taller than the visible area from the button to the edge of the window. The category
column and the flyout each scroll within themselves (
overscroll-contain), not the overlay as a whole: otherwise the overlay's frame would clip the flyout, which stands beside the category column. On iOS the height is capped atmin(24rem, 60dvh), like theMenupanel. - Appearance. Smooth, on the kit's motion scale; with
prefers-reduced-motion, no animation. - Server. The server renders a closed button; the overlay is mounted in a portal after opening.