Menhir

Select

View as Markdown

A button that shows one value out of a list and opens the list to change it: the WAI-ARIA select-only combobox with a listbox. A form control: a visually hidden input beside the Trigger submits the value under name, blocks a submission while required and empty, follows a form reset, and takes a value the browser fills in. The Popup is a popover="auto" element, so the browser puts it in the top layer, above every z-index, and closes it on Escape or a press outside; there is no portal. Focus stays on the Trigger, which names the active item in aria-activedescendant: the arrow keys, Home, End, Page Up and Page Down move through the items, and typing moves to the next item that starts with what was typed, on a closed Select too, which opens. Enter, Space, Tab or a click selects an item and closes the list; Escape closes it and keeps the value. The active item has data-active and the selected one data-selected. Menhir places the Popup with Floating UI, below the Trigger and aligned to its start, flipping it when it would leave the viewport, and keeps it shown until its exit animation has ended. It writes --anchor-width and --available-height on the Popup: use them for its min-width and max-height. Name the Select with a <label> for the Trigger.

Size
11.8 kB

Example

Styling
Apple
Banana
Cherry
Citrus
Lemon
Orange
SelectExample.tsx
import { Select } from "menhir/select";

const item =
  "flex cursor-default items-center gap-2 rounded px-2 py-1.5 select-none data-active:bg-stone-100 data-disabled:opacity-50 dark:data-active:bg-stone-800";
const indicator = "w-3 data-unselected:invisible";

export default function SelectExample() {
  return (
    <div class="flex flex-col gap-1.5 text-sm">
      <label for="select-fruit-tailwind">Fruit</label>
      <Select.Root name="fruit" defaultValue="apple">
        <Select.Trigger
          id="select-fruit-tailwind"
          class="flex min-w-40 cursor-pointer items-center justify-between gap-2 rounded-md border border-stone-300 px-3 py-1.5 after:content-['▾'] focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-stone-500 dark:border-stone-600"
        >
          <Select.Value
            class="data-placeholder:opacity-60"
            placeholder="Pick a fruit"
          />
        </Select.Trigger>
        <Select.Popup
          sideOffset={4}
          class="m-0 max-h-(--available-height) min-w-(--anchor-width) origin-(--transform-origin) overflow-y-auto rounded-lg border border-stone-200 bg-white p-1 text-sm text-inherit shadow-lg transition-[opacity,scale] duration-100 data-closed:scale-95 data-closed:opacity-0 dark:border-stone-700 dark:bg-stone-900 starting:data-open:scale-95 starting:data-open:opacity-0"
        >
          <Select.Item class={item} value="apple">
            <Select.ItemIndicator class={indicator}>✓</Select.ItemIndicator>
            Apple
          </Select.Item>
          <Select.Item class={item} value="banana">
            <Select.ItemIndicator class={indicator}>✓</Select.ItemIndicator>
            Banana
          </Select.Item>
          <Select.Item class={item} value="cherry" disabled>
            <Select.ItemIndicator class={indicator}>✓</Select.ItemIndicator>
            Cherry
          </Select.Item>
          <Select.Group>
            <Select.GroupLabel class="px-2 pt-1.5 pb-1 pl-7 text-xs opacity-70">
              Citrus
            </Select.GroupLabel>
            <Select.Item class={item} value="lemon">
              <Select.ItemIndicator class={indicator}>✓</Select.ItemIndicator>
              Lemon
            </Select.Item>
            <Select.Item class={item} value="orange">
              <Select.ItemIndicator class={indicator}>✓</Select.ItemIndicator>
              Orange
            </Select.Item>
          </Select.Group>
        </Select.Popup>
      </Select.Root>
    </div>
  );
}

Anatomy

<label for="fruit" />
<Select.Root>
  <Select.Trigger id="fruit">
    <Select.Value />
  </Select.Trigger>
  <Select.Popup>
    <Select.Item>
      <Select.ItemIndicator />
    </Select.Item>
    <Select.Group>
      <Select.GroupLabel />
      <Select.Item />
    </Select.Group>
  </Select.Popup>
</Select.Root>

API reference

Root

Holds the value and the open state, and renders no element. The Trigger and the Popup have data-open or data-closed, data-disabled and data-required.

