NavigationMenu unstable
A navigation landmark whose triggers open panels of links, shown one at a time in a shared popup next to the trigger.
NavigationMenu is headless: it owns the ARIA and data attributes, the
arrow keys, the hover timing, dismissal, the popup's place in the top
layer and its position, and the consumer draws the markup. The consumer
owns which item is open, passes it as value, and stores what the
RequestedOpen and RequestedClose events ask for. A list with links
only needs none of this: NavigationLinks is the same list without
panels, at a fraction of the size.
Each trigger is a disclosure button: aria-expanded says whether its
panel is open, aria-controls names the popup while it is, and focus
stays on the trigger as the panel opens. The panel holds ordinary links,
not menu items.
Anatomy
| Part | Element |
|---|---|
root |
<nav> |
list |
<ul> inside the root |
item |
<li> inside the list |
trigger |
<button> in an item, that opens its panel |
link |
<a> in an item or a panel |
portal |
<div> in the top layer, around the parts below |
positioner |
<div> placed next to the open trigger |
popup |
<div> around the panel |
viewport |
<div> around the open item's panel |
arrow |
<div> that points at the trigger |
Each part takes the consumer's builder and returns it with the part
applied, and the consumer adds the children. trigger takes
{ element, value, disabled } and link takes
{ element, active, closeOnClick }. The portal returns undefined while
no panel shows. Draw the portal inside the root, after the list, and the
open item's panel inside the viewport: navigationMenu.value is the item
it shows, which stays the last one through the exit transition. arrow
is optional.
The portal is shown as a manual popover, so the popup sits in the top
layer, at the document's origin, or at the viewport's corner with
positionMethod: "fixed". With topLayer: false, it renders in place,
hidden while closed. The positioner's inline style carries left,
top and the variables Positioning describes. sideOffset is padding
on the positioner's side facing the trigger, so the pointer can cross the
gap. The viewport sizes the popup as the panel changes, as PreviewCard's
viewport does: --positioner-width and --positioner-height on the
positioner, --popup-width and --popup-height on the popup, and
data-activation-direction on the viewport while it moves between
triggers.
Data attributes
Each present with an empty value or absent, except the ones with a value:
trigger:data-popup-openwhile its panel is open,data-disabledlink:data-activeon the active linkpositioner:data-open/data-closed,data-side,data-align,data-anchor-hiddenpopup:data-open/data-closed,data-starting-style,data-ending-style,data-side,data-alignviewport:data-transitioning,data-activation-directionarrow:data-open/data-closed,data-side,data-align,data-uncentered
The popup stays shown until its own transitions and animations finish,
then closes and emits Settled.
Keyboard
Triggers and links share the list's arrow keys, as in NavigationLinks:
Right and Left in a horizontal menu, swapped when it is right-to-left, and
Down and Up in a vertical one. Enter and Space press a trigger. Down opens
the focused trigger's panel in a horizontal menu, and Right in a vertical
one, or Left when it is right-to-left. Focus stays on the trigger.
The popup is drawn after the list, so Tab takes the panel's stops as if
they followed the open trigger: Tab from the trigger goes to the panel's
first stop, Shift+Tab from there back to the trigger, Tab from the panel's
last stop to what follows the trigger and asks to close, with
focus-out, and Shift+Tab from there into the panel. Tab from anywhere
else skips the panel. A panel with no stop leaves Tab to the browser.
Escape asks to close, wherever focus is, and with focus in the panel focus
goes back to the trigger as the close starts. A press outside the root and
the popup asks to close, and so does focus leaving both. Neither moves
focus. The popup is inert through its exit.
Hover
The mouse resting on a trigger for delay milliseconds, 50 by default,
asks to open its panel. While a panel is open, hovering another trigger
switches to it at once. A panel opened by hover asks to close
closeDelay milliseconds, 50 by default, after the pointer leaves the
triggers and the popup, and a press on its trigger within 500 milliseconds
of the hover keeps it open. A touch never opens a panel by hover.
Limits
- There are no submenus, no arrow keys inside a panel, and the panels other than the open one aren't in the page.
Example A header with a Products panel and a plain Docs link
import { UiSchema } from "effect/ui"import { Html, HtmlServer } from "effect/ui/html"import { NavigationMenu } from "effect/ui/kit"import { Effect, Schema } from "effect"
const HeaderMessage = UiSchema.TaggedEnum({ RequestedOpenNav: { value: Schema.String }, RequestedCloseNav: {}})
const Header = Html.component({ name: "Header", model: Schema.Struct({ nav: Schema.NullOr(Schema.String) }), message: HeaderMessage}).make({ init: () => ({ nav: "products" }), update: { RequestedOpenNav: ($, { value }) => ({ nav: value }), RequestedCloseNav: () => ({ nav: null }) }, view: (h, model) => NavigationMenu(h, { props: { value: model.nav, sideOffset: 8 }, onEmit: { RequestedOpen: ({ value }) => HeaderMessage.RequestedOpenNav({ value }), RequestedClose: () => HeaderMessage.RequestedCloseNav(), Settled: () => undefined }, view: (navigationMenu) => navigationMenu.root(h.nav.attr("aria-label", "Main")).children( navigationMenu.list(h.ul).children( navigationMenu.item(h.li).children( navigationMenu.trigger({ element: h.button, value: "products" }).children("Products") ), navigationMenu.item(h.li).children( navigationMenu.link({ element: h.a.attr("href", "/docs") }).children("Docs") ) ), navigationMenu.portal(h.div)?.children( navigationMenu.positioner(h.div).children( navigationMenu.popup(h.div.class("panel")).children( navigationMenu.viewport(h.div).children( navigationMenu.value === "products" && h.a.attr("href", "/products/ui").children("UI") ) ) ) ) ) })})
// The open trigger controls the popup, whose links are plain links.const { html } = await Effect.runPromise(HtmlServer.renderToHtml(Header, {}))const controls = /<button[^>]* aria-expanded="true"[^>]* aria-controls="([^"]+)"[^>]*>Products/.exec(html)if (controls === null) throw new Error("Products doesn't control an open panel")if (!html.includes(`id="${controls[1]}"`)) throw new Error("the controlled popup is missing")if (html.includes("role=\"menuitem\"")) throw new Error("a panel link is a menu item")