skip section navigation

Selector

Selector lets someone choose one string value from a dropdown. keep the selected value in your app and use the option labels for the words people see.

New articles will go to Weekend reading.

usage

svelte
<script>
	import { Selector } from '@ciabi/loafkit-ui';
	let shelf = $state('weekend');
	const options = [
		{ value: 'weekend', label: 'Weekend reading' },
		{ value: 'garden', label: 'Garden notes' },
		{ value: 'recipes', label: 'Recipes to try' }
	];
</script>

<Selector label="Save to shelf" {options} bind:selected={shelf} />
<p>New articles will go to {options.find(option => option.value === shelf)?.label}.</p>

state and options

the label appears inside the trigger. use aria-label for an explicit accessible name and aria-describedby for guidance. invalid adds a data attribute; it doesn’t create a form validity rule.

Selector has no name prop or hidden form input. include the selected value in app submission logic, or add a separate hidden input when using FormData.

keyboard and dismissal

Enter or Space opens the list. arrows move through options; Home and End move to the edges. selecting returns focus to the trigger. Escape closes and returns focus, Tab closes while leaving normal tab navigation, and an outside pointer closes without stealing its target’s focus.

submit an app-owned selection

svelte
<script lang="ts">
import { Selector } from '@ciabi/loafkit-ui';
let formShelf = $state('');
let shelfError = $state('');
let submittedShelf = $state('');
</script>

<form onsubmit={event => {
	event.preventDefault();
	if (!formShelf) { shelfError = 'choose a shelf first.'; return; }
	shelfError = '';
	submittedShelf = String(new FormData(event.currentTarget).get('shelf'));
}}>
	<Selector label="collection shelf" bind:selected={formShelf} invalid={!!shelfError} aria-describedby="selector-form-error"
		options={[{value:'weekend',label:'weekend reading'},{value:'garden',label:'garden notes'}]} />
	<input type="hidden" name="shelf" value={formShelf} />
	<p id="selector-form-error" role="status">{shelfError}</p>
	<button type="submit">save shelf</button>
</form>
{#if submittedShelf}<p role="status">submitted shelf: {submittedShelf}</p>{/if}

API reference

Selector

Component
options
Default: []

distinct string values with visible labels.

label
Default: ''

text in the trigger.

disabled
Default: false

makes the control unavailable.

fullWidth
Default: true

fills the available width.

selected
Default: ''

the selected option value.

onValueChange

called with the selected string value.

id

unique ID used by the component and its control relationships.

aria-label

an explicit accessible name.

aria-describedby

IDs of supporting descriptions.

invalid
Default: false

marks the visual invalid state; your app supplies validation.

class
Default: ''

extra classes on the component root.

style

inline styles on the component root.

SelectOption

Type

one option supplied to Selector; this local shape is not a package export.

value

distinct app value returned on selection.

label

visible option text.