Fragiola

API: components

Menu

A menu of actions opened by a button, with submenus, roving focus and typeahead, non-modal by default, following the APG menu button pattern in each framework.

A list of actions opened by a button, with nested submenus. The highlighted item is the focused one (roving focus), typing jumps to an item, and each level is a layer. It follows the WAI-ARIA APG menu button pattern: non-modal by default, with a modal option that locks the page's scroll and blocks the pointer outside. Its items may be actions, checkbox items and radio items. A menu opened at the pointer from an area is a ContextMenu; a bar of menus is a Menubar, whose items are Menu Triggers.

File menu
Gallery

Behaviour

  • Open: ArrowDown, Enter or Space on the Trigger opens it on the first item, ArrowUp on the last; a press opens it with the popup focused and no item highlighted.
  • Inside: ArrowDown and ArrowUp move (wrapping with loopFocus), Home and End jump, a typed character moves to the next item that starts with it. A disabled item stays focusable but is never activated (APG). Hovering an item highlights it.
  • Activate: Enter, Space or a click fires the item's click, then closes every level (reason item-press) and focuses the trigger.
  • Checkbox and radio items: Space toggles a CheckboxItem (checks a RadioItem) and keeps the menu open; Enter toggles it and closes every level (APG); a click toggles it and closes only with closeOnClick (default false, Base UI). Each change reports onCheckedChange or the RadioGroup's onValueChange with the reason item-press. An indicator renders only while its item is checked, unless keepMounted.
  • Submenus: ArrowRight (ArrowLeft in RTL), Enter or Space on a SubmenuTrigger opens its submenu on its first item, hovering it opens it after delay; ArrowLeft closes it. While the pointer travels to an open submenu, crossing other items does not close it (a safe area).
  • Close: Escape closes one level (escape-key), focus back on its trigger. Tab closes every level (focus-out) and moves on from the trigger. A press outside closes them all (outside-press).
  • Modal (modal): while open, the page does not scroll and a press outside reaches no element of the page: it only closes the menu, focus back on the trigger. The keyboard is unchanged.
  • Layers: each level is a layer of the nested overlay tree: a submenu is a child of its menu, and a menu opened from a popover or a dialog is that overlay's child, so a press inside the parent closes the menu only.
  • Detached triggers: Triggers anywhere in the page open the same Root through a handle, each with a payload (detached triggers). The menu anchors to the active trigger and its popup is labelled by it; a press on another trigger while open moves the menu there (it stays open, with the new payload).
  • Lifecycle: onOpenChangeComplete(open) reports the end of each transition, the actions close() and unmount() act from code, and the Portal's keepMounted keeps the Positioner in the page while closed (hidden, data-closed).

Anatomy

partelementdata-primitives-partrole
Rootnoneholds the state of one menu level
Triggerbuttonmenu-triggeropens the menu
Portalnonemoves the Positioner to the end of body
Positionerdivmenu-positionerplaces a level next to its trigger
Popupdivmenu-popupthe menu
Itemdivmenu-iteman action
Separatordivmenu-separatorseparates items
Groupdivmenu-groupgroups items under a label
GroupLabeldivmenu-group-labelnames its group
CheckboxItemdivmenu-checkbox-iteman item that toggles a setting
CheckboxItemIndicatorspanmenu-checkbox-item-indicatorinside a CheckboxItem, rendered while it is checked
RadioGroupdivmenu-radio-groupholds the value of its radio items
RadioItemdivmenu-radio-iteman item of a set where one is checked
RadioItemIndicatorspanmenu-radio-item-indicatorinside a RadioItem, rendered while it is checked
SubmenuRootnonea nested level
SubmenuTriggerdivmenu-submenu-triggerthe item that opens a submenu

Usage

import { Menu } from "@fragiola/primitives-react/menu";

