@tableslayer/ui 0.2.9 → 0.3.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.
Files changed (29) hide show
  1. package/dist/components/Autocomplete/Autocomplete.svelte +874 -0
  2. package/dist/components/Autocomplete/Autocomplete.svelte.d.ts +26 -0
  3. package/dist/components/Autocomplete/index.d.ts +2 -0
  4. package/dist/components/Autocomplete/index.js +2 -0
  5. package/dist/components/Autocomplete/types.d.ts +81 -0
  6. package/dist/components/Autocomplete/types.js +1 -0
  7. package/dist/components/Dialog/Dialog.svelte +174 -0
  8. package/dist/components/Dialog/Dialog.svelte.d.ts +23 -0
  9. package/dist/components/Dialog/index.d.ts +2 -0
  10. package/dist/components/Dialog/index.js +2 -0
  11. package/dist/components/Dialog/types.d.ts +20 -0
  12. package/dist/components/Dialog/types.js +1 -0
  13. package/dist/components/Input/FileInput.svelte.d.ts +1 -1
  14. package/dist/components/Input/InputCheckbox.svelte.d.ts +1 -1
  15. package/dist/components/NumberInput/NumberInput.svelte +241 -0
  16. package/dist/components/NumberInput/NumberInput.svelte.d.ts +16 -0
  17. package/dist/components/NumberInput/index.d.ts +2 -0
  18. package/dist/components/NumberInput/index.js +2 -0
  19. package/dist/components/NumberInput/types.d.ts +18 -0
  20. package/dist/components/NumberInput/types.js +1 -0
  21. package/dist/components/Tabs/Tabs.svelte +204 -0
  22. package/dist/components/Tabs/Tabs.svelte.d.ts +26 -0
  23. package/dist/components/Tabs/index.d.ts +2 -0
  24. package/dist/components/Tabs/index.js +2 -0
  25. package/dist/components/Tabs/types.d.ts +25 -0
  26. package/dist/components/Tabs/types.js +1 -0
  27. package/dist/components/index.d.ts +4 -0
  28. package/dist/components/index.js +4 -0
  29. package/package.json +1 -1
