@classic-homes/theme-svelte 1.28.0 → 1.29.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
<script lang="ts">
|
|
2
2
|
import { Combobox as ComboboxPrimitive } from 'bits-ui';
|
|
3
|
+
import { DEV } from 'esm-env';
|
|
3
4
|
import { cn } from '../utils.js';
|
|
5
|
+
import type { SelectGroup } from '../types/components.js';
|
|
4
6
|
import { computeDescribedBy } from '../utils/form.js';
|
|
5
7
|
import Spinner from './Spinner.svelte';
|
|
6
8
|
import Label from './Label.svelte';
|
|
@@ -17,8 +19,8 @@
|
|
|
17
19
|
value?: string;
|
|
18
20
|
/** Callback when value changes */
|
|
19
21
|
onValueChange?: (value: string) => void;
|
|
20
|
-
/** Array of
|
|
21
|
-
options: ComboboxOption[];
|
|
22
|
+
/** Array of options or grouped options */
|
|
23
|
+
options: (ComboboxOption | SelectGroup)[];
|
|
22
24
|
/** Placeholder text for the input */
|
|
23
25
|
placeholder?: string;
|
|
24
26
|
/** Message to display when no results found */
|
|
@@ -45,6 +47,10 @@
|
|
|
45
47
|
debounceMs?: number;
|
|
46
48
|
/** Additional class for the trigger */
|
|
47
49
|
class?: string;
|
|
50
|
+
/** Accessible name for the input, when there is no visible label */
|
|
51
|
+
'aria-label'?: string;
|
|
52
|
+
/** ID of an element that labels the input */
|
|
53
|
+
'aria-labelledby'?: string;
|
|
48
54
|
}
|
|
49
55
|
|
|
50
56
|
let {
|
|
@@ -64,6 +70,8 @@
|
|
|
64
70
|
onSearch,
|
|
65
71
|
debounceMs = 300,
|
|
66
72
|
class: className,
|
|
73
|
+
'aria-label': ariaLabelProp,
|
|
74
|
+
'aria-labelledby': ariaLabelledby,
|
|
67
75
|
}: Props = $props();
|
|
68
76
|
|
|
69
77
|
// Generate unique IDs for accessibility ($props.id() is SSR-stable)
|
|
@@ -79,16 +87,53 @@
|
|
|
79
87
|
let debounceTimer: ReturnType<typeof setTimeout> | null = null;
|
|
80
88
|
let open = $state(false);
|
|
81
89
|
|
|
82
|
-
//
|
|
83
|
-
|
|
90
|
+
// The placeholder is only a last-resort name: an aria-label would override
|
|
91
|
+
// an external <label for={id}> or aria-labelledby.
|
|
92
|
+
const fallbackAriaLabel = $derived(
|
|
93
|
+
ariaLabelProp ?? (!label && !id && !ariaLabelledby ? placeholder : undefined)
|
|
94
|
+
);
|
|
84
95
|
|
|
85
|
-
|
|
86
|
-
|
|
96
|
+
if (DEV) {
|
|
97
|
+
$effect(() => {
|
|
98
|
+
if (!label && !id && !ariaLabelProp && !ariaLabelledby) {
|
|
99
|
+
console.warn(
|
|
100
|
+
'[Combobox] Missing accessible label. Provide a label prop or an id with a matching <label for="...">.'
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function isGroup(item: ComboboxOption | SelectGroup): item is SelectGroup {
|
|
107
|
+
return 'options' in item;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// Flat list for the label lookup and for bits-ui's items
|
|
111
|
+
const flatItems = $derived(options.flatMap((item) => (isGroup(item) ? item.options : [item])));
|
|
112
|
+
|
|
113
|
+
const selectedLabel = $derived(flatItems.find((opt) => opt.value === value)?.label);
|
|
114
|
+
|
|
115
|
+
// Always filter options locally based on search query; groups keep only
|
|
116
|
+
// their matching options and drop out when none match.
|
|
117
|
+
const filteredOptions = $derived.by((): (ComboboxOption | SelectGroup)[] => {
|
|
87
118
|
if (!searchQuery) return options;
|
|
88
119
|
const query = searchQuery.toLowerCase();
|
|
89
|
-
|
|
120
|
+
const matches = (opt: ComboboxOption) => opt.label.toLowerCase().includes(query);
|
|
121
|
+
return options.flatMap((item): (ComboboxOption | SelectGroup)[] => {
|
|
122
|
+
if (!isGroup(item)) return matches(item) ? [item] : [];
|
|
123
|
+
const groupOptions = item.options.filter(matches);
|
|
124
|
+
return groupOptions.length > 0 ? [{ ...item, options: groupOptions }] : [];
|
|
125
|
+
});
|
|
90
126
|
});
|
|
91
127
|
|
|
128
|
+
const filteredCount = $derived(
|
|
129
|
+
filteredOptions.reduce((n: number, item) => n + (isGroup(item) ? item.options.length : 1), 0)
|
|
130
|
+
);
|
|
131
|
+
|
|
132
|
+
// Closed, the input shows the selected label; open, it holds what the user
|
|
133
|
+
// types, with the selected label as the placeholder so the full list shows.
|
|
134
|
+
const inputValue = $derived(open ? searchQuery : (selectedLabel ?? ''));
|
|
135
|
+
const inputPlaceholder = $derived(open ? selectedLabel || placeholder : placeholder);
|
|
136
|
+
|
|
92
137
|
function handleSearchInput(e: Event) {
|
|
93
138
|
const target = e.target as HTMLInputElement;
|
|
94
139
|
searchQuery = target.value;
|
|
@@ -127,12 +172,41 @@
|
|
|
127
172
|
const resultsAnnouncement = $derived.by(() => {
|
|
128
173
|
if (loading) return 'Loading options...';
|
|
129
174
|
if (!open) return '';
|
|
130
|
-
if (
|
|
131
|
-
const count =
|
|
175
|
+
if (filteredCount === 0) return emptyMessage;
|
|
176
|
+
const count = filteredCount;
|
|
132
177
|
return `${count} ${count === 1 ? 'option' : 'options'} available`;
|
|
133
178
|
});
|
|
134
179
|
</script>
|
|
135
180
|
|
|
181
|
+
{#snippet comboboxItem(option: ComboboxOption)}
|
|
182
|
+
<ComboboxPrimitive.Item
|
|
183
|
+
value={option.value}
|
|
184
|
+
label={option.label}
|
|
185
|
+
disabled={option.disabled}
|
|
186
|
+
class="relative flex w-full cursor-pointer select-none items-center rounded-sm py-1.5 pl-8 pr-2 text-sm outline-none data-[highlighted]:bg-accent data-[highlighted]:text-accent-foreground data-[disabled]:pointer-events-none data-[disabled]:opacity-50 data-[disabled]:cursor-default"
|
|
187
|
+
>
|
|
188
|
+
{#if option.value === value}
|
|
189
|
+
<span class="absolute left-2 flex h-3.5 w-3.5 items-center justify-center">
|
|
190
|
+
<svg
|
|
191
|
+
xmlns="http://www.w3.org/2000/svg"
|
|
192
|
+
width="16"
|
|
193
|
+
height="16"
|
|
194
|
+
viewBox="0 0 24 24"
|
|
195
|
+
fill="none"
|
|
196
|
+
stroke="currentColor"
|
|
197
|
+
stroke-width="2"
|
|
198
|
+
stroke-linecap="round"
|
|
199
|
+
stroke-linejoin="round"
|
|
200
|
+
class="h-4 w-4"
|
|
201
|
+
>
|
|
202
|
+
<polyline points="20 6 9 17 4 12" />
|
|
203
|
+
</svg>
|
|
204
|
+
</span>
|
|
205
|
+
{/if}
|
|
206
|
+
{option.label}
|
|
207
|
+
</ComboboxPrimitive.Item>
|
|
208
|
+
{/snippet}
|
|
209
|
+
|
|
136
210
|
<!-- One structure whether or not there is a label, hint or error, so the
|
|
137
211
|
focused control is never remounted when an error appears. -->
|
|
138
212
|
<div class="space-y-2">
|
|
@@ -155,11 +229,12 @@
|
|
|
155
229
|
onOpenChange={handleOpenChange}
|
|
156
230
|
onValueChange={handleValueChange}
|
|
157
231
|
{value}
|
|
232
|
+
items={flatItems}
|
|
158
233
|
>
|
|
159
234
|
<div class="relative">
|
|
160
235
|
<ComboboxPrimitive.Input
|
|
161
236
|
id={componentId}
|
|
162
|
-
placeholder={
|
|
237
|
+
placeholder={inputPlaceholder}
|
|
163
238
|
oninput={handleSearchInput}
|
|
164
239
|
onfocus={() => (open = true)}
|
|
165
240
|
onclick={() => (open = true)}
|
|
@@ -170,9 +245,15 @@
|
|
|
170
245
|
)}
|
|
171
246
|
role="combobox"
|
|
172
247
|
aria-expanded={open}
|
|
248
|
+
aria-label={fallbackAriaLabel}
|
|
249
|
+
aria-labelledby={ariaLabelledby}
|
|
173
250
|
aria-invalid={error ? 'true' : undefined}
|
|
174
251
|
aria-describedby={describedBy}
|
|
175
|
-
|
|
252
|
+
>
|
|
253
|
+
{#snippet child({ props })}
|
|
254
|
+
<input {...props} value={inputValue} />
|
|
255
|
+
{/snippet}
|
|
256
|
+
</ComboboxPrimitive.Input>
|
|
176
257
|
</div>
|
|
177
258
|
|
|
178
259
|
<!-- Portal to <body>: escapes transformed/overflow-hidden ancestors
|
|
@@ -190,38 +271,24 @@
|
|
|
190
271
|
<Spinner size="sm" />
|
|
191
272
|
<span class="ml-2 text-sm text-muted-foreground">Loading...</span>
|
|
192
273
|
</div>
|
|
193
|
-
{:else if
|
|
274
|
+
{:else if filteredCount === 0}
|
|
194
275
|
<div class="py-6 text-center text-sm text-muted-foreground">
|
|
195
276
|
{emptyMessage}
|
|
196
277
|
</div>
|
|
197
278
|
{:else}
|
|
198
|
-
{#each filteredOptions as
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
viewBox="0 0 24 24"
|
|
212
|
-
fill="none"
|
|
213
|
-
stroke="currentColor"
|
|
214
|
-
stroke-width="2"
|
|
215
|
-
stroke-linecap="round"
|
|
216
|
-
stroke-linejoin="round"
|
|
217
|
-
class="h-4 w-4"
|
|
218
|
-
>
|
|
219
|
-
<polyline points="20 6 9 17 4 12" />
|
|
220
|
-
</svg>
|
|
221
|
-
</span>
|
|
222
|
-
{/if}
|
|
223
|
-
{option.label}
|
|
224
|
-
</ComboboxPrimitive.Item>
|
|
279
|
+
{#each filteredOptions as item}
|
|
280
|
+
{#if isGroup(item)}
|
|
281
|
+
<ComboboxPrimitive.Group>
|
|
282
|
+
<ComboboxPrimitive.GroupHeading class="py-1.5 pl-8 pr-2 text-sm font-semibold">
|
|
283
|
+
{item.label}
|
|
284
|
+
</ComboboxPrimitive.GroupHeading>
|
|
285
|
+
{#each item.options as option}
|
|
286
|
+
{@render comboboxItem(option)}
|
|
287
|
+
{/each}
|
|
288
|
+
</ComboboxPrimitive.Group>
|
|
289
|
+
{:else}
|
|
290
|
+
{@render comboboxItem(item)}
|
|
291
|
+
{/if}
|
|
225
292
|
{/each}
|
|
226
293
|
{/if}
|
|
227
294
|
</div>
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { SelectGroup } from '../types/components.js';
|
|
1
2
|
export interface ComboboxOption {
|
|
2
3
|
value: string;
|
|
3
4
|
label: string;
|
|
@@ -8,8 +9,8 @@ interface Props {
|
|
|
8
9
|
value?: string;
|
|
9
10
|
/** Callback when value changes */
|
|
10
11
|
onValueChange?: (value: string) => void;
|
|
11
|
-
/** Array of
|
|
12
|
-
options: ComboboxOption[];
|
|
12
|
+
/** Array of options or grouped options */
|
|
13
|
+
options: (ComboboxOption | SelectGroup)[];
|
|
13
14
|
/** Placeholder text for the input */
|
|
14
15
|
placeholder?: string;
|
|
15
16
|
/** Message to display when no results found */
|
|
@@ -36,6 +37,10 @@ interface Props {
|
|
|
36
37
|
debounceMs?: number;
|
|
37
38
|
/** Additional class for the trigger */
|
|
38
39
|
class?: string;
|
|
40
|
+
/** Accessible name for the input, when there is no visible label */
|
|
41
|
+
'aria-label'?: string;
|
|
42
|
+
/** ID of an element that labels the input */
|
|
43
|
+
'aria-labelledby'?: string;
|
|
39
44
|
}
|
|
40
45
|
declare const Combobox: import("svelte").Component<Props, {}, "value">;
|
|
41
46
|
type Combobox = ReturnType<typeof Combobox>;
|
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
import { validateNonEmptyArray } from '../validation.js';
|
|
5
5
|
import { DEV } from 'esm-env';
|
|
6
6
|
import Label from './Label.svelte';
|
|
7
|
+
import Combobox from './Combobox.svelte';
|
|
7
8
|
|
|
8
9
|
export interface SelectOption {
|
|
9
10
|
value: string;
|
|
@@ -45,6 +46,11 @@
|
|
|
45
46
|
'aria-label'?: string;
|
|
46
47
|
/** ID of an element that labels the trigger */
|
|
47
48
|
'aria-labelledby'?: string;
|
|
49
|
+
/**
|
|
50
|
+
* Type to narrow the options: `true` always renders a Combobox; a number
|
|
51
|
+
* renders one once there are at least that many options.
|
|
52
|
+
*/
|
|
53
|
+
searchable?: boolean | number;
|
|
48
54
|
}
|
|
49
55
|
|
|
50
56
|
let {
|
|
@@ -62,6 +68,7 @@
|
|
|
62
68
|
hint,
|
|
63
69
|
'aria-label': ariaLabelProp,
|
|
64
70
|
'aria-labelledby': ariaLabelledby,
|
|
71
|
+
searchable = false,
|
|
65
72
|
}: Props = $props();
|
|
66
73
|
|
|
67
74
|
// Generate unique IDs for accessibility using sequential counter
|
|
@@ -132,6 +139,10 @@
|
|
|
132
139
|
return items;
|
|
133
140
|
});
|
|
134
141
|
|
|
142
|
+
const useCombobox = $derived(
|
|
143
|
+
typeof searchable === 'number' ? flatItems.length >= searchable : searchable
|
|
144
|
+
);
|
|
145
|
+
|
|
135
146
|
function handleValueChange(newValue: string) {
|
|
136
147
|
value = newValue;
|
|
137
148
|
onValueChange?.(newValue);
|
|
@@ -267,30 +278,49 @@
|
|
|
267
278
|
</SelectPrimitive.Root>
|
|
268
279
|
{/snippet}
|
|
269
280
|
|
|
270
|
-
|
|
281
|
+
{#if useCombobox}
|
|
282
|
+
<Combobox
|
|
283
|
+
bind:value
|
|
284
|
+
{onValueChange}
|
|
285
|
+
{options}
|
|
286
|
+
{placeholder}
|
|
287
|
+
{disabled}
|
|
288
|
+
{name}
|
|
289
|
+
{required}
|
|
290
|
+
{label}
|
|
291
|
+
{id}
|
|
292
|
+
{error}
|
|
293
|
+
{hint}
|
|
294
|
+
aria-label={ariaLabelProp}
|
|
295
|
+
aria-labelledby={ariaLabelledby}
|
|
296
|
+
class={className}
|
|
297
|
+
/>
|
|
298
|
+
{:else}
|
|
299
|
+
<!-- One structure whether or not there is a label, hint or error, so the
|
|
271
300
|
focused control is never remounted when an error appears. -->
|
|
272
|
-
<div class="space-y-2">
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
301
|
+
<div class="space-y-2">
|
|
302
|
+
{#if label}
|
|
303
|
+
<Label for={componentId} {disabled}>
|
|
304
|
+
{label}
|
|
305
|
+
{#if required}
|
|
306
|
+
<span class="text-destructive ml-0.5" aria-hidden="true">*</span>
|
|
307
|
+
<span class="sr-only">(required)</span>
|
|
308
|
+
{/if}
|
|
309
|
+
</Label>
|
|
310
|
+
{/if}
|
|
282
311
|
|
|
283
|
-
|
|
312
|
+
{@render selectRoot(fallbackAriaLabel)}
|
|
284
313
|
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
314
|
+
{#if hint && !error}
|
|
315
|
+
<p id={hintId} class="text-sm text-muted-foreground">
|
|
316
|
+
{hint}
|
|
317
|
+
</p>
|
|
318
|
+
{/if}
|
|
290
319
|
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
</div>
|
|
320
|
+
{#if error}
|
|
321
|
+
<p id={errorId} class="text-sm text-destructive" role="alert" aria-live="polite">
|
|
322
|
+
{error}
|
|
323
|
+
</p>
|
|
324
|
+
{/if}
|
|
325
|
+
</div>
|
|
326
|
+
{/if}
|
|
@@ -36,6 +36,11 @@ interface Props {
|
|
|
36
36
|
'aria-label'?: string;
|
|
37
37
|
/** ID of an element that labels the trigger */
|
|
38
38
|
'aria-labelledby'?: string;
|
|
39
|
+
/**
|
|
40
|
+
* Type to narrow the options: `true` always renders a Combobox; a number
|
|
41
|
+
* renders one once there are at least that many options.
|
|
42
|
+
*/
|
|
43
|
+
searchable?: boolean | number;
|
|
39
44
|
}
|
|
40
45
|
declare const Select: import("svelte").Component<Props, {}, "value">;
|
|
41
46
|
type Select = ReturnType<typeof Select>;
|