<Menu.Root>
    <Menu.Trigger>File</Menu.Trigger>
    <Menu.Portal>
        <Menu.Positioner sideOffset={4}>
            <Menu.Popup>
                <Menu.Item onClick={() => create()}>New file</Menu.Item>
                <Menu.SubmenuRoot>
                    <Menu.SubmenuTrigger>Share</Menu.SubmenuTrigger>
                    <Menu.Portal>
                        <Menu.Positioner>
                            <Menu.Popup>
                                <Menu.Item onClick={() => share("email")}>Email</Menu.Item>
                            </Menu.Popup>
                        </Menu.Positioner>
                    </Menu.Portal>
                </Menu.SubmenuRoot>
                <Menu.Separator />
                <Menu.Item disabled>Export</Menu.Item>
            </Menu.Popup>
        </Menu.Positioner>
    </Menu.Portal>
</Menu.Root>;

Checkbox and radio items:

<Menu.Popup>
    <Menu.CheckboxItem checked={toolbar} onCheckedChange={setToolbar}>
        <Menu.CheckboxItemIndicator>✓</Menu.CheckboxItemIndicator>
        Show toolbar
    </Menu.CheckboxItem>
    <Menu.Group>
        <Menu.GroupLabel>Sort by</Menu.GroupLabel>
        <Menu.RadioGroup value={sort} onValueChange={setSort}>
            <Menu.RadioItem value="name">
                <Menu.RadioItemIndicator>•</Menu.RadioItemIndicator>
                Name
            </Menu.RadioItem>
            <Menu.RadioItem value="date">
                <Menu.RadioItemIndicator>•</Menu.RadioItemIndicator>
                Date
            </Menu.RadioItem>
        </Menu.RadioGroup>
    </Menu.Group>
</Menu.Popup>;

Props

Every part also takes what the primitive contract gives: render (Vue: as and the render slot), a class and a style, a ref, and any attribute, which it forwards. The names below are React's; Vue binds them the same way (v-model:open, @open-change, :loop-focus), and Angular as inputs and outputs ([(open)], (openChangeDetails), [loopFocus]). An item's activation is its click event (onClick, @click, (click)). A CheckboxItem binds v-model:checked (@checked-change) in Vue and [(checked)] in Angular, whose (checkedChangeDetails) emits { value, details }; a RadioGroup v-model:value and [(value)], (valueChangeDetails).

Root

proptypedefaultdescription
openbooleanthe open state, controlled
defaultOpenbooleanfalsethe initial open state, uncontrolled
onOpenChange(open: boolean, details: OpenChangeDetails) => voidevery change, with its reason (trigger-press, list-navigation, item-press, escape-key, outside-press, focus-out); called before the change, details.cancel() stops it
onOpenChangeComplete(open: boolean) => voidonce an opening or a closing has finished its transitions (not on the first render, nor for an exit a reopening interrupted)
actionsRef (react){ current: OverlayActions | null }receives close() (reason imperative-action) and unmount() (ends an exit now); Vue exposes them on the Root's template ref, Angular as the root directive's methods
handleMenuHandle<Payload>connects Triggers rendered outside the Root (detached triggers)
triggerIdstring | nullthe active trigger's id, controlled together with open
defaultTriggerIdstring | nullnullthe initial active trigger, uncontrolled
loopFocusbooleantruethe arrow keys wrap at the ends
modalbooleanfalselocks the page's scroll and blocks the pointer outside while open

Trigger

proptypedefaultdescription
disabledbooleanfalsea disabled trigger opens nothing
handleMenuHandle<Payload>the handle of a Root this Trigger opens from anywhere, outside it
payloadPayloadwhat the Root's content receives while this Trigger is the active one
idstringgeneratedthe Trigger's id: triggerId and the handle's open(id) name it

Portal

proptypedefaultdescription
container (react, vue)Elementdocument.bodywhere the Positioner goes
keepMounted (react, vue)booleanfalsekeep the content rendered while closed (before the first opening and after an exit), hidden and data-closed; reopening shows the same elements
options (angular)Element | { container?, keepMounted? }{}the same two options: *menuPortal="{ keepMounted: true }", or *menuPortal="element" for a container alone

Positioner

proptypedefaultdescription
side"top" | "bottom" | "inline-start" | "inline-end" | "left" | "right""bottom"; a submenu's "inline-end"the side of the trigger; inline-* follow the direction
align"start" | "center" | "end""start"the alignment along that side
sideOffsetnumber0the distance from the trigger, in px
alignOffsetnumber0the shift along the trigger's edge, in px
collisionPaddingnumber8the space kept from the viewport's edges, in px
anchorElementthe triggerthe element to place the level next to

