Mediquo documentation home

Menu

A dropdown of actions anchored to a button.

mq-menu is a compound primitive modelled on Base UI's Menu: a trigger in slot="trigger" and one mq-menu-item per action. It ships from @mediquo/ui as custom elements and as React components.

Install

// Custom elements — the import registers <mq-menu> and <mq-menu-item>
import "@mediquo/ui/components/menu";
// React
import { Menu, MenuItem } from "@mediquo/ui/react/menu";

<Menu label="Más opciones" side="top" align="end">
  <button slot="trigger">Opciones</button>
  <MenuItem onClick={reschedule}>Modificar cita</MenuItem>
  <MenuItem variant="negative" onClick={cancel}>
    Anular cita
  </MenuItem>
</Menu>;

Positioning

  • The popup renders in the browser's top layer (Popover API), so an ancestor with overflow: hidden or a transform cannot clip it, and no z-index is needed.
  • It opens on side (default bottom) and flips to the other side when only that one has room. align (default start) lines up an edge with the trigger, then the popup shifts so it stays 8px inside the viewport.
  • It follows the trigger on scroll and resize while open.
  • Browsers without the Popover API (before Safari 17 and Chrome 114) fall back to position: fixed: it still stays inside the viewport, but a transformed ancestor can clip it.

Accessibility

It implements the WAI-ARIA menu button pattern.

  • The popup is role="menu" and label is its accessible name; each item is role="menuitem".
  • The trigger gets aria-haspopup="menu" and an aria-expanded that follows open. A native <button> takes the attributes directly; mq-button forwards them to its inner button.
  • Enter, Space and ArrowDown open on the first item, ArrowUp on the last. A mouse or touch click focuses the menu itself, so no item is highlighted until the pointer moves over one or an arrow key is pressed. Arrows move between items and wrap; Home and End jump to the ends.
  • Enter or Space on an item runs it. Running an item or pressing Escape closes the menu and gives focus back to the trigger. Tab closes it and moves on from the trigger.
  • A pointer down outside closes it and leaves focus where it was.
  • The mouse moves focus to the item under it, so the keyboard and the pointer never highlight two items at once.

Motion

Like shadcn's dropdown: it opens with a fade, a zoom from 95% and a short slide away from the trigger, and closes with a fade and zoom back, in 150ms. With prefers-reduced-motion it only fades.

Behaviour worth knowing

  • Listen to click on each item for its action; the menu closes itself afterwards, so there is no open state to reset.
  • onopenchange fires only for user-driven changes. Setting open yourself does not emit it.

Styling

  • ::part(popup) styles the popup (background, border, radius, shadow, min-width).
  • mq-menu-item can be styled directly from the surrounding scope; ::part(item) reaches its padding. variant="negative" uses --mq-feedback-error-strong.