@zazz-ui/core 0.1.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.
- package/CHANGELOG.md +19 -0
- package/LICENSE +21 -0
- package/README.md +61 -0
- package/dist/sri.json +75 -0
- package/dist/zazz.css +1 -0
- package/dist/zazz.js +1 -0
- package/examples/about.html +590 -0
- package/examples/components.html +1040 -0
- package/examples/forms.html +511 -0
- package/examples/index.html +268 -0
- package/examples/layout.html +320 -0
- package/examples/products.html +846 -0
- package/examples/responsive.html +339 -0
- package/package.json +85 -0
- package/src/base/_layers.css +12 -0
- package/src/base/_layout.css +568 -0
- package/src/base/_reset.css +515 -0
- package/src/base/_typography.css +495 -0
- package/src/base/_utilities.css +6628 -0
- package/src/base/_variables.css +682 -0
- package/src/base/_view-transitions.css +103 -0
- package/src/base/command-score.d.ts +12 -0
- package/src/base/command-score.d.ts.map +1 -0
- package/src/base/command-score.js +151 -0
- package/src/base/command-score.ts +192 -0
- package/src/base/dialog-lifecycle.d.ts +11 -0
- package/src/base/dialog-lifecycle.d.ts.map +1 -0
- package/src/base/dialog-lifecycle.js +65 -0
- package/src/base/dialog-lifecycle.ts +80 -0
- package/src/base/embla.d.ts +90 -0
- package/src/base/embla.d.ts.map +1 -0
- package/src/base/embla.js +643 -0
- package/src/base/embla.ts +791 -0
- package/src/base/hotkeys.d.ts +82 -0
- package/src/base/hotkeys.d.ts.map +1 -0
- package/src/base/hotkeys.js +141 -0
- package/src/base/hotkeys.ts +195 -0
- package/src/base/navigation.d.ts +2 -0
- package/src/base/navigation.d.ts.map +1 -0
- package/src/base/navigation.js +136 -0
- package/src/base/navigation.ts +151 -0
- package/src/base/reveal.d.ts +80 -0
- package/src/base/reveal.d.ts.map +1 -0
- package/src/base/reveal.js +305 -0
- package/src/base/reveal.ts +379 -0
- package/src/base/signals.d.ts +89 -0
- package/src/base/signals.d.ts.map +1 -0
- package/src/base/signals.js +134 -0
- package/src/base/signals.ts +164 -0
- package/src/base/typeahead.d.ts +105 -0
- package/src/base/typeahead.d.ts.map +1 -0
- package/src/base/typeahead.js +411 -0
- package/src/base/typeahead.ts +500 -0
- package/src/base/utils.d.ts +56 -0
- package/src/base/utils.d.ts.map +1 -0
- package/src/base/utils.js +88 -0
- package/src/base/utils.ts +98 -0
- package/src/base/zazz-element.d.ts +44 -0
- package/src/base/zazz-element.d.ts.map +1 -0
- package/src/base/zazz-element.js +59 -0
- package/src/base/zazz-element.ts +73 -0
- package/src/globals.d.ts +148 -0
- package/src/head.d.ts +95 -0
- package/src/head.d.ts.map +1 -0
- package/src/head.js +311 -0
- package/src/head.ts +433 -0
- package/src/index.css +71 -0
- package/src/index.d.ts +36 -0
- package/src/index.d.ts.map +1 -0
- package/src/index.js +35 -0
- package/src/index.ts +36 -0
- package/src/manifest.d.ts +90 -0
- package/src/manifest.d.ts.map +1 -0
- package/src/manifest.js +614 -0
- package/src/manifest.ts +667 -0
- package/src/primitives/accordion/accordion.css +104 -0
- package/src/primitives/accordion/accordion.html +69 -0
- package/src/primitives/alert-dialog/alert-dialog.css +38 -0
- package/src/primitives/alert-dialog/alert-dialog.html +53 -0
- package/src/primitives/autocomplete/autocomplete-groups.html +77 -0
- package/src/primitives/autocomplete/autocomplete.css +114 -0
- package/src/primitives/autocomplete/autocomplete.d.ts +30 -0
- package/src/primitives/autocomplete/autocomplete.d.ts.map +1 -0
- package/src/primitives/autocomplete/autocomplete.html +66 -0
- package/src/primitives/autocomplete/autocomplete.js +41 -0
- package/src/primitives/autocomplete/autocomplete.ts +46 -0
- package/src/primitives/avatar/avatar.html +44 -0
- package/src/primitives/badge/badge-icon.html +36 -0
- package/src/primitives/badge/badge.css +251 -0
- package/src/primitives/badge/badge.html +7 -0
- package/src/primitives/breadcrumbs/breadcrumbs.html +9 -0
- package/src/primitives/button/button-icon-only.html +84 -0
- package/src/primitives/button/button-icon.html +82 -0
- package/src/primitives/button/button.css +319 -0
- package/src/primitives/button/button.html +8 -0
- package/src/primitives/button-group/button-group-vertical.html +6 -0
- package/src/primitives/button-group/button-group.css +94 -0
- package/src/primitives/button-group/button-group.html +64 -0
- package/src/primitives/card/card-subgrid.html +118 -0
- package/src/primitives/card/card.html +53 -0
- package/src/primitives/carousel/carousel.css +61 -0
- package/src/primitives/carousel/carousel.d.ts +16 -0
- package/src/primitives/carousel/carousel.d.ts.map +1 -0
- package/src/primitives/carousel/carousel.html +315 -0
- package/src/primitives/carousel/carousel.js +73 -0
- package/src/primitives/carousel/carousel.ts +84 -0
- package/src/primitives/checkbox/checkbox-tasklist.html +71 -0
- package/src/primitives/checkbox/checkbox.css +82 -0
- package/src/primitives/checkbox/checkbox.d.ts +21 -0
- package/src/primitives/checkbox/checkbox.d.ts.map +1 -0
- package/src/primitives/checkbox/checkbox.html +17 -0
- package/src/primitives/checkbox/checkbox.js +190 -0
- package/src/primitives/checkbox/checkbox.ts +219 -0
- package/src/primitives/combobox/combobox-multiselect.html +91 -0
- package/src/primitives/combobox/combobox.css +307 -0
- package/src/primitives/combobox/combobox.d.ts +100 -0
- package/src/primitives/combobox/combobox.d.ts.map +1 -0
- package/src/primitives/combobox/combobox.html +73 -0
- package/src/primitives/combobox/combobox.js +503 -0
- package/src/primitives/combobox/combobox.ts +559 -0
- package/src/primitives/command/command-actions.d.ts +31 -0
- package/src/primitives/command/command-actions.d.ts.map +1 -0
- package/src/primitives/command/command-actions.html +89 -0
- package/src/primitives/command/command-actions.js +70 -0
- package/src/primitives/command/command-actions.ts +77 -0
- package/src/primitives/command/command-dialog.html +77 -0
- package/src/primitives/command/command.css +151 -0
- package/src/primitives/command/command.d.ts +52 -0
- package/src/primitives/command/command.d.ts.map +1 -0
- package/src/primitives/command/command.html +119 -0
- package/src/primitives/command/command.js +146 -0
- package/src/primitives/command/command.ts +157 -0
- package/src/primitives/dialog/dialog-with-form.html +200 -0
- package/src/primitives/dialog/dialog.css +229 -0
- package/src/primitives/dialog/dialog.html +90 -0
- package/src/primitives/fields/fields.css +278 -0
- package/src/primitives/input/input-icon-leading.html +19 -0
- package/src/primitives/input/input-icon-trailing.html +18 -0
- package/src/primitives/input/input.css +114 -0
- package/src/primitives/input/input.html +61 -0
- package/src/primitives/input-group/input-group-password-group.html +108 -0
- package/src/primitives/input-group/input-group.css +215 -0
- package/src/primitives/input-group/input-group.html +132 -0
- package/src/primitives/kbd/kbd-group.html +18 -0
- package/src/primitives/kbd/kbd.css +87 -0
- package/src/primitives/kbd/kbd.html +7 -0
- package/src/primitives/lightbox/lightbox.css +459 -0
- package/src/primitives/lightbox/lightbox.d.ts +7 -0
- package/src/primitives/lightbox/lightbox.d.ts.map +1 -0
- package/src/primitives/lightbox/lightbox.html +665 -0
- package/src/primitives/lightbox/lightbox.js +96 -0
- package/src/primitives/lightbox/lightbox.ts +113 -0
- package/src/primitives/menu/menu-interest.html +52 -0
- package/src/primitives/menu/menu.css +55 -0
- package/src/primitives/menu/menu.d.ts +38 -0
- package/src/primitives/menu/menu.d.ts.map +1 -0
- package/src/primitives/menu/menu.html +70 -0
- package/src/primitives/menu/menu.js +117 -0
- package/src/primitives/menu/menu.ts +131 -0
- package/src/primitives/menubar/menubar-help-search.html +103 -0
- package/src/primitives/menubar/menubar.html +166 -0
- package/src/primitives/meter/meter.css +178 -0
- package/src/primitives/meter/meter.html +38 -0
- package/src/primitives/mobile-menu/mobile-menu.css +145 -0
- package/src/primitives/mobile-menu/mobile-menu.html +614 -0
- package/src/primitives/navigation-menu/navigation-menu-featured.html +76 -0
- package/src/primitives/navigation-menu/navigation-menu-icon-grid.html +158 -0
- package/src/primitives/navigation-menu/navigation-menu-interest.html +103 -0
- package/src/primitives/navigation-menu/navigation-menu-megamenu.html +141 -0
- package/src/primitives/navigation-menu/navigation-menu-simple.html +96 -0
- package/src/primitives/navigation-menu/navigation-menu.css +295 -0
- package/src/primitives/navigation-menu/navigation-menu.html +576 -0
- package/src/primitives/otp/otp.css +126 -0
- package/src/primitives/otp/otp.d.ts +89 -0
- package/src/primitives/otp/otp.d.ts.map +1 -0
- package/src/primitives/otp/otp.html +22 -0
- package/src/primitives/otp/otp.js +226 -0
- package/src/primitives/otp/otp.ts +279 -0
- package/src/primitives/password-group/password-group.css +238 -0
- package/src/primitives/password-group/password-group.d.ts +25 -0
- package/src/primitives/password-group/password-group.d.ts.map +1 -0
- package/src/primitives/password-group/password-group.html +109 -0
- package/src/primitives/password-group/password-group.js +76 -0
- package/src/primitives/password-group/password-group.ts +95 -0
- package/src/primitives/popover/popover.css +233 -0
- package/src/primitives/progress/progress-indeterminate.html +1 -0
- package/src/primitives/progress/progress.css +105 -0
- package/src/primitives/progress/progress.html +7 -0
- package/src/primitives/prose/prose.html +65 -0
- package/src/primitives/radio/radio.css +84 -0
- package/src/primitives/radio/radio.html +17 -0
- package/src/primitives/reveal/reveal.css +167 -0
- package/src/primitives/select/multiselect.d.ts +43 -0
- package/src/primitives/select/multiselect.d.ts.map +1 -0
- package/src/primitives/select/multiselect.js +147 -0
- package/src/primitives/select/multiselect.ts +188 -0
- package/src/primitives/select/select-align.html +49 -0
- package/src/primitives/select/select-multiple.html +12 -0
- package/src/primitives/select/select-sides.html +65 -0
- package/src/primitives/select/select.css +481 -0
- package/src/primitives/select/select.html +16 -0
- package/src/primitives/separator/separator.css +49 -0
- package/src/primitives/separator/separator.html +16 -0
- package/src/primitives/slider/slider.css +122 -0
- package/src/primitives/slider/slider.html +1 -0
- package/src/primitives/switch/switch.css +126 -0
- package/src/primitives/switch/switch.html +13 -0
- package/src/primitives/table/table-alternating.html +36 -0
- package/src/primitives/table/table-grid.html +30 -0
- package/src/primitives/table/table-size-sm.html +26 -0
- package/src/primitives/table/table.css +158 -0
- package/src/primitives/table/table.html +45 -0
- package/src/primitives/tabs/tabs.css +328 -0
- package/src/primitives/tabs/tabs.d.ts +35 -0
- package/src/primitives/tabs/tabs.d.ts.map +1 -0
- package/src/primitives/tabs/tabs.html +75 -0
- package/src/primitives/tabs/tabs.js +84 -0
- package/src/primitives/tabs/tabs.ts +93 -0
- package/src/primitives/textarea/textarea.css +78 -0
- package/src/primitives/textarea/textarea.html +10 -0
- package/src/primitives/toaster/toaster.css +309 -0
- package/src/primitives/toaster/toaster.d.ts +138 -0
- package/src/primitives/toaster/toaster.d.ts.map +1 -0
- package/src/primitives/toaster/toaster.html +77 -0
- package/src/primitives/toaster/toaster.js +736 -0
- package/src/primitives/toaster/toaster.ts +823 -0
- package/src/primitives/toggle/toggle.css +295 -0
- package/src/primitives/toggle/toggle.html +68 -0
- package/src/primitives/toggle-group/toggle-group-multiple.html +14 -0
- package/src/primitives/toggle-group/toggle-group-vertical.html +14 -0
- package/src/primitives/toggle-group/toggle-group.css +73 -0
- package/src/primitives/toggle-group/toggle-group.html +53 -0
- package/src/primitives/toolbar/toolbar.html +45 -0
- package/src/primitives/tooltip/tooltip-disabled.html +19 -0
- package/src/primitives/tooltip/tooltip-sides.html +57 -0
- package/src/primitives/tooltip/tooltip-with-kbd.html +31 -0
- package/src/primitives/tooltip/tooltip.css +235 -0
- package/src/primitives/tooltip/tooltip.html +12 -0
- package/src/primitives/utilities/aspect-ratio.html +17 -0
- package/src/primitives/utilities/basis.html +27 -0
- package/src/primitives/utilities/col-span.html +23 -0
- package/src/primitives/utilities/compose.html +19 -0
- package/src/primitives/utilities/flex-align.html +26 -0
- package/src/primitives/utilities/flex-direction.html +36 -0
- package/src/primitives/utilities/flex-grow.html +17 -0
- package/src/primitives/utilities/gap.html +62 -0
- package/src/primitives/utilities/grid-cols.html +20 -0
- package/src/primitives/utilities/grid-pile.html +5 -0
- package/src/primitives/utilities/grid-responsive.html +31 -0
- package/src/primitives/utilities/margin-auto.html +13 -0
- package/src/primitives/utilities/object-fit.html +41 -0
- package/src/primitives/utilities/order.html +17 -0
- package/src/primitives/utilities/overflow.html +26 -0
- package/src/primitives/utilities/padding-compose.html +23 -0
- package/src/primitives/utilities/padding.html +30 -0
- package/src/primitives/utilities/position.html +13 -0
- package/src/primitives/utilities/responsive-flex.html +22 -0
- package/src/primitives/utilities/responsive-grid.html +31 -0
- package/src/primitives/utilities/size.html +22 -0
- package/src/primitives/utilities/width.html +22 -0
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @fileoverview TC39 Signals wrapper: the kit reactive-state seam.
|
|
5
|
+
* @description Zazz bets on the TC39 Signals proposal for component state. This
|
|
6
|
+
* module is the only file in the kit allowed to import `signal-polyfill`;
|
|
7
|
+
* component scripts import `state`, `computed`, and `effect` from here, so when
|
|
8
|
+
* the proposal's API shifts or engines ship native signals, one file changes.
|
|
9
|
+
*
|
|
10
|
+
* Division of labor (how Zazz components use signals):
|
|
11
|
+
* - DOM events and observers are **input adapters**: they write into `state`.
|
|
12
|
+
* - `computed` holds **pure derived logic**: the unit-testable part.
|
|
13
|
+
* - `effect` is the **output adapter**: it writes results back to the DOM.
|
|
14
|
+
* Imperative concerns (timers, transition choreography, DOM construction) stay
|
|
15
|
+
* imperative; the DOM itself remains the source of truth for element lists.
|
|
16
|
+
*
|
|
17
|
+
* The bare `signal-polyfill` specifier resolves through the page's import map in
|
|
18
|
+
* browsers (pinned jsDelivr URL) and through `node_modules` in tests/bundlers.
|
|
19
|
+
*
|
|
20
|
+
* @see https://github.com/tc39/proposal-signals
|
|
21
|
+
* @see https://github.com/proposal-signals/signal-polyfill
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
import { Signal } from "signal-polyfill";
|
|
25
|
+
|
|
26
|
+
// The disposer `effect` returns implements the dispose protocol, and `using`
|
|
27
|
+
// compiles (target ES2022) to try/finally helpers that read this well-known
|
|
28
|
+
// symbol at runtime; engines without native Explicit Resource Management
|
|
29
|
+
// (Safari) don't define it, so give them a local stand-in.
|
|
30
|
+
(Symbol as { dispose: symbol }).dispose ??= Symbol("Symbol.dispose");
|
|
31
|
+
|
|
32
|
+
// --- State and computed ---
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* @description Creates a mutable signal: a reactive value read with `.get()`
|
|
36
|
+
* and written with `.set()`. Reads inside `computed`/`effect` are tracked.
|
|
37
|
+
*
|
|
38
|
+
* @param initialValue - The starting value.
|
|
39
|
+
* @param options - Signal options (e.g. a custom `equals`).
|
|
40
|
+
* @returns The state signal.
|
|
41
|
+
* @example
|
|
42
|
+
* const expanded = state(false);
|
|
43
|
+
* expanded.set(true);
|
|
44
|
+
*/
|
|
45
|
+
function state<T>(initialValue: T, options?: Signal.Options<T>): Signal.State<T> {
|
|
46
|
+
return new Signal.State(initialValue, options);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* @description Creates a derived signal: recomputed lazily when a tracked
|
|
51
|
+
* dependency changes, cached otherwise. Keep the computation pure.
|
|
52
|
+
*
|
|
53
|
+
* @param computation - Pure function deriving the value from other signals.
|
|
54
|
+
* @param options - Signal options (e.g. a custom `equals`).
|
|
55
|
+
* @returns The computed signal.
|
|
56
|
+
* @example
|
|
57
|
+
* const paused = computed(() => expanded.get() || hidden.get());
|
|
58
|
+
*/
|
|
59
|
+
function computed<T>(computation: () => T, options?: Signal.Options<T>): Signal.Computed<T> {
|
|
60
|
+
return new Signal.Computed(computation, options);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// --- Effects ---
|
|
64
|
+
|
|
65
|
+
/** Options for `effect`. */
|
|
66
|
+
interface EffectOptions {
|
|
67
|
+
/** Aborting disposes the effect: pass an element's controller signal. */
|
|
68
|
+
signal?: AbortSignal;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
/** An effect callback returns either nothing or a cleanup function. */
|
|
72
|
+
type EffectCallback = () => void | (() => void);
|
|
73
|
+
|
|
74
|
+
/** The disposer `effect` returns: callable, and a `using`-compatible `Disposable`. */
|
|
75
|
+
type EffectDispose = (() => void) & Disposable;
|
|
76
|
+
|
|
77
|
+
// The proposal deliberately ships no effect(): this is the reference
|
|
78
|
+
// implementation from the polyfill README: one shared Watcher notifies
|
|
79
|
+
// synchronously on the first dirty signal, and re-runs are batched to a
|
|
80
|
+
// microtask so N writes in one task trigger one re-run.
|
|
81
|
+
let needsEnqueue = true;
|
|
82
|
+
|
|
83
|
+
const watcher = new Signal.subtle.Watcher(() => {
|
|
84
|
+
if (needsEnqueue) {
|
|
85
|
+
needsEnqueue = false;
|
|
86
|
+
queueMicrotask(processPending);
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* @description Re-runs every dirty effect and re-arms the watcher.
|
|
92
|
+
*
|
|
93
|
+
* @private
|
|
94
|
+
*/
|
|
95
|
+
function processPending(): void {
|
|
96
|
+
needsEnqueue = true;
|
|
97
|
+
for (const dirty of watcher.getPending()) {
|
|
98
|
+
dirty.get();
|
|
99
|
+
}
|
|
100
|
+
watcher.watch();
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* @description Runs `callback` immediately, tracks every signal it reads, and
|
|
105
|
+
* re-runs it (batched to a microtask) whenever one changes. The callback may
|
|
106
|
+
* return a cleanup function, run before each re-run and on disposal.
|
|
107
|
+
*
|
|
108
|
+
* @param callback - The tracked side effect.
|
|
109
|
+
* @param options - Pass an element's `AbortSignal` to tie disposal to teardown.
|
|
110
|
+
* @returns A dispose function (idempotent; also invoked by the abort signal).
|
|
111
|
+
* It implements `Symbol.dispose`, so a scoped effect (TypeScript sources only)
|
|
112
|
+
* can be bound with `using` and disposed automatically at scope exit.
|
|
113
|
+
* @example
|
|
114
|
+
* const dispose = effect(() => {
|
|
115
|
+
* node.dataset.paused = String(paused.get());
|
|
116
|
+
* });
|
|
117
|
+
* @example
|
|
118
|
+
* using dispose = effect(() => track(count.get())); // disposed at scope exit
|
|
119
|
+
*/
|
|
120
|
+
function effect(callback: EffectCallback, options: EffectOptions = {}): EffectDispose {
|
|
121
|
+
let cleanup: (() => void) | void;
|
|
122
|
+
let disposed = false;
|
|
123
|
+
|
|
124
|
+
const tracked = new Signal.Computed(() => {
|
|
125
|
+
if (typeof cleanup === "function") cleanup();
|
|
126
|
+
cleanup = callback();
|
|
127
|
+
});
|
|
128
|
+
|
|
129
|
+
watcher.watch(tracked);
|
|
130
|
+
tracked.get();
|
|
131
|
+
|
|
132
|
+
const dispose = (): void => {
|
|
133
|
+
if (disposed) return;
|
|
134
|
+
disposed = true;
|
|
135
|
+
watcher.unwatch(tracked);
|
|
136
|
+
if (typeof cleanup === "function") cleanup();
|
|
137
|
+
cleanup = undefined;
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
options.signal?.addEventListener("abort", dispose, { once: true });
|
|
141
|
+
return Object.assign(dispose, { [Symbol.dispose]: dispose });
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// --- Public API ---
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* @namespace Signals
|
|
148
|
+
* @description Reactive state for Zazz components, wrapping the TC39 Signals
|
|
149
|
+
* polyfill. Import `state`/`computed`/`effect` from this module, never from
|
|
150
|
+
* `signal-polyfill` directly.
|
|
151
|
+
*
|
|
152
|
+
* @property state - Creates a mutable signal.
|
|
153
|
+
* @property computed - Creates a pure derived signal.
|
|
154
|
+
* @property effect - Runs a tracked side effect with batched re-runs.
|
|
155
|
+
*/
|
|
156
|
+
const Signals = { state, computed, effect };
|
|
157
|
+
|
|
158
|
+
// Attach to window for the documented public API, then export for module consumers.
|
|
159
|
+
if (typeof window !== "undefined") {
|
|
160
|
+
window.Signals = Signals;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
export { Signals, state, computed, effect };
|
|
164
|
+
export type { EffectCallback, EffectDispose, EffectOptions };
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { ZazzElement } from "./zazz-element.ts";
|
|
2
|
+
/** What the ranker needs to know about one item. */
|
|
3
|
+
interface ItemFacts {
|
|
4
|
+
/** The text scored and committed: `data-value` ?? trimmed text content. */
|
|
5
|
+
value: string;
|
|
6
|
+
/** Extra match targets from `data-keywords`. */
|
|
7
|
+
keywords: string[];
|
|
8
|
+
}
|
|
9
|
+
/** The ranker's verdict for one item, in input order. */
|
|
10
|
+
interface RankedItem {
|
|
11
|
+
/** The item's index in the input array. */
|
|
12
|
+
index: number;
|
|
13
|
+
/** `commandScore` result, 0 to 1; 1 for every item when the query is empty. */
|
|
14
|
+
score: number;
|
|
15
|
+
/** Whether the item should be hidden. */
|
|
16
|
+
hidden: boolean;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* @description Scores every item against the query. An empty query leaves all
|
|
20
|
+
* items visible with a neutral score.
|
|
21
|
+
*
|
|
22
|
+
* @param query - What the user typed.
|
|
23
|
+
* @param items - Facts for each item, in DOM order.
|
|
24
|
+
* @returns One verdict per item, same order as the input.
|
|
25
|
+
*/
|
|
26
|
+
declare function rankItems(query: string, items: readonly ItemFacts[]): RankedItem[];
|
|
27
|
+
/**
|
|
28
|
+
* @description Reducer for active-item keyboard navigation. ArrowDown from
|
|
29
|
+
* nothing highlights the first item, ArrowUp from nothing the last; both wrap.
|
|
30
|
+
*
|
|
31
|
+
* @param current - The currently active index, -1 for none.
|
|
32
|
+
* @param key - The `KeyboardEvent.key` pressed.
|
|
33
|
+
* @param count - How many items are visible.
|
|
34
|
+
* @returns The next active index, -1 when there is nothing to highlight.
|
|
35
|
+
*/
|
|
36
|
+
declare function nextActiveIndex(current: number, key: string, count: number): number;
|
|
37
|
+
/**
|
|
38
|
+
* @description Base class for the typeahead family. Subclasses set the slot
|
|
39
|
+
* prefix and commit behavior; the base owns query state, ranking, keyboard
|
|
40
|
+
* navigation, ARIA wiring, and (when `managesPanel`) the manual popover.
|
|
41
|
+
*/
|
|
42
|
+
declare abstract class TypeaheadElement extends ZazzElement {
|
|
43
|
+
#private;
|
|
44
|
+
/** Slot prefix: `"autocomplete"` finds `autocomplete-panel`, `-list`, `-item`. */
|
|
45
|
+
protected abstract readonly slotPrefix: string;
|
|
46
|
+
/** Whether this element opens/closes its own `popover="manual"` panel. */
|
|
47
|
+
protected readonly managesPanel: boolean;
|
|
48
|
+
/**
|
|
49
|
+
* Whether focusing the input opens the panel. Combobox opts out: its input
|
|
50
|
+
* holds a committed display value, so tabbing through a form must not pop
|
|
51
|
+
* the list open, any more than tabbing to a select opens its picker.
|
|
52
|
+
*/
|
|
53
|
+
protected readonly openOnFocus: boolean;
|
|
54
|
+
/** Whether ranking also re-orders visually via inline `order`. */
|
|
55
|
+
protected get sortByScore(): boolean;
|
|
56
|
+
/** Whether filtering auto-highlights the best item (command palettes do). */
|
|
57
|
+
protected readonly autoHighlight: boolean;
|
|
58
|
+
/**
|
|
59
|
+
* Applies a committed item: fill the input, sync a value, activate.
|
|
60
|
+
* `source` indicates how the commit happened: a pointer commit has already run
|
|
61
|
+
* the item native activation (link, invoker command); a keyboard commit
|
|
62
|
+
* has not.
|
|
63
|
+
*/
|
|
64
|
+
protected abstract commit(item: HTMLElement, source: "keyboard" | "pointer"): void;
|
|
65
|
+
protected searchInput: HTMLInputElement | null;
|
|
66
|
+
protected panel: HTMLElement | null;
|
|
67
|
+
protected readonly query: import("signal-polyfill").Signal.State<string>;
|
|
68
|
+
protected readonly open: import("signal-polyfill").Signal.State<boolean>;
|
|
69
|
+
protected readonly activeIndex: import("signal-polyfill").Signal.State<number>;
|
|
70
|
+
protected setup(signal: AbortSignal): void;
|
|
71
|
+
/**
|
|
72
|
+
* @description The panel's items, in DOM order.
|
|
73
|
+
*
|
|
74
|
+
* @returns Every `<prefix>-item` element in the panel.
|
|
75
|
+
*/
|
|
76
|
+
protected items(): HTMLElement[];
|
|
77
|
+
/**
|
|
78
|
+
* @description The visible items in visual order: score order when
|
|
79
|
+
* `data-sort="score"`, DOM order otherwise, so arrow keys always follow
|
|
80
|
+
* what the user sees.
|
|
81
|
+
*
|
|
82
|
+
* @param items - All items, DOM order.
|
|
83
|
+
* @param ranked - The ranker's verdicts for those items.
|
|
84
|
+
* @returns Visible items in visual order.
|
|
85
|
+
*/
|
|
86
|
+
protected visibleItems(items: readonly HTMLElement[], ranked: readonly RankedItem[]): HTMLElement[];
|
|
87
|
+
/**
|
|
88
|
+
* @description The text an item matches and commits with. The text-content
|
|
89
|
+
* fallback excludes `<kbd>` shortcut hints, which are presentation, not
|
|
90
|
+
* value ("Go to docs ⇧⌘D" matches and announces as "Go to docs").
|
|
91
|
+
*
|
|
92
|
+
* @param item - The item element.
|
|
93
|
+
* @returns `data-value` when present, trimmed hint-free text otherwise.
|
|
94
|
+
*/
|
|
95
|
+
protected itemValue(item: HTMLElement): string;
|
|
96
|
+
/**
|
|
97
|
+
* @description Escape with the panel already closed. Autocomplete and command
|
|
98
|
+
* empty the field; combobox overrides this, because its input carries a
|
|
99
|
+
* committed display value rather than the filter.
|
|
100
|
+
*/
|
|
101
|
+
protected clearQuery(): void;
|
|
102
|
+
}
|
|
103
|
+
export { TypeaheadElement, rankItems, nextActiveIndex };
|
|
104
|
+
export type { ItemFacts, RankedItem };
|
|
105
|
+
//# sourceMappingURL=typeahead.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"typeahead.d.ts","sourceRoot":"","sources":["typeahead.ts"],"names":[],"mappings":"AAuBA,OAAO,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAKhD,oDAAoD;AACpD,UAAU,SAAS;IACjB,2EAA2E;IAC3E,KAAK,EAAE,MAAM,CAAC;IACd,gDAAgD;IAChD,QAAQ,EAAE,MAAM,EAAE,CAAC;CACpB;AAED,yDAAyD;AACzD,UAAU,UAAU;IAClB,2CAA2C;IAC3C,KAAK,EAAE,MAAM,CAAC;IACd,+EAA+E;IAC/E,KAAK,EAAE,MAAM,CAAC;IACd,yCAAyC;IACzC,MAAM,EAAE,OAAO,CAAC;CACjB;AAED;;;;;;;GAOG;AACH,iBAAS,SAAS,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,SAAS,SAAS,EAAE,GAAG,UAAU,EAAE,CAM3E;AAED;;;;;;;;GAQG;AACH,iBAAS,eAAe,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,CAc5E;AAMD;;;;GAIG;AACH,uBAAe,gBAAiB,SAAQ,WAAW;;IACjD,kFAAkF;IAClF,SAAS,CAAC,QAAQ,CAAC,QAAQ,CAAC,UAAU,EAAE,MAAM,CAAC;IAC/C,0EAA0E;IAC1E,SAAS,CAAC,QAAQ,CAAC,YAAY,EAAE,OAAO,CAAQ;IAChD;;;;OAIG;IACH,SAAS,CAAC,QAAQ,CAAC,WAAW,EAAE,OAAO,CAAQ;IAC/C,kEAAkE;IAClE,SAAS,KAAK,WAAW,IAAI,OAAO,CAEnC;IACD,6EAA6E;IAC7E,SAAS,CAAC,QAAQ,CAAC,aAAa,EAAE,OAAO,CAAS;IAElD;;;;;OAKG;IACH,SAAS,CAAC,QAAQ,CAAC,MAAM,CAAC,IAAI,EAAE,WAAW,EAAE,MAAM,EAAE,UAAU,GAAG,SAAS,GAAG,IAAI,CAAC;IAEnF,SAAS,CAAC,WAAW,EAAE,gBAAgB,GAAG,IAAI,CAAQ;IACtD,SAAS,CAAC,KAAK,EAAE,WAAW,GAAG,IAAI,CAAQ;IAE3C,SAAS,CAAC,QAAQ,CAAC,KAAK,iDAAa;IACrC,SAAS,CAAC,QAAQ,CAAC,IAAI,kDAAgB;IACvC,SAAS,CAAC,QAAQ,CAAC,WAAW,iDAAa;IAE3C,SAAS,CAAC,KAAK,CAAC,MAAM,EAAE,WAAW,GAAG,IAAI,CA2MzC;IAED;;;;OAIG;IACH,SAAS,CAAC,KAAK,IAAI,WAAW,EAAE,CAM/B;IAED;;;;;;;;OAQG;IACH,SAAS,CAAC,YAAY,CACpB,KAAK,EAAE,SAAS,WAAW,EAAE,EAC7B,MAAM,EAAE,SAAS,UAAU,EAAE,GAC5B,WAAW,EAAE,CAIf;IA6BD;;;;;;;OAOG;IACH,SAAS,CAAC,SAAS,CAAC,IAAI,EAAE,WAAW,GAAG,MAAM,CAO7C;IAED;;;;OAIG;IACH,SAAS,CAAC,UAAU,IAAI,IAAI,CAK3B;CA6EF;AAED,OAAO,EAAE,gBAAgB,EAAE,SAAS,EAAE,eAAe,EAAE,CAAC;AACxD,YAAY,EAAE,SAAS,EAAE,UAAU,EAAE,CAAC"}
|
|
@@ -0,0 +1,411 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @fileoverview Shared typeahead engine for autocomplete, combobox, and command.
|
|
4
|
+
* @description The common runtime behind the filter-as-you-type family: a
|
|
5
|
+
* query signal fed by the input, score-based ranking of real DOM items
|
|
6
|
+
* (`commandScore`), active-item keyboard navigation with
|
|
7
|
+
* `aria-activedescendant` (focus never leaves the input), and, for form
|
|
8
|
+
* controls whose panel sits outside the input, ownership of a
|
|
9
|
+
* `popover="manual"` panel with outside-pointerdown and Escape close paths.
|
|
10
|
+
*
|
|
11
|
+
* Subclasses (`UiAutocomplete`, `UiCombobox`, `UiCommand`) declare their slot
|
|
12
|
+
* prefix and commit semantics; everything else lives here. Filtering only
|
|
13
|
+
* writes `hidden` and (when ranking) inline `order`: the DOM is never
|
|
14
|
+
* restructured, so forms, focus, and progressive enhancement stay untouched.
|
|
15
|
+
* Groups and empty states hide with CSS `:has()`, not code.
|
|
16
|
+
*
|
|
17
|
+
* Item facts come from the markup: the match/commit text is `data-value`
|
|
18
|
+
* (falling back to text content) and `data-keywords` adds extra match
|
|
19
|
+
* targets. `data-sort="score"` on the root re-ranks visually via `order`.
|
|
20
|
+
*/
|
|
21
|
+
import { commandScore } from "./command-score.js";
|
|
22
|
+
import { ZazzElement } from "./zazz-element.js";
|
|
23
|
+
import { effect, state } from "./signals.js";
|
|
24
|
+
/**
|
|
25
|
+
* @description Scores every item against the query. An empty query leaves all
|
|
26
|
+
* items visible with a neutral score.
|
|
27
|
+
*
|
|
28
|
+
* @param query - What the user typed.
|
|
29
|
+
* @param items - Facts for each item, in DOM order.
|
|
30
|
+
* @returns One verdict per item, same order as the input.
|
|
31
|
+
*/
|
|
32
|
+
function rankItems(query, items) {
|
|
33
|
+
const trimmed = query.trim();
|
|
34
|
+
return items.map((item, index) => {
|
|
35
|
+
const score = trimmed === "" ? 1 : commandScore(item.value, trimmed, item.keywords);
|
|
36
|
+
return { index, score, hidden: score <= 0 };
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* @description Reducer for active-item keyboard navigation. ArrowDown from
|
|
41
|
+
* nothing highlights the first item, ArrowUp from nothing the last; both wrap.
|
|
42
|
+
*
|
|
43
|
+
* @param current - The currently active index, -1 for none.
|
|
44
|
+
* @param key - The `KeyboardEvent.key` pressed.
|
|
45
|
+
* @param count - How many items are visible.
|
|
46
|
+
* @returns The next active index, -1 when there is nothing to highlight.
|
|
47
|
+
*/
|
|
48
|
+
function nextActiveIndex(current, key, count) {
|
|
49
|
+
if (count === 0)
|
|
50
|
+
return -1;
|
|
51
|
+
switch (key) {
|
|
52
|
+
case "ArrowDown":
|
|
53
|
+
return current < 0 ? 0 : (current + 1) % count;
|
|
54
|
+
case "ArrowUp":
|
|
55
|
+
return current < 0 ? count - 1 : (current - 1 + count) % count;
|
|
56
|
+
case "Home":
|
|
57
|
+
return 0;
|
|
58
|
+
case "End":
|
|
59
|
+
return count - 1;
|
|
60
|
+
default:
|
|
61
|
+
return current;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
// --- Element base ---
|
|
65
|
+
let typeaheadIdCounter = 0;
|
|
66
|
+
/**
|
|
67
|
+
* @description Base class for the typeahead family. Subclasses set the slot
|
|
68
|
+
* prefix and commit behavior; the base owns query state, ranking, keyboard
|
|
69
|
+
* navigation, ARIA wiring, and (when `managesPanel`) the manual popover.
|
|
70
|
+
*/
|
|
71
|
+
class TypeaheadElement extends ZazzElement {
|
|
72
|
+
/** Whether this element opens/closes its own `popover="manual"` panel. */
|
|
73
|
+
managesPanel = true;
|
|
74
|
+
/**
|
|
75
|
+
* Whether focusing the input opens the panel. Combobox opts out: its input
|
|
76
|
+
* holds a committed display value, so tabbing through a form must not pop
|
|
77
|
+
* the list open, any more than tabbing to a select opens its picker.
|
|
78
|
+
*/
|
|
79
|
+
openOnFocus = true;
|
|
80
|
+
/** Whether ranking also re-orders visually via inline `order`. */
|
|
81
|
+
get sortByScore() {
|
|
82
|
+
return this.getAttribute("data-sort") === "score";
|
|
83
|
+
}
|
|
84
|
+
/** Whether filtering auto-highlights the best item (command palettes do). */
|
|
85
|
+
autoHighlight = false;
|
|
86
|
+
searchInput = null;
|
|
87
|
+
panel = null;
|
|
88
|
+
query = state("");
|
|
89
|
+
open = state(false);
|
|
90
|
+
activeIndex = state(-1);
|
|
91
|
+
setup(signal) {
|
|
92
|
+
const prefix = this.slotPrefix;
|
|
93
|
+
const panel = this.querySelector(`[data-slot~="${prefix}-panel"]`);
|
|
94
|
+
const input = this.querySelector('input[role="combobox"]') ??
|
|
95
|
+
this.querySelector(`[data-slot~="${prefix}-input"]`);
|
|
96
|
+
if (!(panel instanceof HTMLElement) || !(input instanceof HTMLInputElement))
|
|
97
|
+
return;
|
|
98
|
+
this.panel = panel;
|
|
99
|
+
this.searchInput = input;
|
|
100
|
+
const list = panel.querySelector(`[data-slot~="${prefix}-list"]`);
|
|
101
|
+
if (list instanceof HTMLElement) {
|
|
102
|
+
list.id ||= `ui-${prefix}-list-${++typeaheadIdCounter}`;
|
|
103
|
+
input.setAttribute("aria-controls", list.id);
|
|
104
|
+
}
|
|
105
|
+
// Input adapters: DOM events only write signals
|
|
106
|
+
input.addEventListener("input", () => {
|
|
107
|
+
this.query.set(input.value);
|
|
108
|
+
this.activeIndex.set(this.autoHighlight ? 0 : -1);
|
|
109
|
+
if (this.managesPanel && !this.#inlinePanel()) {
|
|
110
|
+
this.open.set(input.value.length >= this.#minLength());
|
|
111
|
+
}
|
|
112
|
+
}, { signal });
|
|
113
|
+
input.addEventListener("keydown", (event) => this.#onKeydown(event), { signal });
|
|
114
|
+
if (this.#inlinePanel()) {
|
|
115
|
+
// Inline variant: a panel without [popover] renders in flow and is
|
|
116
|
+
// always open; there is nothing to show, hide, or light-dismiss
|
|
117
|
+
this.open.set(true);
|
|
118
|
+
input.setAttribute("aria-expanded", "true");
|
|
119
|
+
}
|
|
120
|
+
else if (this.managesPanel) {
|
|
121
|
+
if (this.openOnFocus) {
|
|
122
|
+
input.addEventListener("focus", () => {
|
|
123
|
+
if (input.value.length >= this.#minLength())
|
|
124
|
+
this.open.set(true);
|
|
125
|
+
}, { signal });
|
|
126
|
+
}
|
|
127
|
+
// Outside pointerdown closes; inside the panel it must not steal focus
|
|
128
|
+
document.addEventListener("pointerdown", (event) => {
|
|
129
|
+
if (!(event.target instanceof Node))
|
|
130
|
+
return;
|
|
131
|
+
if (this.contains(event.target))
|
|
132
|
+
return;
|
|
133
|
+
this.open.set(false);
|
|
134
|
+
}, { signal });
|
|
135
|
+
this.addEventListener("focusout", (event) => {
|
|
136
|
+
const next = event.relatedTarget;
|
|
137
|
+
if (next instanceof Node && this.contains(next))
|
|
138
|
+
return;
|
|
139
|
+
this.open.set(false);
|
|
140
|
+
}, { signal });
|
|
141
|
+
}
|
|
142
|
+
else if (panel instanceof HTMLDialogElement) {
|
|
143
|
+
// Native <dialog> surface: mirror the dialog-lifecycle events
|
|
144
|
+
this.addEventListener("zazz:dialog-open", (event) => {
|
|
145
|
+
if (event.target !== panel)
|
|
146
|
+
return;
|
|
147
|
+
this.open.set(true);
|
|
148
|
+
input.focus();
|
|
149
|
+
}, { signal });
|
|
150
|
+
this.addEventListener("zazz:dialog-close", (event) => {
|
|
151
|
+
if (event.target !== panel)
|
|
152
|
+
return;
|
|
153
|
+
this.open.set(false);
|
|
154
|
+
}, { signal });
|
|
155
|
+
}
|
|
156
|
+
else {
|
|
157
|
+
// Native popover="auto" surface owns open/close; mirror it
|
|
158
|
+
panel.addEventListener("toggle", (event) => {
|
|
159
|
+
const opened = event.newState === "open";
|
|
160
|
+
this.open.set(opened);
|
|
161
|
+
if (opened)
|
|
162
|
+
input.focus();
|
|
163
|
+
}, { signal });
|
|
164
|
+
}
|
|
165
|
+
// Keep focus in the input. List rows are not focusable, so a mousedown on
|
|
166
|
+
// one blurs the input, and the resulting focusout closes the panel (and, in
|
|
167
|
+
// combobox, reverts the query) *before* the click that commits, and the
|
|
168
|
+
// commit's own input.focus() then re-fires the focus handler that reopens
|
|
169
|
+
// it. mousedown, not pointerdown: preventing pointerdown would also cancel
|
|
170
|
+
// touch panning inside the scrollable panel. Scoped to the list rather than
|
|
171
|
+
// the whole panel so native scrollbar dragging and header text selection
|
|
172
|
+
// survive. Suppressing focus transfer does not suppress the click, so link
|
|
173
|
+
// and invoker items still activate.
|
|
174
|
+
panel.addEventListener("mousedown", (event) => {
|
|
175
|
+
if (!(event.target instanceof Element))
|
|
176
|
+
return;
|
|
177
|
+
if (event.target.closest("input, textarea, select, [contenteditable]"))
|
|
178
|
+
return;
|
|
179
|
+
if (!event.target.closest(`[data-slot~="${prefix}-list"]`))
|
|
180
|
+
return;
|
|
181
|
+
event.preventDefault();
|
|
182
|
+
}, { signal });
|
|
183
|
+
// Item click = commit (the mousedown guard above keeps focus in the input,
|
|
184
|
+
// so no focusout races the click)
|
|
185
|
+
panel.addEventListener("click", (event) => {
|
|
186
|
+
if (!(event.target instanceof Element))
|
|
187
|
+
return;
|
|
188
|
+
const item = event.target.closest(`[data-slot~="${prefix}-item"]`);
|
|
189
|
+
if (item && !item.hidden)
|
|
190
|
+
this.commit(item, "pointer");
|
|
191
|
+
}, { signal });
|
|
192
|
+
// Output adapter 1: panel visibility and expanded state (the inline
|
|
193
|
+
// variant is unconditionally open, so it binds no visibility effect)
|
|
194
|
+
if (this.#inlinePanel()) {
|
|
195
|
+
// Nothing to drive
|
|
196
|
+
}
|
|
197
|
+
else if (this.managesPanel) {
|
|
198
|
+
effect(() => {
|
|
199
|
+
const opened = this.open.get();
|
|
200
|
+
input.setAttribute("aria-expanded", String(opened));
|
|
201
|
+
if (opened && !this.#panelOpen())
|
|
202
|
+
panel.showPopover();
|
|
203
|
+
else if (!opened && this.#panelOpen())
|
|
204
|
+
panel.hidePopover();
|
|
205
|
+
if (!opened)
|
|
206
|
+
this.activeIndex.set(-1);
|
|
207
|
+
}, { signal });
|
|
208
|
+
}
|
|
209
|
+
else {
|
|
210
|
+
effect(() => {
|
|
211
|
+
input.setAttribute("aria-expanded", String(this.open.get()));
|
|
212
|
+
}, { signal });
|
|
213
|
+
}
|
|
214
|
+
// Output adapter 2: ranking, visibility, highlight, activedescendant.
|
|
215
|
+
// All three signals are read up front so every run tracks the same set;
|
|
216
|
+
// `open` both gates and subscribes.
|
|
217
|
+
effect(() => {
|
|
218
|
+
const open = this.open.get();
|
|
219
|
+
const query = this.query.get();
|
|
220
|
+
const active = this.activeIndex.get();
|
|
221
|
+
// Re-filtering the list while the popover is still fading out is the
|
|
222
|
+
// visible "flash on close": committing or reverting clears the query,
|
|
223
|
+
// and the rows collapse or re-expand mid-transition. While a
|
|
224
|
+
// self-managed panel is closed nothing is written: the rows keep their
|
|
225
|
+
// last filter state and highlight through the exit transition, and
|
|
226
|
+
// reopening re-ranks in the same microtask drain as showPopover(), so
|
|
227
|
+
// both land before one paint. The inline variant has no panel to fade,
|
|
228
|
+
// and a panel owned by a native surface (command) can become visible
|
|
229
|
+
// before its `toggle` task mirrors `open`, so neither is gated.
|
|
230
|
+
if (!open && this.managesPanel && !this.#inlinePanel()) {
|
|
231
|
+
input.removeAttribute("aria-activedescendant");
|
|
232
|
+
return;
|
|
233
|
+
}
|
|
234
|
+
const { items, ranked, visible } = this.#rank(query);
|
|
235
|
+
const sort = this.sortByScore;
|
|
236
|
+
for (const verdict of ranked) {
|
|
237
|
+
const item = items[verdict.index];
|
|
238
|
+
item.hidden = verdict.hidden;
|
|
239
|
+
if (sort)
|
|
240
|
+
item.style.order = String(-Math.round(verdict.score * 1000));
|
|
241
|
+
else if (item.style.order)
|
|
242
|
+
item.style.removeProperty("order");
|
|
243
|
+
}
|
|
244
|
+
visible.forEach((item, index) => {
|
|
245
|
+
item.id ||= `ui-${prefix}-item-${++typeaheadIdCounter}`;
|
|
246
|
+
if (index === active)
|
|
247
|
+
item.setAttribute("data-highlighted", "");
|
|
248
|
+
else
|
|
249
|
+
item.removeAttribute("data-highlighted");
|
|
250
|
+
});
|
|
251
|
+
const highlighted = active >= 0 ? visible[active] : undefined;
|
|
252
|
+
if (highlighted) {
|
|
253
|
+
input.setAttribute("aria-activedescendant", highlighted.id);
|
|
254
|
+
highlighted.scrollIntoView({ block: "nearest" });
|
|
255
|
+
}
|
|
256
|
+
else {
|
|
257
|
+
input.removeAttribute("aria-activedescendant");
|
|
258
|
+
}
|
|
259
|
+
}, { signal });
|
|
260
|
+
}
|
|
261
|
+
/**
|
|
262
|
+
* @description The panel's items, in DOM order.
|
|
263
|
+
*
|
|
264
|
+
* @returns Every `<prefix>-item` element in the panel.
|
|
265
|
+
*/
|
|
266
|
+
items() {
|
|
267
|
+
const panel = this.panel;
|
|
268
|
+
if (!panel)
|
|
269
|
+
return [];
|
|
270
|
+
return Array.from(panel.querySelectorAll(`[data-slot~="${this.slotPrefix}-item"]`)).filter((node) => node instanceof HTMLElement);
|
|
271
|
+
}
|
|
272
|
+
/**
|
|
273
|
+
* @description The visible items in visual order: score order when
|
|
274
|
+
* `data-sort="score"`, DOM order otherwise, so arrow keys always follow
|
|
275
|
+
* what the user sees.
|
|
276
|
+
*
|
|
277
|
+
* @param items - All items, DOM order.
|
|
278
|
+
* @param ranked - The ranker's verdicts for those items.
|
|
279
|
+
* @returns Visible items in visual order.
|
|
280
|
+
*/
|
|
281
|
+
visibleItems(items, ranked) {
|
|
282
|
+
const visible = ranked.filter((verdict) => !verdict.hidden);
|
|
283
|
+
if (this.sortByScore)
|
|
284
|
+
visible.sort((a, b) => b.score - a.score || a.index - b.index);
|
|
285
|
+
return visible.map((verdict) => items[verdict.index]);
|
|
286
|
+
}
|
|
287
|
+
/**
|
|
288
|
+
* @description Ranks every item against a query without touching the DOM.
|
|
289
|
+
* Keyboard navigation must not read `item.hidden`: the output effect above
|
|
290
|
+
* deliberately leaves those flags stale while the panel is closed, so a fresh
|
|
291
|
+
* ranking is the only trustworthy filter state.
|
|
292
|
+
*
|
|
293
|
+
* @param query - What the user typed.
|
|
294
|
+
* @returns The items in DOM order, their verdicts, and the visible subset in
|
|
295
|
+
* visual order.
|
|
296
|
+
* @private
|
|
297
|
+
*/
|
|
298
|
+
#rank(query) {
|
|
299
|
+
const items = this.items();
|
|
300
|
+
const ranked = rankItems(query, items.map((item) => ({
|
|
301
|
+
value: this.itemValue(item),
|
|
302
|
+
keywords: (item.getAttribute("data-keywords") ?? "").split(/\s+/).filter(Boolean),
|
|
303
|
+
})));
|
|
304
|
+
return { items, ranked, visible: this.visibleItems(items, ranked) };
|
|
305
|
+
}
|
|
306
|
+
/**
|
|
307
|
+
* @description The text an item matches and commits with. The text-content
|
|
308
|
+
* fallback excludes `<kbd>` shortcut hints, which are presentation, not
|
|
309
|
+
* value ("Go to docs ⇧⌘D" matches and announces as "Go to docs").
|
|
310
|
+
*
|
|
311
|
+
* @param item - The item element.
|
|
312
|
+
* @returns `data-value` when present, trimmed hint-free text otherwise.
|
|
313
|
+
*/
|
|
314
|
+
itemValue(item) {
|
|
315
|
+
const explicit = item.getAttribute("data-value");
|
|
316
|
+
if (explicit !== null)
|
|
317
|
+
return explicit;
|
|
318
|
+
if (!item.querySelector("kbd, ui-kbd-group"))
|
|
319
|
+
return item.textContent?.trim() ?? "";
|
|
320
|
+
const clone = item.cloneNode(true);
|
|
321
|
+
for (const hint of clone.querySelectorAll("kbd, ui-kbd-group"))
|
|
322
|
+
hint.remove();
|
|
323
|
+
return clone.textContent?.trim() ?? "";
|
|
324
|
+
}
|
|
325
|
+
/**
|
|
326
|
+
* @description Escape with the panel already closed. Autocomplete and command
|
|
327
|
+
* empty the field; combobox overrides this, because its input carries a
|
|
328
|
+
* committed display value rather than the filter.
|
|
329
|
+
*/
|
|
330
|
+
clearQuery() {
|
|
331
|
+
const input = this.searchInput;
|
|
332
|
+
if (!input)
|
|
333
|
+
return;
|
|
334
|
+
input.value = "";
|
|
335
|
+
this.query.set("");
|
|
336
|
+
}
|
|
337
|
+
/**
|
|
338
|
+
* @description Minimum query length before the panel opens (`data-min-length`).
|
|
339
|
+
*
|
|
340
|
+
* @returns The threshold, 0 by default.
|
|
341
|
+
* @private
|
|
342
|
+
*/
|
|
343
|
+
#minLength() {
|
|
344
|
+
const raw = Number(this.getAttribute("data-min-length"));
|
|
345
|
+
return Number.isFinite(raw) && raw > 0 ? raw : 0;
|
|
346
|
+
}
|
|
347
|
+
/**
|
|
348
|
+
* @description Whether the panel is the inline variant: rendered in flow
|
|
349
|
+
* without `[popover]`, and therefore always open.
|
|
350
|
+
*
|
|
351
|
+
* @returns True for an inline panel.
|
|
352
|
+
* @private
|
|
353
|
+
*/
|
|
354
|
+
#inlinePanel() {
|
|
355
|
+
return this.managesPanel && this.panel !== null && !this.panel.hasAttribute("popover");
|
|
356
|
+
}
|
|
357
|
+
/**
|
|
358
|
+
* @description Whether the panel popover is currently shown (polyfill-aware).
|
|
359
|
+
*
|
|
360
|
+
* @returns True when open.
|
|
361
|
+
* @private
|
|
362
|
+
*/
|
|
363
|
+
#panelOpen() {
|
|
364
|
+
return this.panel?.matches(":popover-open, .\\:popover-open") ?? false;
|
|
365
|
+
}
|
|
366
|
+
/**
|
|
367
|
+
* @description Keyboard contract on the search input: arrows/Home/End move
|
|
368
|
+
* the highlight, Enter commits it, Escape closes then clears (only when the
|
|
369
|
+
* element manages its own panel: native surfaces own Escape themselves).
|
|
370
|
+
*
|
|
371
|
+
* @param event - The keydown event.
|
|
372
|
+
* @private
|
|
373
|
+
*/
|
|
374
|
+
#onKeydown(event) {
|
|
375
|
+
const input = this.searchInput;
|
|
376
|
+
if (!input)
|
|
377
|
+
return;
|
|
378
|
+
if (event.key === "Escape" && this.managesPanel) {
|
|
379
|
+
// The inline variant has no panel to close: Escape only clears
|
|
380
|
+
if (this.open.get() && !this.#inlinePanel()) {
|
|
381
|
+
event.preventDefault();
|
|
382
|
+
this.open.set(false);
|
|
383
|
+
}
|
|
384
|
+
else if (input.value) {
|
|
385
|
+
event.preventDefault();
|
|
386
|
+
this.clearQuery();
|
|
387
|
+
}
|
|
388
|
+
return;
|
|
389
|
+
}
|
|
390
|
+
if (["ArrowDown", "ArrowUp", "Home", "End"].includes(event.key)) {
|
|
391
|
+
// Home/End belong to the text caret unless an item is already active
|
|
392
|
+
if ((event.key === "Home" || event.key === "End") && this.activeIndex.get() < 0)
|
|
393
|
+
return;
|
|
394
|
+
event.preventDefault();
|
|
395
|
+
if (this.managesPanel && !this.open.get())
|
|
396
|
+
this.open.set(true);
|
|
397
|
+
const count = this.#rank(this.query.get()).visible.length;
|
|
398
|
+
this.activeIndex.set(nextActiveIndex(this.activeIndex.get(), event.key, count));
|
|
399
|
+
return;
|
|
400
|
+
}
|
|
401
|
+
if (event.key === "Enter") {
|
|
402
|
+
const active = this.activeIndex.get();
|
|
403
|
+
const item = active >= 0 ? this.#rank(this.query.get()).visible[active] : undefined;
|
|
404
|
+
if (item) {
|
|
405
|
+
event.preventDefault();
|
|
406
|
+
this.commit(item, "keyboard");
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
export { TypeaheadElement, rankItems, nextActiveIndex };
|