Dropdown menu
A menu of actions or choices that opens from a button, such as the camera or microphone in use.
mq-dropdown-menu is the console's shadcn/Radix DropdownMenu ported to a
custom element: the same look, keyboard, focus handling and open/close
animation. It ships from @mediquo/ui as custom elements and as React
components.
Install
// Custom elements — the import registers <mq-dropdown-menu> and friends
import "@mediquo/ui/components/dropdown-menu";// React
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuRadioGroup,
DropdownMenuRadioItem,
} from "@mediquo/ui/react/dropdown-menu";Usage
The trigger is your own native <button> in the trigger slot; the menu is
mq-dropdown-menu-content, next to it. A device list is a radio group: it
holds the value and reports onvaluechange when the user picks another one.
html`<mq-dropdown-menu>
<button slot="trigger" aria-label=${msg("Cámara")}>…</button>
<mq-dropdown-menu-content side="top">
<mq-dropdown-menu-label>${msg("Cámara")}</mq-dropdown-menu-label>
<mq-dropdown-menu-radio-group
.value=${this.cameraId}
@onvaluechange=${(event: MqDropdownMenuValueChangeEvent) =>
this.useCamera(event.value)}
>
${this.cameras.map(
(camera) => html`<mq-dropdown-menu-radio-item value=${camera.deviceId}>
${camera.label}
</mq-dropdown-menu-radio-item>`,
)}
</mq-dropdown-menu-radio-group>
</mq-dropdown-menu-content>
</mq-dropdown-menu>`;| Element | What it is |
|---|---|
mq-dropdown-menu | Holds open and wires the trigger to the menu. Slot trigger for the button. |
mq-dropdown-menu-content | The menu. side, align, side-offset place it. |
mq-dropdown-menu-item | An action. disabled. |
mq-dropdown-menu-checkbox-item | An on/off option. checked, disabled. |
mq-dropdown-menu-radio-group | One choice among its radio items. value. |
mq-dropdown-menu-radio-item | A choice. value, disabled. Checked when it matches the group's value. |
mq-dropdown-menu-label | A heading for the items after it. Not focusable. |
mq-dropdown-menu-separator | A line between groups of items. |
mq-dropdown-menu-group | Groups items (role="group"). |
| Attribute (property) | On | Default |
|---|---|---|
open | mq-dropdown-menu | false |
side | mq-dropdown-menu-content | bottom |
align | mq-dropdown-menu-content | center |
side-offset (sideOffset) | mq-dropdown-menu-content | 4 (px) |
value | radio group, radio item | "" |
checked | checkbox item | false |
disabled | every item | false |
| Event | From | Payload | Notes |
|---|---|---|---|
onopenchange | mq-dropdown-menu | event.open | The user opened or closed it. |
onselect | every item | — | Chosen by click, Enter or Space. preventDefault() keeps the menu open. |
oncheckedchange | checkbox item | event.checked | Toggled. |
onvaluechange | radio group | event.value | Another radio item was chosen. |
All of them bubble and cross shadow roots. close() on mq-dropdown-menu
closes it as the user would.
Keyboard and focus
- On the trigger,
Enter,SpaceandArrowDownopen the menu with focus on the first item. A mouse press opens it on press, focusing the menu itself so no item is highlighted until the arrows or the pointer move. - In the menu,
ArrowUp/ArrowDownmove without wrapping,Home/End(andPageUp/PageDown) jump to the ends, and typing jumps to the item whose text starts with what was typed; the same letter again cycles through those. Disabled items are skipped. EnterorSpacechooses the focused item and closes the menu;Escapecloses it. Both give focus back to the trigger.Tabcloses it and moves on from the trigger.- Hovering an item highlights it, by focusing it, as in Radix. You can also press the trigger, drag onto an item and release.
- A press outside closes it and still reaches what was pressed.
Accessibility
- The trigger gets
aria-haspopup="menu",aria-expanded, and while openaria-controlswith the menu's id (generated when missing). The menu isrole="menu", named by the trigger (aria-labelledby), and the items aremenuitem,menuitemcheckbox(aria-checked) andmenuitemradio(aria-checked). - Use a native
<button>as the trigger.mq-buttonkeeps its button in its shadow root, where these attributes cannot reach. An icon-only trigger needs anaria-label. - The highlighted item gets the console's tint, and a focus ring when it came from the keyboard: the tint alone is too faint to follow.
- A disabled trigger (
disabledoraria-disabled="true") does not open. - The animation is dropped under
prefers-reduced-motion: reduce.
Positioning and stacking
The menu opens in the browser's top layer through the Popover API
(popover="manual"), and is placed with @floating-ui/dom, the library Radix
uses, with Radix's middleware: offset, shift along the side, flip, then size.
- An
overflow: hiddenancestor (the video stage) cannot clip it, a shadow root cannot contain it, and noz-indexon the page can cover it. It also stacks above an openmq-dialog. - It flips to the opposite side and shifts along its side to stay on screen, follows the trigger as the page scrolls or resizes, and scrolls inside once it is taller than the room left.
- Without the Popover API (browsers before 2024) it falls back to
position: fixedwithz-index: 50.
Styling
The --dropdown-menu-* tokens are exported from @mediquo/ui/tokens (as
components.dropdownMenu) and defined in tokens.css. Colours come from the
--mq-* semantic tokens, or the SDK theme's --color-* where those are not
loaded.
mq-dropdown-menu-content {
--dropdown-menu-min-width: 12.5rem;
--dropdown-menu-radius: 0.5rem;
--dropdown-menu-shadow: 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1);
--dropdown-menu-animation-duration: 150ms;
}Items stay concentric with the card: their radius is --dropdown-menu-radius
minus the card's 0.25rem inline padding.
part is exposed for content (the card, e.g. to cap its width), and on items
for indicator and, on radio items, label.
Behaviour worth knowing
- It is not modal, unlike Radix's default: the page keeps scrolling and stays interactive while it is open, and the rest of the page is not hidden from screen readers.
- Choosing a checkbox or radio item closes the menu, as in Radix; call
preventDefault()ononselectto keep it open. - Sub-menus are not ported.