Select

Select a value from a list of options presented in a dropdown menu, similar to the native HTML select element. This component is based on the WAI-ARIA Combobox Pattern.

Favorite fruit
import {
  Select,
  SelectItem,
  SelectLabel,
  SelectPopover,
  useSelectState,
} from "ariakit/select";
import "./style.css";

export default function Example() {
  const select = useSelectState({
    defaultValue: "Apple",
    sameWidth: true,
    gutter: 4,
  });
  return (
    <div className="wrapper">
      <SelectLabel state={select}>Favorite fruit</SelectLabel>
      <Select state={select} className="select" />
      <SelectPopover state={select} className="popover">
        <SelectItem className="select-item" value="Apple" />
        <SelectItem className="select-item" value="Banana" />
        <SelectItem className="select-item" value="Grape" disabled />
        <SelectItem className="select-item" value="Orange" />
      </SelectPopover>
    </div>
  );
}

Installation

npm install ariakit

Learn more in Getting started.

API

useSelectState()

<SelectLabel />
<Select>
  <SelectArrow />
</Select>
<SelectList|SelectPopover>
  <SelectGroup>
    <SelectGroupLabel />
    <SelectRow>
      <SelectItem>
        <SelectItemCheck />
      </SelectItem>
      <SelectSeparator />
    </SelectRow>
  </SelectGroup>
</SelectList|SelectPopover>

Styling

Styling the active item

When browsing the list with a keyboard (or hovering over items with the mouse when the focusOnHover prop is true), the active item element will have a data-active-item attribute. You can use this attribute to style the active item:

.select-item[data-active-item] {
  background-color: hsl(204 100% 40%);
  color: white;
}

Learn more in Styling.