File, Edit and View along the top of an editor: menus the arrow keys move through as one control.
- 6.2 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
File, Edit and View along the top of an editor: menus the arrow keys move through as one control. Use Menu instead when the app needs one menu, not a row of them.
Usage
import { Menubar, MenubarState } from "./ui/uicontext/components/Menubar.ts"const Message = UiSchema.TaggedEnum({ ChangedMenus: { state: MenubarState }, Picked: { value: Schema.String } })export const AppMenus = (h: Html.H<typeof Message.Type>, state: MenubarState) => Menubar(h, { label: "App", menus: [ { name: "File", entries: [{ _tag: "Item", value: "new", label: "New", shortcut: "mod+n" }] }, { name: "Edit", entries: [{ _tag: "Item", value: "undo", label: "Undo", shortcut: "mod+z" }] }, ], state, onState: (next) => Message.ChangedMenus({ state: next }), onSelect: (event) => (event._tag === "RequestedPick" ? Message.Picked({ value: event.value }) : undefined), })Examples
Framed, with a disabled menu
framed draws the bar on a surface of its own. A menu with disabled stays in the bar and is skipped by the keys.
API reference
MenubarView function
Options
| Name | Type | Description |
|---|---|---|
key | string | Names the menubar among others in the same view; needed for more than one. |
labelrequired | string | What the bar is for: "Editor". |
menusrequired | MenubarMenu[] | — |
staterequired | MenubarState | — |
onStaterequired | (state: MenubarState) => Message | — |
onSelectrequired | (event: typeof MenuEvents.Type, menu: string) => Message | An action picked, a checkbox toggled or a radio option chosen, with the menu it was in. |
framed | boolean | A border and a surface around the bar. |
disabled | boolean | — |
style | Style | — |
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 |
|---|---|
| →Right arrow ←Left arrow | Move along the bar; in an open menu, open the next or the previous one |
| HomeHome EndEnd | Go to the first or the last menu |
| EnterEnter SpaceSpace ↓Down arrow | Open the menu |
| ↓Down arrow ↑Up arrow | Move between items |
| a letter | Jump to the next item that starts with what’s typed |
| EscEscape | Close the menu and put focus back on its name |