Mediquo documentation home

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>`;
ElementWhat it is
mq-dropdown-menuHolds open and wires the trigger to the menu. Slot trigger for the button.
mq-dropdown-menu-contentThe menu. side, align, side-offset place it.
mq-dropdown-menu-itemAn action. disabled.
mq-dropdown-menu-checkbox-itemAn on/off option. checked, disabled.
mq-dropdown-menu-radio-groupOne choice among its radio items. value.
mq-dropdown-menu-radio-itemA choice. value, disabled. Checked when it matches the group's value.
mq-dropdown-menu-labelA heading for the items after it. Not focusable.
mq-dropdown-menu-separatorA line between groups of items.
mq-dropdown-menu-groupGroups items (role="group").
Attribute (property)OnDefault
openmq-dropdown-menufalse
sidemq-dropdown-menu-contentbottom
alignmq-dropdown-menu-contentcenter
side-offset (sideOffset)mq-dropdown-menu-content4 (px)
valueradio group, radio item""
checkedcheckbox itemfalse
disabledevery itemfalse
EventFromPayloadNotes
onopenchangemq-dropdown-menuevent.openThe user opened or closed it.
onselectevery item—Chosen by click, Enter or Space. preventDefault() keeps the menu open.
oncheckedchangecheckbox itemevent.checkedToggled.
onvaluechangeradio groupevent.valueAnother 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, Space and ArrowDown open 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/ArrowDown move without wrapping, Home/End (and PageUp/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.
  • Enter or Space chooses the focused item and closes the menu; Escape closes it. Both give focus back to the trigger. Tab closes 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 open aria-controls with the menu's id (generated when missing). The menu is role="menu", named by the trigger (aria-labelledby), and the items are menuitem, menuitemcheckbox (aria-checked) and menuitemradio (aria-checked).
  • Use a native <button> as the trigger. mq-button keeps its button in its shadow root, where these attributes cannot reach. An icon-only trigger needs an aria-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 (disabled or aria-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: hidden ancestor (the video stage) cannot clip it, a shadow root cannot contain it, and no z-index on the page can cover it. It also stacks above an open mq-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: fixed with z-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() on onselect to keep it open.
  • Sub-menus are not ported.