Components · Overlays
ContextMenu
The menu of actions on one thing that opens at the pointer on right-click, or on press and hold on a touch screen.
import { ContextMenu } from "@botiverse/raft-ui";A context menu is the menu of actions on one thing that opens where people right-click on desktop, or where they press and hold on a touch screen. It is the same menu that the thing's ⋯ button opens as a DropdownMenu: the same items, in the same order.
When to use
Which overlay fits which job is in Choosing a component.
Anatomy
ContextMenu has the same parts as DropdownMenu, with the ContextMenu prefix: ContextMenuItem, ContextMenuItemLabel, ContextMenuShortcut, ContextMenuRadioGroup, ContextMenuRadioItem, ContextMenuCheckboxItem, ContextMenuLabel, ContextMenuSeparator, ContextMenuSubmenu, ContextMenuSubmenuTrigger, ContextMenuFooter, ContextMenuPinnedFooter (experimental, from @botiverse/raft-ui/wip), ContextMenuPortal and ContextMenuPopup. They look and behave as described in DropdownMenu. It has no item count part.
ContextMenu: the root. Required.ContextMenuTrigger: the area that opens the menu: the message, the row, the card. Required.ContextMenuContent: the menu, portaled and opened at the pointer. Its items scroll inside it when they don't fit. Required.
States
The highlight, the current value and disabled items are the same as in DropdownMenu.
Behavior
- The menu opens at the pointer, or at the finger after press and hold, with its top-left corner there.
- When it would cross the window edge, it shifts to stay at least 8px inside the window instead of flipping.
- A submenu opens beside its item as in a DropdownMenu: to the end side, its first item level with the item, flipping to the other side of the item where only that side has room, and sliding along the window's edge, still level with its item, where neither side has. It never drops above or below its item.
- Where a shell keeps part of the window for itself (
--raft-frame-inset-top,--raft-frame-inset-start,--raft-frame-inset-end), it stays 8px inside that part instead of inside the window's edge. The start and end insets are the corners of the top band, so they count only for a menu opened at a point in the band; one opened under it keeps its 8px from the window's edge, as on the web (Page structure). The web sets none. - It stays where it opened while the page behind it scrolls.
- It is at least 240px wide and as wide as its longest item, never wider than the room the window leaves it; an item longer than that wraps or is cut, as in a DropdownMenu.
- A menu longer than the room it has scrolls inside it, keeps the highlighted item in view, fades at its cut edges and keeps a
ContextMenuPinnedFooterbelow its items, as a DropdownMenu does. - Choosing an action or a radio item closes the menu, and a checkbox item leaves it open, as in DropdownMenu. Esc and a click outside close it too.
- An item is as tall as its content, with constant padding, as in a DropdownMenu (Forms).
Content
The item labels follow DropdownMenu: sentence case, and the same words as the dialog an item opens.
Accessibility
- ContextMenu wraps the Base UI ContextMenu. It provides the
menurole, the item roles, arrow keys, typeahead and Esc. - Name the menu after the thing it acts on with
aria-labelonContextMenuContent("Message from Wug"). - Right-click and press and hold are not available from the keyboard. Offer every action of a context menu from a visible control as well, usually the ⋯ DropdownMenu.
Rules
- Build the context menu and the ⋯ menu from one list of items, so they never drift apart.
- Use the same menu on desktop and on a phone. Don't build a separate phone sheet for the same actions.
- Follow the rules of DropdownMenu: no color on destructive items, and the quick reactions first in the message menu.
API
ContextMenu
ContextMenuCheckboxItem
Also takes the attributes of its element.
ContextMenuCheckboxItemIndicator
Also takes the attributes of its element.
ContextMenuContent
Also takes the attributes of its element.
ContextMenuFooter
Takes the attributes of its element.
ContextMenuItem
Also takes the attributes of its element.
ContextMenuItemLabel, ContextMenuLabel, ContextMenuPopup
Takes the attributes of its element.
ContextMenuPortal
Also takes the attributes of its element.
ContextMenuRadioGroup
Also takes the attributes of its element.
ContextMenuRadioItem
Also takes the attributes of its element.
ContextMenuRadioItemIndicator
Also takes the attributes of its element.
ContextMenuSeparator
Also takes the attributes of its element.
ContextMenuShortcut
Takes the attributes of its element.
ContextMenuSubmenu
ContextMenuSubmenuTrigger
Also takes the attributes of its element.
ContextMenuTrigger
Also takes the attributes of its element.
ContextMenuPinnedFooter
Takes the attributes of its element.
Related
Changes
- 0.106.2 · FixedContextMenu: a submenu opens beside its item as a DropdownMenu's does ([ContextMenu › Behavior](https://raft-design.botiverse.build/components/context-menu/#behavior)): a
ContextMenuContentinside aContextMenuSubmenutakes the same position as a DropdownMenu submenu: the end side of its item (inline-end, so it follows the writing direction), its first item level with it, flipped to the other side where only that side has room, slid along the window's edge where neither has. - 0.106.0 · Design SystemLong menus and popovers scroll inside the room they have ([DropdownMenu › Long menus](https://raft-design.botiverse.build/components/dropdown-menu/#long-menus), [Popover › A long popover](https://raft-design.botiverse.build/components/popover/#a-long-popover), [Elevation › Popups](https://raft-design.botiverse.build/foundations/elevation/#popups)): a popup is capped at the space left between its trigger and the window's edge, and what doesn't fit scrolls inside it, never clipped. The highlighted item of a menu stays in view and clear of the fade; the cut edges fade over up to 24px; an action on the whole list stays below the items (
DropdownMenuPinnedFooter); a popover keeps its header and buttons and scrolls its body. Changed in this release:DropdownMenuContent,ContextMenuContent,PopoverContent,PopoverBody,PreviewCardContent,PreviewCardBody. - 0.106.0 · Added
DropdownMenuPinnedFooterandContextMenuPinnedFooter(work in progress, from@botiverse/raft-ui/wip): items kept below a long menu's scrolling items, after a separator, at any scroll. Write it as the menu's last child; the arrow keys reach it after the last item. - 0.104.0 · Design SystemTooltips on a touch screen, and who owns a long press ([Tooltip › Touch screens](https://raft-design.botiverse.build/components/tooltip/#touch-screens), [States › Dragging on a touch screen](https://raft-design.botiverse.build/foundations/states/#dragging-on-a-touch-screen)): on a touch screen a tooltip opens on a press and hold, on a disabled button too (
focusableWhenDisabled); a long press belongs to the layer that owns it (a long-press menu, a touch drag, a link), and the tooltip takes it only when nothing else wants it. Changed in this release:TooltipTrigger, and the long-press ownersContextMenuTrigger,SortableTabsTab,SortableComposerAttachmentandTaskDraggable. - 0.103.4 · FixedFrame insets (
Popover,DropdownMenu,ContextMenu,Select,Combobox,ComboboxSelect,Tooltip,PreviewCard,CoachMark,NotificationCenter,MobileNavServerPopover,ToastPositioner,ComposerSuggestionList):--raft-frame-inset-startand--raft-frame-inset-endare the corners of the window's top band (--raft-frame-inset-top), not columns down its sides. A popup anchored under the band keeps its own padding from the window's edge; only one anchored in the band (a title-bar control, or a context menu opened in the bar) keeps 8px clear of a corner. - 0.103.3 · FixedFrame insets (
useFrameInsets,frameCollisionBoundary): opening a positioned popup (Popover,DropdownMenu,Select,Combobox,ComboboxSelect,ContextMenu,Tooltip,PreviewCard,NotificationCenter,MobileNavServerPopover,ComposerSuggestionList) no longer stalls the main thread when--raft-frame-inset-*holds acalc()orenv(titlebar-area-*)expression on an overlay title bar. - 0.1.0 · AddedExtended
Lightboxwith thumbnail strip and related chrome parts; addedComboboxTriggerCountandContextMenuPopup. - 0.0.20 · AddedAdded
ContextMenuItemLabel,ContextMenuFooter,DropdownMenuItemLabel, andDropdownMenuFooterfor truncatable menu labels and structured footer content.