FormsSelect
Searchastronomic.dev# Select
For one choice from a short list in a form, with the kit's control look.
Static: no JavaScript.
## Usage
```astro
import Select from "@astronomic-dev/ui/components/Select.astro";
```
## Examples
### One choice
A neighborhood, with an empty first option that means any.
```astro
---
import Field from "@astronomic-dev/ui/components/Field.astro";
import Select from "@astronomic-dev/ui/components/Select.astro";
---
<Field label="Neighborhood" for="select-area" class="max-w-xs">
<Select id="select-area" name="area">
<option value="" selected>Any neighborhood</option>
<option value="berkeley">Berkeley</option>
<option value="highlands">Highlands</option>
<option value="rino">RiNo</option>
<option value="sloans-lake">Sloan's Lake</option>
<option value="washington-park">Washington Park</option>
</Select>
</Field>
```
### With the kit's list
picker draws the open list in the kit's look where the browser has base-select (Chrome, Edge, Safari). In Firefox the same closed field opens the browser's own list.
```astro
---
import Field from "@astronomic-dev/ui/components/Field.astro";
import Select from "@astronomic-dev/ui/components/Select.astro";
---
<div class="grid max-w-xs">
<Field label="Beds" for="select-picker-beds">
<Select id="select-picker-beds" name="beds" picker>
<option value="">Any</option>
<option value="1">1+</option>
<option value="2">2+</option>
<option value="3">3+</option>
<option value="4">4+</option>
</Select>
</Field>
</div>
```
### Invalid
A required time left empty: the danger edge, and the error under it from the Field.
```astro
---
import Field from "@astronomic-dev/ui/components/Field.astro";
import Select from "@astronomic-dev/ui/components/Select.astro";
import { describedBy } from "@astronomic-dev/ui/lib/field.ts";
const error = "Pick a time for the tour.";
---
<Field label="Tour time" for="select-time" hint="Mountain Time." error={error} required class="max-w-xs">
<Select id="select-time" name="time" required invalid aria-describedby={describedBy("select-time", { hint: true, error })}>
<option value="" selected>Choose a time</option>
<option value="10:00">10:00 am</option>
<option value="13:00">1:00 pm</option>
<option value="16:00">4:00 pm</option>
</Select>
</Field>
```
### Disabled
A value the reader cannot change, with a hint that says why.
```astro
---
import Field from "@astronomic-dev/ui/components/Field.astro";
import Select from "@astronomic-dev/ui/components/Select.astro";
import { describedBy } from "@astronomic-dev/ui/lib/field.ts";
---
<Field label="Agent" for="select-agent" hint="Set by the listing." class="max-w-xs">
<Select id="select-agent" name="agent" disabled aria-describedby={describedBy("select-agent", { hint: true })}>
<option value="lena-marquez-holt">Lena Marquez-Holt</option>
</Select>
</Field>
```
## Props
### Select
| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `invalid` | `boolean` | `false` | Marks the value as wrong (aria-invalid) and shows the danger border. |
| `picker` | `boolean` | `false` | Draws the open list in the kit's look where the browser has base-select. Elsewhere the browser's own list opens. Off by default, so a long form keeps the list a phone's reader knows. |
| `class` | `string` | None | More classes for the <select>, merged with cn(), so yours win on conflicts. |
| Slot | Description |
| --- | --- |
| `default` | The <option> elements (and <optgroup>s, if you need them). |
Any other attribute goes on the <select>: id, name, required, disabled, form, aria-describedby.
## Accessibility
- Renders a native <select>, so the keyboard, the screen reader and a phone's own picker work as they do on any site.
- With picker it is still a native <select>: the keyboard, the screen reader, the form and a script that sets its value work the same.
- It has no label of its own: put it in a Field, or give it an aria-label.
- The chevron is drawn by the kit, hidden from screen readers, and lets clicks through to the select.
- For one choice only. For several choices, use checkboxes (FilterChoice's option chips): a native multiple select is hard to use with a mouse.
- invalid sets aria-invalid="true" and the danger border; link the Field's error with aria-describedby (describedBy()).
Select
FreeFor one choice from a short list in a form, with the kit's control look.
- File
- Select.astro
- Group
- Forms
- Kind
- Static: no JavaScript
- Examples
- 4
Examples
Drag the orange handle to set the width. Frames get real breakpoints.
- Static
- Interactive
- Framed
One choice
A neighborhood, with an empty first option that means any.
Force statemain.astro
examples/select/main.astro
---
import Field from "@astronomic-dev/ui/components/Field.astro";
import Select from "@astronomic-dev/ui/components/Select.astro";
---
<Field label="Neighborhood" for="select-area" class="max-w-xs">
<Select id="select-area" name="area">
<option value="" selected>Any neighborhood</option>
<option value="berkeley">Berkeley</option>
<option value="highlands">Highlands</option>
<option value="rino">RiNo</option>
<option value="sloans-lake">Sloan's Lake</option>
<option value="washington-park">Washington Park</option>
</Select>
</Field>With the kit's list
picker draws the open list in the kit's look where the browser has base-select (Chrome, Edge, Safari). In Firefox the same closed field opens the browser's own list.
This preview did not load.
picker.astro
examples/select/picker.astro
---
import Field from "@astronomic-dev/ui/components/Field.astro";
import Select from "@astronomic-dev/ui/components/Select.astro";
---
<div class="grid max-w-xs">
<Field label="Beds" for="select-picker-beds">
<Select id="select-picker-beds" name="beds" picker>
<option value="">Any</option>
<option value="1">1+</option>
<option value="2">2+</option>
<option value="3">3+</option>
<option value="4">4+</option>
</Select>
</Field>
</div>Invalid
A required time left empty: the danger edge, and the error under it from the Field.
Mountain Time.
Pick a time for the tour.
Force stateinvalid.astro
examples/select/invalid.astro
---
import Field from "@astronomic-dev/ui/components/Field.astro";
import Select from "@astronomic-dev/ui/components/Select.astro";
import { describedBy } from "@astronomic-dev/ui/lib/field.ts";
const error = "Pick a time for the tour.";
---
<Field label="Tour time" for="select-time" hint="Mountain Time." error={error} required class="max-w-xs">
<Select id="select-time" name="time" required invalid aria-describedby={describedBy("select-time", { hint: true, error })}>
<option value="" selected>Choose a time</option>
<option value="10:00">10:00 am</option>
<option value="13:00">1:00 pm</option>
<option value="16:00">4:00 pm</option>
</Select>
</Field>Disabled
A value the reader cannot change, with a hint that says why.
Set by the listing.
Force statedisabled.astro
examples/select/disabled.astro
---
import Field from "@astronomic-dev/ui/components/Field.astro";
import Select from "@astronomic-dev/ui/components/Select.astro";
import { describedBy } from "@astronomic-dev/ui/lib/field.ts";
---
<Field label="Agent" for="select-agent" hint="Set by the listing." class="max-w-xs">
<Select id="select-agent" name="agent" disabled aria-describedby={describedBy("select-agent", { hint: true })}>
<option value="lena-marquez-holt">Lena Marquez-Holt</option>
</Select>
</Field>Minimal · Light