No props of its own.

Item

proptypedefaultdescription
disabledbooleanfalsefocusable, but never activated
labelstringits textthe text typeahead matches
closeOnClickbooleantrueactivating it closes every level

Separator

No props of its own.

Group

No props of its own.

GroupLabel

proptypedefaultdescription
id (angular)stringgeneratedthe id its group's aria-labelledby points at; React and Vue take it as a plain attribute

CheckboxItem

proptypedefaultdescription
checkedbooleanthe checked state, controlled
defaultCheckedbooleanfalsethe initial state, uncontrolled
onCheckedChange(checked: boolean, details: { reason, event }) => voidevery change (reason item-press)
disabledbooleanfalsefocusable, but never toggled
labelstringits textthe text typeahead matches
closeOnClickbooleanfalsea click also closes every level (Enter always closes, Space never)

CheckboxItemIndicator

proptypedefaultdescription
keepMounted (react, vue)booleanfalserendered while the item is unchecked too
options (angular){ keepMounted? } | true{}the same option: *menuCheckboxItemIndicator="{ keepMounted: true }", or true

RadioGroup

proptypedefaultdescription
valuestring | nullthe checked RadioItem's value, controlled (null: none)
defaultValuestring | nullnullthe initial value, uncontrolled
onValueChange(value: string | null, details: { reason, event }) => voidevery change (reason item-press)
disabledbooleanfalsedisables every RadioItem of the group

RadioItem

proptypedefaultdescription
valuestringrequiredthe group's value when this item is checked
disabledbooleanfalsefocusable, but never checked
labelstringits textthe text typeahead matches
closeOnClickbooleanfalsea click also closes every level (Enter always closes, Space never)

RadioItemIndicator

proptypedefaultdescription
keepMounted (react, vue)booleanfalserendered while the item is unchecked too
options (angular){ keepMounted? } | true{}the same option: *menuRadioItemIndicator="{ keepMounted: true }", or true
proptypedefaultdescription
openbooleanthe submenu's open state, controlled
defaultOpenbooleanfalseits initial open state, uncontrolled
onOpenChange(open: boolean, details: OpenChangeDetails) => voidevery change, with its reason; details.cancel() stops it
onOpenChangeComplete(open: boolean) => voidonce an opening or a closing has finished its transitions
actionsRef (react){ current: OverlayActions | null }receives the submenu's close() and unmount()
loopFocusbooleantruethe arrow keys wrap at the ends
modal (angular)booleanread on the root menu only ([menuSubmenuRoot] is the MenuRoot directive): a submenu follows its root
handle (angular)read on the root menu only, as modal: a submenu opens from its SubmenuTrigger only
triggerId (angular)read on the root menu only
defaultTriggerId (angular)read on the root menu only
proptypedefaultdescription
disabledbooleanfalsefocusable, but opens nothing
labelstringits textthe text typeahead matches
openOnHoverbooleantruehovering opens the submenu
delaynumber100ms of hover before it opens

State and part attributes

partattributes
Triggeraria-haspopup="menu", aria-expanded ("true" on the active trigger while open), aria-controls (the popup, on the active trigger while open), data-popup-open (the active trigger), data-disabled, an id
Positionerrole="presentation", data-side, data-align, data-open / data-closed, data-nested (a submenu); hidden while kept mounted and closed
Popuprole="menu", aria-labelledby (the active trigger), aria-orientation="vertical", tabindex="-1", data-side, data-align, data-open / data-closed, data-nested, data-starting-style, data-ending-style
Itemrole="menuitem", tabindex="-1", aria-disabled, data-highlighted, data-disabled
SubmenuTriggeras an Item, plus aria-haspopup="menu", aria-expanded, aria-controls, data-popup-open
CheckboxItemrole="menuitemcheckbox", tabindex="-1", aria-checked, data-checked / data-unchecked, data-highlighted, aria-disabled and data-disabled
RadioGrouprole="group", data-disabled; named by your aria-label, or a Group's GroupLabel around it
RadioItemrole="menuitemradio", tabindex="-1", aria-checked, data-checked / data-unchecked, data-highlighted, aria-disabled and data-disabled (itself or its group)
CheckboxItemIndicator, RadioItemIndicatoraria-hidden="true", data-checked / data-unchecked, data-disabled
Separatorrole="separator", aria-orientation="horizontal"
Grouprole="group", aria-labelledby (its GroupLabel)
GroupLabelan id

