-
-
Notifications
You must be signed in to change notification settings - Fork 343
/
Copy pathCombobox.svelte
149 lines (144 loc) · 4.11 KB
/
Combobox.svelte
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
<script lang="ts">
import { fade } from 'svelte/transition';
import * as combobox from '@zag-js/combobox';
import { useMachine, normalizeProps, mergeProps } from '@zag-js/svelte';
import type { ComboboxProps } from './types.js';
import { useId } from '$lib/internal/use-id.js';
let {
data = $bindable([]),
value = $bindable([]),
label = '',
// Base
base = '',
width = '',
classes = '',
// Label
labelBase = 'label',
labelText = 'label-text',
labelClasses = '',
// Input
inputGroupBase = 'input-group grid-cols-[1fr_auto]',
inputGroupInput = '',
inputGroupButton = 'input-group-cell',
inputGroupArrow = '',
inputGroupClasses = '',
// Positioner
positionerBase = '',
positionerZIndex = '',
positionerClasses = '',
// Content
contentBase = 'card p-2',
contentBackground = 'preset-outlined-surface-200-800 bg-surface-50-950',
contentSpaceY = 'space-y-1',
contentClasses = '',
// Option
optionBase = 'btn justify-start w-full',
optionHover = 'hover:preset-tonal',
optionActive = 'preset-filled-primary-500',
optionClasses = '',
// Snippets
arrow,
// Events
onclick,
// Zag ---
...zagProps
}: ComboboxProps = $props();
// Zag
let options = $state.raw(data);
const collection = combobox.collection({
items: data,
// Map data structure
itemToValue: (item) => item.value,
itemToString: (item) => item.label
});
const [snapshot, send] = useMachine(
combobox.machine({
id: useId(),
collection,
value: $state.snapshot(value),
loopFocus: true,
onOpenChange() {
options = data;
},
onInputValueChange({ inputValue }) {
const filtered = data.filter((item) => item.label.toLowerCase().includes(inputValue.toLowerCase()));
const newOptions = filtered.length > 0 ? filtered : data;
collection.setItems(newOptions);
options = newOptions;
},
onValueChange(event) {
value = event.value;
}
}),
{
context: {
...zagProps,
get data() {
return $state.snapshot(data);
},
get value() {
return $state.snapshot(value);
}
}
}
);
const api = $derived(combobox.connect(snapshot, send, normalizeProps));
const triggerProps = $derived(mergeProps(api.getTriggerProps(), { onclick }));
</script>
<span {...api.getRootProps()} class="{base} {width} {classes}" data-testid="combobox">
<!-- Label -->
<label {...api.getLabelProps()} class="{labelBase} {labelClasses}">
{#if label}<span class={labelText}>{label}</span>{/if}
<!-- Input Group -->
<div {...api.getControlProps()} class="{inputGroupBase} {inputGroupClasses}">
<!-- Input -->
<input {...api.getInputProps()} class={inputGroupInput} />
<!-- Arrow -->
<button {...triggerProps} class={inputGroupButton}>
{#if arrow}
{@render arrow()}
{:else}
<svg
xmlns="http://www.w3.org/2000/svg"
width="16"
height="16"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
stroke-linecap="round"
stroke-linejoin="round"
style="opacity: 0.5"
class={inputGroupArrow}
>
<path d="m6 9 6 6 6-6" />
</svg>
{/if}
</button>
</div>
</label>
<!-- Menu -->
{#if api.open}
<div {...api.getPositionerProps()} transition:fade={{ duration: 100 }} class="{positionerBase} {positionerZIndex} {positionerClasses}">
{#if options.length > 0}
<!-- Content (list) -->
<nav {...api.getContentProps()} class="{contentBase} {contentBackground} {contentSpaceY} {contentClasses}">
{#each options as item}
{@const isChecked = api.getItemProps({ item })['data-state'] === 'checked'}
{@const displayClass = isChecked ? optionActive : optionHover}
<!-- Option -->
<!-- ZagJs should have set button type to "button" here. See https://github.com/skeletonlabs/skeleton/pull/2998#discussion_r1855511385 -->
<button {...api.getItemProps({ item })} class="{optionBase} {displayClass} {optionClasses}" type="button">
{item.label}
</button>
{/each}
</nav>
{/if}
</div>
{/if}
</span>
<style lang="postcss">
[data-part='item'][data-highlighted]:not([data-state='checked']) {
@apply bg-surface-500/10;
}
</style>