Select
Single selects get a chevron drawn from --icon-chevron. Multiple
and sized selects keep the native list, with no icon.
| Class | Does what |
|---|---|
select | Full-width select with a chevron |
select-error | Error color, for when aria-invalid is not available |
Basic
<label class="label">
Environment
<select class="select">
<option>Development</option>
<option>Staging</option>
<option>Production</option>
</select>
</label>
<label>
Environment
<select>
<option>Development</option>
<option>Staging</option>
<option>Production</option>
</select>
</label>
Multiple
<select class="select" multiple size="3">
<option>One</option>
<option>Two</option>
<option>Three</option>
</select>
<select multiple size="3">
<option>One</option>
<option>Two</option>
<option>Three</option>
</select>
With Kobalte
alpha The class names and states here are stable. The look is not: these styles can change in a minor release, without a major version bump.
The native <select> above is settled. What follows is not: the
listbox styling and .menu-selected are new.
Kobalte's Select
is a button plus a listbox, not a native <select>. The Kobalte
layer gives the trigger the field styling above and styles the listbox and its
options. Select.Content, the panel around the listbox, has no role
of its own, so it takes .dropdown-content by hand.
<select class="select">
<option>Development</option>
<option>Production</option>
</select>
<select class="select">
<option>Development</option>
<option>Production</option>
</select>
import { Select } from "@kobalte/core/select";
<Select
options={["Development", "Staging", "Production"]}
value={env()}
onChange={setEnv}
itemComponent={props => (
<Select.Item item={props.item}>
<Select.ItemLabel>{props.item.rawValue}</Select.ItemLabel>
</Select.Item>
)}
>
<Select.Trigger>
<Select.Value>{state => state.selectedOption()}</Select.Value>
</Select.Trigger>
<Select.Portal>
<Select.Content class="dropdown-content">
<Select.Listbox />
</Select.Content>
</Select.Portal>
</Select>
<div>
<button aria-haspopup="listbox" aria-expanded="true" data-expanded style="margin-bottom: 0.25rem">
<span>Production</span>
</button>
<div class="dropdown-content" data-expanded>
<ul role="listbox">
<li role="option">Development</li>
<li role="option" data-highlighted>Staging</li>
<li role="option" data-selected aria-selected="true">Production</li>
</ul>
</div>
</div>
Leave out Select.Icon: the field draws its own chevron, the same
one the native select gets.
On the class-based build
Put the classes on the parts yourself. The rows follow Kobalte's state without
any extra classes: a highlighted row matches data-highlighted and a
selected one aria-selected, which is also what Radix and Ark set.
For a compact trigger, such as a version switcher, use a button and keep
Select.Icon; for a form field, use .select and leave
it out, since the field draws its own chevron.
<Select
options={versions()}
optionValue="id"
optionTextValue="label"
value={current()}
onChange={setCurrent}
itemComponent={props => (
<Select.Item item={props.item} class="menu-item">
<Select.ItemLabel>{props.item.rawValue.label}</Select.ItemLabel>
</Select.Item>
)}
>
<Select.Trigger class="btn btn-secondary" aria-label="Version">
<Select.Value>{state => state.selectedOption().label}</Select.Value>
<Select.Icon>▾</Select.Icon>
</Select.Trigger>
<Select.Portal>
<Select.Content class="dropdown-content">
<Select.Listbox class="menu" />
</Select.Content>
</Select.Portal>
</Select>
Disabled
<select class="select" disabled>
<option>Production</option>
</select>
<select class="select" disabled>
<option>Production</option>
</select>
<Select disabled options={envs} value={env()} onChange={setEnv} itemComponent={Item}>
<Select.Trigger>
<Select.Value>{state => state.selectedOption()}</Select.Value>
</Select.Trigger>
</Select>
<button aria-haspopup="listbox" aria-expanded="false" disabled data-disabled>
<span>Production</span>
</button>
The trigger is a form field, so it carries the same bottom margin as an
.input. That margin is overridden in this demo because the
listbox sits inline here; in an app the content is portaled and positioned by
Kobalte's popper, which sets the gap with its gutter prop.
data-selected marks the chosen option and data-highlighted
the one under the pointer or caret, so they get different treatments:
.menu-selected and .menu-active.
Composition
Common ways to put this together with other components. The general rules are on Composition.
Sort beside search
A search field that takes the width, and a sort select beside it.
<div class="field-row" style="flex-wrap: wrap">
<label class="label" style="flex: 1 1 16rem">
<p>Search</p>
<input class="input" type="search" />
</label>
<label class="label" style="flex: 0 1 12rem">
<p>Sort</p>
<select class="select">
<option>Newest</option>
<option>Oldest</option>
<option>A to Z</option>
</select>
</label>
</div>
- Layout: both are labels in one wrapping row, so their label text and fields line up without extra rules. The search grows from
16remand the select stays near12rem; on a phone the select wraps under the search at the left edge..flipis the wrong tool here: in a column, a flex basis becomes a height. - Controls: a select is full width by default, so its width comes from its label in the row.