Fragiola

API: components

ContextMenu

A menu opened from an area at the pointer, by a right-click, a long press or the ContextMenu key, built on Menu's parts, in each framework.

A menu of actions opened from an area of the page: a right-click opens it at the pointer, a touch long press at the finger, and the ContextMenu key or Shift+F10 at the focused element. Its content is a Menu's: items, groups, separators, checkbox and radio items, submenus. The menu follows the WAI-ARIA APG menu pattern (roles, keys, roving focus); the APG has no context menu pattern of its own, so its opening follows Base UI and the platforms. A menu opened by a button is a Menu.

ContextMenu has its own entry and is built on Menu's parts, whose part attribute names the context menu (context-menu-item). Only its Root and its Trigger area are its own.

File manager
Gallery

Behaviour

  • Right-click on the area, or on an element inside it: the browser's menu is prevented and the menu opens with its popup's corner at the pointer (reason trigger-press), the popup focused and no item highlighted. The mouseup that ends the right-click activates nothing: an item activates on a click only. A right-click on the area while the menu is open closes it (an outside press) and opens it again at the new point.
  • Touch long press: a touch held still on the area for 500 ms opens it at the press point (trigger-press). A move beyond 10 px, a release or a cancel before then opens nothing; the click and the contextmenu that follow a long press are ignored. Mouse and pen long presses do nothing.
  • Keyboard: the ContextMenu key or Shift+F10, with focus on the area or inside it, opens the menu anchored at the focused element with the first item highlighted (list-navigation). The browser's own contextmenu after the key neither reopens it nor shows the browser's menu.
  • Inside: every key and pointer rule is Menu's: arrows, Home and End, typeahead, Enter and Space, checkbox and radio items, submenus with their safe area. A contextmenu inside the menu is prevented and does nothing.
  • Close: Escape closes one level (escape-key), an item closes them all (item-press), a press outside closes them all (outside-press), Tab closes them all (focus-out) and moves on from the element that had focus when it opened. Focus returns to the element that had focus when the menu opened (a right-click focuses the focusable element it presses), when it is still in the page and not disabled; else focus does not move.
  • Positioning: the root level is anchored to a point (a virtual anchor), side="bottom" align="start" with no offset: its start corner is at the point, flipped and shifted to stay in the viewport. In RTL its start corner is its right edge. It does not follow the page's scroll.
  • Modal (modal, off by default): as Menu's, the page does not scroll and the pointer outside is blocked while open.
  • Lifecycle: onOpenChange before the change (details.cancel() keeps it as it is), onOpenChangeComplete(open), the actions close() and unmount(), and the Portal's keepMounted, as every overlay's. A context menu has one Trigger area: no handle and no triggerId.

Anatomy

partelementdata-primitives-partrole
Rootnoneholds the state of the root menu level
Triggerdivcontext-menu-triggerthe area the menu opens from (no button)
Portalnonemoves the Positioner to the end of body
Positionerdivcontext-menu-positionerplaces a level at its point, or next to its SubmenuTrigger
Popupdivcontext-menu-popupthe menu
Itemdivcontext-menu-iteman action
Separatordivcontext-menu-separatorseparates items
Groupdivcontext-menu-groupgroups items under a label
GroupLabeldivcontext-menu-group-labelnames its group
CheckboxItemdivcontext-menu-checkbox-iteman item that toggles a setting
CheckboxItemIndicatorspancontext-menu-checkbox-item-indicatorrendered while its item is checked
RadioGroupdivcontext-menu-radio-groupholds the value of its radio items
RadioItemdivcontext-menu-radio-iteman item of a set where one is checked
RadioItemIndicatorspancontext-menu-radio-item-indicatorrendered while its item is checked
SubmenuRootnonea nested level
SubmenuTriggerdivcontext-menu-submenu-triggerthe item that opens a submenu

Usage

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

<ContextMenu.Root>
    <ContextMenu.Trigger className="file-area">
        <button type="button">report.pdf</button>
        <button type="button">notes.txt</button>
    </ContextMenu.Trigger>
    <ContextMenu.Portal>
        <ContextMenu.Positioner>
            <ContextMenu.Popup aria-label="File actions">
                <ContextMenu.Item onClick={() => open()}>Open</ContextMenu.Item>
                <ContextMenu.Item onClick={() => rename()}>Rename</ContextMenu.Item>
                <ContextMenu.Separator />
                <ContextMenu.SubmenuRoot>
                    <ContextMenu.SubmenuTrigger>Share</ContextMenu.SubmenuTrigger>
                    <ContextMenu.Portal>
                        <ContextMenu.Positioner>
                            <ContextMenu.Popup>
                                <ContextMenu.Item onClick={() => share("email")}>Email</ContextMenu.Item>
                            </ContextMenu.Popup>
                        </ContextMenu.Positioner>
                    </ContextMenu.Portal>
                </ContextMenu.SubmenuRoot>
            </ContextMenu.Popup>
        </ContextMenu.Positioner>
    </ContextMenu.Portal>
</ContextMenu.Root>;

No button names the popup: give it an aria-label. Turn off text selection and the system callout of a long press in your styles (user-select: none, -webkit-touch-callout: none).

Props

