Selector.svelte 2.78 KB
Newer Older
Timothy J. Baek's avatar
Timothy J. Baek committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<script lang="ts">
	import { Select } from 'bits-ui';

	import { flyAndScale } from '$lib/utils/transitions';

	import { createEventDispatcher } from 'svelte';
	import ChevronDown from '../icons/ChevronDown.svelte';
	import Check from '../icons/Check.svelte';
	import Search from '../icons/Search.svelte';

	const dispatch = createEventDispatcher();

	export let value = '';
	export let placeholder = 'Select a model';
15
16
17
	export let searchEnabled = true;
	export let searchPlaceholder = 'Search a model';

Timothy J. Baek's avatar
Timothy J. Baek committed
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
	export let items = [
		{ value: 'mango', label: 'Mango' },
		{ value: 'watermelon', label: 'Watermelon' },
		{ value: 'apple', label: 'Apple' },
		{ value: 'pineapple', label: 'Pineapple' },
		{ value: 'orange', label: 'Orange' }
	];

	let searchValue = '';

	$: filteredItems = searchValue
		? items.filter((item) => item.value.includes(searchValue.toLowerCase()))
		: items;
</script>

<Select.Root
	{items}
	onOpenChange={() => {
		searchValue = '';
	}}
	selected={items.find((item) => item.value === value)}
	onSelectedChange={(selectedItem) => {
		value = selectedItem.value;
	}}
>
	<Select.Trigger class="relative w-full" aria-label={placeholder}>
		<Select.Value
			class="inline-flex h-input px-0.5 w-full outline-none bg-transparent truncate text-lg font-semibold placeholder-gray-400  focus:outline-none"
			{placeholder}
		/>
		<ChevronDown className="absolute end-2 top-1/2 -translate-y-[45%] size-3.5" strokeWidth="2.5" />
	</Select.Trigger>
	<Select.Content
		class="w-full rounded-lg  bg-white dark:bg-gray-900 dark:text-white shadow-lg border border-gray-300/30 dark:border-gray-700/50  outline-none"
		transition={flyAndScale}
		sideOffset={4}
	>
55
56
57
58
		<slot>
			{#if searchEnabled}
				<div class="flex items-center gap-2.5 px-5 mt-3.5 mb-3">
					<Search className="size-4" strokeWidth="2.5" />
Timothy J. Baek's avatar
Timothy J. Baek committed
59

60
61
62
63
64
65
					<input
						bind:value={searchValue}
						class="w-full text-sm bg-transparent outline-none"
						placeholder={searchPlaceholder}
					/>
				</div>
Timothy J. Baek's avatar
Timothy J. Baek committed
66

67
68
				<hr class="border-gray-100 dark:border-gray-800" />
			{/if}
Timothy J. Baek's avatar
Timothy J. Baek committed
69

70
71
72
73
74
75
76
77
			<div class="px-3 my-2 max-h-80 overflow-y-auto">
				{#each filteredItems as item}
					<Select.Item
						class="flex w-full font-medium line-clamp-1 select-none items-center rounded-button py-2 pl-3 pr-1.5 text-sm  text-gray-700 dark:text-gray-100  outline-none transition-all duration-75 hover:bg-gray-100 dark:hover:bg-gray-850 rounded-lg cursor-pointer data-[highlighted]:bg-muted"
						value={item.value}
						label={item.label}
					>
						{item.label}
Timothy J. Baek's avatar
Timothy J. Baek committed
78

79
80
81
82
83
84
85
86
87
88
						{#if value === item.value}
							<div class="ml-auto">
								<Check />
							</div>
						{/if}
					</Select.Item>
				{:else}
					<div>
						<div class="block px-5 py-2 text-sm text-gray-700 dark:text-gray-100">
							No results found
Timothy J. Baek's avatar
Timothy J. Baek committed
89
						</div>
90
91
92
93
					</div>
				{/each}
			</div>
		</slot>
Timothy J. Baek's avatar
Timothy J. Baek committed
94
95
	</Select.Content>
</Select.Root>