Components
Native select
A real <select> that takes the same items as Select and submits the same form data. On phones and tablets it opens the operating system's
own picker.
Live demo
Pick a country and watch the bound value change. The open list is drawn by your browser.
Usage
The items are Select's: options, separators and labelled groups. Swapping one component for the other with the same props keeps the value, the submitted data and what a form reset restores.
<script>
import { NativeSelect } from '@frostycle/rime/select';
let country = $state(null);
const items = [
{ value: 'id', label: 'Indonesia' },
{ value: 'th', label: 'Thailand' }
];
</script>
<label for="country">Country</label>
<NativeSelect id="country" {items} bind:value={country} name="country" placeholder="Choose a country" />Native on touch devices
Neither component detects the device. To use the operating system's picker on touch screens, choose the component with a media query in your app.
<script>
import { MediaQuery } from 'svelte/reactivity';
import { NativeSelect, Select } from '@frostycle/rime/select';
const coarse = new MediaQuery('(pointer: coarse)', false);
</script>
<label for="country">Country</label>
{#if coarse.current}
<NativeSelect id="country" {items} bind:value={country} name="country" />
{:else}
<Select id="country" {items} bind:value={country} name="country" />
{/if}The query is unknown on the server, so it falls back to false: touch devices get
Select until the page hydrates, then switch. Style both the same way to keep the swap from shifting the layout.
Props
| Prop | Type | Notes |
|---|---|---|
items | (T | SelectSeparator | SelectGroup<T>)[] | Required. The same data as Select. Groups render as <optgroup>, separators as <hr>, and a group with no options is left out. |
value | string | null | Bindable. null shows the placeholder. A value with no matching option shows an empty field and still submits. Default null. |
onchange | (value: string | null) => void | Called when the user picks a different value. Not called when the parent sets value. |
id | string | The select's id, so <label for> names the field. Default a generated id. |
name | string | Renders a hidden input carrying the value, empty when null, exactly as Select does. The select itself has no name. A pick that changes the value fires bubbling input and change events from the hidden input. |
placeholder | string | Text of the empty first option, shown while the value is null. It cannot be picked. |
disabled | boolean | Disables the select and renders no hidden input. Default false. |
required | boolean | Native required: the browser blocks submit while the value is null or '', with its own message on the select. Adds aria-invalid and data-invalid once a validity check fails. Default false. |
classes | { select } | Classes for the select element. |
State attributes
Style state with data-* attributes rather than props. They are present or absent,
with no value. The browser handles the keyboard.
| Element | Attributes |
|---|---|
| Select | data-focus, data-hover, data-active, data-disabled, data-invalid |
Forms
With name, a hidden input carries the value, empty when it is null, so the form data matches Select's. A form reset restores the value from when
the component mounted. With required, the browser blocks submit while nothing is
chosen and shows its message on the select. data-invalid appears once a validity
check fails and stays while the field is empty, until a reset.
Behaviour to plan for
classes.select styles the closed control only; the open list belongs to the
operating system. Snippets are not available, because a native option holds only text.
Select treats an option whose value is '' as missing under required. A native select accepts it once picked, so avoid empty values in
required fields.