# Menubar

> File, Edit and View along the top of an editor: menus the arrow keys move through as one control.

Page: https://uicontext.com/docs/components/menubar

Group: Overlays.

## When to use

File, Edit and View along the top of an editor: menus the arrow keys move through as one control. Use [Menu](https://uicontext.com/docs/components/menu.md) instead when the app needs one menu, not a row of them.

## Usage

```ts
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

Each example is a complete file. Its imports point where the CLI installs the source.

### A document editor

The example the page opens with: Menubar in “A document editor”.

```ts title="src/demo.ts" source="www/src/examples/menubar/demo.ts"
import { UiSchema } from "effect/ui"
import { Html } from "effect/ui/html"
import * as stylex from "@stylexjs/stylex"
import { Schema } from "effect"
import type { MenuEntry } from "./ui/uicontext/components/Menu.ts"
import { Menubar, menubarClosed, MenubarState } from "./ui/uicontext/components/Menubar.ts"
import { sx } from "./ui/uicontext/components/Sx.ts"
import { color, corner, font, space, stroke } from "./ui/uicontext/components/tokens.stylex.ts"

const styles = stylex.create({
	window: { display: "grid", inlineSize: "min(100%, 40rem)", overflow: "hidden", borderWidth: stroke.hairline, borderStyle: "solid", borderColor: color.line, borderRadius: corner.card, backgroundColor: color.panel },
	top: { paddingBlock: space.xs, paddingInline: space.sm, borderBlockEndWidth: stroke.hairline, borderBlockEndStyle: "solid", borderBlockEndColor: color.line },
	page: { minBlockSize: 140, paddingBlock: space.lg, paddingInline: space.xl, fontSize: font.base, lineHeight: 1.6, color: color.fg },
	said: { margin: 0, paddingBlock: space.sm, paddingInline: space.xl, borderBlockStartWidth: stroke.hairline, borderBlockStartStyle: "solid", borderBlockStartColor: color.line, fontSize: font.sm, color: color.muted },
})

const Message = UiSchema.TaggedEnum({
	ChangedMenus: { state: MenubarState },
	Picked: { value: Schema.String },
	Toggled: { value: Schema.String, checked: Schema.Boolean },
	Zoomed: { value: Schema.String },
})

const said: Readonly<Record<string, string>> = {
	new: "New document.",
	open: "Open a document…",
	"export:pdf": "Exported as PDF.",
	"export:docx": "Exported as Word.",
	"export:md": "Exported as Markdown.",
	print: "Printing…",
	undo: "Undid typing.",
	redo: "Nothing to redo.",
	cut: "Cut.",
	copy: "Copied.",
	paste: "Pasted.",
	find: "Find in document…",
	bold: "Bold.",
	italic: "Italic.",
	link: "Insert a link…",
	clear: "Cleared formatting.",
}

// a document editor's menus: actions with shortcuts, a submenu, settings that stay checked, a zoom level
export const DocumentEditor = Html.component({
	name: "DocumentEditor",
	model: Schema.Struct({ menus: MenubarState, ruler: Schema.Boolean, outline: Schema.Boolean, zoom: Schema.String, said: Schema.String }),
	message: Message,
}).make({
	init: () => ({ menus: menubarClosed, ruler: true, outline: false, zoom: "100", said: "" }),
	update: {
		ChangedMenus: ($, { state }) => $.evolve({ menus: state }),
		Picked: ($, { value }) => $.evolve({ said: said[value] ?? value }),
		Toggled: ($, { value, checked }) => (value === "ruler" ? $.evolve({ ruler: checked, said: checked ? "Ruler shown." : "Ruler hidden." }) : $.evolve({ outline: checked, said: checked ? "Outline shown." : "Outline hidden." })),
		Zoomed: ($, { value }) => $.evolve({ zoom: value, said: `Zoom ${value}%.` }),
	},
	view: (h, model) => {
		const file: ReadonlyArray<MenuEntry> = [
			{ _tag: "Item", value: "new", label: "New", shortcut: "mod+n" },
			{ _tag: "Item", value: "open", label: "Open…", shortcut: "mod+o" },
			{ _tag: "Separator" },
			{
				_tag: "Submenu",
				value: "export",
				label: "Export as",
				entries: [
					{ _tag: "Item", value: "export:pdf", label: "PDF" },
					{ _tag: "Item", value: "export:docx", label: "Word (.docx)" },
					{ _tag: "Item", value: "export:md", label: "Markdown" },
				],
			},
			{ _tag: "Item", value: "print", label: "Print…", shortcut: "mod+p" },
		]
		const edit: ReadonlyArray<MenuEntry> = [
			{ _tag: "Item", value: "undo", label: "Undo", shortcut: "mod+z" },
			{ _tag: "Item", value: "redo", label: "Redo", shortcut: "mod+shift+z", disabled: true },
			{ _tag: "Separator" },
			{ _tag: "Item", value: "cut", label: "Cut", shortcut: "mod+x" },
			{ _tag: "Item", value: "copy", label: "Copy", shortcut: "mod+c" },
			{ _tag: "Item", value: "paste", label: "Paste", shortcut: "mod+v" },
			{ _tag: "Separator" },
			{ _tag: "Item", value: "find", label: "Find…", shortcut: "mod+f" },
		]
		const view: ReadonlyArray<MenuEntry> = [
			{ _tag: "Checkbox", value: "ruler", label: "Ruler", checked: model.ruler },
			{ _tag: "Checkbox", value: "outline", label: "Outline", checked: model.outline },
			{ _tag: "Separator" },
			{ _tag: "Radio", group: "zoom", label: "Zoom", value: model.zoom, options: [{ value: "75", label: "75%" }, { value: "100", label: "100%" }, { value: "150", label: "150%" }] },
		]
		const format: ReadonlyArray<MenuEntry> = [
			{ _tag: "Item", value: "bold", label: "Bold", shortcut: "mod+b" },
			{ _tag: "Item", value: "italic", label: "Italic", shortcut: "mod+i" },
			{ _tag: "Item", value: "link", label: "Link…", shortcut: "mod+k" },
			{ _tag: "Separator" },
			{ _tag: "Item", value: "clear", label: "Clear formatting", shortcut: "mod+\\" },
		]
		return h.div.attrs(sx(styles.window)).children(
			h.div.attrs(sx(styles.top)).children(
				Menubar(h, {
					key: "editor",
					label: "Document",
					menus: [{ name: "File", entries: file }, { name: "Edit", entries: edit }, { name: "View", entries: view }, { name: "Format", entries: format }, { name: "Help", entries: [{ _tag: "Item", value: "shortcuts", label: "Keyboard shortcuts", shortcut: "mod+/" }], disabled: false }],
					state: model.menus,
					onState: (state) => Message.ChangedMenus({ state }),
					onSelect: (event) =>
						event._tag === "RequestedToggle" ? Message.Toggled({ value: event.value, checked: event.checked }) : event._tag === "RequestedPickRadio" ? Message.Zoomed({ value: event.value }) : Message.Picked({ value: event.value }),
				}),
			),
			h.div.attrs({ dir: "auto", ...sx(styles.page) }).children("Q1 launch plan. Pricing moves to seats on 1 February; the migration email goes out a week before."),
			h.p.attrs({ role: "status", ...sx(styles.said) }).children(model.said === "" ? `Zoom ${model.zoom}% · Ruler ${model.ruler ? "on" : "off"}` : model.said),
		)
	},
})
```

### 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.

```ts title="src/design.ts" source="www/src/examples/menubar/design.ts"
import { UiSchema } from "effect/ui"
import { Html } from "effect/ui/html"
import * as stylex from "@stylexjs/stylex"
import { Schema } from "effect"
import { Menubar, menubarClosed, MenubarState } from "./ui/uicontext/components/Menubar.ts"
import { sx } from "./ui/uicontext/components/Sx.ts"
import { color, font, space } from "./ui/uicontext/components/tokens.stylex.ts"

const styles = stylex.create({
	wrap: { display: "grid", gap: space.sm, inlineSize: "min(100%, 34rem)" },
	said: { margin: 0, fontSize: font.sm, color: color.muted },
})

const Message = UiSchema.TaggedEnum({ ChangedMenus: { state: MenubarState }, Picked: { value: Schema.String }, Snapped: { checked: Schema.Boolean } })

// a design tool's compact bar, framed, with one menu that can't be used yet
export const CanvasMenus = Html.component({
	name: "CanvasMenus",
	model: Schema.Struct({ menus: MenubarState, snap: Schema.Boolean, said: Schema.String }),
	message: Message,
}).make({
	init: () => ({ menus: menubarClosed, snap: true, said: "" }),
	update: {
		ChangedMenus: ($, { state }) => $.evolve({ menus: state }),
		Picked: ($, { value }) => $.evolve({ said: `${value}.` }),
		Snapped: ($, { checked }) => $.evolve({ snap: checked, said: checked ? "Snapping to grid." : "Free placement." }),
	},
	view: (h, model) =>
		h.div.attrs(sx(styles.wrap)).children(
			Menubar(h, {
				key: "canvas",
				label: "Canvas",
				framed: true,
				menus: [
					{
						name: "Object",
						entries: [
							{ _tag: "Item", value: "Grouped", label: "Group", shortcut: "mod+g" },
							{ _tag: "Item", value: "Ungrouped", label: "Ungroup", shortcut: "mod+shift+g" },
							{ _tag: "Separator" },
							{ _tag: "Label", label: "Order" },
							{ _tag: "Item", value: "Brought to front", label: "Bring to front", shortcut: "mod+shift+]" },
							{ _tag: "Item", value: "Sent to back", label: "Send to back", shortcut: "mod+shift+[" },
						],
					},
					{
						name: "Arrange",
						entries: [
							{ _tag: "Item", value: "Aligned left", label: "Align left" },
							{ _tag: "Item", value: "Centered", label: "Align centers" },
							{ _tag: "Item", value: "Spaced evenly", label: "Distribute evenly" },
							{ _tag: "Separator" },
							{ _tag: "Checkbox", value: "snap", label: "Snap to grid", checked: model.snap },
						],
					},
					{ name: "Plugins", entries: [], disabled: true },
				],
				state: model.menus,
				onState: (state) => Message.ChangedMenus({ state }),
				onSelect: (event) => (event._tag === "RequestedToggle" ? Message.Snapped({ checked: event.checked }) : event._tag === "RequestedPick" ? Message.Picked({ value: event.value }) : undefined),
			}),
			h.p.attrs({ role: "status", ...sx(styles.said) }).children(model.said),
		),
})
```

## API reference

### Menubar (view function)

**Options**

| Name | Type | Description |
| --- | --- | --- |
| `key` | `string` | Names the menubar among others in the same view; needed for more than one. |
| `label` (required) | `string` | What the bar is for: "Editor". |
| `menus` (required) | `MenubarMenu[]` | — |
| `state` (required) | `MenubarState` | — |
| `onState` (required) | `(state: MenubarState) => Message` | — |
| `onSelect` (required) | `(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` | — |

### Menu (component)

**Props**

| Name | Type | Description |
| --- | --- | --- |
| `label` (required) | `string` | The button's text, and its accessible name |
| `entries` (required) | `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 |

### MenuPreview (view function)

**Options**

| Name | Type | Description |
| --- | --- | --- |
| `entries` (required) | `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 |

### MenuRows (view function)

**Options**

| Name | Type | Description |
| --- | --- | --- |
| `submenu` (required) | `string \| null` | The value of the submenu that's open, or null |
| `pick` (required) | `(value: string, checked: boolean \| undefined) => Message` | — |
| `openSubmenu` (required) | `(value: string) => Message` | — |
| `closeSubmenu` (required) | `(value: string) => Message` | — |

## Keyboard and accessibility

### Keys

| Keys | Does |
| --- | --- |
| Right arrow, Left arrow | Move along the bar; in an open menu, open the next or the previous one |
| Home, End | Go to the first or the last menu |
| Enter, Space, Down arrow | Open the menu |
| Down arrow, Up arrow | Move between items |
| A–Z | Jump to the next item that starts with what’s typed |
| Escape | Close the menu and put focus back on its name |

On a Mac, Control is Command and Alt is Option. Left and Right trade places in right-to-left text.

## Installation

```sh
pnpm dlx shadcn@latest add checkfu/uicontext/menubar
```

With npm: `npx shadcn@latest add checkfu/uicontext/menubar`. With yarn: `yarn dlx shadcn@latest add checkfu/uicontext/menubar`. With bun: `bunx --bun shadcn@latest add checkfu/uicontext/menubar`.

It writes these files:

- `src/ui/uicontext/components/Button.ts`
- `src/ui/uicontext/components/Direction.ts`
- `src/ui/uicontext/components/Kbd.ts`
- `src/ui/uicontext/components/Menu.ts`
- `src/ui/uicontext/components/Menubar.ts`
- `src/ui/uicontext/components/Motion.ts`
- `src/ui/uicontext/components/Sx.ts`
- `src/ui/uicontext/components/button.stylex.ts`
- `src/ui/uicontext/components/tokens.stylex.ts`
- `src/ui/uicontext/components/variants.ts`

And installs the npm packages `@stylexjs/stylex`, `effect`.

## Source

- [src/Menubar.ts](https://github.com/checkfu/uicontext/blob/main/src/Menubar.ts)
- [src/Menu.ts](https://github.com/checkfu/uicontext/blob/main/src/Menu.ts)

## Used in

- [SaaS dashboard](https://uicontext.com/blocks/saas-dashboard.md)
- [Task manager](https://uicontext.com/blocks/issue-tracker.md)
- [Company brain](https://uicontext.com/blocks/doc-workspace.md)
- [Analytics dashboard](https://uicontext.com/blocks/analytics.md)
- [Email](https://uicontext.com/blocks/email.md)
- [Storefront](https://uicontext.com/blocks/storefront.md)
- [Helpdesk](https://uicontext.com/blocks/helpdesk.md)
- [Team chat](https://uicontext.com/blocks/team-chat.md)
- [Design](https://uicontext.com/blocks/design-tool.md)
- [Whiteboard](https://uicontext.com/blocks/whiteboard.md)

## Related

- [Dialog](https://uicontext.com/docs/components/dialog.md) (`dialog`): A modal window with a title, a description and actions.
- [Menu](https://uicontext.com/docs/components/menu.md) (`menu`): A dropdown menu of actions, checkbox and radio items, shortcuts and submenus.
- [Context Menu](https://uicontext.com/docs/components/context-menu.md) (`context-menu`): The menu a right click or long press opens over rows, files and cards.
- [Sheet](https://uicontext.com/docs/components/sheet.md) (`sheet`): A panel that slides over the page from a side or the bottom.
- [Toast](https://uicontext.com/docs/components/toast.md) (`toast`): Short notifications with an optional action like Undo.
- [Popover](https://uicontext.com/docs/components/popover.md) (`popover`): Content that opens beside its trigger and closes when you click away.
