The menu a right click or long press opens over rows, files and cards.
- 6.1 kB JS 4.7 kB CSS gzipped
- No serious axe issues
- Server-rendered, hydrates
- 44px touch targets
- 6 keys checked
- Right-to-left, arrows swap
- 15 tests
When to use
The menu a right click or long press opens over rows, files and cards. Use Menu instead when the menu opens from a button.
Usage
import { ContextMenu, ContextMenuState } from "./ui/uicontext/components/ContextMenu.ts"const Message = UiSchema.TaggedEnum({ ChangedMenu: { menu: ContextMenuState }, Picked: { value: Schema.String, file: Schema.NullOr(Schema.String) },})export const FileMenu = (h: Html.H<typeof Message.Type>, menu: ContextMenuState) => ContextMenu(h, { state: menu, onState: (next) => Message.ChangedMenu({ menu: next }), entries: [ { _tag: "Item", value: "rename", label: "Rename", shortcut: "f2" }, { _tag: "Separator" }, { _tag: "Item", value: "trash", label: "Move to trash", tone: "danger" }, ], onSelect: (event, file) => (event._tag === "RequestedPick" ? Message.Picked({ value: event.value, file }) : undefined), view: (area) => area(h.div, "report").children("Report.pdf"), })Examples
API reference
ContextMenuView function
Options
| Name | Type | Description |
|---|---|---|
key | string | Names the menu among others in the same view; needed for more than one. |
staterequired | ContextMenuState | — |
onStaterequired | (state: ContextMenuState) => Message | — |
entriesrequired | MenuEntry[] | The entries, as Menu takes them: usually built for state.target. |
onSelectrequired | (event: typeof MenuEvents.Type, target: string | null) => Message | An action picked, a checkbox toggled or a radio option chosen, with the area it was for. |
disabled | boolean | The browser's own menu shows instead: a text field, a read-only view. |
viewrequired | (area: ContextMenuArea) => View.Content | Draws the page. area(builder, id) makes an element open this menu, and the id comes back as state.target; leave the id out for a single area. |
MenuComponent
Props
| Name | Type | Description |
|---|---|---|
labelrequired | string | The button's text, and its accessible name |
entriesrequired | MenuEntry[] | The menu's actions, checkbox and radio items, separators, labels and submenus, in order |
iconOnly | boolean | Show only an icon on the button (the label still names it): "⋯" by default |
icon | string | The icon the button shows instead of its label, by name; "chevron" draws a down chevron |
emphasis | "outline" | "ghost" | Outline (the default) or ghost, as Button's |
size | "xs" | "sm" | "md" | "xs": a 24px icon button, for a row's or a block's actions |
align | "start" | "end" | Which edge of the button the menu lines up with |
disabled | boolean | The button can't open the menu |
tabbable | boolean | False for a row's menu that its row opens from the keyboard (Shift+F10): the button leaves the Tab order, so a long list isn't a Tab stop per row |
Events
| Event | Payload | Description |
|---|---|---|
RequestedPick | { value: string } | An action was picked |
RequestedToggle | { value: string; checked: boolean } | A checkbox item asks for checked |
RequestedPickRadio | { group: string; value: string } | A radio item asks for value in its group |
MenuPreviewView function
Options
| Name | Type | Description |
|---|---|---|
entriesrequired | MenuEntry[] | The items to draw, as Menu takes them |
highlighted | string | The value of the item drawn as highlighted, as if the pointer were on it |
MenuRowsView function
Options
| Name | Type | Description |
|---|---|---|
submenurequired | string | null | The value of the submenu that's open, or null |
pickrequired | (value: string, checked: boolean | undefined) => Message | — |
openSubmenurequired | (value: string) => Message | — |
closeSubmenurequired | (value: string) => Message | — |
Keyboard and accessibility
| Keys | Does |
|---|---|
| ShiftF10Shift F10 | On a focused area, open its menu |
| ↓Down arrow ↑Up arrow | Move between items |
| EnterEnter SpaceSpace | Pick the item, or check it |
| →Right arrow ←Left arrow | Open or close a submenu |
| a letter | Jump to the next item that starts with what’s typed |
| EscEscape TabTab | Close the menu and put focus back |