PropTypeDefaultDescription
valuestring | null—The selected item's value, when controlled; null selects none. Pair it with onValueChange.
defaultValuestring | nullnullThe selected item's value at first, when uncontrolled. A form reset goes back to it.
onValueChange(value: string | null) => void—Called with the new value when the user selects an item, the browser fills one in, or the form is reset. A controlled Select changes only if value does.
openboolean—Whether the list is open, when controlled. Pair it with onOpenChange.
defaultOpenbooleanfalseWhether the list is open at first, when uncontrolled.
onOpenChange(open: boolean, details: { reason, event }) => void—Called with the new value when the user opens or closes the list, and why: "trigger-press" (a press or a key on the Trigger), "item-press" (an item was selected), "escape-key", "outside-press", "focus-out" (Tab) or "close-request" (such as another popover opening). A controlled Select changes only if open does.
namestring—The name the value is submitted under, with the form.
formstring—The id of the form the Select belongs to, when it is not inside it.
requiredboolean—Whether the form can be submitted only once an item is selected. The Trigger has aria-required.
disabledboolean—Whether the Select ignores the user: the Trigger is disabled, and the value is not submitted.
autocompletestring—What the browser may fill in, such as "country". A filled value selects the item with that value, else the one with that label.

Trigger

<button type="button" role="combobox" aria-haspopup="listbox"> that shows the value and opens the list, with aria-expanded, with popovertarget and aria-controls naming the Popup, and with aria-activedescendant naming the active item. It keeps focus while the list is open. A combobox takes no name from its content: name it with a <label for>, a <label> around it, aria-label or aria-labelledby. The hidden input follows it. Its render function must render a <button>.

PropTypeDefaultDescription
render(props) => JSX.Element—Renders the trigger with your own button component. Read once.

Value

<span> with the selected item's label: its label prop, else its text. It has data-placeholder while no item is selected. The server cannot read a label from the items, which come after it, so it renders the value itself until the items have mounted; give it a function when your labels differ from your values.

PropTypeDefaultDescription
placeholderJSX.Element—What it shows while no item is selected.
children(value: string) => JSX.Element—The label of a value, shown instead of the selected item's own, on the server too.
render(props) => JSX.Element—Renders the value with your own component. Read once.

Popup

<div popover="auto" role="listbox">, placed against the Trigger and labelled by it. It takes no focus. Its id is generated, for the Trigger to name. It has the CSS variables --anchor-width, the Trigger's width, and --available-height, the space left in the viewport. Render it right after the Trigger.

PropTypeDefaultDescription
side"top" | "right" | "bottom" | "left""bottom"The side of the Trigger it is placed against. It moves to the other side when it does not fit, and data-side says where it went.
align"start" | "center" | "end""start"Where along that side it sits. start and end follow the writing direction.
sideOffsetnumber0The gap between the Trigger and the Popup, in pixels.
alignOffsetnumber0The shift along the Trigger's side, in pixels.
collisionPaddingnumber5The space it keeps from the viewport's edges, in pixels.
anchorElement | VirtualElement | (() => Element | VirtualElement | undefined)—What it is placed against instead of the Trigger: an element, a virtual element with a getBoundingClientRect(), or a function returning one, for an element you hold in a ref.
render(props) => JSX.Element—Renders the popup with your own component. Read once.

Item

<div role="option"> with aria-selected and data-selected or data-unselected. A click selects it and closes the list. Takes your id, else a generated one.

PropTypeDefaultDescription
valuestring—The Select's value while this item is selected. Required, and not an empty string.
disabledboolean—Whether it is disabled: the keys and the pointer still reach it, but it is not selected.
labelstring—Its text, when its content is not its label, such as an icon beside it: what typing matches and the Value shows.
render(props) => JSX.Element—Renders the item with your own component. Read once.

ItemIndicator

aria-hidden <span> with its item's data-selected or data-unselected, always rendered: show a check mark by state with CSS.

PropTypeDefaultDescription
render(props) => JSX.Element—Renders the indicator with your own component. Read once.

Group

<div role="group"> for items that belong together, named by its GroupLabel.

PropTypeDefaultDescription
render(props) => JSX.Element—Renders the group with your own component. Read once.

GroupLabel

<div> that names its Group. The keys do not reach it. Takes your id, else a generated one.

PropTypeDefaultDescription
render(props) => JSX.Element—Renders the label with your own component. Read once.

Keyboard

KeysAction
Enter, SpaceClosed, opens the list on the selected item, else the first. Open, selects the active item and closes the list.
Down ArrowClosed, opens the list on the selected item, else the first; with Alt too. Open, moves to the next item, and stops at the last.
Up ArrowClosed, opens the list on the first item. Open, moves to the previous item, and stops at the first; with Alt, selects the active item and closes the list.
Home, EndMoves to the first or last item, and opens the list if it is closed.
Page Up, Page DownMoves ten items up or down.
A characterMoves to the next item whose label starts with it, and opens the list if it is closed, without changing the value; characters typed within a second make one string.
EscapeCloses the list and keeps the value.
Tab, Shift + TabOpen, selects the active item, closes the list and moves focus on.