Menu
Glass panels that grow out of their source. Press and slide straight onto a row to choose it, or tap to open and tap again. Also the context menu and the menu-style picker.
import { ContextMenu, Menu, MenuButton, MenuDivider, MenuPicker, MenuSection, MenuToggle, Submenu } from "@adamexu/glass";Menu
A press on the source swells it and, lifted or held, opens the menu about 270 ms in (sooner on an iPad). The source goes round and the panel grows out of it, opening upward when there is no room below. A finger still down can slide on to a row and lift there to choose. The demos sit in a PresentationRoot over their phone frame (see Alert).
const [pinned, setPinned] = useState(true);
<Menu label="Options">
<MenuControlGroup size="medium">
<MenuButton title="Copy" icon={<Copy size={20} />} action={() => setLast("Copy")} />
<MenuButton title="Share" icon={<Share size={20} />} action={() => setLast("Share")} />
<MenuButton title="Edit" icon={<Pencil size={20} />} action={() => setLast("Edit")} />
</MenuControlGroup>
<MenuButton title="Rename" icon={<Pencil size={20} />} action={() => setLast("Rename")} />
<MenuButton title="Subtitled" subtitle="With a second line" icon={<Star size={20} />} action={() => setLast("Subtitled")} />
<MenuToggle title="Pinned" icon={<Heart size={20} />} isOn={pinned} onChange={setPinned} />
<MenuSection title="Sort">
<MenuButton title="By Name" action={() => setLast("By Name")} />
<MenuButton title="By Date" disabled />
</MenuSection>
<Submenu title="More" icon={<Ellipsis size={20} />}>
<MenuButton title="One" icon={<Star size={20} />} action={() => setLast("One")} />
<MenuButton title="Two" action={() => setLast("Two")} />
</Submenu>
<MenuButton title="Delete" role="destructive" icon={<Trash2 size={20} />} action={() => setLast("Delete")} />
</Menu>The items must be these components (or fragments of them): the menu reads their props and does not render your own wrapper components. The same tree can be passed as data with items (MenuNode[]). A submenu opens over its parent on a phone, with its title row going back, and beside it on an iPad; resting on its row half a second opens it too.
In a list, and MenuPicker
buttonStyle="none" draws only the label, as a plain row button in research's demo app. MenuPicker shows the value and an up-down chevron in the accent colour; its menu checks the selection.
const flavors = [
{ value: "Vanilla", title: "Vanilla" },
{ value: "Chocolate", title: "Chocolate" },
{ value: "Strawberry", title: "Strawberry" },
];
const [flavor, setFlavor] = useState("Chocolate");
<NavigationStack
root={
<NavigationPage title="Controls">
<List>
<Section header="Menu">
<Menu buttonStyle="none" label={<span style={{ color: "var(--list-accent)" }}>Options</span>}>
<MenuButton title="Copy" icon={<Copy size={20} />} action={() => setLast("Copy")} />
<MenuButton title="Share" icon={<Share size={20} />} action={() => setLast("Share")} />
<MenuDivider />
<MenuButton title="Delete" role="destructive" icon={<Trash2 size={20} />} action={() => setLast("Delete")} />
</Menu>
<LabeledContent title="Flavor">
<MenuPicker options={flavors} selection={flavor} onChange={setFlavor} aria-label="Flavor" />
</LabeledContent>
</Section>
</List>
</NavigationPage>
}
/>ContextMenu
A long press lifts the content: it creeps up under the finger, and at 0.7 s the page dims and the panel grows under it (above it on an iPad). Lifted or moved before then, nothing happens. The finger can go on to a row and lift there.
<ContextMenu
menu={
<>
<MenuButton title="Copy" icon={<Copy size={20} />} action={() => setLast("Copy")} />
<MenuButton title="Share" icon={<Share size={20} />} action={() => setLast("Share")} />
<MenuButton title="Favorite" icon={<Heart size={20} />} action={() => setLast("Favorite")} />
</>
}
>
<div style={{ width: 160, height: 44, borderRadius: 16, background: "rgb(230, 77, 77)", color: "#fff", display: "grid", placeItems: "center" }}>
Hold me
</div>
</ContextMenu>Props
Menu
| Prop | Type | Default | Description |
|---|---|---|---|
| label | ReactNode | - | The source's label. A string also names the menu. |
| children | ReactNode | - | The items: MenuButton, MenuToggle, MenuDivider, MenuSection, Submenu, MenuControlGroup. |
| items | MenuNode[] | - | The items as data, instead of children. |
| buttonStyle | ButtonStyleName | "none" | "glass" | The source Button's style; none draws only the label. |
| tint, icon | Button props | - | The source Button's tint and icon. |
| order | "automatic" | "fixed" | "priority" | "automatic" | automatic: a menu that opens upward lists from the bottom. |
| align | "center" | "trailing" | "center" | How the panel lines up with the source. |
| disabled | boolean | false | The source doesn't open it. |
| renderSource | (props: SourceProps) => ReactNode | - | Draw your own source: spread the handlers, ref and aria attributes on it. |
| defaultOpen | boolean | false | Open as soon as it is ready. |
| aria-label | string | - | The menu's label; a string label is used otherwise. |
| className, style | string, CSSProperties | - | On the source's box. |
MenuButton
| Prop | Type | Default | Description |
|---|---|---|---|
| title | string | - | Required. The row's title. |
| action | () => void | - | Runs when it is chosen. |
| icon | ReactNode | - | The row's icon, or the cell's in a MenuControlGroup. |
| subtitle | string | - | A second line. |
| role | "destructive" | "cancel" | - | destructive draws it red. |
| disabled | boolean | - | Dimmed; can't be chosen. |
| keepOpen | boolean | - | Choosing it leaves the menu open. |
MenuToggle
| Prop | Type | Default | Description |
|---|---|---|---|
| isOn | boolean | - | Required. Draws a check when on. |
| onChange | (on: boolean) => void | - | Runs with the new state when it is chosen. |
| title, icon, subtitle, disabled, keepOpen | - | As MenuButton. |
MenuSection, MenuDivider
| Prop | Type | Default | Description |
|---|---|---|---|
| title | string | - | MenuSection: a header over its items. |
| children | ReactNode | - | MenuSection: its items. MenuDivider takes no props: it draws a divider. |
Submenu
| Prop | Type | Default | Description |
|---|---|---|---|
| title | string | - | Required. The row's title and the submenu's title. |
| icon | ReactNode | - | The row's icon. |
| disabled | boolean | - | Can't be opened. |
| order | "automatic" | "fixed" | "priority" | - | As Menu. |
| children | ReactNode | - | Its items. |
MenuControlGroup
| Prop | Type | Default | Description |
|---|---|---|---|
| size | "small" | "medium" | "medium" | A row of cells: medium holds 3 (icon over title), small 4 (icons). |
| children | ReactNode | - | MenuButton elements. |
ContextMenu
| Prop | Type | Default | Description |
|---|---|---|---|
| children | ReactNode | - | Required. What a long press lifts. |
| menu | ReactNode | - | The items, as in Menu. |
| items | MenuNode[] | - | The items as data, instead of menu. |
| preview | ReactNode | - | Shown 240 x 160 in place of the lifted content. |
| disabled | boolean | false | Doesn't open. |
| aria-label | string | - | The menu's label. |
| className, style | string, CSSProperties | - | On the content's box. |
MenuPicker
| Prop | Type | Default | Description |
|---|---|---|---|
| options | { value: T; title: string; icon?: ReactNode; disabled?: boolean }[] | - | Required. The choices. |
| selection | T | - | Required. The selected value. |
| onChange | (value: T) => void | - | Runs with the chosen value. |
| tint | string | accent blue | The value and chevron's colour. |
| order | "automatic" | "fixed" | "priority" | "fixed" | As Menu. |
| disabled | boolean | false | Doesn't open. |
| aria-label | string | - | Names the picker. |
MenuAnchor
| Prop | Type | Default | Description |
|---|---|---|---|
| items | MenuNode[] | - | Required. The items. |
| children | (source: SourceProps) => ReactNode | - | Required. Your source: spread the handlers, ref and aria attributes on it. |
| order, align, disabled, aria-label | - | As Menu. | |
| anchor | RefObject<HTMLElement | null> | - | What the panel is placed against and grows from, when larger than the source. |
| openAfter | number | - | Touch-down to opening, in ms. |
| onMorph | () => void | - | Runs as it sets off. |
| keepFocus | boolean | - | Focus stays where it is (a text field's, with its selection). |
| fadeOut | boolean | - | Goes by fading where it is, not back into its source. |
Keyboard and accessibility
Enter, Space or ArrowDown on the source open the menu. ArrowUp, ArrowDown, Home and End walk the rows, letters find one, Enter or Space chooses, and Escape or Tab closes it, returning focus to the source. The panel is a role="menu"; rows are menuitem or menuitemcheckbox with aria-checked; the source has aria-haspopup="menu" and aria-expanded.
Menus portal to the root of their PresentationRoot, or document.body, and are placed in its window. The glass targets Chromium.
SwiftUI
| SwiftUI | @adamexu/glass |
|---|---|
| Menu("Options") { Button(...) } | <Menu label="Options"><MenuButton title action> |
| Menu { } label: { } / .menuOrder(.fixed) | <Menu renderSource={...}> / order="fixed" |
| Button(title, role: .destructive) { } in a menu | <MenuButton title role="destructive" action> |
| Toggle(title, isOn:) in a menu | <MenuToggle title isOn onChange> |
| Divider() / Section("Title") in a menu | <MenuDivider /> / <MenuSection title="Title"> |
| Menu("More") inside a menu | <Submenu title="More"> |
| ControlGroup { }.controlGroupStyle(.compactMenu / .menu) | <MenuControlGroup size="small" / "medium"> |
| .contextMenu { } preview: { } | <ContextMenu menu={...} preview={...}> |
| Picker(selection:) { }.pickerStyle(.menu) | <MenuPicker options selection onChange> |
| .buttonStyle(.plain) on a Menu in a List | <Menu buttonStyle="none"> |