Skip to content
Cynosurev0.5.0
SearchStorybookGitHubnpmColour mode: System. Switch to Light

Components / Overlays

ContextMenu

A menu of commands for one thing, opened where the person points: right-click with a mouse, a long press on a touch screen, or Shift+F10 on the keyboard.

Right click opens where you point

When to useLink to "When to use"

  • Shortcuts on an item: a file row, a card, a node in a canvas or tree.

Use something else when:

  • the commands have a visible button: Menu. A context menu is hidden, so every command in it must also be reachable another way.
  • the commands apply to the whole app: CommandPalette

UsageLink to "Usage"

children is a function: spread the props it receives onto one element, and make that element focusable so keyboard users can open the menu.

tsx
Copy
import { ContextMenu } from "@arshad-shah/cynosure";

<ContextMenu label="File actions" items={items} onSelect={run}>
  {(props) => (
    <div {...props} tabIndex={0}>
      report.pdf
    </div>
  )}
</ContextMenu>

Items are the same as Menu's: icons, shortcuts, sections (labelled, or separated by lines only), submenus (an item with items; Right arrow, Enter or a click opens one, Left or Escape closes it, and choosing in one closes the whole menu), disabled, danger and checkable items.

Many targetsLink to "Many targets"

For the rows of a list or the tabs of a tab list, use one ContextMenu for all of them: spread targetFor(key) (the render prop's second argument) onto each, with a key such as the row's item. label and items may then be functions of that key, and onSelect and onOpenChange receive it after the value. The menu sits beside the list, not in it, so a list that may hold only its own items (a tablist, a tree) stays valid, and focus returns to the target that opened it. Tabs and Tree host their context menus this way.

tsx
Copy
<ContextMenu<File>
  label={(file) => `${file.name} actions`}
  items={(file) => commandsFor(file)}
  onSelect={(command, file) => run(command, file)}
>
  {(_, targetFor) => files.map((file) => <li key={file.id} {...targetFor(file)} tabIndex={0}>{file.name}</li>)}
</ContextMenu>
One menu for many targets

Coloured iconsLink to "Coloured icons"

Because the item type is Menu's, iconTone and iconColor colour an item's icon here as well, with the meaning of Icon's tone and color. Icons without them stay muted, and a disabled item shows its icon muted.

Item icons keep their colour

PropsLink to "Props"

Every prop of ContextMenu
PropWhat it doesTypeDefault
childrenRequired
Render the target: (props) => <div {...props} tabIndex={0}>…</div>. Make it focusable for keyboard users. For many targets (the rows of a list, the tabs of a tab list), spread targetFor(key) onto each instead, with a key (any value, such as the row's item) that label, items, onSelect and onOpenChange receive back. One menu then serves them all, beside the list rather than inside it.(props: ContextMenuTargetProps, targetFor: (key: K) => ContextMenuKeyTargetProps) => ReactNode
labelRequired
Accessible name of the menu; with many targets, a function of the key of the target it opened for.string | ((key: K) => string)
itemsRequired
The commands, as in Menu: items, or sections of items with a group name. With many targets, a function of the key of the target it opened for.MenuEntry[] | ((key: K) => MenuEntry[])
onSelectRequired
Called when an item is chosen, with its value (and, with many targets, the key of the target).(value: string, key: K) => void
onOpenChange
Called when the menu opens or closes, e.g. to highlight the row it belongs to (with many targets, its key).((open: boolean, key: K) => void)
disabled
The browser's own menu shows instead.boolean
className
Added to the menu panel, for layout.string
style
Layout only, applied to the panel.CSSProperties

Generated from the type definitions in @arshad-shah/cynosure 0.5.0.

Behaviour and accessibilityLink to "Behaviour and accessibility"

Behaviour and accessibility, table 1
InputAction
Right-clickOpens with the menu's corner at the pointer
Shift+F10 / context menu keyOpens under the focused target
Long press (500ms) on touchOpens at the finger; a tap or a scroll does not
Arrow keys, Home, End, lettersMove through items (as in Menu)
Enter / SpaceRun the item and close
Escape, Tab, click outsideClose; focus returns to the target
  • The target gets aria-keyshortcuts="Shift+F10". Mention the menu in the target's description if it holds commands found nowhere else.
  • Near the edges of the window the menu flips to stay on screen.
  • With disabled, the browser's own menu shows.
  • On touch screens, add -webkit-touch-callout: none and user-select: none to the target so the system callout does not compete with the menu.
Keyboard opens at the target

ThemingLink to "Theming"

Shares Menu's tokens: --cy-menu-min-width, --cy-menu-max-width, --cy-menu-radius and --cy-menu-row-height.

Menu · CommandPalette · Tree

These examples run Cynosure 0.5.0 (commit 50ce30b), the version on npm.

Getting started
Theming
Icons
Accessibility
Code editors and highlighters
Using Cynosure without React
Build-tool adapters
Components
Accordion
AppShell
AspectRatio
Breadcrumbs
Calendar
Card
Container
Disclosure
Divider
DocumentViewport
Grid
Inline
PageHeader
PanZoomCanvas
ScrollArea
SideNav
SortableList
SplitPane
Stack
StatusBar
Tabs
TitleBar
Toolbar
Tree
VirtualList
ZoomControls
Announcer
Avatar
Badge
CodeBlock
DescriptionList
EmptyState
ErrorBoundary
Field
Heading
IconTile
Image
Kbd
List
LoadingDots
Notice
ProgressBar
Prose
Skeleton
Stat
Steps
Table
Text
Button
Checkbox
Checklist
ColorField
ColorSwatches
Combobox
CompareSlider
CopyButton
DateField
FileDrop
IconButton
KeyValueEditor
Link
NumberField
Pagination
PathField
RadioGroup
SearchField
SegmentedControl
Select
SelectionFrame
SignaturePad
Slider
Switch
TagInput
TextArea
TextField
ThemeToggle
TimeField
ButtonBase
ChoiceBase
DialogBase
FileInputBase
HiddenValue
Icon
InputBase
LinkBase
LiveRegion
TextAreaBase
ChatMessage
Choice
DataGrid
DeviceFrame
HighlightedText
HotkeyField
ListRow
OptionList
OverlayFrame
QRCode
RowActions
Section
SiteFooter
SiteHeader
SkipLink
SortButton
VisuallyHidden
CommandPalette
ContextMenu
Dialog
FloatingToolbar
HoverCard
Menu
Menubar
Popover
ProgressOverlay
Sheet
Toast
Tooltip
Changelog