Menubar unstable
A row of menus, such as File, Edit and View along the top of an editor, that the arrow keys move through as one control.
Menubar is headless: it keeps one of its triggers in the page's Tab
order, moves focus between them, and tells each Menu drawn inside it
that it sits in a menubar. Each menu is an ordinary Menu call inside
the root, and the consumer owns which one is open, as for any Menu.
Anatomy
| Part | Element |
|---|---|
root |
<div> around the menus |
The root takes the consumer's builder and returns it, and the consumer
draws the menus inside it. It adds no markup. A menu inside a menubar
draws its trigger and positioner as it would anywhere else: the
trigger becomes a menuitem of the menubar, the popup opens below it,
or beside it in a vertical menubar, and lines up with its start.
Data attributes
On the root, data-orientation holds the orientation, and these are
present with an empty value or absent:
data-modaldata-has-submenu-open: while one of its menus is open
A menu's parts keep their Menu data attributes. When the open menu
moves to another trigger from the keyboard or the pointer, the
positioner and the popup of the menu that opens get
data-instant="group", and so do those of the menu that closes when
focus moves to the other one: turn the transitions off then.
Keyboard
One trigger is in the page's Tab order: the one that last had focus, or the first enabled one. Before mounting, including in server-rendered HTML, an enabled menubar uses the root as its Tab stop instead. On mount, focus on the root moves to the first enabled trigger. On a trigger, ArrowRight and ArrowLeft, or ArrowDown and ArrowUp in a vertical menubar, move focus to the next and the previous trigger, and Home and End to the first and the last. Focusing a trigger opens nothing. Enter and Space open its menu, and so do ArrowDown and ArrowUp, on the first or the last item, or, in a vertical menubar, the arrow key that points away from the bar.
In an open menu of a horizontal menubar, ArrowRight and ArrowLeft open the next and the previous menu, unless the highlighted item opens a submenu or the key closes the submenu that has focus. In a vertical menubar the arrow key that points back at the bar closes the menu. Escape closes it, and focus goes back to its trigger.
Pointer
A press on a trigger opens its menu, and another press closes it. Hovering a trigger opens nothing until one of the menus is open: then hovering another trigger opens its menu at once, and the open one closes.
Usage
Keep which menu is open as one value in the Model. A menu that closes
because another one opened can report RequestedClose after the other's
RequestedOpen, so clear the value only while it still names the menu that
reports it.
Draw each menu's trigger and positioner inside the root: the menubar
finds its triggers there, in document order. A menu's own modal,
openOnHover and delay don't apply in a menubar. The menubar's
modal does, and the triggers and the empty parts of the bar stay
clickable over the modal layer. A disabled menubar disables every menu
in it. The menubar follows the nearest DirectionProvider above it or,
without one, the root's CSS direction.
Example A File and an Edit menu, with the open one in the consumer's Model
import { UiSchema } from "effect/ui"import { Html, HtmlServer } from "effect/ui/html"import { Menu, Menubar } from "effect/ui/kit"import { Effect, Schema } from "effect"
const menus = [ { name: "File", items: [{ value: "new", label: "New" }, { value: "save", label: "Save" }] }, { name: "Edit", items: [{ value: "undo", label: "Undo" }, { value: "redo", label: "Redo" }] }]
const EditorMessage = UiSchema.TaggedEnum({ RequestedOpenMenu: { menu: Schema.String }, RequestedCloseMenu: { menu: Schema.String }, PickedAction: { value: Schema.String }})
const Editor = Html.component({ name: "Editor", model: Schema.Struct({ open: Schema.NullOr(Schema.String) }), message: EditorMessage}).make({ init: () => ({ open: null }), update: { RequestedOpenMenu: ($, { menu }) => $.evolve({ open: menu }), RequestedCloseMenu: ($, { menu }) => $.model.open === menu ? $.evolve({ open: null }) : $.model, PickedAction: ($) => $.model }, view: (h, model) => Menubar(h, { props: {}, view: (menubar) => menubar.root(h.div.class("menubar")).children( menus.map(({ items, name }) => Menu(h, { key: name, props: { open: model.open === name, items }, onEmit: { RequestedOpen: () => EditorMessage.RequestedOpenMenu({ menu: name }), RequestedClose: () => EditorMessage.RequestedCloseMenu({ menu: name }), Picked: ({ value }) => EditorMessage.PickedAction({ value }), Highlighted: () => undefined, RequestedQuery: () => undefined, Settled: () => undefined }, view: (menu) => h.div( menu.trigger({ element: h.button }).children(name), menu.positioner(h.div).children( menu.popup(h.div).children( items.map((item) => menu.item({ element: h.div, value: item.value }).children(item.label)) ) ) ) }) ) ) })})
// The bar is a menubar, and each trigger is one of its items.const { html } = await Effect.runPromise(HtmlServer.renderToHtml(Editor, {}))if (!/<div[^>]* role="menubar"/.test(html)) throw new Error("the root isn't a menubar")if (!/<button[^>]* role="menuitem"[^>]*>Edit</.test(html)) throw new Error("the trigger isn't a menubar item")Components
A headless menubar. Call it with the consumer's h, the props, and a
view closure that draws the root with menubar.root(h.div) and a
Menu inside it for each menu.
Details
The parts also return orientation, modal, disabled and
hasSubmenuOpen, whether one of its menus is open, for markup that
depends on them.
The root writes role="menubar", aria-orientation and its data
attributes over what the consumer set on the builder before the part,
and tabindex="0" until it has found its triggers, unless it's disabled.
A trigger in the menubar writes role="menuitem" and tabindex over
the consumer's, besides what a Menu trigger writes.
The menubar shares its state with the menus in its view closure,
which is how a Menu inside reads it. It emits nothing: each menu
reports its own RequestedOpen and RequestedClose.
History restore saves nothing of it: after Back, forward or a reload it
starts from init.
Signature
declare const Menubar: Headless<{ readonly disabled?: boolean; readonly loopFocus?: boolean; readonly modal?: boolean; readonly orientation?: "horizontal" | "vertical";}, never, { disabled: boolean; hasSubmenuOpen: boolean; modal: boolean; orientation: "horizontal" | "vertical"; root: Part<"div", [], never>;}, never, { readonly _tag: "Navigated"; readonly from: string | null; readonly key: string; readonly open: boolean;} | { readonly _tag: "FocusedTrigger"; readonly trigger: string;}, Shared<"Menubar">, { readonly current: string | null; readonly disabled: boolean; readonly hasSubmenuOpen: boolean; readonly modal: boolean; readonly opening: string | null; readonly orientation: "horizontal" | "vertical"; readonly rtl: boolean; readonly tabStop: string | null;}, { readonly open: boolean; readonly trigger: string | null;}, SharedKey<"Menubar", { readonly current: string | null; readonly disabled: boolean; readonly hasSubmenuOpen: boolean; readonly modal: boolean; readonly opening: string | null; readonly orientation: "horizontal" | "vertical"; readonly rtl: boolean; readonly tabStop: string | null;}, { readonly _tag: "Navigated"; readonly from: string | null; readonly key: string; readonly open: boolean;} | { readonly _tag: "FocusedTrigger"; readonly trigger: string;}, { readonly open: boolean; readonly trigger: string | null;}, Shared<"Menubar">>, { readonly current: string | null; readonly highlighted: string | null; readonly menus: readonly Array<{ readonly open: boolean; readonly trigger: string | null; }>; readonly moves: number; readonly opening: string | null; readonly rtl: boolean; readonly tracked: boolean; readonly triggers: readonly Array<string>;}, { readonly _tag: "ChangedMenus"; readonly menus: readonly Array<{ readonly open: boolean; readonly trigger: string | null; }>;} | { readonly _tag: "FoundTriggers"; readonly rtl: boolean; readonly triggers: readonly Array<string>;} | { readonly _tag: "Navigated"; readonly from: string | null; readonly key: string; readonly open: boolean;} | { readonly _tag: "FocusedTrigger"; readonly trigger: string;}, QueryEntries<{}, {}>>Models
The props of a Menubar.
Signature
type MenubarProps = typeof MenubarProps.TypeSchemas
MenubarProps
unstableThe props of a Menubar.
Details
orientation:"horizontal", the default, moves between the triggers with ArrowRight and ArrowLeft, and"vertical"with ArrowDown and ArrowUp.modal: while a menu is open, the page doesn't scroll and a press outside the menubar and the menu only closes the menu. True when left out. A menu drawn in place, withtopLayer: falseon theMenu, is never modal.disabled: every menu in the menubar ignores the user.loopFocus: the arrow keys wrap from the last trigger to the first. True when left out.
Signature
declare const MenubarProps: Struct<{ readonly disabled: optional<Boolean>; readonly loopFocus: optional<Boolean>; readonly modal: optional<Boolean>; readonly orientation: optional<Literals<readonly ["horizontal", "vertical"]>>;}>