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.
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.
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.
<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>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.
PropsLink to "Props"
| Prop | What it does | Type | Default |
|---|---|---|---|
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"
| Input | Action |
|---|---|
| Right-click | Opens with the menu's corner at the pointer |
| Shift+F10 / context menu key | Opens under the focused target |
| Long press (500ms) on touch | Opens at the finger; a tap or a scroll does not |
| Arrow keys, Home, End, letters | Move through items (as in Menu) |
| Enter / Space | Run the item and close |
| Escape, Tab, click outside | Close; 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: noneanduser-select: noneto the target so the system callout does not compete with the menu.
ThemingLink to "Theming"
Shares Menu's tokens: --cy-menu-min-width, --cy-menu-max-width, --cy-menu-radius and --cy-menu-row-height.
RelatedLink to "Related"
Menu · CommandPalette · Tree
These examples run Cynosure 0.5.0 (commit 50ce30b), the version on npm.