A dropdown menu of actions, checkbox and radio items, shortcuts and submenus.
Installation
Usage
import { Menu } from "./ui/uicontext/components/Menu.ts"const Message = UiSchema.TaggedEnum({ PickedAction: { value: Schema.String }, ToggledPinned: { pinned: Schema.Boolean },})// `pinned` lives in your Model; the menu asks for the change and your update makes itexport const ProjectMenu = (h: Html.H<typeof Message.Type>, pinned: boolean) => Menu(h, { props: { label: "Project actions", entries: [ { _tag: "Item", value: "rename", label: "Rename" }, { _tag: "Checkbox", value: "pinned", label: "Pinned", checked: pinned }, { _tag: "Separator" }, { _tag: "Item", value: "delete", label: "Delete", tone: "danger" }, ], }, onEmit: { Picked: ({ value }) => Message.PickedAction({ value }), Toggled: ({ checked }) => Message.ToggledPinned({ pinned: checked }), PickedRadio: () => undefined, }, })Examples
Radio groups
Each group asks for one value. Checkbox and radio items keep the menu open, so you can change several.
Row actions
A small icon-only trigger at the end of each row, with the menu lined up to its right edge.
API reference
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 |
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 |
Events
| Event | Payload | Description |
|---|---|---|
Picked | { value: string } | An action was picked |
Toggled | { value: string; checked: boolean } | A checkbox item asks for checked |
PickedRadio | { 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 |
Accessibility
- Follows the menu button pattern: the arrow keys move, typing jumps to an item, Enter or Space picks, Right opens a submenu and Left closes it.
- Escape closes the menu and puts focus back on its button. Checkbox and radio items say whether they're checked, and shortcuts are read through
aria-keyshortcuts. - An icon-only trigger is named by its
label.