Menu

ds-menu is the engine that Select and Context are built on. It draws a column of entries, one square each, moves through them with the keyboard, places itself by an anchor and closes when it should. Use Select for a menu from a button or a field, and Context for a menu where the pointer is. Reach for ds-menu itself for a list of choices under a field, or when a product decides for itself what a choice does.

Actions for a recording

A Select's actions pair real icons and short labels on one-square rows.

A menu in place

The engine alone, standing on the page. Press an entry, then use the arrows, Home and End; a disabled entry is skipped.

A menu from a button

Press Actions. It stands half a square from its button. The engine leaves closing on a choice to its owner, so Escape or a press outside closes it here; Select closes on a choice by itself.

<ds-button id="recording-actions" secondary>Actions</ds-button><ds-menu floating for="recording-actions" role="menu" aria-label="Recording actions">  <ds-button menu-item role="menuitem">Download</ds-button>  <ds-button menu-item role="menuitem">Duplicate</ds-button>  <ds-button menu-item role="menuitem">Archive</ds-button></ds-menu>

An entry is a shared ds-button or ds-file-picker, a native button or a, or any element with the role option, menuitem, menuitemradio or menuitemcheckbox. Keyboard movement reads only those roles, so give every entry one. The menu is a listbox unless its role says otherwise; use menu for actions.

Every entry is one square high, whatever button styles surround it. Between the entries there is nothing. A frame of half a square stands round them, its 1px line inside that half square, so n entries are n + 1 squares high. There is no shadow. A menu in place is at least eight squares wide and at most sixteen high, and scrolls past that.

MarkOnWhat it does
data-ds-menu-separatorAn hrA line, one square with the line.
data-ds-menu-labelWordsA muted label on one square.
data-text-valueAn entryThe words typing searches, in place of its own.
data-ds-menu-rowAn entry in a gridTakes the grid’s whole width, as an action.
data-ds-menu-startAn entry in a gridStarts a new section at the first column.

compact draws filled rows with no frame, 19px with the 1px between them, a square’s pitch. columns draws a small grid of one-square cells, for a palette.

floating puts the menu in the browser’s popover layer, over the page. open shows it. Without floating the menu stands in the page and open means nothing.

A menu opens from the button for names, an id in the same document or shadow root. trigger sets the element from a script and wins when both are set. The opener gets aria-haspopup, aria-expanded and, when the menu has an id, aria-controls, and gets its own values back when the opener changes.

trigger-mode says how it opens.

ModeHow it opens
clickA press on the trigger, or Arrow Down and Arrow Up on it. The default.
contextA right click, the Context Menu key or Shift+F10, or a touch held for half a second, at that point. context-selector lets one container serve many rows.
submenuBeside its row: a press, hover, the forward arrow, Enter or Space.
manualOnly from a script, with open or openAt().

hover opens a menu while a mouse or pen rests on the trigger, and closes it 180ms after the pointer leaves, so the gap can be crossed, unless focus is inside it. A trigger that is a link then follows its link when pressed, and the arrow keys still open the menu. Touch does not hover.

A menu stands half a square from its anchor, by default below it. placement asks for top, right or left. It flips when its side lacks the room and the other has more, and it stays on the visible screen. Its height and the room it takes round down to whole squares. Its width is --ds-menu-width, or else the anchor’s width rounded up to whole squares and at least four. A context, submenu or manual menu is eight squares wide. data-ds-hug makes a menu as wide as its words instead, rounded up to whole squares.

Escape closes the top menu only. A press or focus outside closes it, Tab closes the whole branch, and in a nested menu the back arrow closes that level. Closing a menu closes the menus inside it.

The engine does not close on a choice: that is Select’s and Context’s rule, and the reason this page’s example stays open. Call select() from a choice’s own handler to close the whole branch the same way.

Focus returns to the opener after Escape, a choice or the back arrow. After a press outside or Tab it stays where the reader moved it.

Call dismissMenusForNavigation() before a route swap. It closes every floating menu, and a hover menu that appears under a resting pointer stays closed until the pointer has left it. Click and keyboard opening work at once. A menu reconnected from a page cache comes back closed.

KeyWhat it does
Arrow Down, Arrow UpMove between entries, skipping disabled and hidden ones and wrapping at the ends. On the trigger they open the menu, at the checked entry or else the first or the last.
Arrow Left, Arrow RightIn a grid, move between cells. With aria-orientation="horizontal", in the reading direction.
Home, EndGo to the first or the last enabled entry.
A letterFinds an entry by its first letters; repeating one letter cycles through them.
Enter, SpacePress the entry, as any button.
Escape, TabClose the menu.

Keys typed into a field inside the menu stay with the field. In a grid, Arrow Down and Arrow Up keep their column across short rows and full-width actions.

Set controller to a field and focus stays there. The arrows send ds-menu-active with the next entry’s id, and unless that is cancelled the menu makes it current in active-id. Enter presses the active entry, and Home, End and modified keys stay with the field. A press on an entry does not take focus from the field. Token field is built this way.

Give the menu an aria-label unless a visible label names it. Hover opening never moves focus; every other opening moves it into the menu, unless a controller holds it. In a role="menu" a mouse moving over an entry focuses it. Set the focusOnOpen property to false to show a menu open without taking the reader’s focus, as an example beside the page does. Disabled entries stay in the list and the arrows skip them. In a floating menu one entry holds the Tab stop at a time.

AttributeTypeDefaultWhat it does
floatingbooleanfalseDraws it in the popover layer.
openbooleanfalseShows a floating menu.
forstringEmptyThe id of the button that opens it.
trigger-modestringclickclick, context, submenu or manual.
context-selectorstringEmptyThe rows a context trigger serves.
hoverbooleanfalseOpens under a resting pointer.
placementstringbottomtop, bottom, right or left.
compactbooleanfalseFilled rows with no frame.
columnsnumber1Grid columns; more than one makes a grid.
disabledbooleanfalseStops new requests to open, without closing one already shown.
active-idstringEmptyThe current entry when a controller is set.

These are properties, not attributes: trigger (the opening element), anchor (an element or a rectangle to stand by, in place of the trigger), controller (the field that keeps focus) and focusOnOpen, true by default.

EventDetailWhat it means
ds-menu-toggleopen, trigger, pointA trigger asked to open or close it. Cancel to decline.
ds-menu-closereasonThe menu asked to close: escape, outside, select, back, parent or navigation. Cancel to keep it open.
ds-menu-close-focusnoneFocus is about to return. Cancel to place it yourself.
ds-menu-activeidThe arrows asked for this entry. Cancel to decline.

All four bubble and cross shadow roots.

MethodWhat it does
openAt(point, control, options)Opens at a point on the screen, for control. options.focus false leaves focus where it is.
requestClose(reason)Asks to close, as the keys do, sending ds-menu-close.
select()Closes the whole branch with the reason select.
place()Places it again.
renderNow()Draws a newly inserted menu before focus moves into it.
TokenWhat it does
--ds-menu-widthThe width of a floating menu, in place of its anchor’s.
--ds-menu-max-heightThe tallest it grows, sixteen squares by default.

The element sets --ds-menu-anchor-width, --ds-menu-available-height and --ds-menu-columns itself.

Copy menu through Sync 1. Select and Context bring it with them. On Apple platforms it is DS1Menu with DS1MenuItem, and a floating menu is the system’s own.

Select · Context · Token field