Select

Usage

Elderberry
Elderberry
<Select
	label="Fruit"
	name="fruit"
	options={[
		{ value: '', label: '' },
		{ value: 'apple', label: 'Apple', selected: true },
		{ value: 'apricot', label: 'Apricot' },
		{ value: 'banana', label: 'Banana' },
	]}
/>
<Select
	label="Fruit"
	variant="filled"
	name="fruit"
	options={[
		{ value: '', label: '', selected: true },
		{ value: 'apple', label: 'Apple' },
		{ value: 'apricot', label: 'Apricot' },
		{ value: 'banana', label: 'Banana' },
	]}
/>

Disabled

<Select
	label="Fruit"
	name="fruit"
	disabled
	options={[{ value: '', label: '' }]}
/>
<Select
	label="Fruit"
	variant="filled"
	name="fruit"
	disabled
	options={[{ value: '', label: '', selected: true }]}
/>

Validation

<form>
	<Select
		label="Genre"
		name="genre"
		required
		options={[
			{ value: '', label: '' },
			{ value: 'rock', label: 'Rock' },
			{ value: 'pop', label: 'Pop' },
			{ value: 'jazz', label: 'Jazz' },
		]}
	/>
	<div class="button-area">
		<Button type="submit">Send</Button>
	</div>
</form>

Multiple selection

<Select
		label="Favorite fruit"
		name="fruit"
		style="max-width: 300px"
		multiple
		options={[
			{ value: 'apple', label: 'Apple' },
			{ value: 'apricot', label: 'Apricot' },
			{ value: 'banana', label: 'Banana' },
			{ value: 'cherry', label: 'Cherry' },
			{ value: 'elderberry', label: 'Elderberry' },
			{ value: 'fig', label: 'Fig' },
		]}
	/>

Virtual list

An option uses the Intersection Observer API to improve performance. This enables lists of up to 300 options. Beyond that the Select component uses a virtual list to render the options, which has the limitation of having a fixed height. The switchover point is virtualThreshold; lower it when your options are expensive to render.

<Select
	label="Virtual list"
	name="virtual_list"
	style="max-width: 300px"
	options={[
		...Array.from({ length: 4001 }, (_, i) => ({
			value: `option${i + 1}`,
			label: `Option ${i + 1}`,
		})),
	]}
/>

Icons

<Select
	label="Favorite fruit"
	name="fruit"
	options={[
		{ value: '', label: '' },
		{ value: 'apple', label: 'Apple' },
		{ value: 'apricot', label: 'Apricot' },
		{ value: 'banana', label: 'Banana' },
	]}
>
	{#snippet start()}
		<Icon>favorite</Icon>
	{/snippet}
</Select>
<Select
	label="Favorite car"
	name="car"
	options={[
		{ value: '', label: '' },
		{ value: 'audi', label: 'Audi' },
		{ value: 'bmw', label: 'BMW' },
		{ value: 'mercedes', label: 'Mercedes' },
		{ value: 'vw', label: 'Volkswagen' },
	]}
>
	{#snippet end()}
		<Icon>favorite</Icon>
	{/snippet}
</Select>

Theming

Filled select tokens

TokenDefault value
--np-filled-select-text-field-container-shape--np-shape-corner-extra-small
--np-filled-select-text-field-container-color--np-color-surface-container-highest

Filled select example

<Select
	variant="filled"
	label="Filled"
	options={[
		{ value: '', label: '', selected: true },
		{ value: 'apple', label: 'Apple' },
		{ value: 'apricot', label: 'Apricot' },
		{ value: 'banana', label: 'Banana' },
	]}
	--np-filled-select-text-field-container-shape="0"
	--np-filled-select-text-field-container-color="var(--np-color-surface-container)"
	--np-color-primary="var(--np-color-tertiary)"
/>

Outlined select tokens

TokenDefault value
--np-outlined-select-text-field-container-shape--np-shape-corner-extra-small

Sizing tokens

These apply to both variants and control how wide the select is allowed to get.

TokenDefault value
--np-select-min-width210px
--np-select-max-width100%

Outlined select example

<Select
	options={[
		{ value: '', label: '', selected: true },
		{ value: 'apple', label: 'Apple' },
		{ value: 'apricot', label: 'Apricot' },
		{ value: 'banana', label: 'Banana' },
	]}
	label="Outlined"
	--np-outlined-select-text-field-container-shape="0"
	--np-color-primary="var(--np-color-tertiary)"
/>

API

Attributes

AttributeTypeDefaultDescription
optionsSelectOption[][]Required. The options to choose from. See the table below for their shape.
variant'outlined' | 'filled''outlined'Defines the visual style of the select component.
labelstring | undefinedundefinedSpecifies the floating label for the select field.
startSnippet | undefinedundefinedIcon displayed at the beginning of the select field.
endSnippet | undefinedundefinedReplaces the arrow
supportingTextstring''Provides additional information below the select, such as usage instructions.
issues{ message: string }[]undefinedDisplays error messages below the select. Optimized to use with remote form field issues.
requiredbooleanfalseIndicates whether the select field is required.
noAsteriskbooleanfalseDisables the asterisk on the floating label when the select field is required.
disabledbooleanfalseIndicates whether the select field is disabled.
multipleboolean | null | undefinedundefinedAllows multiple selections when set to true.
clampMenuWidthbooleanfalseRestricts the menu width to match the width of the select component.
virtualThresholdnumber300Number of options from which on the menu renders as a virtual list.
...attributesHTMLAttributes<HTMLDivElement>Attributes are passed to the component container.

SelectOption

PropertyTypeDescription
valuestring | numberRequired. Value submitted with the form.
labelstringRequired. Text shown for the option.
selectedboolean | null | undefinedPreselects the option, and is the state a form reset returns it to.
disabledboolean | undefinedMakes the option non-selectable.

Bindables

AttributeTypeDescription
elementHTMLElementA reference to the root DOM element of the component. This variable is bound using bind:this, allowing direct access to the underlying HTML element for manipulation or querying within the component's logic.
valueanyValue of the underlying select