import { Button } from "@senvenli/react/action";
import { Text } from "@senvenli/react/display";
import { Box } from "@senvenli/react/layout";
import { ContextMenu } from "@senvenli/react/overlay";
import { useState } from "react";
export default function Example() {
const [last, setLast] = useState("nothing yet");
return (
<ContextMenu
aria-label="Actions for ORD-482"
items={[
{ id: "open", label: "Open", icon: "external-link" },
{ id: "duplicate", label: "Duplicate", icon: "copy" },
{ id: "delete", label: "Delete", icon: "trash-2", tone: "danger" },
]}
onAction={(id) => setLast(String(id))}
>
<Box padding="space-200" background="surface-sunken" radius="large">
<Button variant="subtle">ORD-482 · Jordan Lee</Button>
<Text size="body-small" tone="subtle">Last action: {last}</Text>
</Box>
</ContextMenu>
);
}import { ContextMenu, ContextMenuTrigger } from "@senvenli/react/overlay";Examples
Composed from parts
A `ContextMenuTrigger` holds the region; the parts after it are the menu.import { Text } from "@senvenli/react/display";
import { Box } from "@senvenli/react/layout";
import { ContextMenu, ContextMenuTrigger, MenuItem, MenuSeparator } from "@senvenli/react/overlay";
export default function Example() {
return (
<ContextMenu aria-label="Actions for report.pdf">
<ContextMenuTrigger>
<Box padding="space-200" background="surface-sunken" radius="large">
<Text>report.pdf · right-click or long-press</Text>
</Box>
</ContextMenuTrigger>
<MenuItem id="open" icon="external-link">
Open
</MenuItem>
<MenuItem id="rename" icon="pencil" shortcut="F2">
Rename
</MenuItem>
<MenuSeparator />
<MenuItem id="delete" icon="trash-2" tone="danger">
Delete
</MenuItem>
</ContextMenu>
);
}Guidelines
When to use it
- Rows, cards or a canvas where people expect right-click actions.
Use something else when
- The actions belong to a visible button. Menu
Do and avoid
- Never the only way to an action: put it in a visible Menu too.
- Same order as the row's menu.
Accessibility
Built in
- Opens at the pointer, or under the focused element with Shift+F10 or the Menu key; focus goes back on close.
You provide
- An aria-label for the menu.
- With children: a textValue on a MenuItem whose label is not plain text, so typing can jump to it; the destructive item last, after a MenuSeparator.
API reference
ContextMenu
| Prop | Type | Default | Description |
|---|---|---|---|
childrenRequired | React.ReactNode | – | The region a right-click, a long press or Shift+F10 opens the menu on: a row, a card, a canvas. Or, to compose the menu from parts: a |
items | readonly MenuEntry[] | – | The menu as data; leave it out when composing with a |
onAction | (id: Key) => void | – | |
disabledKeys | Iterable<Key> | – | |
aria-labelRequired | string | – | Names the menu ("Actions for ORD-482"). |
childrenRequiredReact.ReactNodeThe region a right-click, a long press or Shift+F10 opens the menu on: a row, a card, a canvas. Or, to compose the menu from parts: a
ContextMenuTriggerholding that region, followed byMenuItem,MenuSection,MenuSubmenuandMenuSeparator.itemsreadonly MenuEntry[]The menu as data; leave it out when composing with a
ContextMenuTrigger.onAction(id: Key) => voiddisabledKeysIterable<Key>aria-labelRequiredstringNames the menu ("Actions for ORD-482").
ContextMenuTrigger
| Prop | Type | Default | Description |
|---|---|---|---|
childrenRequired | React.ReactNode | – | The region that opens the menu. |
childrenRequiredReact.ReactNodeThe region that opens the menu.