@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,500 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @fileoverview Shared typeahead engine for autocomplete, combobox, and command.
|
|
5
|
+
* @description The common runtime behind the filter-as-you-type family: a
|
|
6
|
+
* query signal fed by the input, score-based ranking of real DOM items
|
|
7
|
+
* (`commandScore`), active-item keyboard navigation with
|
|
8
|
+
* `aria-activedescendant` (focus never leaves the input), and, for form
|
|
9
|
+
* controls whose panel sits outside the input, ownership of a
|
|
10
|
+
* `popover="manual"` panel with outside-pointerdown and Escape close paths.
|
|
11
|
+
*
|
|
12
|
+
* Subclasses (`UiAutocomplete`, `UiCombobox`, `UiCommand`) declare their slot
|
|
13
|
+
* prefix and commit semantics; everything else lives here. Filtering only
|
|
14
|
+
* writes `hidden` and (when ranking) inline `order`: the DOM is never
|
|
15
|
+
* restructured, so forms, focus, and progressive enhancement stay untouched.
|
|
16
|
+
* Groups and empty states hide with CSS `:has()`, not code.
|
|
17
|
+
*
|
|
18
|
+
* Item facts come from the markup: the match/commit text is `data-value`
|
|
19
|
+
* (falling back to text content) and `data-keywords` adds extra match
|
|
20
|
+
* targets. `data-sort="score"` on the root re-ranks visually via `order`.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
import { commandScore } from "./command-score.ts";
|
|
24
|
+
import { ZazzElement } from "./zazz-element.ts";
|
|
25
|
+
import { effect, state } from "./signals.ts";
|
|
26
|
+
|
|
27
|
+
// --- Pure ranking and navigation ---
|
|
28
|
+
|
|
29
|
+
/** What the ranker needs to know about one item. */
|
|
30
|
+
interface ItemFacts {
|
|
31
|
+
/** The text scored and committed: `data-value` ?? trimmed text content. */
|
|
32
|
+
value: string;
|
|
33
|
+
/** Extra match targets from `data-keywords`. */
|
|
34
|
+
keywords: string[];
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** The ranker's verdict for one item, in input order. */
|
|
38
|
+
interface RankedItem {
|
|
39
|
+
/** The item's index in the input array. */
|
|
40
|
+
index: number;
|
|
41
|
+
/** `commandScore` result, 0 to 1; 1 for every item when the query is empty. */
|
|
42
|
+
score: number;
|
|
43
|
+
/** Whether the item should be hidden. */
|
|
44
|
+
hidden: boolean;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* @description Scores every item against the query. An empty query leaves all
|
|
49
|
+
* items visible with a neutral score.
|
|
50
|
+
*
|
|
51
|
+
* @param query - What the user typed.
|
|
52
|
+
* @param items - Facts for each item, in DOM order.
|
|
53
|
+
* @returns One verdict per item, same order as the input.
|
|
54
|
+
*/
|
|
55
|
+
function rankItems(query: string, items: readonly ItemFacts[]): RankedItem[] {
|
|
56
|
+
const trimmed = query.trim();
|
|
57
|
+
return items.map((item, index) => {
|
|
58
|
+
const score = trimmed === "" ? 1 : commandScore(item.value, trimmed, item.keywords);
|
|
59
|
+
return { index, score, hidden: score <= 0 };
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* @description Reducer for active-item keyboard navigation. ArrowDown from
|
|
65
|
+
* nothing highlights the first item, ArrowUp from nothing the last; both wrap.
|
|
66
|
+
*
|
|
67
|
+
* @param current - The currently active index, -1 for none.
|
|
68
|
+
* @param key - The `KeyboardEvent.key` pressed.
|
|
69
|
+
* @param count - How many items are visible.
|
|
70
|
+
* @returns The next active index, -1 when there is nothing to highlight.
|
|
71
|
+
*/
|
|
72
|
+
function nextActiveIndex(current: number, key: string, count: number): number {
|
|
73
|
+
if (count === 0) return -1;
|
|
74
|
+
switch (key) {
|
|
75
|
+
case "ArrowDown":
|
|
76
|
+
return current < 0 ? 0 : (current + 1) % count;
|
|
77
|
+
case "ArrowUp":
|
|
78
|
+
return current < 0 ? count - 1 : (current - 1 + count) % count;
|
|
79
|
+
case "Home":
|
|
80
|
+
return 0;
|
|
81
|
+
case "End":
|
|
82
|
+
return count - 1;
|
|
83
|
+
default:
|
|
84
|
+
return current;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
// --- Element base ---
|
|
89
|
+
|
|
90
|
+
let typeaheadIdCounter = 0;
|
|
91
|
+
|
|
92
|
+
/**
|
|
93
|
+
* @description Base class for the typeahead family. Subclasses set the slot
|
|
94
|
+
* prefix and commit behavior; the base owns query state, ranking, keyboard
|
|
95
|
+
* navigation, ARIA wiring, and (when `managesPanel`) the manual popover.
|
|
96
|
+
*/
|
|
97
|
+
abstract class TypeaheadElement extends ZazzElement {
|
|
98
|
+
/** Slot prefix: `"autocomplete"` finds `autocomplete-panel`, `-list`, `-item`. */
|
|
99
|
+
protected abstract readonly slotPrefix: string;
|
|
100
|
+
/** Whether this element opens/closes its own `popover="manual"` panel. */
|
|
101
|
+
protected readonly managesPanel: boolean = true;
|
|
102
|
+
/**
|
|
103
|
+
* Whether focusing the input opens the panel. Combobox opts out: its input
|
|
104
|
+
* holds a committed display value, so tabbing through a form must not pop
|
|
105
|
+
* the list open, any more than tabbing to a select opens its picker.
|
|
106
|
+
*/
|
|
107
|
+
protected readonly openOnFocus: boolean = true;
|
|
108
|
+
/** Whether ranking also re-orders visually via inline `order`. */
|
|
109
|
+
protected get sortByScore(): boolean {
|
|
110
|
+
return this.getAttribute("data-sort") === "score";
|
|
111
|
+
}
|
|
112
|
+
/** Whether filtering auto-highlights the best item (command palettes do). */
|
|
113
|
+
protected readonly autoHighlight: boolean = false;
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Applies a committed item: fill the input, sync a value, activate.
|
|
117
|
+
* `source` indicates how the commit happened: a pointer commit has already run
|
|
118
|
+
* the item native activation (link, invoker command); a keyboard commit
|
|
119
|
+
* has not.
|
|
120
|
+
*/
|
|
121
|
+
protected abstract commit(item: HTMLElement, source: "keyboard" | "pointer"): void;
|
|
122
|
+
|
|
123
|
+
protected searchInput: HTMLInputElement | null = null;
|
|
124
|
+
protected panel: HTMLElement | null = null;
|
|
125
|
+
|
|
126
|
+
protected readonly query = state("");
|
|
127
|
+
protected readonly open = state(false);
|
|
128
|
+
protected readonly activeIndex = state(-1);
|
|
129
|
+
|
|
130
|
+
protected setup(signal: AbortSignal): void {
|
|
131
|
+
const prefix = this.slotPrefix;
|
|
132
|
+
const panel = this.querySelector(`[data-slot~="${prefix}-panel"]`);
|
|
133
|
+
const input =
|
|
134
|
+
this.querySelector<HTMLInputElement>('input[role="combobox"]') ??
|
|
135
|
+
this.querySelector<HTMLInputElement>(`[data-slot~="${prefix}-input"]`);
|
|
136
|
+
if (!(panel instanceof HTMLElement) || !(input instanceof HTMLInputElement)) return;
|
|
137
|
+
this.panel = panel;
|
|
138
|
+
this.searchInput = input;
|
|
139
|
+
|
|
140
|
+
const list = panel.querySelector(`[data-slot~="${prefix}-list"]`);
|
|
141
|
+
if (list instanceof HTMLElement) {
|
|
142
|
+
list.id ||= `ui-${prefix}-list-${++typeaheadIdCounter}`;
|
|
143
|
+
input.setAttribute("aria-controls", list.id);
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// Input adapters: DOM events only write signals
|
|
147
|
+
input.addEventListener(
|
|
148
|
+
"input",
|
|
149
|
+
() => {
|
|
150
|
+
this.query.set(input.value);
|
|
151
|
+
this.activeIndex.set(this.autoHighlight ? 0 : -1);
|
|
152
|
+
if (this.managesPanel && !this.#inlinePanel()) {
|
|
153
|
+
this.open.set(input.value.length >= this.#minLength());
|
|
154
|
+
}
|
|
155
|
+
},
|
|
156
|
+
{ signal },
|
|
157
|
+
);
|
|
158
|
+
|
|
159
|
+
input.addEventListener("keydown", (event) => this.#onKeydown(event), { signal });
|
|
160
|
+
|
|
161
|
+
if (this.#inlinePanel()) {
|
|
162
|
+
// Inline variant: a panel without [popover] renders in flow and is
|
|
163
|
+
// always open; there is nothing to show, hide, or light-dismiss
|
|
164
|
+
this.open.set(true);
|
|
165
|
+
input.setAttribute("aria-expanded", "true");
|
|
166
|
+
} else if (this.managesPanel) {
|
|
167
|
+
if (this.openOnFocus) {
|
|
168
|
+
input.addEventListener(
|
|
169
|
+
"focus",
|
|
170
|
+
() => {
|
|
171
|
+
if (input.value.length >= this.#minLength()) this.open.set(true);
|
|
172
|
+
},
|
|
173
|
+
{ signal },
|
|
174
|
+
);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// Outside pointerdown closes; inside the panel it must not steal focus
|
|
178
|
+
document.addEventListener(
|
|
179
|
+
"pointerdown",
|
|
180
|
+
(event) => {
|
|
181
|
+
if (!(event.target instanceof Node)) return;
|
|
182
|
+
if (this.contains(event.target)) return;
|
|
183
|
+
this.open.set(false);
|
|
184
|
+
},
|
|
185
|
+
{ signal },
|
|
186
|
+
);
|
|
187
|
+
|
|
188
|
+
this.addEventListener(
|
|
189
|
+
"focusout",
|
|
190
|
+
(event) => {
|
|
191
|
+
const next = event.relatedTarget;
|
|
192
|
+
if (next instanceof Node && this.contains(next)) return;
|
|
193
|
+
this.open.set(false);
|
|
194
|
+
},
|
|
195
|
+
{ signal },
|
|
196
|
+
);
|
|
197
|
+
} else if (panel instanceof HTMLDialogElement) {
|
|
198
|
+
// Native <dialog> surface: mirror the dialog-lifecycle events
|
|
199
|
+
this.addEventListener(
|
|
200
|
+
"zazz:dialog-open",
|
|
201
|
+
(event) => {
|
|
202
|
+
if (event.target !== panel) return;
|
|
203
|
+
this.open.set(true);
|
|
204
|
+
input.focus();
|
|
205
|
+
},
|
|
206
|
+
{ signal },
|
|
207
|
+
);
|
|
208
|
+
this.addEventListener(
|
|
209
|
+
"zazz:dialog-close",
|
|
210
|
+
(event) => {
|
|
211
|
+
if (event.target !== panel) return;
|
|
212
|
+
this.open.set(false);
|
|
213
|
+
},
|
|
214
|
+
{ signal },
|
|
215
|
+
);
|
|
216
|
+
} else {
|
|
217
|
+
// Native popover="auto" surface owns open/close; mirror it
|
|
218
|
+
panel.addEventListener(
|
|
219
|
+
"toggle",
|
|
220
|
+
(event) => {
|
|
221
|
+
const opened = (event as ToggleEvent).newState === "open";
|
|
222
|
+
this.open.set(opened);
|
|
223
|
+
if (opened) input.focus();
|
|
224
|
+
},
|
|
225
|
+
{ signal },
|
|
226
|
+
);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
// Keep focus in the input. List rows are not focusable, so a mousedown on
|
|
230
|
+
// one blurs the input, and the resulting focusout closes the panel (and, in
|
|
231
|
+
// combobox, reverts the query) *before* the click that commits, and the
|
|
232
|
+
// commit's own input.focus() then re-fires the focus handler that reopens
|
|
233
|
+
// it. mousedown, not pointerdown: preventing pointerdown would also cancel
|
|
234
|
+
// touch panning inside the scrollable panel. Scoped to the list rather than
|
|
235
|
+
// the whole panel so native scrollbar dragging and header text selection
|
|
236
|
+
// survive. Suppressing focus transfer does not suppress the click, so link
|
|
237
|
+
// and invoker items still activate.
|
|
238
|
+
panel.addEventListener(
|
|
239
|
+
"mousedown",
|
|
240
|
+
(event) => {
|
|
241
|
+
if (!(event.target instanceof Element)) return;
|
|
242
|
+
if (event.target.closest("input, textarea, select, [contenteditable]")) return;
|
|
243
|
+
if (!event.target.closest(`[data-slot~="${prefix}-list"]`)) return;
|
|
244
|
+
event.preventDefault();
|
|
245
|
+
},
|
|
246
|
+
{ signal },
|
|
247
|
+
);
|
|
248
|
+
|
|
249
|
+
// Item click = commit (the mousedown guard above keeps focus in the input,
|
|
250
|
+
// so no focusout races the click)
|
|
251
|
+
panel.addEventListener(
|
|
252
|
+
"click",
|
|
253
|
+
(event) => {
|
|
254
|
+
if (!(event.target instanceof Element)) return;
|
|
255
|
+
const item = event.target.closest<HTMLElement>(`[data-slot~="${prefix}-item"]`);
|
|
256
|
+
if (item && !item.hidden) this.commit(item, "pointer");
|
|
257
|
+
},
|
|
258
|
+
{ signal },
|
|
259
|
+
);
|
|
260
|
+
|
|
261
|
+
// Output adapter 1: panel visibility and expanded state (the inline
|
|
262
|
+
// variant is unconditionally open, so it binds no visibility effect)
|
|
263
|
+
if (this.#inlinePanel()) {
|
|
264
|
+
// Nothing to drive
|
|
265
|
+
} else if (this.managesPanel) {
|
|
266
|
+
effect(
|
|
267
|
+
() => {
|
|
268
|
+
const opened = this.open.get();
|
|
269
|
+
input.setAttribute("aria-expanded", String(opened));
|
|
270
|
+
if (opened && !this.#panelOpen()) panel.showPopover();
|
|
271
|
+
else if (!opened && this.#panelOpen()) panel.hidePopover();
|
|
272
|
+
if (!opened) this.activeIndex.set(-1);
|
|
273
|
+
},
|
|
274
|
+
{ signal },
|
|
275
|
+
);
|
|
276
|
+
} else {
|
|
277
|
+
effect(
|
|
278
|
+
() => {
|
|
279
|
+
input.setAttribute("aria-expanded", String(this.open.get()));
|
|
280
|
+
},
|
|
281
|
+
{ signal },
|
|
282
|
+
);
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
// Output adapter 2: ranking, visibility, highlight, activedescendant.
|
|
286
|
+
// All three signals are read up front so every run tracks the same set;
|
|
287
|
+
// `open` both gates and subscribes.
|
|
288
|
+
effect(
|
|
289
|
+
() => {
|
|
290
|
+
const open = this.open.get();
|
|
291
|
+
const query = this.query.get();
|
|
292
|
+
const active = this.activeIndex.get();
|
|
293
|
+
|
|
294
|
+
// Re-filtering the list while the popover is still fading out is the
|
|
295
|
+
// visible "flash on close": committing or reverting clears the query,
|
|
296
|
+
// and the rows collapse or re-expand mid-transition. While a
|
|
297
|
+
// self-managed panel is closed nothing is written: the rows keep their
|
|
298
|
+
// last filter state and highlight through the exit transition, and
|
|
299
|
+
// reopening re-ranks in the same microtask drain as showPopover(), so
|
|
300
|
+
// both land before one paint. The inline variant has no panel to fade,
|
|
301
|
+
// and a panel owned by a native surface (command) can become visible
|
|
302
|
+
// before its `toggle` task mirrors `open`, so neither is gated.
|
|
303
|
+
if (!open && this.managesPanel && !this.#inlinePanel()) {
|
|
304
|
+
input.removeAttribute("aria-activedescendant");
|
|
305
|
+
return;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
const { items, ranked, visible } = this.#rank(query);
|
|
309
|
+
const sort = this.sortByScore;
|
|
310
|
+
for (const verdict of ranked) {
|
|
311
|
+
const item = items[verdict.index];
|
|
312
|
+
item.hidden = verdict.hidden;
|
|
313
|
+
if (sort) item.style.order = String(-Math.round(verdict.score * 1000));
|
|
314
|
+
else if (item.style.order) item.style.removeProperty("order");
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
visible.forEach((item, index) => {
|
|
318
|
+
item.id ||= `ui-${prefix}-item-${++typeaheadIdCounter}`;
|
|
319
|
+
if (index === active) item.setAttribute("data-highlighted", "");
|
|
320
|
+
else item.removeAttribute("data-highlighted");
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
const highlighted = active >= 0 ? visible[active] : undefined;
|
|
324
|
+
if (highlighted) {
|
|
325
|
+
input.setAttribute("aria-activedescendant", highlighted.id);
|
|
326
|
+
highlighted.scrollIntoView({ block: "nearest" });
|
|
327
|
+
} else {
|
|
328
|
+
input.removeAttribute("aria-activedescendant");
|
|
329
|
+
}
|
|
330
|
+
},
|
|
331
|
+
{ signal },
|
|
332
|
+
);
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
/**
|
|
336
|
+
* @description The panel's items, in DOM order.
|
|
337
|
+
*
|
|
338
|
+
* @returns Every `<prefix>-item` element in the panel.
|
|
339
|
+
*/
|
|
340
|
+
protected items(): HTMLElement[] {
|
|
341
|
+
const panel = this.panel;
|
|
342
|
+
if (!panel) return [];
|
|
343
|
+
return Array.from(panel.querySelectorAll(`[data-slot~="${this.slotPrefix}-item"]`)).filter(
|
|
344
|
+
(node): node is HTMLElement => node instanceof HTMLElement,
|
|
345
|
+
);
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
/**
|
|
349
|
+
* @description The visible items in visual order: score order when
|
|
350
|
+
* `data-sort="score"`, DOM order otherwise, so arrow keys always follow
|
|
351
|
+
* what the user sees.
|
|
352
|
+
*
|
|
353
|
+
* @param items - All items, DOM order.
|
|
354
|
+
* @param ranked - The ranker's verdicts for those items.
|
|
355
|
+
* @returns Visible items in visual order.
|
|
356
|
+
*/
|
|
357
|
+
protected visibleItems(
|
|
358
|
+
items: readonly HTMLElement[],
|
|
359
|
+
ranked: readonly RankedItem[],
|
|
360
|
+
): HTMLElement[] {
|
|
361
|
+
const visible = ranked.filter((verdict) => !verdict.hidden);
|
|
362
|
+
if (this.sortByScore) visible.sort((a, b) => b.score - a.score || a.index - b.index);
|
|
363
|
+
return visible.map((verdict) => items[verdict.index]);
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
/**
|
|
367
|
+
* @description Ranks every item against a query without touching the DOM.
|
|
368
|
+
* Keyboard navigation must not read `item.hidden`: the output effect above
|
|
369
|
+
* deliberately leaves those flags stale while the panel is closed, so a fresh
|
|
370
|
+
* ranking is the only trustworthy filter state.
|
|
371
|
+
*
|
|
372
|
+
* @param query - What the user typed.
|
|
373
|
+
* @returns The items in DOM order, their verdicts, and the visible subset in
|
|
374
|
+
* visual order.
|
|
375
|
+
* @private
|
|
376
|
+
*/
|
|
377
|
+
#rank(query: string): {
|
|
378
|
+
items: HTMLElement[];
|
|
379
|
+
ranked: RankedItem[];
|
|
380
|
+
visible: HTMLElement[];
|
|
381
|
+
} {
|
|
382
|
+
const items = this.items();
|
|
383
|
+
const ranked = rankItems(
|
|
384
|
+
query,
|
|
385
|
+
items.map((item) => ({
|
|
386
|
+
value: this.itemValue(item),
|
|
387
|
+
keywords: (item.getAttribute("data-keywords") ?? "").split(/\s+/).filter(Boolean),
|
|
388
|
+
})),
|
|
389
|
+
);
|
|
390
|
+
return { items, ranked, visible: this.visibleItems(items, ranked) };
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
/**
|
|
394
|
+
* @description The text an item matches and commits with. The text-content
|
|
395
|
+
* fallback excludes `<kbd>` shortcut hints, which are presentation, not
|
|
396
|
+
* value ("Go to docs ⇧⌘D" matches and announces as "Go to docs").
|
|
397
|
+
*
|
|
398
|
+
* @param item - The item element.
|
|
399
|
+
* @returns `data-value` when present, trimmed hint-free text otherwise.
|
|
400
|
+
*/
|
|
401
|
+
protected itemValue(item: HTMLElement): string {
|
|
402
|
+
const explicit = item.getAttribute("data-value");
|
|
403
|
+
if (explicit !== null) return explicit;
|
|
404
|
+
if (!item.querySelector("kbd, ui-kbd-group")) return item.textContent?.trim() ?? "";
|
|
405
|
+
const clone = item.cloneNode(true) as HTMLElement;
|
|
406
|
+
for (const hint of clone.querySelectorAll("kbd, ui-kbd-group")) hint.remove();
|
|
407
|
+
return clone.textContent?.trim() ?? "";
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
/**
|
|
411
|
+
* @description Escape with the panel already closed. Autocomplete and command
|
|
412
|
+
* empty the field; combobox overrides this, because its input carries a
|
|
413
|
+
* committed display value rather than the filter.
|
|
414
|
+
*/
|
|
415
|
+
protected clearQuery(): void {
|
|
416
|
+
const input = this.searchInput;
|
|
417
|
+
if (!input) return;
|
|
418
|
+
input.value = "";
|
|
419
|
+
this.query.set("");
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
/**
|
|
423
|
+
* @description Minimum query length before the panel opens (`data-min-length`).
|
|
424
|
+
*
|
|
425
|
+
* @returns The threshold, 0 by default.
|
|
426
|
+
* @private
|
|
427
|
+
*/
|
|
428
|
+
#minLength(): number {
|
|
429
|
+
const raw = Number(this.getAttribute("data-min-length"));
|
|
430
|
+
return Number.isFinite(raw) && raw > 0 ? raw : 0;
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
/**
|
|
434
|
+
* @description Whether the panel is the inline variant: rendered in flow
|
|
435
|
+
* without `[popover]`, and therefore always open.
|
|
436
|
+
*
|
|
437
|
+
* @returns True for an inline panel.
|
|
438
|
+
* @private
|
|
439
|
+
*/
|
|
440
|
+
#inlinePanel(): boolean {
|
|
441
|
+
return this.managesPanel && this.panel !== null && !this.panel.hasAttribute("popover");
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
/**
|
|
445
|
+
* @description Whether the panel popover is currently shown (polyfill-aware).
|
|
446
|
+
*
|
|
447
|
+
* @returns True when open.
|
|
448
|
+
* @private
|
|
449
|
+
*/
|
|
450
|
+
#panelOpen(): boolean {
|
|
451
|
+
return this.panel?.matches(":popover-open, .\\:popover-open") ?? false;
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
/**
|
|
455
|
+
* @description Keyboard contract on the search input: arrows/Home/End move
|
|
456
|
+
* the highlight, Enter commits it, Escape closes then clears (only when the
|
|
457
|
+
* element manages its own panel: native surfaces own Escape themselves).
|
|
458
|
+
*
|
|
459
|
+
* @param event - The keydown event.
|
|
460
|
+
* @private
|
|
461
|
+
*/
|
|
462
|
+
#onKeydown(event: KeyboardEvent): void {
|
|
463
|
+
const input = this.searchInput;
|
|
464
|
+
if (!input) return;
|
|
465
|
+
|
|
466
|
+
if (event.key === "Escape" && this.managesPanel) {
|
|
467
|
+
// The inline variant has no panel to close: Escape only clears
|
|
468
|
+
if (this.open.get() && !this.#inlinePanel()) {
|
|
469
|
+
event.preventDefault();
|
|
470
|
+
this.open.set(false);
|
|
471
|
+
} else if (input.value) {
|
|
472
|
+
event.preventDefault();
|
|
473
|
+
this.clearQuery();
|
|
474
|
+
}
|
|
475
|
+
return;
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
if (["ArrowDown", "ArrowUp", "Home", "End"].includes(event.key)) {
|
|
479
|
+
// Home/End belong to the text caret unless an item is already active
|
|
480
|
+
if ((event.key === "Home" || event.key === "End") && this.activeIndex.get() < 0) return;
|
|
481
|
+
event.preventDefault();
|
|
482
|
+
if (this.managesPanel && !this.open.get()) this.open.set(true);
|
|
483
|
+
const count = this.#rank(this.query.get()).visible.length;
|
|
484
|
+
this.activeIndex.set(nextActiveIndex(this.activeIndex.get(), event.key, count));
|
|
485
|
+
return;
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
if (event.key === "Enter") {
|
|
489
|
+
const active = this.activeIndex.get();
|
|
490
|
+
const item = active >= 0 ? this.#rank(this.query.get()).visible[active] : undefined;
|
|
491
|
+
if (item) {
|
|
492
|
+
event.preventDefault();
|
|
493
|
+
this.commit(item, "keyboard");
|
|
494
|
+
}
|
|
495
|
+
}
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
export { TypeaheadElement, rankItems, nextActiveIndex };
|
|
500
|
+
export type { ItemFacts, RankedItem };
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @fileoverview Data attribute and value parsing utilities.
|
|
3
|
+
* @description Shared helpers for converting HTML data-attribute strings into
|
|
4
|
+
* typed JavaScript values.
|
|
5
|
+
*
|
|
6
|
+
* This is a **public, consumer-facing** surface (`window.Utils`, documented at
|
|
7
|
+
* /docs/components/utils), not just kit plumbing: data-attribute configuration
|
|
8
|
+
* is the kit's documented pattern for authoring a component
|
|
9
|
+
* (CONVENTIONS.scripts.md), so anyone writing a component in the Zazz idiom
|
|
10
|
+
* needs this exact parser to match kit behaviour. Keep it here rather than
|
|
11
|
+
* folding it into a caller — see docs/adr/0004-keep-utils-public.md.
|
|
12
|
+
*
|
|
13
|
+
* Callers that need a typed result should wrap it at their own boundary (see
|
|
14
|
+
* `readCarouselOptions` in embla.ts) rather than narrowing at each call.
|
|
15
|
+
*/
|
|
16
|
+
/**
|
|
17
|
+
* @description Converts string values to their appropriate JavaScript types.
|
|
18
|
+
*
|
|
19
|
+
* @param value - The string value to convert.
|
|
20
|
+
* @returns Converted value in the appropriate type.
|
|
21
|
+
*
|
|
22
|
+
* @example
|
|
23
|
+
* parseValue("true"); // true
|
|
24
|
+
* parseValue("42"); // 42
|
|
25
|
+
* parseValue("[1,2,3]"); // [1, 2, 3]
|
|
26
|
+
* parseValue("hello"); // "hello"
|
|
27
|
+
*/
|
|
28
|
+
declare function parseValue(value: string): boolean | number | unknown[] | string;
|
|
29
|
+
/**
|
|
30
|
+
* @description Extracts and parses data attributes with a specific prefix from a DOM node.
|
|
31
|
+
*
|
|
32
|
+
* Converts kebab-case attribute names to camelCase object keys.
|
|
33
|
+
*
|
|
34
|
+
* @param node - The DOM element to extract attributes from.
|
|
35
|
+
* @param prefix - The attribute prefix to look for (e.g. `"data-carousel-"`).
|
|
36
|
+
* @returns Object with camelCase keys and parsed values.
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* // <div data-carousel-auto-play="true" data-carousel-slide-count="5">
|
|
40
|
+
* parseDataAttributes(element, "data-carousel-");
|
|
41
|
+
* // { autoPlay: true, slideCount: 5 }
|
|
42
|
+
*/
|
|
43
|
+
declare const parseDataAttributes: (node: Element, prefix: string) => Record<string, unknown>;
|
|
44
|
+
/**
|
|
45
|
+
* @namespace Utils
|
|
46
|
+
* @description Shared DOM and data-attribute parsing utilities.
|
|
47
|
+
*
|
|
48
|
+
* @property parseValue - Converts string values to typed values.
|
|
49
|
+
* @property parseDataAttributes - Parses prefixed data attributes on a node.
|
|
50
|
+
*/
|
|
51
|
+
declare const Utils: {
|
|
52
|
+
parseValue: typeof parseValue;
|
|
53
|
+
parseDataAttributes: typeof parseDataAttributes;
|
|
54
|
+
};
|
|
55
|
+
export { Utils };
|
|
56
|
+
//# sourceMappingURL=utils.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["utils.ts"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;GAcG;AAIH;;;;;;;;;;;GAWG;AACH,iBAAS,UAAU,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,GAAG,MAAM,GAAG,OAAO,EAAE,GAAG,MAAM,CAexE;AAID;;;;;;;;;;;;;GAaG;AACH,QAAA,MAAM,mBAAmB,SAAU,OAAO,UAAU,MAAM,KAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAYlF,CAAC;AAEF;;;;;;GAMG;AACH,QAAA,MAAM,KAAK;;;CAGV,CAAC;AAQF,OAAO,EAAE,KAAK,EAAE,CAAC"}
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @fileoverview Data attribute and value parsing utilities.
|
|
4
|
+
* @description Shared helpers for converting HTML data-attribute strings into
|
|
5
|
+
* typed JavaScript values.
|
|
6
|
+
*
|
|
7
|
+
* This is a **public, consumer-facing** surface (`window.Utils`, documented at
|
|
8
|
+
* /docs/components/utils), not just kit plumbing: data-attribute configuration
|
|
9
|
+
* is the kit's documented pattern for authoring a component
|
|
10
|
+
* (CONVENTIONS.scripts.md), so anyone writing a component in the Zazz idiom
|
|
11
|
+
* needs this exact parser to match kit behaviour. Keep it here rather than
|
|
12
|
+
* folding it into a caller — see docs/adr/0004-keep-utils-public.md.
|
|
13
|
+
*
|
|
14
|
+
* Callers that need a typed result should wrap it at their own boundary (see
|
|
15
|
+
* `readCarouselOptions` in embla.ts) rather than narrowing at each call.
|
|
16
|
+
*/
|
|
17
|
+
// --- Data type conversion ---
|
|
18
|
+
/**
|
|
19
|
+
* @description Converts string values to their appropriate JavaScript types.
|
|
20
|
+
*
|
|
21
|
+
* @param value - The string value to convert.
|
|
22
|
+
* @returns Converted value in the appropriate type.
|
|
23
|
+
*
|
|
24
|
+
* @example
|
|
25
|
+
* parseValue("true"); // true
|
|
26
|
+
* parseValue("42"); // 42
|
|
27
|
+
* parseValue("[1,2,3]"); // [1, 2, 3]
|
|
28
|
+
* parseValue("hello"); // "hello"
|
|
29
|
+
*/
|
|
30
|
+
function parseValue(value) {
|
|
31
|
+
if (value === "true")
|
|
32
|
+
return true;
|
|
33
|
+
if (value === "false")
|
|
34
|
+
return false;
|
|
35
|
+
if (!Number.isNaN(Number(value)) && value.trim() !== "")
|
|
36
|
+
return Number(value);
|
|
37
|
+
if (value.startsWith("[") && value.endsWith("]")) {
|
|
38
|
+
try {
|
|
39
|
+
return JSON.parse(value);
|
|
40
|
+
}
|
|
41
|
+
catch {
|
|
42
|
+
return value;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return value;
|
|
46
|
+
}
|
|
47
|
+
// --- Data attribute parsing ---
|
|
48
|
+
/**
|
|
49
|
+
* @description Extracts and parses data attributes with a specific prefix from a DOM node.
|
|
50
|
+
*
|
|
51
|
+
* Converts kebab-case attribute names to camelCase object keys.
|
|
52
|
+
*
|
|
53
|
+
* @param node - The DOM element to extract attributes from.
|
|
54
|
+
* @param prefix - The attribute prefix to look for (e.g. `"data-carousel-"`).
|
|
55
|
+
* @returns Object with camelCase keys and parsed values.
|
|
56
|
+
*
|
|
57
|
+
* @example
|
|
58
|
+
* // <div data-carousel-auto-play="true" data-carousel-slide-count="5">
|
|
59
|
+
* parseDataAttributes(element, "data-carousel-");
|
|
60
|
+
* // { autoPlay: true, slideCount: 5 }
|
|
61
|
+
*/
|
|
62
|
+
const parseDataAttributes = (node, prefix) => {
|
|
63
|
+
const options = {};
|
|
64
|
+
for (const attr of node.attributes) {
|
|
65
|
+
if (attr.name.startsWith(prefix)) {
|
|
66
|
+
const key = attr.name.replace(prefix, "").replace(/-([a-z])/g, (_, c) => c.toUpperCase());
|
|
67
|
+
options[key] = parseValue(attr.value);
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return options;
|
|
71
|
+
};
|
|
72
|
+
/**
|
|
73
|
+
* @namespace Utils
|
|
74
|
+
* @description Shared DOM and data-attribute parsing utilities.
|
|
75
|
+
*
|
|
76
|
+
* @property parseValue - Converts string values to typed values.
|
|
77
|
+
* @property parseDataAttributes - Parses prefixed data attributes on a node.
|
|
78
|
+
*/
|
|
79
|
+
const Utils = {
|
|
80
|
+
parseValue,
|
|
81
|
+
parseDataAttributes,
|
|
82
|
+
};
|
|
83
|
+
// Attach to window for the documented public API (`window.Utils`), and export
|
|
84
|
+
// for module consumers (embla.js imports it via the main.js bundle).
|
|
85
|
+
if (typeof window !== "undefined") {
|
|
86
|
+
window.Utils = Utils;
|
|
87
|
+
}
|
|
88
|
+
export { Utils };
|