Select

Allow users to choose from a list of options.

Preview
Code

Basic Example

<script>
	import { Select, SelectTrigger, SelectContent SelectOption, SelectValue } from 'lithesome';

	let value = $state();
</script>

<Select bind:value>
	<SelectTrigger>
		<SelectValue />
	</SelectTrigger>
	<SelectContent>
		<SelectOption value="item-1">Item 1</SelectOption>
		<SelectOption value="item-2">Item 2</SelectOption>
		<SelectOption value="item-3">Item 3</SelectOption>
	</SelectContent>
</Select>

Custom selected labels

If you’re using the <SelectValue /> component and want to change how the labels are rendered, simply pass the custom snippet block and access the labels via the state prop.

<script>
	import { Select, SelectTrigger, SelectContent SelectOption, SelectValue } from 'lithesome';

	let value = $state();
</script>

<Select bind:value>
	<SelectTrigger>
		<SelectValue>
			{#snippet custom({ props, state })}
				<span {...props}>
					{state.placeholderVisible ? 'Select an option...' : state.selectedLabels.join(' - ')}
				</span>
			{/snippet}
		</SelectValue>
	</SelectTrigger>
	<SelectContent>
		<SelectOption value="item-1">Item 1</SelectOption>
		<SelectOption value="item-2">Item 2</SelectOption>
		<SelectOption value="item-3">Item 3</SelectOption>
	</SelectContent>
</Select>

API Reference

<Select />

PropertyTypeDescription
value string | string[]

The currently selected option(s).

unselectable boolean

Allows a non-array value to be unselected.

onValueChanged (value: string | string[]) => void

Fires whenever the `value` prop changes.

children Snippet<[ S extends Record<string, any> ? S : never ]>

The default snippet to render.

visible boolean

The current visibility of the contents.

disabled boolean

Disables the entire component tree.

portalTarget HTMLElement | string

The DOM target to mount the content to.

floatingConfig FloatingConfig

The underlying FloatingUI config.

StateTypeDescription
visiblebooleanTrue if the contents are visible.

<SelectTrigger />

PropertyTypeDescription
ref E

The reference to the underlying element.

children Snippet<[ S extends Record<string, any> ? S : never ]>

The default snippet to render.

custom Snippet<[ S extends Record<string, any> ? { props: P; state: S; } : { props: P; } ]>

Snippet to be used when wanting a custom implementation. This will tell Lithesome not the render the default element and any state along with it.

StateTypeDescription
visiblebooleanTrue if the contents are visible.

<SelectContent />

PropertyTypeDescription
ref E

The reference to the underlying element.

children Snippet<[ S extends Record<string, any> ? S : never ]>

The default snippet to render.

custom Snippet<[ S extends Record<string, any> ? { props: P; state: S; } : { props: P; } ]>

Snippet to be used when wanting a custom implementation. This will tell Lithesome not the render the default element and any state along with it.

StateTypeDescription
visiblebooleanTrue if the contents are visible.

<SelectArrow />

PropertyTypeDescription
ref E

The reference to the underlying element.

children Snippet<[ S extends Record<string, any> ? S : never ]>

The default snippet to render.

custom Snippet<[ S extends Record<string, any> ? { props: P; state: S; } : { props: P; } ]>

Snippet to be used when wanting a custom implementation. This will tell Lithesome not the render the default element and any state along with it.

StateTypeDescription
visiblebooleanTrue if the contents are visible.

<SelectOption />

PropertyTypeDescription
value string

The value of the option.

disabled boolean

Disables the option, skipping mouse/keyboard navigation and stopping events from firing.

label string

The label to be display on the option and if this option is selected. If this prop is not provided, the text content of the option is used.

ref E

The reference to the underlying element.

children Snippet<[ S extends Record<string, any> ? S : never ]>

The default snippet to render.

custom Snippet<[ S extends Record<string, any> ? { props: P; state: S; } : { props: P; } ]>

Snippet to be used when wanting a custom implementation. This will tell Lithesome not the render the default element and any state along with it.

StateTypeDescription
hoveredbooleanTrue if the option is hovered.
selectedbooleanTrue if the option is selected.

<SelectValue />

PropertyTypeDescription
placeholder string

The value displayed when no option(s) is selected.

ref E

The reference to the underlying element.

children Snippet<[ S extends Record<string, any> ? S : never ]>

The default snippet to render.

custom Snippet<[ S extends Record<string, any> ? { props: P; state: S; } : { props: P; } ]>

Snippet to be used when wanting a custom implementation. This will tell Lithesome not the render the default element and any state along with it.

StateTypeDescription
visiblebooleanTrue if the contents are visible.
placeholderVisiblebooleanTrue if no options are selected.
selectedLabelsstring[]The currently selected options.