@@ -0,0 +1,874 @@
1
+ <script lang="ts">
2
+ import { autoUpdate, computePosition, flip, offset, shift, size as fit } from '@floating-ui/dom';
3
+ import {
4
+ IconCheck,
5
+ IconChevronDown,
6
+ IconCircleXFilled,
7
+ IconCornerDownLeft,
8
+ IconPlus,
9
+ IconX
10
+ } from '@tabler/icons-svelte';
11
+ import { Icon } from '../Icon';
12
+ import { tick, untrack } from 'svelte';
13
+ import type { AutocompleteOption, AutocompleteProps } from './types';
14
+
15
+ let {
16
+ options,
17
+ selected = $bindable([]),
18
+ onSelectedChange,
19
+ isMultiple = false,
20
+ isPlainText = false,
21
+ variant = 'default',
22
+ size = 'md',
23
+ isDisabled = false,
24
+ query = $bindable(''),
25
+ onQueryChange,
26
+ isOpen = $bindable(false),
27
+ onIsOpenChange,
28
+ onCreate,
29
+ createLabel = (text) => `Add ${text} as a custom option`,
30
+ isClearable = true,
31
+ filter,
32
+ isCaseSensitive = false,
33
+ sortMatchesBy = 'none',
34
+ delimiter,
35
+ showSuggestions = true,
36
+ isLoading = false,
37
+ debounce = 150,
38
+ emptyText = 'There aren’t any options available',
39
+ noMatchText = (text) => `${text} doesn’t match any options`,
40
+ allSelectedText = 'You’ve selected all available options',
41
+ alreadyAddedText = (label) => `${label} has already been added`,
42
+ loadingText = 'Loading options',
43
+ start,
44
+ option: optionSnippet,
45
+ positioning = { placement: 'bottom-start', offset: 4 },
46
+ element = $bindable(),
47
+ ...restProps
48
+ }: AutocompleteProps = $props();
49
+
50
+ const uid = $props.id();
51
+ const listId = `${uid}-list`;
52
+ const optionId = (index: number) => `${uid}-option-${index}`;
53
+
54
+ let root = $state<HTMLElement | null>(null);
55
+ let menu = $state<HTMLElement | null>(null);
56
+ let floatingStyles = $state('');
57
+ let activeIndex = $state(-1);
58
+
59
+ // ---- The options: a list given outright, or whatever a function finds for the text typed.
60
+ let found = $state<AutocompleteOption[]>([]);
61
+ let isFinding = $state(false);
62
+ /** Every option seen so far, so that a choice can be named when it is no longer among those found. */
63
+ const known = new Map<string, AutocompleteOption>();
64
+ const remember = (list: AutocompleteOption[]) => {
65
+ for (const each of list) known.set(each.value, each);
66
+ };
67
+
68
+ const fold = (text: string) => (isCaseSensitive ? text : text.toLowerCase());
69
+ /** What has been typed, as it is shown; and as it is matched. */
70
+ const typed = $derived(query.trim());
71
+ const needle = $derived(fold(typed));
72
+ const matches = (each: AutocompleteOption, text: string) =>
73
+ filter ? filter(each, text) : fold(each.label).includes(text) || fold(each.description ?? '').includes(text);
74
+
75
+ let request = 0;
76
+ $effect(() => {
77
+ if (typeof options !== 'function') return;
78
+ const find = options;
79
+ const text = needle;
80
+ if (!isOpen) return;
81
+ const mine = ++request;
82
+ isFinding = true;
83
+ const timer = setTimeout(
84
+ () => {
85
+ find(text)
86
+ .then((list) => {
87
+ if (mine !== request) return;
88
+ remember(list);
89
+ found = list;
90
+ })
91
+ .catch(() => {
92
+ if (mine === request) found = [];
93
+ })
94
+ .finally(() => {
95
+ if (mine === request) isFinding = false;
96
+ });
97
+ },
98
+ text ? debounce : 0
99
+ );
100
+ return () => clearTimeout(timer);
101
+ });
102
+ const isBusy = $derived(isFinding || isLoading);
103
+
104
+ /** Everything there is to choose from, before the text typed narrows it. */
105
+ const pool = $derived.by(() => {
106
+ if (typeof options === 'function') return found;
107
+ remember(options);
108
+ return options;
109
+ });
110
+ const isSelected = (value: string) => selected.includes(value);
111
+ /** The option of a value: among those there are now, or one seen before that a search has since left out. */
112
+ const optionOf = (value: string) => pool.find((each) => each.value === value) ?? known.get(value);
113
+ const labelOf = (value: string) => optionOf(value)?.label ?? value;
114
+
115
+ /** The options offered. Of several choices, those already made are in the field and not in the list. */
116
+ const listed = $derived.by(() => {
117
+ const open = isMultiple ? pool.filter((each) => !isSelected(each.value)) : pool;
118
+ if (typeof options === 'function' || !needle) return open;
119
+ const hits = open.filter((each) => matches(each, needle));
120
+ if (sortMatchesBy !== 'startsWith') return hits;
121
+ const begins = (each: AutocompleteOption) => fold(each.label).startsWith(needle);
122
+ return [...hits.filter(begins), ...hits.filter((each) => !begins(each))];
123
+ });
124
+
125
+ /** The choice already made that the text typed names, if it names one. */
126
+ const alreadyAdded = $derived(
127
+ needle && isMultiple ? selected.find((value) => fold(labelOf(value)) === needle) : undefined
128
+ );
129
+ const canCreate = $derived(
130
+ !!onCreate && needle.length > 0 && !alreadyAdded && !pool.some((each) => fold(each.label) === needle)
131
+ );
132
+ /** How many rows can be moved to: the options, and the row that offers a new one. */
133
+ const rows = $derived(listed.length + (canCreate ? 1 : 0));
134
+ /** What the list says when it has no rows. */
135
+ const message = $derived(
136
+ isBusy
137
+ ? loadingText
138
+ : alreadyAdded
139
+ ? alreadyAddedText(labelOf(alreadyAdded))
140
+ : needle
141
+ ? noMatchText(typed)
142
+ : pool.length > 0 && isMultiple
143
+ ? allSelectedText
144
+ : emptyText
145
+ );
146
+
147
+ // ---- Open and shut.
148
+ const setOpen = (open: boolean) => {
149
+ if (isOpen === open) return;
150
+ isOpen = open;
151
+ onIsOpenChange?.(open);
152
+ };
153
+ const setQuery = (text: string) => {
154
+ if (element && element.value !== text) element.value = text;
155
+ if (query === text) return;
156
+ query = text;
157
+ onQueryChange?.(text);
158
+ };
159
+ const open = () => {
160
+ if (isDisabled || !showSuggestions) return;
161
+ setOpen(true);
162
+ };
163
+ const close = () => {
164
+ setOpen(false);
165
+ activeIndex = -1;
166
+ };
167
+
168
+ // The row that Enter would take: the first that can be chosen whenever the list changes under the text typed.
169
+ const firstEnabled = () => listed.findIndex((each) => !each.isDisabled);
170
+ $effect(() => {
171
+ void needle;
172
+ void rows;
173
+ if (!isOpen) return;
174
+ untrack(() => {
175
+ if (needle) activeIndex = listed.length > 0 ? firstEnabled() : canCreate ? listed.length : -1;
176
+ else if (activeIndex >= rows) activeIndex = -1;
177
+ });
178
+ });
179
+
180
+ $effect(() => {
181
+ if (!isOpen || !root || !menu) return;
182
+ const anchor = root;
183
+ const floating = menu;
184
+ return autoUpdate(anchor, floating, () => {
185
+ void computePosition(anchor, floating, {
186
+ // Fixed, so that a scrolling panel the field sits in does not cut the list off.
187
+ strategy: 'fixed',
188
+ placement: positioning.placement ?? 'bottom-start',
189
+ middleware: [
190
+ offset(positioning.offset ?? 4),
191
+ flip(),
192
+ shift({ padding: 8 }),
193
+ fit({
194
+ padding: 8,
195
+ apply({ rects, availableHeight }) {
196
+ floating.style.minWidth = `${rects.reference.width}px`;
197
+ floating.style.maxHeight = `${Math.max(96, Math.min(320, availableHeight))}px`;
198
+ }
199
+ })
200
+ ]
201
+ }).then(({ x, y }) => {
202
+ floatingStyles = `left: ${x}px; top: ${y}px;`;
203
+ });
204
+ });
205
+ });
206
+
207
+ $effect(() => {
208
+ if (!isOpen || activeIndex < 0) return;
209
+ const index = activeIndex;
210
+ void tick().then(() => document.getElementById(optionId(index))?.scrollIntoView({ block: 'nearest' }));
211
+ });
212
+
213
+ // ---- Choosing.
214
+ const change = (next: string[]) => {
215
+ selected = next;
216
+ onSelectedChange?.(next);
217
+ };
218
+ /** Takes an option. Of several it is added and the list stays open for the next; one alone replaces what was chosen. */
219
+ const choose = (each: AutocompleteOption, keepFocus = true) => {
220
+ if (each.isDisabled) return;
221
+ remember([each]);
222
+ setQuery('');
223
+ if (isMultiple) {
224
+ if (!isSelected(each.value)) change([...selected, each.value]);
225
+ if (keepFocus) element?.focus();
226
+ return;
227
+ }
228
+ change([each.value]);
229
+ close();
230
+ };
231
+ const create = async (text: string, keepFocus = true) => {
232
+ if (!onCreate || !text) return;
233
+ const made = await onCreate(text);
234
+ if (made) choose(made, keepFocus);
235
+ };
236
+ const take = (index: number) => {
237
+ if (index < 0) return;
238
+ if (index < listed.length) choose(listed[index]);
239
+ else if (canCreate) void create(typed);
240
+ };
241
+ /** Takes each of several pieces of text: the option of that name where there is one, or else a new one. */
242
+ const takeAll = async (texts: string[], keepFocus = true) => {
243
+ const seen = new Set<string>();
244
+ for (const text of texts.map((each) => each.trim())) {
245
+ if (!text || seen.has(fold(text))) continue;
246
+ seen.add(fold(text));
247
+ const named = pool.find((each) => fold(each.label) === fold(text));
248
+ if (named) choose(named, keepFocus);
249
+ else await create(text, keepFocus);
250
+ if (!isMultiple) break;
251
+ }
252
+ setQuery('');
253
+ };
254
+ const remove = (value: string) => change(selected.filter((each) => each !== value));
255
+ const clear = () => {
256
+ change([]);
257
+ setQuery('');
258
+ element?.focus();
259
+ open();
260
+ };
261
+ /**
262
+ * The field is left. Text still in it becomes a choice where it may (the option it names, or
263
+ * a new one), and is otherwise dropped: the field never keeps text that is not a choice, since
264
+ * it would look like one.
265
+ */
266
+ const leave = () => {
267
+ const text = typed;
268
+ close();
269
+ if (text && (canCreate || pool.some((each) => fold(each.label) === fold(text)))) void takeAll([text], false);
270
+ else setQuery('');
271
+ };
272
+
273
+ const step = (by: number) => {
274
+ if (rows === 0) return;
275
+ let index = activeIndex;
276
+ for (let tried = 0; tried < rows; tried++) {
277
+ index = (index + by + rows) % rows;
278
+ if (index >= listed.length || !listed[index].isDisabled) break;
279
+ }
280
+ activeIndex = index;
281
+ };
282
+
283
+ const oninput = (event: Event & { currentTarget: HTMLInputElement }) => {
284
+ const text = event.currentTarget.value;
285
+ restProps.oninput?.(event);
286
+ if (delimiter && text.includes(delimiter)) {
287
+ void takeAll(text.split(delimiter));
288
+ return;
289
+ }
290
+ setQuery(text);
291
+ open();
292
+ };
293
+ const onkeydown = (event: KeyboardEvent & { currentTarget: HTMLInputElement }) => {
294
+ restProps.onkeydown?.(event);
295
+ if (event.defaultPrevented || isDisabled) return;
296
+ switch (event.key) {
297
+ case 'ArrowDown':
298
+ event.preventDefault();
299
+ if (!isOpen) {
300
+ open();
301
+ // One choice is still in the list: the list opens on it.
302
+ activeIndex = listed.findIndex((each) => isSelected(each.value));
303
+ if (activeIndex < 0) step(1);
304
+ } else step(1);
305
+ break;
306
+ case 'ArrowUp':
307
+ event.preventDefault();
308
+ if (!isOpen) open();
309
+ step(-1);
310
+ break;
311
+ case 'PageDown':
312
+ case 'PageUp':
313
+ if (!isOpen) break;
314
+ event.preventDefault();
315
+ activeIndex = event.key === 'PageDown' ? rows : -1;
316
+ step(event.key === 'PageDown' ? -1 : 1);
317
+ break;
318
+ case 'Enter':
319
+ if (!isOpen && !typed) break;
320
+ event.preventDefault();
321
+ if (isOpen && activeIndex >= 0) take(activeIndex);
322
+ else if (canCreate) void create(typed);
323
+ break;
324
+ case 'Escape':
325
+ if (!isOpen && !query) break;
326
+ event.preventDefault();
327
+ event.stopPropagation();
328
+ close();
329
+ setQuery('');
330
+ break;
331
+ case 'Backspace':
332
+ if (query === '' && selected.length > 0 && (isMultiple || isClearable)) remove(selected[selected.length - 1]);
333
+ break;
334
+ case 'Tab':
335
+ leave();
336
+ break;
337
+ }
338
+ };
339
+ const onfocus = (event: FocusEvent & { currentTarget: HTMLInputElement }) => {
340
+ open();
341
+ restProps.onfocus?.(event);
342
+ };
343
+ const onWindowPointerdown = (event: PointerEvent) => {
344
+ if (!isOpen && !query) return;
345
+ const target = event.target as Node;
346
+ if (root?.contains(target) || menu?.contains(target)) return;
347
+ leave();
348
+ };
349
+
350
+ /** A label cut at what was typed, so that the part that matches can be marked. */
351
+ const parts = (label: string): [string, string, string] => {
352
+ const at = needle ? fold(label).indexOf(needle) : -1;
353
+ return at < 0
354
+ ? [label, '', '']
355
+ : [label.slice(0, at), label.slice(at, at + needle.length), label.slice(at + needle.length)];
356
+ };
357
+
358
+ /** Black or white, whichever reads over a hex colour; nothing for a colour given some other way. */
359
+ const inkOver = (color: string): string | undefined => {
360
+ const hex = /^#([0-9a-f]{3}|[0-9a-f]{6})$/i.exec(color.trim())?.[1];
361
+ if (!hex) return undefined;
362
+ const full = hex.length === 3 ? [...hex].map((each) => each + each).join('') : hex;
363
+ const [red, green, blue] = [0, 2, 4].map((at) => parseInt(full.slice(at, at + 2), 16));
364
+ return (red * 299 + green * 587 + blue * 114) / 1000 > 150 ? '#000000' : '#ffffff';
365
+ };
366
+ const pillStyle = (value: string) => {
367
+ const color = optionOf(value)?.color;
368
+ if (!color) return undefined;
369
+ const ink = inkOver(color);
370
+ return `background: ${color};${ink ? ` color: ${ink};` : ''}`;
371
+ };
372
+
373
+ /** One choice shown as words in the field, which give way to whatever is typed. */
374
+ const isPlain = $derived(!isMultiple && isPlainText);
375
+ const pills = $derived(isPlain ? [] : selected);
376
+
377
+ const classes = $derived([
378
+ 'autocomplete',
379
+ `autocomplete--${variant}`,
380
+ `autocomplete--${size}`,
381
+ isMultiple && 'autocomplete--isMultiple',
382
+ isOpen && 'autocomplete--isOpen',
383
+ isDisabled && 'autocomplete--isDisabled',
384
+ restProps.class ?? ''
385
+ ]);
386
+ /** What a screen reader is told as the list changes. */
387
+ const status = $derived(
388
+ !isOpen ? '' : rows === 0 ? message : `${listed.length} ${listed.length === 1 ? 'option' : 'options'}`
389
+ );
390
+ </script>
391
+
392
+ <!--
393
+ @component
394
+ ## Autocomplete
395
+
396
+ A combo box: a field that holds what has been chosen as pills, with a place to type beside
397
+ them, and under it a list of the options that match what is typed. One choice or several.
398
+ `selected` is the values chosen, as it is for `Select`. The options are a list, or a function
399
+ that finds those matching the text. With `onCreate`, text that matches nothing can be made
400
+ into a new option; with a `delimiter`, several can be typed or pasted at once.
401
+
402
+ @example
403
+ ```svelte
404
+ <Autocomplete
405
+ options={[
406
+ { value: 'woods', label: 'Woods' },
407
+ { value: 'hills', label: 'Hills', description: 'Rough going' }
408
+ ]}
409
+ bind:selected
410
+ isMultiple
411
+ placeholder="Terrain"
412
+ />
413
+ ```
414
+ -->
415
+ <svelte:window onpointerdown={onWindowPointerdown} />
416
+
417
+ <div bind:this={root} class={classes}>
418
+ <!-- svelte-ignore a11y_click_events_have_key_events, a11y_no_static_element_interactions -->
419
+ <div class="autocomplete__field" onclick={() => element?.focus()}>
420
+ {#if start}
421
+ <div class="autocomplete__start">{@render start()}</div>
422
+ {/if}
423
+ <div class="autocomplete__chosen">
424
+ {#each pills as value (value)}
425
+ <span class="autocomplete__pill" style={pillStyle(value)}>
426
+ <span class="autocomplete__pillLabel">{labelOf(value)}</span>
427
+ {#if isMultiple}
428
+ <button
429
+ type="button"
430
+ class="autocomplete__pillRemove"
431
+ aria-label={`Remove ${labelOf(value)}`}
432
+ tabindex="-1"
433
+ disabled={isDisabled}
434
+ onclick={(event) => {
435
+ event.stopPropagation();
436
+ remove(value);
437
+ element?.focus();
438
+ }}
439
+ >
440
+ <Icon Icon={IconX} size="0.75rem" />
441
+ </button>
442
+ {/if}
443
+ </span>
444
+ {/each}
445
+ {#if isPlain && selected.length > 0 && !query}
446
+ <span class="autocomplete__plain">{labelOf(selected[0])}</span>
447
+ {/if}
448
+ <input
449
+ {...restProps}
450
+ bind:this={element}
451
+ class="autocomplete__input"
452
+ type="text"
453
+ role="combobox"
454
+ autocomplete="off"
455
+ autocapitalize="off"
456
+ spellcheck="false"
457
+ aria-autocomplete="list"
458
+ aria-expanded={isOpen}
459
+ aria-controls={listId}
460
+ aria-activedescendant={isOpen && activeIndex >= 0 ? optionId(activeIndex) : undefined}
461
+ placeholder={selected.length === 0 ? restProps.placeholder : undefined}
462
+ disabled={isDisabled}
463
+ value={query}
464
+ {oninput}
465
+ {onkeydown}
466
+ {onfocus}
467
+ onclick={open}
468
+ />
469
+ </div>
470
+ {#if isClearable && selected.length > 0 && !isDisabled}
471
+ <button
472
+ type="button"
473
+ class="autocomplete__clear"
474
+ aria-label="Clear"
475
+ tabindex="-1"
476
+ onclick={(event) => {
477
+ event.stopPropagation();
478
+ clear();
479
+ }}
480
+ >
481
+ <Icon Icon={IconCircleXFilled} size="0.875rem" />
482
+ </button>
483
+ {/if}
484
+ {#if showSuggestions}
485
+ <button
486
+ type="button"
487
+ class="autocomplete__toggle"
488
+ aria-label={isOpen ? 'Close the list' : 'Open the list'}
489
+ tabindex="-1"
490
+ disabled={isDisabled}
491
+ onpointerdown={(event) => event.preventDefault()}
492
+ onclick={(event) => {
493
+ event.stopPropagation();
494
+ if (isOpen) close();
495
+ else {
496
+ element?.focus();
497
+ open();
498
+ }
499
+ }}
500
+ >
501
+ <Icon Icon={IconChevronDown} size="0.875rem" />
502
+ </button>
503
+ {/if}
504
+ </div>
505
+
506
+ <div class="autocomplete__status" role="status" aria-live="polite">{status}</div>
507
+
508
+ {#if isOpen && showSuggestions}
509
+ <ul
510
+ bind:this={menu}
511
+ id={listId}
512
+ class="autocomplete__menu"
513
+ style={floatingStyles}
514
+ role="listbox"
515
+ aria-label="Choose from the following options"
516
+ aria-multiselectable={isMultiple}
517
+ aria-busy={isBusy}
518
+ >
519
+ {#each listed as each, index (each.value)}
520
+ {#if each.group && each.group !== listed[index - 1]?.group}
521
+ <li class="autocomplete__group" role="presentation">{each.group}</li>
522
+ {/if}
523
+ <!-- svelte-ignore a11y_click_events_have_key_events -->
524
+ <li
525
+ id={optionId(index)}
526
+ class={[
527
+ 'autocomplete__option',
528
+ index === activeIndex && 'autocomplete__option--isActive',
529
+ isSelected(each.value) && 'autocomplete__option--isSelected',
530
+ each.isDisabled && 'autocomplete__option--isDisabled'
531
+ ]}
532
+ role="option"
533
+ aria-selected={isSelected(each.value)}
534
+ aria-disabled={each.isDisabled}
535
+ onpointerenter={() => !each.isDisabled && (activeIndex = index)}
536
+ onpointerdown={(event) => event.preventDefault()}
537
+ onclick={() => choose(each)}
538
+ >
539
+ {#if !isMultiple}
540
+ <span class="autocomplete__mark">
541
+ {#if isSelected(each.value)}<Icon Icon={IconCheck} size="0.875rem" />{/if}
542
+ </span>
543
+ {/if}
544
+ {#if optionSnippet}
545
+ {@render optionSnippet({
546
+ option: each,
547
+ isActive: index === activeIndex,
548
+ isSelected: isSelected(each.value),
549
+ query: needle
550
+ })}
551
+ {:else}
552
+ {@const [before, hit, after] = parts(each.label)}
553
+ <!-- The three parts are one word: a line break between them would put a space in it. -->
554
+ <!-- prettier-ignore -->
555
+ <span class="autocomplete__label">{before}<mark class="autocomplete__match">{hit}</mark>{after}</span>
556
+ {#if each.description}
557
+ <span class="autocomplete__description">{each.description}</span>
558
+ {/if}
559
+ {/if}
560
+ <span class="autocomplete__enter" aria-hidden="true"><Icon Icon={IconCornerDownLeft} size="0.75rem" /></span>
561
+ </li>
562
+ {/each}
563
+ {#if canCreate}
564
+ <!-- svelte-ignore a11y_click_events_have_key_events -->
565
+ <li
566
+ id={optionId(listed.length)}
567
+ class={[
568
+ 'autocomplete__option',
569
+ 'autocomplete__option--create',
570
+ activeIndex === listed.length && 'autocomplete__option--isActive'
571
+ ]}
572
+ role="option"
573
+ aria-selected="false"
574
+ onpointerenter={() => (activeIndex = listed.length)}
575
+ onpointerdown={(event) => event.preventDefault()}
576
+ onclick={() => void create(typed)}
577
+ >
578
+ <span class="autocomplete__mark"><Icon Icon={IconPlus} size="0.75rem" /></span>
579
+ <span class="autocomplete__label">{createLabel(typed)}</span>
580
+ <span class="autocomplete__enter" aria-hidden="true"><Icon Icon={IconCornerDownLeft} size="0.75rem" /></span>
581
+ </li>
582
+ {/if}
583
+ {#if rows === 0}
584
+ <li class="autocomplete__empty" role="presentation">{message}</li>
585
+ {/if}
586
+ </ul>
587
+ {/if}
588
+ </div>
589
+
590
+ <style>
591
+ .autocomplete {
592
+ position: relative;
593
+ display: inline-block;
594
+ width: 100%;
595
+ }
596
+
597
+ .autocomplete__field {
598
+ display: flex;
599
+ align-items: center;
600
+ gap: 0.25rem;
601
+ min-height: 2rem;
602
+ padding: 0.1875rem 0.25rem 0.1875rem 0.375rem;
603
+ font-family: var(--font-sans);
604
+ background: var(--inputBg);
605
+ border: var(--borderThin);
606
+ border-color: var(--inputBorderColor);
607
+ border-radius: var(--radius-2);
608
+ cursor: text;
609
+ }
610
+ .autocomplete__field:focus-within {
611
+ border-color: var(--fg);
612
+ background: var(--inputFocusBg);
613
+ }
614
+ .autocomplete--sm .autocomplete__field {
615
+ min-height: 1.5rem;
616
+ padding: 0.0625rem 0.125rem 0.0625rem 0.25rem;
617
+ font-size: 0.875rem;
618
+ }
619
+ .autocomplete--transparent .autocomplete__field {
620
+ border-color: transparent;
621
+ background: transparent;
622
+ }
623
+ .autocomplete--transparent .autocomplete__field:hover {
624
+ border-color: var(--inputBorderColor);
625
+ }
626
+ .autocomplete--transparent .autocomplete__field:focus-within {
627
+ border-color: var(--fg);
628
+ background: var(--inputFocusBg);
629
+ }
630
+ .autocomplete--isDisabled .autocomplete__field {
631
+ cursor: not-allowed;
632
+ opacity: 0.5;
633
+ }
634
+ .autocomplete:has([aria-invalid='true']) .autocomplete__field {
635
+ border-color: var(--fgDanger);
636
+ }
637
+
638
+ .autocomplete__start {
639
+ display: flex;
640
+ align-items: center;
641
+ flex: none;
642
+ color: var(--fgMuted);
643
+ }
644
+
645
+ /* What is chosen and the place to type, which wrap together onto as many lines as they need. */
646
+ .autocomplete__chosen {
647
+ display: flex;
648
+ align-items: center;
649
+ flex-wrap: wrap;
650
+ flex: 1;
651
+ gap: 0.25rem;
652
+ min-width: 0;
653
+ }
654
+
655
+ .autocomplete__plain {
656
+ padding-left: 0.375rem;
657
+ overflow: hidden;
658
+ text-overflow: ellipsis;
659
+ white-space: nowrap;
660
+ }
661
+
662
+ .autocomplete__input {
663
+ flex: 1;
664
+ width: 2rem;
665
+ min-width: 2rem;
666
+ height: 1.5rem;
667
+ padding: 0 0.125rem 0 0.375rem;
668
+ font: inherit;
669
+ color: inherit;
670
+ background: transparent;
671
+ border: none;
672
+ outline: none;
673
+ text-overflow: ellipsis;
674
+ }
675
+ .autocomplete--sm .autocomplete__input {
676
+ height: 1.25rem;
677
+ }
678
+ .autocomplete__input::placeholder {
679
+ color: var(--fgMuted);
680
+ }
681
+ .autocomplete__input:disabled {
682
+ cursor: not-allowed;
683
+ }
684
+ /* After words that are the choice, the place to type is only a caret at their end. */
685
+ .autocomplete__plain + .autocomplete__input {
686
+ padding-left: 0;
687
+ }
688
+
689
+ .autocomplete__toggle,
690
+ .autocomplete__clear,
691
+ .autocomplete__pillRemove {
692
+ display: flex;
693
+ align-items: center;
694
+ justify-content: center;
695
+ flex: none;
696
+ padding: 0;
697
+ color: var(--fgMuted);
698
+ background: transparent;
699
+ border: none;
700
+ border-radius: var(--radius-1);
701
+ cursor: pointer;
702
+ }
703
+ .autocomplete__toggle,
704
+ .autocomplete__clear {
705
+ width: 1.125rem;
706
+ height: 1.5rem;
707
+ }
708
+ /* The two sit together as one group at the end of the field. */
709
+ .autocomplete__clear {
710
+ margin-right: -0.125rem;
711
+ }
712
+ .autocomplete__toggle {
713
+ margin-right: 0.125rem;
714
+ }
715
+ .autocomplete__toggle:hover,
716
+ .autocomplete__clear:hover,
717
+ .autocomplete__pillRemove:hover {
718
+ color: var(--fg);
719
+ }
720
+ .autocomplete__toggle:disabled {
721
+ cursor: not-allowed;
722
+ }
723
+ .autocomplete--isOpen .autocomplete__toggle {
724
+ transform: rotate(180deg);
725
+ }
726
+
727
+ .autocomplete__pill {
728
+ display: inline-flex;
729
+ align-items: center;
730
+ gap: 0.125rem;
731
+ max-width: 100%;
732
+ height: 1.375rem;
733
+ padding: 0 0.375rem;
734
+ font-size: 0.8125rem;
735
+ font-weight: 500;
736
+ background: var(--contrastLow);
737
+ border: solid 1px var(--contrastMedium);
738
+ border-radius: var(--radius-1);
739
+ }
740
+ .autocomplete--isMultiple .autocomplete__pill {
741
+ padding-right: 0.125rem;
742
+ }
743
+ .autocomplete--sm .autocomplete__pill {
744
+ height: 1.125rem;
745
+ font-size: 0.75rem;
746
+ }
747
+ .autocomplete__pillLabel {
748
+ overflow: hidden;
749
+ text-overflow: ellipsis;
750
+ white-space: nowrap;
751
+ }
752
+ .autocomplete__pillRemove {
753
+ width: 1rem;
754
+ height: 1rem;
755
+ color: inherit;
756
+ opacity: 0.6;
757
+ }
758
+ .autocomplete__pillRemove:hover {
759
+ color: inherit;
760
+ opacity: 1;
761
+ }
762
+
763
+ /* Read out, never seen. */
764
+ .autocomplete__status {
765
+ position: absolute;
766
+ width: 1px;
767
+ height: 1px;
768
+ overflow: hidden;
769
+ clip-path: inset(50%);
770
+ white-space: nowrap;
771
+ }
772
+
773
+ .autocomplete__menu {
774
+ position: fixed;
775
+ top: 0;
776
+ left: 0;
777
+ z-index: 1000;
778
+ margin: 0;
779
+ padding: 0.25rem;
780
+ list-style: none;
781
+ overflow-y: auto;
782
+ font-family: var(--font-sans);
783
+ color: var(--fg);
784
+ background: var(--popoverBg);
785
+ border: var(--borderThin);
786
+ border-radius: var(--radius-2);
787
+ box-shadow: var(--shadow-1);
788
+ }
789
+
790
+ .autocomplete__group {
791
+ padding: 0.5rem 0.5rem 0.25rem;
792
+ font-size: 0.6875rem;
793
+ font-weight: 600;
794
+ letter-spacing: 0.05em;
795
+ text-transform: uppercase;
796
+ color: var(--fgMuted);
797
+ }
798
+
799
+ .autocomplete__option {
800
+ display: flex;
801
+ align-items: baseline;
802
+ gap: 0.5rem;
803
+ max-width: min(28rem, calc(100vw - 2rem));
804
+ padding: 0.25rem 0.375rem 0.25rem 0.5rem;
805
+ font-size: 0.875rem;
806
+ white-space: nowrap;
807
+ border: solid 2px transparent;
808
+ cursor: pointer;
809
+ }
810
+ .autocomplete__option--isActive {
811
+ background-color: var(--menuItemHover);
812
+ border: var(--menuItemBorderHover);
813
+ }
814
+ .autocomplete__option--isDisabled {
815
+ color: var(--fgMuted);
816
+ cursor: not-allowed;
817
+ }
818
+
819
+ .autocomplete__label {
820
+ flex: 1;
821
+ min-width: 0;
822
+ overflow: hidden;
823
+ text-overflow: ellipsis;
824
+ }
825
+
826
+ /* The key that takes the option lit. It keeps its place on every row, so that the list does not change width. */
827
+ .autocomplete__enter {
828
+ display: flex;
829
+ align-items: center;
830
+ justify-content: center;
831
+ align-self: center;
832
+ flex: none;
833
+ width: 1.125rem;
834
+ height: 1.125rem;
835
+ color: var(--fgMuted);
836
+ border: solid 1px var(--contrastMedium);
837
+ border-radius: var(--radius-1);
838
+ visibility: hidden;
839
+ }
840
+ .autocomplete__option--isActive .autocomplete__enter {
841
+ visibility: visible;
842
+ }
843
+
844
+ .autocomplete__mark {
845
+ display: flex;
846
+ align-items: center;
847
+ justify-content: center;
848
+ align-self: center;
849
+ flex: none;
850
+ width: 0.875rem;
851
+ height: 0.875rem;
852
+ }
853
+
854
+ .autocomplete__match {
855
+ font-weight: 700;
856
+ color: inherit;
857
+ background: transparent;
858
+ text-decoration: underline;
859
+ text-decoration-color: var(--fgPrimary);
860
+ text-underline-offset: 0.15em;
861
+ }
862
+ .autocomplete__description {
863
+ flex: none;
864
+ padding-left: 0.5rem;
865
+ font-size: 0.75rem;
866
+ color: var(--fgMuted);
867
+ }
868
+
869
+ .autocomplete__empty {
870
+ padding: 0.5rem 0.75rem;
871
+ font-size: 0.875rem;
872
+ color: var(--fgMuted);
873
+ }
874
+ </style>