Context Menu

View as Markdown

The menu a right click or long press opens over rows, files and cards.

Q3 launch plan.pdf2.4 MB
Pricing research — enterprise interviews.docx318 KB
Roadmap 2027.key★41.2 MB
Marketing budget.xlsx96 KB

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

One choice in a group

Radio items set a value and leave the menu open, so you see the change.

ENG-142Checkout fails when the coupon field has a trailing spaceUrgent
ENG-139Export invoices as CSVMedium
ENG-131Dark mode for the billing portalLow

API reference

ContextMenuView function

Options

ContextMenu options
NameTypeDescription
keystringNames the menu among others in the same view; needed for more than one.
staterequiredContextMenuState—
onStaterequired(state: ContextMenuState) => Message—
entriesrequiredMenuEntry[]The entries, as Menu takes them: usually built for state.target.
onSelectrequired(event: typeof MenuEvents.Type, target: string | null) => MessageAn action picked, a checkbox toggled or a radio option chosen, with the area it was for.
disabledbooleanThe browser's own menu shows instead: a text field, a read-only view.
viewrequired(area: ContextMenuArea) => View.ContentDraws 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

Menu props
NameTypeDescription
labelrequiredstringThe button's text, and its accessible name
entriesrequiredMenuEntry[]The menu's actions, checkbox and radio items, separators, labels and submenus, in order
iconOnlybooleanShow only an icon on the button (the label still names it): "⋯" by default
iconstringThe 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
disabledbooleanThe button can't open the menu
tabbablebooleanFalse 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

Menu events
EventPayloadDescription
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

MenuPreview options
NameTypeDescription
entriesrequiredMenuEntry[]The items to draw, as Menu takes them
highlightedstringThe value of the item drawn as highlighted, as if the pointer were on it

MenuRowsView function

Options

MenuRows options
NameTypeDescription
submenurequiredstring | nullThe 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

KeysDoes
Shift F10On a focused area, open its menu
Down arrow Up arrowMove between items
Enter SpacePick the item, or check it
Right arrow Left arrowOpen or close a submenu
a letterJump to the next item that starts with what’s typed
Escape TabClose the menu and put focus back

Installation

pnpm dlx shadcn@latest add checkfu/uicontext/context-menu

Source