Components
Context Menu
A menu that appears at the pointer on right click or long press.
Usage guidelines#
- Use context menus as an enhancement: Don't make a context menu the only way to perform actions. Users may not discover or be able to open a context menu, especially on touch devices or with assistive technology. Always provide visible controls for the actions that are available in the context menu.
Anatomy#
Import the components and place them together:
import { ContextMenu } from '@egoist/solid-base/context-menu';
<ContextMenu.Root>
<ContextMenu.Trigger />
<ContextMenu.Portal>
<ContextMenu.Backdrop />
<ContextMenu.Positioner>
<ContextMenu.Popup>
<ContextMenu.Arrow />
<ContextMenu.Item />
<ContextMenu.LinkItem />
<ContextMenu.Separator />
<ContextMenu.SubmenuRoot>
<ContextMenu.SubmenuTrigger />
</ContextMenu.SubmenuRoot>
<ContextMenu.Group>
<ContextMenu.GroupLabel />
</ContextMenu.Group>
<ContextMenu.RadioGroup>
<ContextMenu.RadioItem>
<ContextMenu.RadioItemIndicator />
</ContextMenu.RadioItem>
</ContextMenu.RadioGroup>
<ContextMenu.CheckboxItem>
<ContextMenu.CheckboxItemIndicator />
</ContextMenu.CheckboxItem>
</ContextMenu.Popup>
</ContextMenu.Positioner>
</ContextMenu.Portal>
</ContextMenu.Root>;Examples#
Menu displays additional demos, many of which apply to the context menu as well.
Nested menu#
To create a submenu, create a <ContextMenu.SubmenuRoot> inside the parent context menu. Use the
<ContextMenu.SubmenuTrigger> part for the menu item that opens the nested menu.
API reference#
<ContextMenu.Root>
A component that creates a context menu activated by right clicking or long pressing. Doesn't render its own HTML element.
onOpenChange| ((open: boolean, eventDetails: ContextMenuRoot.ChangeEventDetails) => void)—Event handler called when the menu is opened or closed.
actionsRefMenuRootTypes.Props['actionsRef']—A ref to imperative actions. - unmount: Ends the closing phase of the menu after an externally controlled closing animation finishes. Call preventUnmountOnClose() in onOpenChange first, otherwise the menu completes closing on its own. Whether it leaves the DOM is decided by keepMounted on the portal. - close: Closes the menu imperatively when called.
disabledbooleanfalseWhether the component should ignore user interaction.
highlightItemOnHoverbooleantrueWhether moving the pointer over items should highlight them. Disabling this prop allows CSS :hover to be differentiated from the :focus (data-highlighted) state.
defaultOpenbooleanfalseWhether the menu is initially open.
To render a controlled menu, use the open prop instead.
openboolean—Whether the menu is currently open.
onOpenChangeComplete((open: boolean) => void)—Event handler called after any animations complete when the menu is opened or closed.
loopFocusbooleantrueWhether to loop keyboard focus back to the first item when the end of the list is reached while using the arrow keys.
onItemHighlighted| (( highlightedItem: HTMLElement, eventDetails: MenuRoot.HighlightEventDetails, ) => void)—Callback fired when an item is highlighted or unhighlighted. Receives the highlighted item element (or undefined if no item is highlighted) and details containing the reason for the change, the event, and the item's text label. The reason can be: - 'keyboard': the highlight changed due to keyboard navigation. - 'pointer': the highlight changed due to pointer hovering. The event may be a MouseEvent rather than a PointerEvent. - 'none': the highlight changed for another reason, such as automatic highlighting while filtering, the item list changing, or the popup opening or closing.
orientationMenuRoot.Orientation'vertical'The visual orientation of the menu. Controls whether roving focus uses up/down or left/right arrow keys.
<ContextMenu.Trigger>
An area that opens the menu on right click or long press. Renders a <div> element.
classstring | ClassValue | ((state: State) => ClassValue)—CSS class applied to the element, or a function that returns a class based on the component's state.
styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—Style applied to the element, or a function that returns a style based on the component's state.
renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.
<ContextMenu.Backdrop>
An overlay displayed beneath the menu popup. Renders a <div> element.
classstring | ClassValue | ((state: State) => ClassValue)—CSS class applied to the element, or a function that returns a class based on the component's state.
styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—Style applied to the element, or a function that returns a style based on the component's state.
renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.
<ContextMenu.Portal>
A portal element that moves the popup to a different part of the DOM. By default, the portal element is appended to <body>. Renders a <div> element.
keepMountedbooleanfalseWhether to keep the portal mounted in the DOM while the popup is hidden.
containerPortalContainer—A parent element to render the portal element into.
classstring | ClassValue | ((state: State) => ClassValue)—CSS class applied to the element, or a function that returns a class based on the component's state.
styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—Style applied to the element, or a function that returns a style based on the component's state.
renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.
<ContextMenu.Positioner>
Positions the context menu popup against the pointer or a custom anchor. Renders a <div> element.
anchorMenuPositionerProps['anchor']—An element to position the popup against. By default, root context menus are positioned at the pointer, and submenus are positioned against their trigger.
sideMenuPositionerProps['side']'bottom'Which side of the anchor element to align the popup against. May automatically change to avoid collisions.
Submenus default to 'inline-end'.
sideOffsetMenuPositionerProps['sideOffset']—Distance between the anchor and the popup in pixels. Also accepts a function that returns the distance to read the dimensions of the anchor and positioner elements, along with its side and alignment.
The function takes a data object parameter with the following properties: - data.anchor: the dimensions of the anchor element with properties width and height. - data.positioner: the dimensions of the positioner element with properties width and height. - data.side: which side of the anchor element the positioner is aligned against. - data.align: how the positioner is aligned relative to the specified side.
Defaults to -5 for root context menus when side is not specified and align is not 'center'. Otherwise, it defaults to 0.
alignMenuPositionerProps['align']'start'How to align the popup relative to the specified side.
alignOffsetMenuPositionerProps['alignOffset']—Additional offset along the alignment axis in pixels. Also accepts a function that returns the offset to read the dimensions of the anchor and positioner elements, along with its side and alignment.
The function takes a data object parameter with the following properties: - data.anchor: the dimensions of the anchor element with properties width and height. - data.positioner: the dimensions of the positioner element with properties width and height. - data.side: which side of the anchor element the positioner is aligned against. - data.align: how the positioner is aligned relative to the specified side.
Defaults to 2 for root context menus when side is not specified and align is not 'center'. Otherwise, it defaults to 0.
arrowPaddingMenuPositionerProps['arrowPadding']—Minimum distance to maintain between the arrow and the edges of the popup.
Root context menus always use 0. Submenus default to 5.
collisionBoundaryBoundary'clipping-ancestors'An element or a rectangle that delimits the area that the popup is confined to.
collisionPaddingPadding5Additional space to maintain from the edge of the collision boundary.
stickybooleanfalseWhether to maintain the popup in the viewport after the anchor element was scrolled out of view.
disableAnchorTrackingbooleanfalseWhether to disable the popup from tracking any layout shift of its positioning anchor.
collisionAvoidanceCollisionAvoidance—Determines how to handle collisions when positioning the popup.
side controls overflow on the preferred placement axis (top/bottom or left/right): - 'flip': keep the requested side when it fits; otherwise try the opposite side (top and bottom, or left and right). - 'shift': never change side; keep the requested side and move the popup within the clipping boundary so it stays visible. - 'none': do not correct side-axis overflow.
align controls overflow on the alignment axis (start/center/end): - 'flip': keep side, but swap start and end when the requested alignment overflows. - 'shift': keep side and requested alignment, then nudge the popup along the alignment axis to fit. - 'none': do not correct alignment-axis overflow.
fallbackAxisSide controls fallback behavior on the perpendicular axis when the preferred axis cannot fit: - 'start': allow perpendicular fallback and try the logical start side first (top before bottom, or left before right in LTR). - 'end': allow perpendicular fallback and try the logical end side first (bottom before top, or right before left in LTR). - 'none': do not fallback to the perpendicular axis.
When side is 'shift', explicitly setting align only supports 'shift' or 'none'. If align is omitted, it defaults to 'flip'.
classstring | ClassValue | ((state: State) => ClassValue)—CSS class applied to the element, or a function that returns a class based on the component's state.
styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—Style applied to the element, or a function that returns a style based on the component's state.
renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.
<ContextMenu.Popup>
A container for the menu items. Renders a <div> element.
finalFocusFocusTargetProp—Determines the element to focus when the menu is closed.
- false: Do not move focus. - true: Move focus based on the default behavior (trigger or previously focused element). - An element or a ref object ({ current }): Move focus to that element. - function: Called with the interaction type (mouse, touch, pen, or keyboard). Return an element to focus, true to use the default behavior, or false/undefined to do nothing.
classstring | ClassValue | ((state: State) => ClassValue)—CSS class applied to the element, or a function that returns a class based on the component's state.
styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—Style applied to the element, or a function that returns a style based on the component's state.
renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.
<ContextMenu.Arrow>
Displays an element positioned against the menu anchor. Renders a <div> element.
classstring | ClassValue | ((state: State) => ClassValue)—CSS class applied to the element, or a function that returns a class based on the component's state.
styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—Style applied to the element, or a function that returns a style based on the component's state.
renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.
<ContextMenu.Group>
Groups related menu items with the corresponding label. Renders a <div> element.
classstring | ClassValue | ((state: State) => ClassValue)—CSS class applied to the element, or a function that returns a class based on the component's state.
styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—Style applied to the element, or a function that returns a style based on the component's state.
renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.
<ContextMenu.GroupLabel>
An accessible label that is automatically associated with its parent group. Renders a <div> element.
idstring—classstring | ClassValue | ((state: State) => ClassValue)—CSS class applied to the element, or a function that returns a class based on the component's state.
styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—Style applied to the element, or a function that returns a style based on the component's state.
renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.
<ContextMenu.Item>
An individual interactive item in the menu. Renders a <div> element.
disabledbooleanfalseWhether the component should ignore user interaction.
labelstring—Overrides the text used for keyboard text navigation and filtering. Falls back to the rendered text when not provided.
closeOnClickbooleantrueWhether to close the menu when the item is clicked.
nativeButtonbooleanfalseWhether the component renders a native <button> element when replacing it via the render prop. Set to true if the rendered element is a native button.
classstring | ClassValue | ((state: State) => ClassValue)—CSS class applied to the element, or a function that returns a class based on the component's state.
styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—Style applied to the element, or a function that returns a style based on the component's state.
renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.
<ContextMenu.CheckboxItem>
A menu item that toggles a setting on or off. Renders a <div> element.
checkedboolean—Whether the checkbox item is currently ticked.
To render an uncontrolled checkbox item, use the defaultChecked prop instead.
defaultCheckedbooleanfalseWhether the checkbox item is initially ticked.
To render a controlled checkbox item, use the checked prop instead.
onCheckedChange| (( checked: boolean, eventDetails: MenuCheckboxItem.ChangeEventDetails, ) => void)—Event handler called when the checkbox item is ticked or unticked.
disabledbooleanfalseWhether the component should ignore user interaction.
labelstring—Overrides the text used for keyboard text navigation and filtering. Falls back to the rendered text when not provided.
closeOnClickbooleanfalseWhether to close the menu when the item is clicked.
nativeButtonbooleanfalseWhether the component renders a native <button> element when replacing it via the render prop. Set to true if the rendered element is a native button.
classstring | ClassValue | ((state: State) => ClassValue)—CSS class applied to the element, or a function that returns a class based on the component's state.
styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—Style applied to the element, or a function that returns a style based on the component's state.
renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.
<ContextMenu.CheckboxItemIndicator>
Indicates whether the checkbox item is ticked. Renders a <span> element.
keepMountedbooleanfalseWhether to keep the HTML element in the DOM when the checkbox item is not checked.
classstring | ClassValue | ((state: State) => ClassValue)—CSS class applied to the element, or a function that returns a class based on the component's state.
styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—Style applied to the element, or a function that returns a style based on the component's state.
renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.
<ContextMenu.LinkItem>
A link in the menu that can be used to navigate to a different page or section. Renders an <a> element.
labelstring—Overrides the text used for keyboard text navigation and filtering. Falls back to the rendered text when not provided.
closeOnClickbooleanfalseWhether to close the menu when the item is clicked.
classstring | ClassValue | ((state: State) => ClassValue)—CSS class applied to the element, or a function that returns a class based on the component's state.
styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—Style applied to the element, or a function that returns a style based on the component's state.
renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.
<ContextMenu.RadioGroup>
Groups related radio items. Renders a <div> element.
valueValue—The controlled value of the radio item that should be currently selected.
To render an uncontrolled radio group, use the defaultValue prop instead.
defaultValueValue—The uncontrolled value of the radio item that should be initially selected.
To render a controlled radio group, use the value prop instead.
onValueChange| ((value: Value, eventDetails: MenuRadioGroup.ChangeEventDetails) => void)—Function called when the selected value changes.
disabledbooleanfalseWhether the component should ignore user interaction.
classstring | ClassValue | ((state: State) => ClassValue)—CSS class applied to the element, or a function that returns a class based on the component's state.
styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—Style applied to the element, or a function that returns a style based on the component's state.
renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.
<ContextMenu.RadioItem>
A menu item that works like a radio button in a given group. Renders a <div> element.
value*any—Value of the radio item. This is the value that will be set in the MenuRadioGroup when the item is selected.
disabledbooleanfalseWhether the component should ignore user interaction.
labelstring—Overrides the text used for keyboard text navigation and filtering. Falls back to the rendered text when not provided.
closeOnClickbooleanfalseWhether to close the menu when the item is clicked.
nativeButtonbooleanfalseWhether the component renders a native <button> element when replacing it via the render prop. Set to true if the rendered element is a native button.
classstring | ClassValue | ((state: State) => ClassValue)—CSS class applied to the element, or a function that returns a class based on the component's state.
styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—Style applied to the element, or a function that returns a style based on the component's state.
renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.
<ContextMenu.RadioItemIndicator>
Indicates whether the radio item is selected. Renders a <span> element.
keepMountedbooleanfalseWhether to keep the HTML element in the DOM when the radio item is not selected.
classstring | ClassValue | ((state: State) => ClassValue)—CSS class applied to the element, or a function that returns a class based on the component's state.
styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—Style applied to the element, or a function that returns a style based on the component's state.
renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.
<ContextMenu.Separator>
A separator element accessible to screen readers. Renders a <div> element.
orientationOrientation'horizontal'The orientation of the separator.
classstring | ClassValue | ((state: State) => ClassValue)—CSS class applied to the element, or a function that returns a class based on the component's state.
styleJSX.CSSProperties | string | ((state: State) => JSX.CSSProperties | string)—Style applied to the element, or a function that returns a style based on the component's state.
renderkeyof JSX.IntrinsicElements | ((props: HTMLProps, state: State) => JSX.Element)—Allows you to replace the component's HTML element with a different tag, or compose it with another component.
Accepts a tag name, or a function that receives the props to spread and the component's state, and returns the element to render.