@r2digisolutions/components 0.19.12 → 0.19.13
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,5 +1,7 @@
|
|
|
1
1
|
<script lang="ts">
|
|
2
|
+
import { on } from 'svelte/events';
|
|
2
3
|
import Chip from '../../atoms/Chip/Chip.svelte';
|
|
4
|
+
import { createId } from '../../../utils/id.js';
|
|
3
5
|
|
|
4
6
|
export interface MultiSelectOption {
|
|
5
7
|
value: string;
|
|
@@ -35,10 +37,19 @@
|
|
|
35
37
|
onchange
|
|
36
38
|
}: MultiSelectProps = $props();
|
|
37
39
|
|
|
38
|
-
let
|
|
40
|
+
let triggerEl = $state<HTMLDivElement | null>(null);
|
|
41
|
+
let listboxEl = $state<HTMLDivElement | null>(null);
|
|
42
|
+
let searchInputEl = $state<HTMLInputElement | null>(null);
|
|
43
|
+
let listboxStyle = $state('');
|
|
39
44
|
let search = $state('');
|
|
40
45
|
let highlighted = $state(0);
|
|
41
46
|
|
|
47
|
+
let autoId = $state<string | undefined>(undefined);
|
|
48
|
+
$effect(() => {
|
|
49
|
+
autoId ??= createId('multiselect');
|
|
50
|
+
});
|
|
51
|
+
const listboxId = $derived(autoId ? `${autoId}-listbox` : undefined);
|
|
52
|
+
|
|
42
53
|
const selectedSet = $derived(new Set(value));
|
|
43
54
|
const selectedOptions = $derived(options.filter((o) => selectedSet.has(o.value)));
|
|
44
55
|
|
|
@@ -50,15 +61,76 @@
|
|
|
50
61
|
|
|
51
62
|
const atMax = $derived(max !== undefined && value.length >= max);
|
|
52
63
|
|
|
53
|
-
function
|
|
54
|
-
if (
|
|
55
|
-
|
|
56
|
-
|
|
64
|
+
function positionListbox() {
|
|
65
|
+
if (!triggerEl) return;
|
|
66
|
+
|
|
67
|
+
const rect = triggerEl.getBoundingClientRect();
|
|
68
|
+
const gap = 6;
|
|
69
|
+
const maxHeight = 256;
|
|
70
|
+
const spaceBelow = window.innerHeight - rect.bottom - gap;
|
|
71
|
+
const spaceAbove = rect.top - gap;
|
|
72
|
+
const openUp = spaceBelow < Math.min(maxHeight, 160) && spaceAbove > spaceBelow;
|
|
73
|
+
const available = Math.max(120, openUp ? spaceAbove : spaceBelow);
|
|
74
|
+
|
|
75
|
+
listboxStyle = [
|
|
76
|
+
`top: ${openUp ? 'auto' : `${rect.bottom + gap}px`}`,
|
|
77
|
+
`bottom: ${openUp ? `${window.innerHeight - rect.top + gap}px` : 'auto'}`,
|
|
78
|
+
`left: ${rect.left}px`,
|
|
79
|
+
`width: ${rect.width}px`,
|
|
80
|
+
`min-width: ${rect.width}px`,
|
|
81
|
+
`max-height: ${Math.min(maxHeight, available)}px`
|
|
82
|
+
].join('; ');
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function openListbox() {
|
|
86
|
+
if (disabled || !listboxEl) return;
|
|
87
|
+
positionListbox();
|
|
88
|
+
if (!listboxEl.matches(':popover-open')) listboxEl.showPopover();
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
function closeListbox() {
|
|
92
|
+
if (listboxEl?.matches(':popover-open')) listboxEl.hidePopover();
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function handleBeforeToggle(event: ToggleEvent) {
|
|
96
|
+
if (event.newState === 'open') {
|
|
97
|
+
if (disabled) {
|
|
98
|
+
event.preventDefault();
|
|
99
|
+
return;
|
|
100
|
+
}
|
|
101
|
+
search = '';
|
|
57
102
|
highlighted = 0;
|
|
103
|
+
positionListbox();
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function handleToggle(event: ToggleEvent) {
|
|
108
|
+
open = event.newState === 'open';
|
|
109
|
+
|
|
110
|
+
if (open) {
|
|
111
|
+
requestAnimationFrame(() => {
|
|
112
|
+
if (searchable) searchInputEl?.focus();
|
|
113
|
+
else listboxEl?.focus();
|
|
114
|
+
});
|
|
115
|
+
} else {
|
|
58
116
|
search = '';
|
|
117
|
+
highlighted = 0;
|
|
59
118
|
}
|
|
60
119
|
}
|
|
61
120
|
|
|
121
|
+
/** Keep bindable `open` in sync with the native popover. */
|
|
122
|
+
$effect(() => {
|
|
123
|
+
if (!listboxEl) return;
|
|
124
|
+
const shouldOpen = open && !disabled;
|
|
125
|
+
const isPopoverOpen = listboxEl.matches(':popover-open');
|
|
126
|
+
if (shouldOpen && !isPopoverOpen) {
|
|
127
|
+
positionListbox();
|
|
128
|
+
listboxEl.showPopover();
|
|
129
|
+
} else if (!shouldOpen && isPopoverOpen) {
|
|
130
|
+
listboxEl.hidePopover();
|
|
131
|
+
}
|
|
132
|
+
});
|
|
133
|
+
|
|
62
134
|
function emit(next: string[]) {
|
|
63
135
|
value = next;
|
|
64
136
|
onchange?.(next);
|
|
@@ -84,49 +156,98 @@
|
|
|
84
156
|
emit([]);
|
|
85
157
|
}
|
|
86
158
|
|
|
87
|
-
function
|
|
88
|
-
if (
|
|
89
|
-
const path = typeof e.composedPath === 'function' ? e.composedPath() : [];
|
|
90
|
-
if (path.includes(rootEl) || rootEl.contains(e.target as Node)) return;
|
|
91
|
-
setOpen(false);
|
|
92
|
-
}
|
|
159
|
+
function handleTriggerKeydown(e: KeyboardEvent) {
|
|
160
|
+
if (disabled) return;
|
|
93
161
|
|
|
94
|
-
|
|
95
|
-
|
|
162
|
+
switch (e.key) {
|
|
163
|
+
case 'ArrowDown':
|
|
164
|
+
case 'ArrowUp':
|
|
165
|
+
e.preventDefault();
|
|
166
|
+
if (!open) openListbox();
|
|
167
|
+
else onListKeydown(e);
|
|
168
|
+
break;
|
|
169
|
+
case 'Enter':
|
|
170
|
+
case ' ':
|
|
171
|
+
if (open) {
|
|
172
|
+
e.preventDefault();
|
|
173
|
+
const opt = filtered[highlighted];
|
|
174
|
+
if (opt) toggle(opt);
|
|
175
|
+
}
|
|
176
|
+
break;
|
|
177
|
+
case 'Escape':
|
|
178
|
+
if (open) {
|
|
179
|
+
e.preventDefault();
|
|
180
|
+
closeListbox();
|
|
181
|
+
}
|
|
182
|
+
break;
|
|
183
|
+
}
|
|
96
184
|
}
|
|
97
185
|
|
|
98
186
|
function onListKeydown(e: KeyboardEvent) {
|
|
99
187
|
const enabled = filtered.filter((o) => !o.disabled);
|
|
100
|
-
if (!enabled.length) return;
|
|
188
|
+
if (!enabled.length && e.key !== 'Escape' && e.key !== 'Tab') return;
|
|
101
189
|
|
|
102
190
|
if (e.key === 'ArrowDown') {
|
|
103
191
|
e.preventDefault();
|
|
192
|
+
e.stopPropagation();
|
|
193
|
+
if (!filtered.length) return;
|
|
104
194
|
highlighted = (highlighted + 1) % filtered.length;
|
|
105
195
|
while (filtered[highlighted]?.disabled) {
|
|
106
196
|
highlighted = (highlighted + 1) % filtered.length;
|
|
107
197
|
}
|
|
108
198
|
} else if (e.key === 'ArrowUp') {
|
|
109
199
|
e.preventDefault();
|
|
200
|
+
e.stopPropagation();
|
|
201
|
+
if (!filtered.length) return;
|
|
110
202
|
highlighted = (highlighted - 1 + filtered.length) % filtered.length;
|
|
111
203
|
while (filtered[highlighted]?.disabled) {
|
|
112
204
|
highlighted = (highlighted - 1 + filtered.length) % filtered.length;
|
|
113
205
|
}
|
|
114
206
|
} else if (e.key === 'Enter') {
|
|
115
207
|
e.preventDefault();
|
|
208
|
+
e.stopPropagation();
|
|
116
209
|
const opt = filtered[highlighted];
|
|
117
210
|
if (opt) toggle(opt);
|
|
211
|
+
} else if (e.key === 'Escape') {
|
|
212
|
+
e.preventDefault();
|
|
213
|
+
e.stopPropagation();
|
|
214
|
+
closeListbox();
|
|
215
|
+
} else if (e.key === 'Tab') {
|
|
216
|
+
closeListbox();
|
|
118
217
|
}
|
|
119
218
|
}
|
|
120
|
-
</script>
|
|
121
219
|
|
|
122
|
-
|
|
220
|
+
/** Prevent options from stealing focus from the search input */
|
|
221
|
+
function handleOptionPointerDown(event: PointerEvent) {
|
|
222
|
+
event.preventDefault();
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
$effect(() => {
|
|
226
|
+
if (!open) return;
|
|
123
227
|
|
|
124
|
-
|
|
228
|
+
const reposition = (event?: Event) => {
|
|
229
|
+
const target = event?.target;
|
|
230
|
+
if (target instanceof Node && listboxEl?.contains(target)) return;
|
|
231
|
+
positionListbox();
|
|
232
|
+
};
|
|
233
|
+
|
|
234
|
+
const offResize = on(window, 'resize', reposition);
|
|
235
|
+
const offScroll = on(window, 'scroll', reposition, { capture: true });
|
|
236
|
+
|
|
237
|
+
return () => {
|
|
238
|
+
offResize();
|
|
239
|
+
offScroll();
|
|
240
|
+
};
|
|
241
|
+
});
|
|
242
|
+
</script>
|
|
243
|
+
|
|
244
|
+
<div class={['relative w-full max-w-md', className]}>
|
|
125
245
|
{#if label}
|
|
126
246
|
<span class="mb-1.5 block text-sm font-medium text-primary">{label}</span>
|
|
127
247
|
{/if}
|
|
128
248
|
|
|
129
249
|
<div
|
|
250
|
+
bind:this={triggerEl}
|
|
130
251
|
class={[
|
|
131
252
|
'flex min-h-10 w-full items-center gap-1.5 rounded-xl border border-border bg-surface-elevated px-2 py-1.5 transition-colors',
|
|
132
253
|
open && 'border-brand-500 ring-2 ring-brand-500/20',
|
|
@@ -136,8 +257,12 @@
|
|
|
136
257
|
<button
|
|
137
258
|
type="button"
|
|
138
259
|
{disabled}
|
|
139
|
-
|
|
260
|
+
popovertarget={listboxId}
|
|
261
|
+
popovertargetaction="toggle"
|
|
262
|
+
onkeydown={handleTriggerKeydown}
|
|
140
263
|
aria-expanded={open}
|
|
264
|
+
aria-haspopup="listbox"
|
|
265
|
+
aria-controls={listboxId}
|
|
141
266
|
class={[
|
|
142
267
|
'flex min-w-0 flex-1 flex-wrap items-center gap-1.5 text-left',
|
|
143
268
|
'focus-visible:outline-none',
|
|
@@ -183,85 +308,117 @@
|
|
|
183
308
|
</svg>
|
|
184
309
|
</div>
|
|
185
310
|
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
{
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
311
|
+
<!-- Popover listbox (top layer + light dismiss). Stays open while toggling options. -->
|
|
312
|
+
<div
|
|
313
|
+
bind:this={listboxEl}
|
|
314
|
+
id={listboxId}
|
|
315
|
+
popover="auto"
|
|
316
|
+
onbeforetoggle={handleBeforeToggle}
|
|
317
|
+
ontoggle={handleToggle}
|
|
318
|
+
onkeydown={onListKeydown}
|
|
319
|
+
role="listbox"
|
|
320
|
+
tabindex={-1}
|
|
321
|
+
aria-multiselectable="true"
|
|
322
|
+
aria-label={label || placeholder}
|
|
323
|
+
style={listboxStyle}
|
|
324
|
+
class={[
|
|
325
|
+
'multiselect-listbox m-0 p-0 inset-auto overflow-hidden',
|
|
326
|
+
'bg-surface-elevated border-border rounded-xl shadow-xl border',
|
|
327
|
+
'flex flex-col outline-none'
|
|
328
|
+
]}
|
|
329
|
+
>
|
|
330
|
+
{#if searchable}
|
|
331
|
+
<div class="border-b border-border p-2 shrink-0">
|
|
332
|
+
<input
|
|
333
|
+
bind:this={searchInputEl}
|
|
334
|
+
bind:value={search}
|
|
335
|
+
type="text"
|
|
336
|
+
placeholder="Search…"
|
|
337
|
+
oninput={() => (highlighted = 0)}
|
|
338
|
+
onkeydown={onListKeydown}
|
|
339
|
+
class="w-full rounded-md border border-border bg-surface-elevated px-3 py-1.5 text-xs text-primary outline-none focus:border-brand-500 focus:ring-2 focus:ring-brand-500/20"
|
|
340
|
+
role="searchbox"
|
|
341
|
+
aria-controls={listboxId}
|
|
342
|
+
aria-autocomplete="list"
|
|
343
|
+
/>
|
|
344
|
+
</div>
|
|
345
|
+
{/if}
|
|
346
|
+
<div class="min-h-0 flex-1 overflow-y-auto p-1.5">
|
|
347
|
+
{#if filtered.length === 0}
|
|
348
|
+
<div class="px-3 py-2.5 text-center text-xs text-muted">{emptyText}</div>
|
|
349
|
+
{:else}
|
|
350
|
+
{#each filtered as option, index (option.value)}
|
|
351
|
+
{@const isSelected = selectedSet.has(option.value)}
|
|
352
|
+
{@const blocked = !isSelected && atMax}
|
|
353
|
+
<button
|
|
354
|
+
type="button"
|
|
355
|
+
role="option"
|
|
356
|
+
disabled={option.disabled || blocked}
|
|
357
|
+
aria-selected={isSelected}
|
|
358
|
+
onpointerdown={handleOptionPointerDown}
|
|
359
|
+
onpointerenter={() => {
|
|
360
|
+
if (!option.disabled) highlighted = index;
|
|
361
|
+
}}
|
|
362
|
+
onclick={() => toggle(option)}
|
|
363
|
+
class={[
|
|
364
|
+
'flex w-full items-center gap-2.5 rounded-lg px-2.5 py-2 text-left text-sm transition-colors',
|
|
365
|
+
(option.disabled || blocked) && 'cursor-not-allowed opacity-40',
|
|
366
|
+
!option.disabled &&
|
|
367
|
+
!blocked &&
|
|
368
|
+
(highlighted === index || isSelected
|
|
369
|
+
? 'bg-brand-500 text-white'
|
|
370
|
+
: 'text-primary hover:bg-surface-overlay')
|
|
371
|
+
]}
|
|
372
|
+
>
|
|
373
|
+
<span
|
|
220
374
|
class={[
|
|
221
|
-
'flex w-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
375
|
+
'flex h-4 w-4 shrink-0 items-center justify-center rounded border transition-colors',
|
|
376
|
+
isSelected
|
|
377
|
+
? highlighted === index || isSelected
|
|
378
|
+
? 'border-white bg-white'
|
|
379
|
+
: 'border-brand-500 bg-brand-500'
|
|
380
|
+
: highlighted === index
|
|
381
|
+
? 'border-white/60'
|
|
382
|
+
: 'border-border-strong'
|
|
228
383
|
]}
|
|
384
|
+
aria-hidden="true"
|
|
229
385
|
>
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
class="h-2.5 w-2.5 text-brand-600"
|
|
246
|
-
viewBox="0 0 24 24"
|
|
247
|
-
fill="none"
|
|
248
|
-
stroke="currentColor"
|
|
249
|
-
stroke-width="3"
|
|
250
|
-
>
|
|
251
|
-
<path stroke-linecap="round" stroke-linejoin="round" d="M5 13l4 4L19 7" />
|
|
252
|
-
</svg>
|
|
253
|
-
{/if}
|
|
254
|
-
</span>
|
|
255
|
-
<span class="min-w-0 flex-1 truncate">{option.label}</span>
|
|
256
|
-
</button>
|
|
257
|
-
{/each}
|
|
258
|
-
{/if}
|
|
259
|
-
</div>
|
|
260
|
-
{#if max !== undefined}
|
|
261
|
-
<div class="border-t border-border px-3 py-2 text-[11px] text-muted">
|
|
262
|
-
{value.length} / {max} selected
|
|
263
|
-
</div>
|
|
386
|
+
{#if isSelected}
|
|
387
|
+
<svg
|
|
388
|
+
class="h-2.5 w-2.5 text-brand-600"
|
|
389
|
+
viewBox="0 0 24 24"
|
|
390
|
+
fill="none"
|
|
391
|
+
stroke="currentColor"
|
|
392
|
+
stroke-width="3"
|
|
393
|
+
>
|
|
394
|
+
<path stroke-linecap="round" stroke-linejoin="round" d="M5 13l4 4L19 7" />
|
|
395
|
+
</svg>
|
|
396
|
+
{/if}
|
|
397
|
+
</span>
|
|
398
|
+
<span class="min-w-0 flex-1 truncate">{option.label}</span>
|
|
399
|
+
</button>
|
|
400
|
+
{/each}
|
|
264
401
|
{/if}
|
|
265
402
|
</div>
|
|
266
|
-
|
|
403
|
+
{#if max !== undefined}
|
|
404
|
+
<div class="border-t border-border px-3 py-2 text-[11px] text-muted shrink-0">
|
|
405
|
+
{value.length} / {max} selected
|
|
406
|
+
</div>
|
|
407
|
+
{/if}
|
|
408
|
+
</div>
|
|
267
409
|
</div>
|
|
410
|
+
|
|
411
|
+
<style>
|
|
412
|
+
.multiselect-listbox {
|
|
413
|
+
position: fixed;
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
.multiselect-listbox:popover-open {
|
|
417
|
+
display: flex;
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
/* Reset UA popover centering */
|
|
421
|
+
.multiselect-listbox:not(:popover-open) {
|
|
422
|
+
display: none;
|
|
423
|
+
}
|
|
424
|
+
</style>
|