Keyboard

keyaction
ArrowDown, Enter, Space on the Triggeropens on the first item
ArrowUp on the Triggeropens on the last item
ArrowDown, ArrowUpthe next or previous item (wrapping with loopFocus)
Home, Endthe first or last item
a characterthe next item that starts with the typed text
Enter, Spaceactivates the item, closing every level
Space on a CheckboxItem or RadioItemtoggles or checks it; the menu stays open
Enter on a CheckboxItem or RadioItemtoggles or checks it, closing every level
ArrowRight (ArrowLeft in RTL) on a SubmenuTriggeropens its submenu on its first item
ArrowLeft (ArrowRight in RTL) in a submenucloses it
Escapecloses the current level
Tab, Shift+Tabcloses every level and moves on from the trigger

Exports

entryexports
@fragiola/primitives-react/menuMenu, MenuRootProps, MenuSubmenuRootProps, MenuTriggerProps, MenuTriggerState, MenuPortalProps, MenuPositionerProps, MenuPositionerState, MenuItemProps, MenuItemState, MenuSubmenuTriggerProps, MenuSubmenuTriggerState, PositionerProps, Side, Align, ChangeDetails, ChangeReason, RenderProp, MenuCheckableItemState, MenuCheckboxItemIndicatorProps, MenuCheckboxItemProps, MenuCheckboxItemState, MenuHandle, MenuRadioGroupProps, MenuRadioItemIndicatorProps, MenuRadioItemProps, MenuRadioItemState, OpenChangeDetails, OverlayActions, OverlayHandle, OverlayPortalProps, OverlayRootProps, OverlayTriggerProps, OverlayTriggerRootProps, createMenuHandle
@fragiola/primitives-vue/menuMenu, MenuRootProps, MenuRootEmits, MenuSubmenuRootProps, MenuTriggerProps, MenuTriggerState, MenuPortalProps, MenuPositionerState, MenuPopupState, MenuItemProps, MenuItemState, MenuSubmenuTriggerProps, MenuSubmenuTriggerState, PositionerProps, Side, Align, ChangeDetails, ChangeReason, RenderSlotProps, MenuHandle, OpenChangeDetails, OverlayActions, OverlayHandle, OverlayPortalProps, OverlayRootEmits, OverlayRootProps, OverlayRootSlots, OverlayTriggerProps, OverlayTriggerRootProps, createMenuHandle, MenuCheckableItemState, MenuCheckboxItemEmits, MenuCheckboxItemIndicatorProps, MenuCheckboxItemProps, MenuCheckboxItemState, MenuRadioGroupEmits, MenuRadioGroupProps, MenuRadioItemIndicatorProps, MenuRadioItemProps, MenuRadioItemState
@fragiola/primitives-angular/menuMenuRoot, MenuTrigger, MenuPortal, MenuPositioner, MenuPopup, MenuItem, MenuSeparator, MenuGroup, MenuGroupLabel, MenuCheckboxItem, MenuCheckboxItemIndicator, MenuRadioGroup, MenuRadioItem, MenuRadioItemIndicator, MenuSubmenuTrigger, injectMenu, MenuState, MenuTriggerState, MenuPositionerState, MenuPopupState, MenuItemState, MenuSubmenuTriggerState, Side, Align, ChangeDetails, ChangeReason, OpenChangeEvent, MenuHandle, OpenChangeDetails, OverlayActions, OverlayHandle, OverlayPortalOptions, createMenuHandle, MENU_SUBMENU, MENU_BAR, MenuBar, MenuBarEntry, menuItems, MenuCheckableItemState, MenuCheckboxItemState, MenuComponent, MenuRadioItemState, MenuRootBase, SafeArea, indicatorOptions