Menu unstable
A button that opens a list of actions, links, checkbox and radio items, and submenus.
Menu is headless: it owns the keyboard and pointer behaviour, focus,
placement and the data attributes, and the consumer draws the markup.
The consumer owns whether the popup is open, passes it as open, and
stores what RequestedOpen and RequestedClose ask for. Picked reports
the item the user chose. items lists every item with its label, in the
order they're drawn, for navigation, typeahead and filtering. A checkbox
or radio item is checked when its entry has checked: true.
Anatomy
| Part | Element |
|---|---|
trigger |
<button> that opens the popup, one or several |
contextTrigger |
<div> that opens it at the pointer on a right click |
submenuTrigger |
<div> of the parent menu's item that opens a submenu |
backdrop |
<div>, a manual popover under the positioner |
positioner |
<div>, a manual popover placed by the menu |
popup |
<div> inside the positioner |
viewport |
<div> inside the popup, around content that changes |
list |
<div> inside the popup, around the items of a filter |
arrow |
<div> inside the popup |
item |
<div>, one per value |
linkItem |
<a>, one per value |
checkboxItem |
<div>, one per value |
checkboxItemIndicator |
<span> inside a checkbox item, shown while checked |
radioGroup |
<div> around radio items |
radioItem |
<div> inside a radio group, one per value |
radioItemIndicator |
<span> inside a radio item, shown while checked |
group |
<div> around items |
groupLabel |
<div> inside a group |
separator |
<div> between items |
input |
<input> inside the popup that filters the items |
clear |
<button> that empties the input |
empty |
<div> shown while the filter matches nothing |
finalFocus |
any focusable element, focused on close |
Each part takes the consumer's builder and returns it with the part
applied, and the consumer adds the children. trigger takes an options
object, { element }, with the optional fields of MenuTriggerOptions.
The items and the indicators take { element, value }, and group
and groupLabel take { element, group }, where group is the
group of the items inside. item closes the menu when picked and the
others don't; a closeOnClick field changes that. The indicators,
clear and empty return undefined while they're hidden, and a
keepMounted field keeps an indicator drawn. Every item value has to be
in items, and an item keys its element with the value.
A submenu is another Menu drawn inside the parent's popup, with the
parent's item value as parentItem. Apply both parts to the item that
opens it, menu.item({ element: submenu.submenuTrigger(h.div), value }),
and own its open like the root's. Picking an item in a submenu closes
every menu above it.
The positioner is a manual popover, so it renders in the top layer and
needs no portal. The browser gives a popover a border, padding, a
background and overflow: auto, so reset those on the positioner and
the backdrop and style the popup instead.
In place
With topLayer: false, the backdrop and the positioner render in place
as ordinary elements, hidden while closed, and the popup is placed and
clipped inside its containing block, as Positioning describes. The
backdrop covers that block, and the menu is never modal, whatever modal
or a Menubar says. A submenu takes topLayer from its parent unless
it sets its own.
CSS variables
Set on the positioner, so the popup inherits them: --anchor-width,
--anchor-height, --available-width, --available-height and
--transform-origin. With a viewport, the positioner also gets
--positioner-width and --positioner-height, the popup's size.
When the viewport moves to another trigger, the popup's --popup-width
and --popup-height go from its old size to its new one and back to
auto once its transitions finish. Size the popup with them and
transition width and height to animate it. The outgoing content
sits in a data-previous element with the old size as --popup-width
and --popup-height, next to the data-current one.
Data attributes
Each present with an empty value or absent, except data-side,
data-align, data-instant, data-activation-direction and
data-orientation, which hold a value:
trigger,submenuTrigger:data-popup-open,data-pressed,data-disabledcontextTrigger:data-popup-open,data-pressedbackdrop:data-open/data-closed,data-starting-style,data-ending-stylepositioner: as the backdrop, anddata-side,data-align,data-anchor-hidden,data-instantpopup: as the positioner, withoutdata-anchor-hidden, anddata-nestedarrow:data-side,data-align,data-uncenteredviewport:data-activation-direction, such as"right down",data-transitioning,data-instant- the items:
data-highlighted,data-disabled; checkbox and radio items and their indicators adddata-checked/data-unchecked input:data-highlightedwhile it has focus and no item doesseparator:data-orientation
data-instant is "click" when the menu opened or closed from the
keyboard, "dismiss" after Escape, and "trigger-change" once the
popup has moved to another trigger: turn the transitions off then.
Keyboard
On the trigger, Enter and Space open the popup, and ArrowDown and
ArrowUp open it on the first or the last enabled item, except inside a
vertical Toolbar, which moves focus with them. In the popup the
arrow keys of its orientation, Home and End move between items,
disabled ones included, and typing moves to the next item whose label
starts with what's typed. Enter and Space pick the highlighted item.
On a submenu trigger, the arrow key towards the submenu in the parent's
direction, Enter and Space open it, and in the submenu the arrow key
back in the submenu's direction closes it. Escape
closes the popup, or with closeParentOnEsc the whole menu, and Tab
closes it and moves focus on from the trigger.
With a filter, focus stays in the input and the highlighted item is its
aria-activedescendant. The arrow keys move the highlight, Enter picks
the item, and Space picks it while the input is empty. Home and End
move the caret once there's text.
Pointer
A mouse press opens the popup, and a release over an item more than
200ms later picks it. With openOnHover, which a submenu has by
default, resting on the trigger for delay opens it, and the popup
closes closeDelay after the pointer leaves both, unless it's crossing
to the popup. On the way to a submenu, the parent's items don't react.
Hovering another trigger of an open menu moves the popup to it at once.
A trigger's own options override the menu's. A press within 500ms of a
hover open keeps it open. A modal menu, the default for a
root menu not opened by hovering, locks the page's scroll, blocks
presses outside, except on the trigger, and hides the rest of the page
from assistive technology with aria-hidden.
A right click on a contextTrigger opens the popup at the pointer, and a
touch held still on it for 500ms opens it under the finger, as a
ContextMenu does. A menu opened that way is modal, blocks the whole
page, closes on Tab and gives focus back to the element that had it, and
a menu opened from its trigger behaves as above. The Menu key and
Shift+F10 open it against the contextTrigger, which defaults to
tabindex="0".
Limits
The menu doesn't read its items from the rendered elements, so items
lists them. A trigger carries no payload: RequestedOpen reports its
triggerId, so draw the content for that trigger. filter picks a
built-in match; for another, pass filter: null and draw only the
items that match. MenuTrigger draws a trigger outside the view. The
menu follows the nearest DirectionProvider above it or, without one,
the trigger's CSS direction.
Example An edit menu with a checkbox item and a submenu, with the open states in the consumer's Model
import { UiSchema } from "effect/ui"import { Html, HtmlServer } from "effect/ui/html"import { Menu } from "effect/ui/kit"import { Effect, Schema } from "effect"
const targets = [{ value: "email", label: "Email" }]
const EditorMessage = UiSchema.TaggedEnum({ RequestedOpenMenu: {}, RequestedCloseMenu: {}, RequestedOpenShare: {}, RequestedCloseShare: {}, PickedAction: { value: Schema.String, checked: Schema.optional(Schema.Boolean) }})
const Editor = Html.component({ name: "Editor", model: Schema.Struct({ open: Schema.Boolean, share: Schema.Boolean, wrap: Schema.Boolean }), message: EditorMessage}).make({ init: () => ({ open: false, share: false, wrap: true }), update: { RequestedOpenMenu: ($) => $.evolve({ open: true }), RequestedCloseMenu: ($) => $.evolve({ open: false }), RequestedOpenShare: ($) => $.evolve({ share: true }), RequestedCloseShare: ($) => $.evolve({ share: false }), PickedAction: ($, { checked, value }) => value === "wrap" ? $.evolve({ wrap: checked === true }) : $.model }, view: (h, model) => { const events = { Picked: ({ checked, value }: { readonly value: string; readonly checked?: boolean | undefined }) => EditorMessage.PickedAction({ value, checked }), Highlighted: () => undefined, RequestedQuery: () => undefined, Settled: () => undefined } return Menu(h, { props: { open: model.open, items: [ { value: "copy", label: "Copy" }, { value: "wrap", label: "Wrap lines", checked: model.wrap }, { value: "share", label: "Share" } ] }, onEmit: { ...events, RequestedOpen: () => EditorMessage.RequestedOpenMenu(), RequestedClose: () => EditorMessage.RequestedCloseMenu() }, view: (editMenu) => h.div( editMenu.trigger({ element: h.button }).children("Edit"), editMenu.positioner(h.div).children( editMenu.popup(h.div).children( editMenu.item({ element: h.div, value: "copy" }).children("Copy"), editMenu.checkboxItem({ element: h.div, value: "wrap" }).children( editMenu.checkboxItemIndicator({ element: h.span, value: "wrap" })?.children("โ"), "Wrap lines" ), editMenu.separator(h.div), Menu(h, { props: { open: model.share, items: targets, parentItem: "share" }, onEmit: { ...events, RequestedOpen: () => EditorMessage.RequestedOpenShare(), RequestedClose: () => EditorMessage.RequestedCloseShare() }, view: (shareMenu) => h.div( editMenu.item({ element: shareMenu.submenuTrigger(h.div), value: "share" }).children("Share"), shareMenu.positioner(h.div).children( shareMenu.popup(h.div).children( shareMenu.item({ element: h.div, value: "email" }).children("Email") ) ) ) }) ) ) ) }) }})
// The trigger says it opens a menu, and the checked item is drawn closed.const { html } = await Effect.runPromise(HtmlServer.renderToHtml(Editor, {}))if (!/<button[^>]* aria-haspopup="menu"[^>]*>Edit</.test(html)) throw new Error("the trigger isn't a menu button")if (!/role="menuitemcheckbox"[^>]* aria-checked="true"/.test(html)) throw new Error("the checkbox item isn't checked")Components
A menu: a button that opens a list of actions, links, checkbox and radio items, and submenus.
Details
The parts also return plain values for markup that depends on them:
open, mounted (the popup shows, through its exit transition),
transitionStatus, nested (it's a submenu), side and align
(where the popup ended up), highlighted (the value of the highlighted
item), openMethod, which is null once the popup closes,
transitioning and activationDirection for the viewport, and
nothingMatches, whether a filter matches no item.
A trigger reports the id the consumer gave it in RequestedOpen: set it
on the builder before the part. One without gets a generated id. The
menu remembers the trigger that opened it, or follows triggerId: only
that one reports it open and anchors the popup. Pressing another one while
the menu is open asks it to open for that one.
Each part's attributes win over the ones the consumer set on the builder before the part, including the ones a state leaves absent. Besides the ones under Data attributes in the module docs, they are:
trigger:id, the one on the builder or a generated one,type="button",aria-haspopup="menu",aria-expanded,aria-controlswhile open, pointing at the element with the menu role, anddisabledcontextTrigger:tabindex="0"unless the builder has one, so the keyboard can reach itsubmenuTrigger:aria-haspopup="menu",aria-expanded, andaria-controlswhile openbackdrop,positioner:role="presentation"andpopover="manual". A submenu's backdrop has it too, but never opens.popup:id, which theidprop sets,tabindex="-1", androle="menu",aria-orientationfor a horizontal menu andaria-labelledbypointing at the trigger unless the builder has one, orrole="presentation"when alistor aninputis drawn inside itlist:id,tabindex="-1", and the popup'srole="menu",aria-orientationandaria-labelledbyitem,linkItem:id,role="menuitem",aria-disabled,tabindex,"0"while it's highlighted or its submenu is open in a menu without a filter and"-1"otherwise, andhiddenwhile the filter leaves it outcheckboxItem,radioItem: asitem, withrole="menuitemcheckbox"or"menuitemradio"andaria-checkedradioGroup:role="group"group:role="group",aria-labelledbypointing at itsgroupLabel, andhiddenwhile the filter leaves none of its itemsgroupLabel:idarrow, the indicators:aria-hidden="true", andidon the arrowseparator:role="separator", witharia-orientation="vertical"in a horizontal menu and"horizontal"otherwiseinput:id,type="text",role="searchbox",inputmode="search",aria-activedescendant,aria-controlspointing at the list,value,disabled, andautocomplete="off",spellcheck,autocorrectandautocapitalizeclear:type="button",tabindex="-1",aria-hidden="true",disabledempty:role="status",aria-live="polite",aria-atomic="true"
The parts handle these events on a layer of their own, so the consumer's handlers for them still run:
trigger:mousedown,mouseup,clickandkeydown, andpointerenterandpointerleavewhen hovering opens it. A trigger with thedisabledoption handles none.contextTrigger:contextmenu,keydown, and touches through its mountsubmenuTrigger:mousedown,clickandkeydown, andpointerenterandpointerleavewhen hovering opens itbackdrop:contextmenu, in a menu opened from acontextTriggerpopup:keydownandkeyup- the items:
pointerenter,pointerleave,mousedown,mouseup,click,keydownandkeyup input:input,keydown,mousedown,focusandblurclear:mousedownandclick
The triggers, the popup and the items cancel the keys they handle, but
Enter on a linkItem follows its href. contextTrigger and the
backdrop cancel contextmenu, so the browser's menu doesn't show, and
clear cancels mousedown, so focus stays in the input. The
positioner's mount places the popup, moves focus, and listens for presses
and focus outside it. If it opens during mousedown and moves focus into
the popup, it cancels the default action to keep focus there.
It emits RequestedOpen, RequestedClose, Picked, Highlighted,
RequestedQuery and Settled.
History restore saves nothing of it: after Back, forward or a reload it
starts from init.
Signature
declare const Menu: Headless<{ readonly align?: "start" | "end" | "center"; readonly alignOffset?: number; readonly anchor?: string | { readonly height: number; readonly width: number; readonly x: number; readonly y: number; }; readonly arrowPadding?: number; readonly autoHighlight?: boolean | "always"; readonly closeDelay?: number; readonly closeParentOnEsc?: boolean; readonly collisionAvoidance?: { readonly align?: "shift" | "none" | "flip"; readonly fallbackAxisSide?: "start" | "none" | "end"; readonly side?: "shift" | "none" | "flip"; }; readonly collisionBoundary?: "clipping-ancestors" | { readonly height: number; readonly width: number; readonly x: number; readonly y: number; } | { readonly id: string; }; readonly collisionPadding?: number | { readonly bottom?: number; readonly left?: number; readonly right?: number; readonly top?: number; }; readonly delay?: number; readonly disableAnchorTracking?: boolean; readonly disabled?: boolean; readonly filter?: "endsWith" | "startsWith" | "contains" | null; readonly finalFocus?: boolean; readonly highlightItemOnHover?: boolean; readonly holdExit?: boolean; readonly id?: string; readonly items: readonly Array<{ readonly checked?: boolean; readonly disabled?: boolean; readonly label: string; readonly value: string; }>; readonly locale?: string; readonly loopFocus?: boolean; readonly modal?: boolean; readonly open: boolean; readonly openMethod?: "mouse" | "touch" | "pen" | "keyboard"; readonly openOnHover?: boolean; readonly openReason?: "none" | "trigger-press" | "outside-press" | "escape-key" | "focus-out" | "item-press" | "list-navigation" | "cancel-open" | "trigger-hover" | "trigger-focus" | "sibling-open"; readonly orientation?: "horizontal" | "vertical"; readonly parentItem?: string; readonly positionMethod?: "fixed" | "absolute"; readonly query?: string; readonly side?: "left" | "right" | "top" | "bottom" | "inline-start" | "inline-end"; readonly sideOffset?: number; readonly sticky?: boolean; readonly topLayer?: boolean; readonly triggerId?: string | null;}, { readonly _tag: "Settled"; readonly open: boolean;} | { readonly _tag: "RequestedOpen"; readonly reason: "none" | "trigger-press" | "outside-press" | "escape-key" | "focus-out" | "item-press" | "list-navigation" | "cancel-open" | "trigger-hover" | "trigger-focus" | "sibling-open"; readonly triggerId: string | null;} | { readonly _tag: "RequestedClose"; readonly reason: "none" | "trigger-press" | "outside-press" | "escape-key" | "focus-out" | "item-press" | "list-navigation" | "cancel-open" | "trigger-hover" | "trigger-focus" | "sibling-open";} | { readonly _tag: "Highlighted"; readonly reason: "none" | "pointer" | "keyboard"; readonly value: string | null;} | { readonly _tag: "Picked"; readonly checked?: boolean; readonly value: string;} | { readonly _tag: "RequestedQuery"; readonly query: string; readonly reason: "none" | "input-change" | "input-clear" | "clear-press";}, { activationDirection: string | null; align: "start" | "end" | "center"; arrow: Part<"div", [], never>; backdrop: Part<"div", [], never>; checkboxItem: <B extends Keyed<"div">>(__namedParameters: { readonly closeOnClick?: boolean; readonly element: B; readonly value: string; }) => ReturnType<B["attr"]> & Pick<<B extends PartTarget<"div"> & { readonly key: (key: string) => PartTarget<"div">; }>(element: B, value: string, ...rest: [closeOnClick?: boolean]) => ReturnType<B["attr"]> & Pick<Part<"div", [value: string, closeOnClick?: boolean], never>, "~R">, "~R">; checkboxItemIndicator: <B extends PartTarget<"span">>(__namedParameters: { readonly element: B; readonly keepMounted?: boolean; readonly value: string; }) => ReturnType<B["attr"]> | undefined & Pick<Part<"span", [item: { readonly checked?: boolean; readonly disabled?: boolean; readonly label: string; readonly value: string; }], never>, "~R">; clear: <B extends PartTarget<"button">>(element: B) => ReturnType<B["attr"]> | undefined & Pick<Part<"button", [], never>, "~R">; contextTrigger: <B extends PartTarget<"div">>(element: B) => ReturnType<B["attr"]> & Pick<Part<"div", [key: string, triggerId: string | null], never>, "~R">; empty: <B extends PartTarget<"div">>(element: B) => ReturnType<B["attr"]> | undefined & Pick<Part<"div", [], never>, "~R">; finalFocus: Part<string, [], never>; group: <B extends PartTarget<"div">>(__namedParameters: { readonly element: B; readonly group: string; }) => ReturnType<B["attr"]> & Pick<Part<"div", [group: string], never>, "~R">; groupLabel: <B extends PartTarget<"div">>(__namedParameters: { readonly element: B; readonly group: string; }) => ReturnType<B["attr"]> & Pick<Part<"div", [group: string], never>, "~R">; highlighted: string | null; input: Part<"input", [], never>; item: <B extends Keyed<"div">>(__namedParameters: { readonly closeOnClick?: boolean; readonly element: B; readonly value: string; }) => ReturnType<B["attr"]> & Pick<<B extends PartTarget<"div"> & { readonly key: (key: string) => PartTarget<"div">; }>(element: B, value: string, ...rest: [closeOnClick?: boolean]) => ReturnType<B["attr"]> & Pick<Part<"div", [value: string, closeOnClick?: boolean], never>, "~R">, "~R">; linkItem: <B extends Keyed<"a">>(__namedParameters: { readonly closeOnClick?: boolean; readonly element: B; readonly value: string; }) => ReturnType<B["attr"]> & Pick<<B extends PartTarget<"a"> & { readonly key: (key: string) => PartTarget<"a">; }>(element: B, value: string, ...rest: [closeOnClick?: boolean]) => ReturnType<B["attr"]> & Pick<Part<"a", [value: string, closeOnClick?: boolean], never>, "~R">, "~R">; list: Part<"div", [], never>; mounted: boolean; nested: boolean; nothingMatches: boolean; open: boolean; openMethod: "mouse" | "touch" | "pen" | "keyboard" | null; popup: Part<"div", [], never>; positioner: Part<"div", [], never>; radioGroup: Part<"div", [], never>; radioItem: <B extends Keyed<"div">>(__namedParameters: { readonly closeOnClick?: boolean; readonly element: B; readonly value: string; }) => ReturnType<B["attr"]> & Pick<<B extends PartTarget<"div"> & { readonly key: (key: string) => PartTarget<"div">; }>(element: B, value: string, ...rest: [closeOnClick?: boolean]) => ReturnType<B["attr"]> & Pick<Part<"div", [value: string, closeOnClick?: boolean], never>, "~R">, "~R">; radioItemIndicator: <B extends PartTarget<"span">>(__namedParameters: { readonly element: B; readonly keepMounted?: boolean; readonly value: string; }) => ReturnType<B["attr"]> | undefined & Pick<Part<"span", [item: { readonly checked?: boolean; readonly disabled?: boolean; readonly label: string; readonly value: string; }], never>, "~R">; separator: Part<"div", [], never>; side: "left" | "right" | "top" | "bottom" | "inline-start" | "inline-end"; submenuTrigger: <B extends PartTarget<"div">>(element: B) => ReturnType<B["attr"]> & Pick<Part<"div", [], never>, "~R">; transitioning: boolean; transitionStatus: MenuTransitionStatus; trigger: <B extends PartTarget<"button">>(options: MenuTriggerOptions & { readonly element: B; }) => ReturnType<B["attr"]> & Pick<Part<"button", [key: string, triggerId: string | null, options: MenuTriggerOptions], never>, "~R">; viewport: Part<"div", [], never>;}, never, { readonly _tag: "RequestedCloseByChild"; readonly reason: "none" | "trigger-press" | "outside-press" | "escape-key" | "focus-out" | "item-press" | "list-navigation" | "cancel-open" | "trigger-hover" | "trigger-focus" | "sibling-open";}, Shared<"Menu">, { readonly active: string | null; readonly disabled: boolean; readonly disabledItems: readonly Array<string>; readonly open: boolean; readonly orientation: "horizontal" | "vertical"; readonly reason: "none" | "trigger-press" | "outside-press" | "escape-key" | "focus-out" | "item-press" | "list-navigation" | "cancel-open" | "trigger-hover" | "trigger-focus" | "sibling-open" | null; readonly rtl: boolean; readonly topLayer: boolean;}, { readonly item: string; readonly open: boolean;}, never, { readonly active: string | null; readonly activeByKey: boolean; readonly barCurrent: string | null; readonly closeDelay: number; readonly closeMethod: "mouse" | "touch" | "pen" | "keyboard" | null; readonly closeReason: "none" | "trigger-press" | "outside-press" | "escape-key" | "focus-out" | "item-press" | "list-navigation" | "cancel-open" | "trigger-hover" | "trigger-focus" | "sibling-open" | null; readonly context: boolean; readonly direction: string | null; readonly focusLeft: boolean; readonly hoverCloseDelay: number; readonly hovering: boolean; readonly hoverTrigger: string | null; readonly hoverTriggerId: string | null; readonly inputFocused: boolean; readonly instant: "group" | "click" | "dismiss" | "trigger-change" | null; readonly justOpened: boolean; readonly keyboard: boolean; readonly longPress: { readonly key: string; readonly triggerId: string | null; readonly x: number; readonly y: number; } | null; readonly mounted: boolean; readonly openMethod: "mouse" | "touch" | "pen" | "keyboard" | null; readonly parentTopLayer: boolean; readonly placement: { readonly align: "start" | "end" | "center"; readonly anchorHidden: boolean; readonly arrowUncentered: boolean; readonly side: "left" | "right" | "top" | "bottom" | "inline-start" | "inline-end"; } | null; readonly point: { readonly height: number; readonly width: number; readonly x: number; readonly y: number; } | null; readonly pressedItem: boolean; readonly pressing: boolean; readonly provided: boolean | null; readonly reason: "none" | "trigger-press" | "outside-press" | "escape-key" | "focus-out" | "item-press" | "list-navigation" | "cancel-open" | "trigger-hover" | "trigger-focus" | "sibling-open" | null; readonly releaseAllowed: boolean; readonly releaseArmed: boolean; readonly rtl: boolean; readonly spaceDown: boolean; readonly started: boolean; readonly stick: boolean; readonly submenus: readonly Array<{ readonly item: string; readonly open: boolean; }>; readonly transitioning: boolean; readonly trigger: string | null; readonly triggerElement: string | null; readonly typeahead: { readonly matched: number; readonly typed: string; readonly typedFrom: number; };}, { readonly _tag: "RenderedFirstFrame";} | { readonly _tag: "FocusedOutside";} | { readonly _tag: "BlurredTrigger";} | { readonly _tag: "FocusedInput";} | { readonly _tag: "BlurredInput";} | { readonly _tag: "ReleasedTrigger";} | { readonly _tag: "LeftItem";} | { readonly _tag: "CompletedWaitBeforeResetTyping";} | { readonly _tag: "ShowedPopup";} | { readonly _tag: "MovedViewport"; readonly direction: string;} | { readonly _tag: "FinishedTransition"; readonly open: boolean;} | { readonly _tag: "PressedOutside"; readonly pointerType: string;} | { readonly _tag: "PressedInput";} | { readonly _tag: "ChangedInput"; readonly value: string;} | { readonly _tag: "PressedInputKey"; readonly key: string; readonly modified: boolean;} | { readonly _tag: "PressedTrigger"; readonly key: string; readonly pointerType: string; readonly triggerId: string | null;} | { readonly _tag: "PressedTriggerKey"; readonly key: string; readonly trigger: string; readonly triggerId: string | null;} | { readonly _tag: "EnteredItem"; readonly value: string;} | { readonly _tag: "PressedItem";} | { readonly _tag: "ReleasedItem"; readonly action: { readonly closeOnClick: boolean; readonly kind: "link" | "checkbox" | "radio" | "item"; readonly value: string; }; readonly pointerType: string;} | { readonly _tag: "ClickedItem"; readonly action: { readonly closeOnClick: boolean; readonly kind: "link" | "checkbox" | "radio" | "item"; readonly value: string; }; readonly pointerType: string;} | { readonly _tag: "ChangedPlacement"; readonly placement: { readonly align: "start" | "end" | "center"; readonly anchorHidden: boolean; readonly arrowUncentered: boolean; readonly side: "left" | "right" | "top" | "bottom" | "inline-start" | "inline-end"; };} | { readonly _tag: "ReleasedPointer"; readonly outside: boolean;} | { readonly _tag: "PressedKey"; readonly action: { readonly closeOnClick: boolean; readonly kind: "link" | "checkbox" | "radio" | "item"; readonly value: string; } | null; readonly key: string;} | { readonly _tag: "PressedContextTrigger"; readonly key: string; readonly pointerType: string; readonly triggerId: string | null; readonly x: number; readonly y: number;} | { readonly _tag: "PressedContextKey"; readonly key: string; readonly triggerId: string | null;} | { readonly _tag: "TouchedContextTrigger"; readonly key: string; readonly triggerId: string | null; readonly x: number; readonly y: number;} | { readonly _tag: "CancelledLongPress";} | { readonly _tag: "CompletedWaitBeforeLongPress";} | { readonly _tag: "CompletedWaitBeforeContextGrace";} | { readonly _tag: "ReleasedContextPointer"; readonly outside: boolean;} | { readonly _tag: "EnteredTrigger"; readonly closeDelay: number; readonly delay: number; readonly key: string; readonly triggerId: string | null;} | { readonly _tag: "LeftTrigger";} | { readonly _tag: "CompletedWaitBeforeHoverOpen";} | { readonly _tag: "LeftHoverArea";} | { readonly _tag: "ReturnedToHoverArea";} | { readonly _tag: "CompletedWaitBeforeHoverClose";} | { readonly _tag: "CompletedWaitBeforeStickExpires";} | { readonly _tag: "PressedSubmenuTrigger"; readonly pointerType: string;} | { readonly _tag: "ClickedSubmenuTrigger"; readonly pointerType: string;} | { readonly _tag: "PressedSubmenuTriggerKey"; readonly key: string;} | { readonly _tag: "ReleasedSpace"; readonly action: { readonly closeOnClick: boolean; readonly kind: "link" | "checkbox" | "radio" | "item"; readonly value: string; } | null;} | { readonly _tag: "ChangedDirection"; readonly rtl: boolean;} | { readonly _tag: "FoundTrigger"; readonly id: string | null;} | { readonly _tag: "TabbedOut";} | { readonly _tag: "AllowedRelease";} | { readonly _tag: "ChangedSubmenus"; readonly submenus: readonly Array<{ readonly item: string; readonly open: boolean; }>;} | { readonly _tag: "ChangedParent"; readonly parent: { readonly active: string | null; readonly disabled: boolean; readonly disabledItems: readonly Array<string>; readonly open: boolean; readonly orientation: "horizontal" | "vertical"; readonly reason: "none" | "trigger-press" | "outside-press" | "escape-key" | "focus-out" | "item-press" | "list-navigation" | "cancel-open" | "trigger-hover" | "trigger-focus" | "sibling-open" | null; readonly rtl: boolean; readonly topLayer: boolean; };} | { readonly _tag: "RequestedCloseByChild"; readonly reason: "none" | "trigger-press" | "outside-press" | "escape-key" | "focus-out" | "item-press" | "list-navigation" | "cancel-open" | "trigger-hover" | "trigger-focus" | "sibling-open";} | { readonly _tag: "SettledViewport";} | { readonly _tag: "SettledTriggerChange";} | { readonly _tag: "ClickedClear";} | { readonly _tag: "PressedBarKey"; readonly key: string; readonly trigger: string;} | { readonly _tag: "FocusedBarTrigger"; readonly key: string; readonly triggerId: string | null;} | { readonly _tag: "ChangedMenubar"; readonly menubar: { readonly current: string | null; readonly disabled: boolean; readonly hasSubmenuOpen: boolean; readonly modal: boolean; readonly opening: string | null; readonly orientation: "horizontal" | "vertical"; readonly rtl: boolean; readonly tabStop: string | null; };} | { readonly _tag: "ChangedProvider"; readonly rtl: boolean | null;}, QueryEntries<{}, {}>>MenuTrigger
unstableA button that opens a menu drawn somewhere else, such as in another
component. Call it with the caller's h, whether the menu is open for
this trigger, the menu's id as popupId, and what a press sends as
onPress, then chain the label.
Details
It sets aria-haspopup="menu", aria-expanded, data-popup-open and
data-pressed in forward, so they win over the caller's, and adds the
menu's id to the caller's aria-controls while the menu is open. With
filtered: true, it adds the list part's id instead. Give the menu
the same id, and the trigger's id as the menu's triggerId: it places
its popup against the trigger, a press on it isn't a press outside, and
focus goes back to it when the menu closes.
A mouse press sends onPress on mousedown, as the menu's own trigger
does, a touch, Enter or Space on click, and ArrowDown or ArrowUp on
keydown while the menu is closed. Opened with openMethod
"keyboard", the menu highlights its first enabled item. onHover is
sent once the pointer has rested on the trigger for delay milliseconds,
100 when left out. Each is a message or a function of the press, which carries the
trigger's id, how the user pressed it and why. Pass the method and the
reason to the menu as openMethod and openReason: a menu opened with
"trigger-hover" closes when the pointer leaves it and the trigger.
Given onPress, a mousedown, click or keydown handler of the
caller's own throws at render: pass the message as onPress. Given
onHover, it reserves the element's mount slot, so the caller can't set
another mount on it.
Signature
declare function MenuTrigger<Msg, R = never>(h: { readonly button: MenuTriggerElement<Msg>;}, options: { readonly element?: MenuTriggerElement<Msg, R>; readonly onHover?: NoInfer<Msg> | (press: MenuTriggerPress) => NoInfer<Msg>; readonly onPress?: NoInfer<Msg> | (press: MenuTriggerPress) => NoInfer<Msg>; readonly props: MenuTriggerProps;}): MenuTriggerElement<Msg, R>Constructors
Models
Why a Menu asks to open or close.
Signature
type MenuChangeReason = typeof MenuChangeReason.TypeWhat a Menu emits.
Signature
type MenuEvents = typeof MenuEvents.TypeText matching as a filtering menu does it, for a filter: null menu that
filters its own items: contains, startsWith and endsWith of a text
and a query, ignoring case, accents and punctuation by default.
Signature
interface MenuFilter { readonly contains: (text: string, query: string) => boolean; readonly endsWith: (text: string, query: string) => boolean; readonly startsWith: (text: string, query: string) => boolean;}How a filtering menu matches an item's label against the query.
Signature
type MenuFilterMode = typeof MenuFilterMode.TypeWhy the highlighted item changed.
Signature
type MenuHighlightReason = typeof MenuHighlightReason.TypeWhy the popup's animations are instant: "click" for a menu opened or
closed from the keyboard, "dismiss" for Escape, "trigger-change"
once the popup has moved to another trigger, and "group" for a menu of
a Menubar that opened from another one, or closed for it.
Signature
type MenuInstant = typeof Instant.TypeHow the menu was opened or closed.
Signature
type MenuInteraction = typeof MenuInteraction.TypeOne item of a Menu.
Signature
type MenuItem = typeof MenuItem.TypeThe props of a Menu.
Signature
type MenuProps = typeof MenuProps.TypeWhy the filter's query changes.
Signature
type MenuQueryReason = typeof MenuQueryReason.TypeWhere a menu's popup is in its transitions: "starting" for the first
frame after it opens, "ending" while it closes, null otherwise.
Signature
type MenuTransitionStatus = "starting" | "ending" | nullA <button> builder: what MenuTrigger accepts as element, from the
caller's h.button, and what it returns.
Signature
type MenuTriggerElement<Msg, R = never> = Html.Element<Msg, R, Html.EventsOf<"button">, HTMLButtonElement, "button">The options of one trigger part besides its element, over the
menu's props of the same name: disabled turns this trigger off, and
openOnHover, delay and closeDelay set how the pointer opens and
closes the menu from it.
Signature
interface MenuTriggerOptions { readonly closeDelay?: number; readonly delay?: number; readonly disabled?: boolean; readonly openOnHover?: boolean;}A press or a hover on a MenuTrigger: the trigger's id, if it has one,
how the user pressed it, and the reason to pass to the menu as
openReason.
Signature
interface MenuTriggerPress { readonly method: "mouse" | "touch" | "pen" | "keyboard"; readonly reason: "trigger-press" | "trigger-hover"; readonly triggerId: string | null;}The props of a MenuTrigger.
Signature
interface MenuTriggerProps { readonly delay?: number; readonly disabled?: boolean; readonly filtered?: boolean; readonly open: boolean; readonly popupId: string;}Schemas
MenuChangeReason
unstableWhy a Menu asks to open or close.
Details
"trigger-press": a press, or Enter or Space, on the trigger, or a right click or a long press on a context trigger"trigger-hover": the pointer rested on a trigger withopenOnHover, or left a menu that hovering opened"trigger-focus": focus moved to a trigger of aMenubarwhile another of its menus was open"list-navigation": ArrowDown or ArrowUp on the trigger, the arrow key that opens a submenu, or the one that closes it"item-press": an item was picked"outside-press": a press outside the trigger and the popup, other than on a scrollbar"escape-key": Escape in the popup, or on the page while it's open"focus-out": focus left the trigger and the popup, or Tab in the popup"sibling-open": another item of the parent menu was highlighted while this submenu was open"cancel-open": the press that opened it was released away from the trigger and the popup, or, for a right click, away from the popup more than 500ms later"none": anything else
A submenu that closes because its parent closed reports the parent's reason.
Signature
declare const MenuChangeReason: Literals<readonly ["trigger-press", "trigger-hover", "trigger-focus", "list-navigation", "item-press", "outside-press", "escape-key", "focus-out", "sibling-open", "cancel-open", "none"]>MenuEvents
unstableWhat a Menu emits.
Details
RequestedOpen: asks foropento be true.triggerIdis theidof the trigger pressed, fortriggerId. A right click on a context trigger of an open menu sends it again, for the new point.RequestedClose: asks foropento be false.Picked: reports that an item was picked.checkedis the new state of a checkbox item, and true for a radio item. It stands for the item's click, for every kind of item.Highlighted: reports that the highlighted item changed, tovalueor to none.RequestedQuery: asks forqueryto bequery. The menu also emits it, withreason: "none", to clear the query after the popup closes.Settled: reports that the popup's transitions ended, after it opened or closed.
Signature
declare const MenuEvents: TaggedEnum<{ Highlighted: TaggedStruct<"Highlighted", { readonly reason: Literals<readonly ["keyboard", "pointer", "none"]>; readonly value: NullOr<String>; }>; Picked: TaggedStruct<"Picked", { readonly checked: optional<Boolean>; readonly value: String; }>; RequestedClose: TaggedStruct<"RequestedClose", { readonly reason: Literals<readonly ["trigger-press", "trigger-hover", "trigger-focus", "list-navigation", "item-press", "outside-press", "escape-key", "focus-out", "sibling-open", "cancel-open", "none"]>; }>; RequestedOpen: TaggedStruct<"RequestedOpen", { readonly reason: Literals<readonly ["trigger-press", "trigger-hover", "trigger-focus", "list-navigation", "item-press", "outside-press", "escape-key", "focus-out", "sibling-open", "cancel-open", "none"]>; readonly triggerId: NullOr<String>; }>; RequestedQuery: TaggedStruct<"RequestedQuery", { readonly query: String; readonly reason: Literals<readonly ["input-change", "input-clear", "clear-press", "none"]>; }>; Settled: TaggedStruct<"Settled", { readonly open: Boolean; }>;}>MenuFilterMode
unstableHow a filtering menu matches an item's label against the query: anywhere in it, at its start, or at its end. Each ignores case, accents and punctuation.
Signature
declare const MenuFilterMode: Literals<readonly ["contains", "startsWith", "endsWith"]>MenuHighlightReason
unstableWhy the highlighted item changed: "keyboard" for arrow keys, Home, End
and typeahead, "pointer" for hovering, and "none" for opening,
closing and filtering.
Signature
declare const MenuHighlightReason: Literals<readonly ["keyboard", "pointer", "none"]>MenuInteraction
unstableHow the menu was opened or closed: "keyboard" for a key or a click
from no pointer.
Signature
declare const MenuInteraction: Literals<readonly ["mouse", "touch", "pen", "keyboard"]>One item of a Menu: the value it reports, the text typeahead and
filtering match, whether it can be picked, and whether a checkbox or
radio item is checked.
Signature
declare const MenuItem: Struct<{ readonly checked: optional<Boolean>; readonly disabled: optional<Boolean>; readonly label: String; readonly value: String;}>The props of a Menu.
Details
open: whether the popup shows. The menu only asks, withRequestedOpenandRequestedClose.items: every item, link, checkbox, radio and submenu trigger, in the order they're drawn. Navigation, typeahead and filtering read them, and the checkbox and radio items readchecked. A disabled submenu trigger doesn't open its submenu.disabled: the trigger and the items ignore the user.modal: while open, the page doesn't scroll, a press outside only closes the menu, and content outside the popup and trigger is hidden from assistive technology. True when left out. A submenu and a menu opened by hovering are never modal.loopFocus: the arrow keys wrap from the last item to the first. True when left out.orientation:"vertical", the default, moves with ArrowDown and ArrowUp, and"horizontal"with ArrowRight and ArrowLeft.highlightItemOnHover: the pointer highlights the item it's over. True when left out.closeParentOnEsc: in a submenu, Escape closes the whole menu and not only the submenu. False when left out.openOnHover: the pointer resting on the trigger fordelaymilliseconds opens the menu, and leaving the trigger and the popup closes it aftercloseDelay. False when left out, true in a submenu.delay,closeDelay: 100 and 0 when left out. A trigger'sMenuTriggerOptionsoverride these three for that trigger.parentItem: in a submenu, the value of the parent's item that opens it. AMenuwith one, drawn inside another one's popup, is its submenu. Without one, aMenudrawn inside another is a menu of its own.triggerId: theidof the trigger the menu is open for, when it has several, or one drawn withMenuTrigger.openMethod,openReason: how aMenuTriggeropened it, which the menu can't see. A menu opened with"trigger-hover"closes when the pointer leaves.id: the popup'sid, which aMenuTriggerpoints at. Generated when left out.finalFocus: on close, focus goes back to the trigger, or to afinalFocuspart. False leaves it where it is. True when left out.holdExit: keeps the closing popup shown, for an exit animation the consumer ends by clearing it.query: the filter's text. Given, the menu filters its items by it, and focus stays in theinputpart. The menu only asks, withRequestedQuery.filter: how items match,"contains"when left out.nullhides none: list and draw only the items that match.locale: the locale the filter compares text in.autoHighlight: filtering highlights the first matching item. True highlights it while the query isn't empty,"always"also when it is. False when left out.- the
Positioning.PositionerFields.sideis"bottom"when left out, and"inline-end"withalign"start"in a submenu.topLayer: falsedraws the menu in place, as the module describes.
Signature
declare const MenuProps: Struct<{ readonly align: optional<Literals<readonly ["start", "center", "end"]>>; readonly alignOffset: optional<Number>; readonly anchor: optional<Union<readonly [String, Struct<{ readonly height: Number; readonly width: Number; readonly x: Number; readonly y: Number; }>]>>; readonly arrowPadding: optional<Number>; readonly autoHighlight: optional<Union<readonly [Boolean, Literal<"always">]>>; readonly closeDelay: optional<Number>; readonly closeParentOnEsc: optional<Boolean>; readonly collisionAvoidance: optional<Struct<{ readonly align: optional<Literals<readonly ["flip", "shift", "none"]>>; readonly fallbackAxisSide: optional<Literals<readonly ["start", "end", "none"]>>; readonly side: optional<Literals<readonly ["flip", "shift", "none"]>>; }>>; readonly collisionBoundary: optional<Union<readonly [Literal<"clipping-ancestors">, Struct<{ readonly id: String; }>, Struct<{ readonly height: Number; readonly width: Number; readonly x: Number; readonly y: Number; }>]>>; readonly collisionPadding: optional<Union<readonly [Number, Struct<{ readonly bottom: optional<Number>; readonly left: optional<Number>; readonly right: optional<Number>; readonly top: optional<Number>; }>]>>; readonly delay: optional<Number>; readonly disableAnchorTracking: optional<Boolean>; readonly disabled: optional<Boolean>; readonly filter: optional<NullOr<Literals<readonly ["contains", "startsWith", "endsWith"]>>>; readonly finalFocus: optional<Boolean>; readonly highlightItemOnHover: optional<Boolean>; readonly holdExit: optional<Boolean>; readonly id: optional<String>; readonly items: $Array<Struct<{ readonly checked: optional<Boolean>; readonly disabled: optional<Boolean>; readonly label: String; readonly value: String; }>>; readonly locale: optional<String>; readonly loopFocus: optional<Boolean>; readonly modal: optional<Boolean>; readonly open: Boolean; readonly openMethod: optional<Literals<readonly ["mouse", "touch", "pen", "keyboard"]>>; readonly openOnHover: optional<Boolean>; readonly openReason: optional<Literals<readonly ["trigger-press", "trigger-hover", "trigger-focus", "list-navigation", "item-press", "outside-press", "escape-key", "focus-out", "sibling-open", "cancel-open", "none"]>>; readonly orientation: optional<Literals<readonly ["horizontal", "vertical"]>>; readonly parentItem: optional<String>; readonly positionMethod: optional<Literals<readonly ["absolute", "fixed"]>>; readonly query: optional<String>; readonly side: optional<Literals<readonly ["top", "bottom", "left", "right", "inline-start", "inline-end"]>>; readonly sideOffset: optional<Number>; readonly sticky: optional<Boolean>; readonly topLayer: optional<Boolean>; readonly triggerId: optional<NullOr<String>>;}>MenuQueryReason
unstableWhy the filter's query changes: "input-change" for typing,
"input-clear" for emptying the input, "clear-press" for the clear
part, and "none" for the menu having closed.
Signature
declare const MenuQueryReason: Literals<readonly ["input-change", "input-clear", "clear-press", "none"]>