Select
Picks one value from a list, such as which camera or microphone to use.
mq-select-menu is the console's Radix Select (popper position) ported to a
custom element: the same look, keyboard, focus handling and opening
animation. It ships from @mediquo/ui as custom elements and as React
components.
It is not called mq-select because @mediquo/elements already registers that
tag with a different API, and a page loading both would keep whichever came
first.
Install
// Custom elements — the import registers <mq-select-menu> and <mq-select-item>
import "@mediquo/ui/components/select";// React
import { Select, SelectItem } from "@mediquo/ui/react/select";Usage
The trigger shows the chosen option's text, or the placeholder. The owner
holds value and follows onvaluechange.
html`<mq-select-menu
label=${msg("Cámara")}
placeholder=${msg("Elige una cámara")}
.value=${this.cameraId}
@onvaluechange=${(event: MqSelectValueChangeEvent) =>
this.useCamera(event.value)}
>
${this.cameras.map(
(camera) => html`<mq-select-item value=${camera.deviceId}>
${camera.label}
</mq-select-item>`,
)}
</mq-select-menu>`;| Attribute | On | What it does |
|---|---|---|
value | mq-select-menu | The chosen option's value. Empty shows the placeholder. |
placeholder | mq-select-menu | Shown while nothing is chosen. |
label | mq-select-menu | Accessible name (aria-label). Renders no text. |
disabled | mq-select-menu | Cannot be opened or changed. |
value | mq-select-item | What choosing it sets. |
disabled | mq-select-item | Skipped by the keyboard, ignored by the pointer. |
| Event | Payload | Notes |
|---|---|---|
onvaluechange | event.value | The user chose another option. Not fired for the one already chosen. |
The select is as wide as its host: set a width on mq-select-menu to make it
fill a form column. The list is at least as wide as the trigger.
Keyboard and focus
- On the trigger,
Enter,Space,ArrowDownandArrowUpopen the list with focus on the chosen option, or the first one. A mouse press opens it on press; touch and pen open on release, so scrolling past it does nothing. - Typing on the closed trigger chooses the first option that starts with what was typed, without opening it.
- In the list,
ArrowUp/ArrowDownmove without wrapping,Home/Endjump to the ends, and typing jumps to a matching option; the same letter again cycles.EnterorSpacechooses,Escapecloses, and both give focus back to the trigger.Tabcloses it and moves on from the trigger. - Hovering an option highlights it; a click chooses it. A press outside closes the list.
Accessibility
- The trigger is a
<button role="combobox">witharia-haspopup="listbox",aria-expandedandaria-controls; the list isrole="listbox"with the same name, and the options arerole="option"witharia-selectedon the chosen one andaria-disabledon disabled ones. - Pass
labeltranslated. The trigger lives in a shadow root, where an outer<label for>cannot reach it, solabelis its name. If you show a visible label next to it, pass the same text. - The highlighted option gets the console's tint, and a focus ring when it came from the keyboard.
- The opening animation is dropped under
prefers-reduced-motion: reduce.
Positioning and stacking
The list opens in the browser's top layer through the Popover API and is
placed with @floating-ui/dom and Radix's middleware, so an overflow: hidden
ancestor or a shadow root cannot clip it and nothing on the page can cover it.
It opens below the trigger, flips above when there is no room, follows the
trigger on scroll and resize, and scrolls inside when it is taller than the
room left. Without the Popover API it falls back to position: fixed with
z-index: 50.
Styling
The --select-* tokens are exported from @mediquo/ui/tokens (as
components.select) and defined in tokens.css. Colours come from the
--mq-* semantic tokens, or the SDK theme's --color-* where those are not
loaded.
mq-select-menu {
--select-height: 2.5rem;
--select-radius: 0.5rem;
--select-min-width: 9rem;
--select-shadow: 0 4px 6px -1px rgb(0 0 0 / 0.1), 0 2px 4px -2px rgb(0 0 0 / 0.1);
--select-animation-duration: 150ms;
}Options stay concentric with the list: their radius is --select-radius minus
the list's 0.25rem padding.
--select-border sets the trigger's border colour alone, for a theme whose
form fields have a stronger border than its popups. It defaults to the
--mq-border-default chain the list uses.
part is exposed for trigger, value, chevron and listbox, and on
mq-select-item for indicator.
Behaviour worth knowing
- As in Radix, the list animates in but closes at once.
- It is not modal, unlike Radix: the page keeps scrolling while it is open.
- Radix's scroll buttons at the ends of a long list are not ported; the list scrolls natively instead.
- It does not take part in forms: there is no
name.