Select
Displays a list of options for the user to pick from—triggered by a button.
Loading preview…
1<script lang="ts">
2 import * as Select from './index.js';
3
4 const fruits = [
5 { value: 'apple', label: 'Apple' },
6 { value: 'banana', label: 'Banana' },
7 { value: 'blueberry', label: 'Blueberry' },
8 { value: 'grapes', label: 'Grapes' },
9 { value: 'pineapple', label: 'Pineapple' }
10 ];
11
12 let value = $state('');
13
14 const triggerContent = $derived(fruits.find((f) => f.value === value)?.label ?? 'Select a fruit');
15</script>
16
17<Select.Root type="single" name="favoriteFruit" bind:value>
18 <Select.Trigger class="w-[180px]">
19 {triggerContent}
20 </Select.Trigger>
21 <Select.Content>
22 <Select.Group>
23 <Select.Label>Fruits</Select.Label>
24 {#each fruits as fruit (fruit.value)}
25 <Select.Item value={fruit.value} label={fruit.label} disabled={fruit.value === 'grapes'}>
26 {fruit.label}
27 </Select.Item>
28 {/each}
29 </Select.Group>
30 </Select.Content>
31</Select.Root>
32Installation
1. Install dependencies
2. Copy these files into your project
- ~/components/select/select.svelte
- ~/components/select/select-trigger.svelte
- ~/components/select/select-portal.svelte
- ~/components/select/select-content.svelte
- ~/components/select/select-item.svelte
- ~/components/select/select-group.svelte
- ~/components/select/select-group-heading.svelte
- ~/components/select/select-label.svelte
- ~/components/select/select-separator.svelte
- ~/components/select/select-scroll-up-button.svelte
- ~/components/select/select-scroll-down-button.svelte
- ~/components/select/index.ts
Usage
Use select for picking one option from a custom popover list (not native HTML select). Set type="single" and bind value. Compose Trigger, Content, Group, Label, and Item.
1<script lang="ts">
2 import * as Select from '~/components/select';
3
4 const fruits = [
5 { value: 'apple', label: 'Apple' },
6 { value: 'banana', label: 'Banana' },
7 { value: 'blueberry', label: 'Blueberry' }
8 ];
9
10 let value = $state('');
11
12 const triggerContent = $derived(fruits.find((f) => f.value === value)?.label ?? 'Select a fruit');
13</script>
14
15<Select.Root type="single" name="favoriteFruit" bind:value>
16 <Select.Trigger class="w-[180px]">
17 {triggerContent}
18 </Select.Trigger>
19 <Select.Content>
20 <Select.Group>
21 <Select.Label>Fruits</Select.Label>
22 {#each fruits as fruit (fruit.value)}
23 <Select.Item value={fruit.value} label={fruit.label}>
24 {fruit.label}
25 </Select.Item>
26 {/each}
27 </Select.Group>
28 </Select.Content>
29</Select.Root>