Skip to content

MenuList ​

MenuList draws a menu from its model: the items, the dividers, the headings and the submenus, described as data.

When to use ​

Use it wherever a menu is described once and shown in more than one place: in a Dropdown with menu, in a Menu inside an element with role="menu", or through the parts and components that take the same model: AppMenu, MenuSheet, Kebab (items), Topbar (menu) and LayerTree (addMenu). For a few fixed actions, write MenuItems directly.

The model ​

A menu is an array of MenuModel, each entry one of three shapes.

ts
import type { MenuModel } from '@sakuzu/kata/svelte';

const menu: MenuModel[] = [
  { id: 'new', label: 'New drawing', kbd: '⌘N' },
  { id: 'export', label: 'Export', items: [{ id: 'png', label: 'PNG' }] },
  { divider: true },
  { heading: 'Show' },
  { id: 'grid', label: 'Grid', checked: true },
];
FieldDescription
idReported when the item is chosen
labelThe name of the item
kbdA key hint at the right end
iconAn icon on the left (a name or a component)
disabledShown but not chosen
checkedA check mark; the items of its level keep the column of marks
dangerA destructive action: red text
hrefA link: the item goes there instead of reporting its id
itemsThe entries of a submenu

{ divider: true } is a MenuDivider and { heading } a MenuHead. isMenuItem tells an item from the other two.

Props ​

PropDefaultDescription
itemsrequiredThe model, MenuModel[]
onselectCalled with the id of the chosen item (not for a link)
oncloseCalled after an item was chosen, to close the menu
inlinefalseSubmenus take the place of the list at every width

level, backLabel and onexit are set by MenuList on its own submenus; the application leaves them out.

Contract ​

Each entry is a MenuItem, a MenuDivider or a MenuHead, so the menu has their heights and their distances. An item with items shows the chevron of a submenu; hovering or pressing it opens the submenu next to its row, in a Menu of its own that is fixed to the window, opens on the left when there is no room on the right and moves up inside the window. Below the narrow width (48rem), and always with inline, the submenu takes the place of the list instead, under a row with an arrow and the name of the submenu that goes back. The up and down arrows, Home and End move the focus between the items of the level shown and wrap at the ends; the right arrow opens a submenu and moves into it, the left arrow goes back to its row. Choosing an item reports its id and calls onclose; a disabled item does nothing.

Example ​

MenuList

Released under the Apache-2.0 license.