Every part also takes what the primitive contract gives. The names below are React's; Vue binds them the same way (v-model:open, @open-change), and Angular as inputs and outputs ([(open)], (openChangeDetails)). The parts after the Trigger are Menu's.

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); details.trigger is the area; called before the change, details.cancel() stops it
onOpenChangeComplete(open: boolean) => voidonce an opening or a closing has finished its transitions
actionsRef (react){ current: OverlayActions | null }receives close() (reason imperative-action) and unmount(); Vue exposes them on the Root's template ref, Angular as the root directive's methods
loopFocusbooleantruethe arrow keys wrap at the ends
modalbooleanfalselocks the page's scroll and blocks the pointer outside while open
disabledbooleanfalsethe area opens nothing, and the browser's own menu shows

Trigger

No props of its own.

Portal

proptypedefaultdescription
container (react, vue)Elementdocument.bodywhere the Positioner goes
keepMounted (react, vue)booleanfalsekeep the content rendered while closed, hidden and data-closed
options (angular)Element | { container?, keepMounted? }{}the same two options: *contextMenuPortal="{ keepMounted: true }"

Positioner

proptypedefaultdescription
side"top" | "bottom" | "inline-start" | "inline-end" | "left" | "right""bottom"; a submenu's "inline-end"the side of the point
align"start" | "center" | "end""start"the alignment along that side
sideOffsetnumber0the distance from the point, in px
alignOffsetnumber0the shift along the point, in px
collisionPaddingnumber8the space kept from the viewport's edges, in px
anchorElementthe point that opened itthe 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: *contextMenuCheckboxItemIndicator="{ 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: *contextMenuRadioItemIndicator="{ 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 level only: a submenu follows its root
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
Triggerdata-popup-open while the menu is open, data-disabled; no role, no aria-haspopup and no inline style (it is an area, not a button)
Positionerrole="presentation", data-side, data-align, data-open / data-closed, data-nested (a submenu); hidden while kept mounted and closed
Popuprole="menu", aria-orientation="vertical", tabindex="-1", data-side, data-align, data-open / data-closed, data-starting-style, data-ending-style; no aria-labelledby on the root level (give it an aria-label); a submenu's is labelled by its SubmenuTrigger and carries data-nested
the other partsas Menu's, with context-menu-* part attributes

Keyboard

keyaction
ContextMenu, Shift+F10 on the area or inside itopens the menu at the focused element, on the first 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 keeps it open on a checkbox or radio item)
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 element focused when it opened

Exports

entryexports
@fragiola/primitives-react/context-menuContextMenu, ContextMenuRootProps, ContextMenuTriggerState, ContextMenuPortalProps, ContextMenuPositionerProps, ContextMenuPositionerState, ContextMenuItemProps, ContextMenuItemState, ContextMenuCheckboxItemProps, ContextMenuCheckboxItemState, ContextMenuCheckboxItemIndicatorProps, ContextMenuRadioGroupProps, ContextMenuRadioItemProps, ContextMenuRadioItemState, ContextMenuRadioItemIndicatorProps, ContextMenuSubmenuRootProps, ContextMenuSubmenuTriggerProps, ContextMenuSubmenuTriggerState, OverlayRootProps, OverlayPortalProps, OverlayActions, OpenChangeDetails, PositionerProps, Side, Align, ChangeDetails, ChangeReason, RenderProp
@fragiola/primitives-vue/context-menuContextMenu, ContextMenuRootProps, ContextMenuRootEmits, ContextMenuTriggerState, ContextMenuPortalProps, ContextMenuPositionerState, ContextMenuPopupState, ContextMenuItemProps, ContextMenuItemState, ContextMenuSubmenuRootProps, ContextMenuSubmenuTriggerProps, ContextMenuSubmenuTriggerState, OverlayRootProps, OverlayRootEmits, OverlayPortalProps, OverlayActions, OpenChangeDetails, PositionerProps, Side, Align, ChangeDetails, ChangeReason, RenderSlotProps, ContextMenuCheckboxItemEmits, ContextMenuCheckboxItemIndicatorProps, ContextMenuCheckboxItemProps, ContextMenuCheckboxItemState, ContextMenuRadioGroupEmits, ContextMenuRadioGroupProps, ContextMenuRadioItemIndicatorProps, ContextMenuRadioItemProps, ContextMenuRadioItemState
@fragiola/primitives-angular/context-menuContextMenuRoot, ContextMenuTrigger, ContextMenuPortal, ContextMenuPositioner, ContextMenuPopup, ContextMenuItem, ContextMenuSeparator, ContextMenuGroup, ContextMenuGroupLabel, ContextMenuCheckboxItem, ContextMenuCheckboxItemIndicator, ContextMenuRadioGroup, ContextMenuRadioItem, ContextMenuRadioItemIndicator, ContextMenuSubmenuRoot, ContextMenuSubmenuTrigger, OverlayActions, OverlayPortalOptions, OpenChangeDetails, OpenChangeEvent, Side, Align, ChangeDetails, ChangeReason, ContextMenuCheckableItemState, ContextMenuCheckboxItemState, ContextMenuItemState, ContextMenuPoint, ContextMenuPopupState, ContextMenuPositionerState, ContextMenuRadioItemState, ContextMenuState, ContextMenuSubmenuTriggerState, ContextMenuTriggerState, ValueChangeEvent