@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.
Files changed (260) hide show
  1. package/CHANGELOG.md +19 -0
  2. package/LICENSE +21 -0
  3. package/README.md +61 -0
  4. package/dist/sri.json +75 -0
  5. package/dist/zazz.css +1 -0
  6. package/dist/zazz.js +1 -0
  7. package/examples/about.html +590 -0
  8. package/examples/components.html +1040 -0
  9. package/examples/forms.html +511 -0
  10. package/examples/index.html +268 -0
  11. package/examples/layout.html +320 -0
  12. package/examples/products.html +846 -0
  13. package/examples/responsive.html +339 -0
  14. package/package.json +85 -0
  15. package/src/base/_layers.css +12 -0
  16. package/src/base/_layout.css +568 -0
  17. package/src/base/_reset.css +515 -0
  18. package/src/base/_typography.css +495 -0
  19. package/src/base/_utilities.css +6628 -0
  20. package/src/base/_variables.css +682 -0
  21. package/src/base/_view-transitions.css +103 -0
  22. package/src/base/command-score.d.ts +12 -0
  23. package/src/base/command-score.d.ts.map +1 -0
  24. package/src/base/command-score.js +151 -0
  25. package/src/base/command-score.ts +192 -0
  26. package/src/base/dialog-lifecycle.d.ts +11 -0
  27. package/src/base/dialog-lifecycle.d.ts.map +1 -0
  28. package/src/base/dialog-lifecycle.js +65 -0
  29. package/src/base/dialog-lifecycle.ts +80 -0
  30. package/src/base/embla.d.ts +90 -0
  31. package/src/base/embla.d.ts.map +1 -0
  32. package/src/base/embla.js +643 -0
  33. package/src/base/embla.ts +791 -0
  34. package/src/base/hotkeys.d.ts +82 -0
  35. package/src/base/hotkeys.d.ts.map +1 -0
  36. package/src/base/hotkeys.js +141 -0
  37. package/src/base/hotkeys.ts +195 -0
  38. package/src/base/navigation.d.ts +2 -0
  39. package/src/base/navigation.d.ts.map +1 -0
  40. package/src/base/navigation.js +136 -0
  41. package/src/base/navigation.ts +151 -0
  42. package/src/base/reveal.d.ts +80 -0
  43. package/src/base/reveal.d.ts.map +1 -0
  44. package/src/base/reveal.js +305 -0
  45. package/src/base/reveal.ts +379 -0
  46. package/src/base/signals.d.ts +89 -0
  47. package/src/base/signals.d.ts.map +1 -0
  48. package/src/base/signals.js +134 -0
  49. package/src/base/signals.ts +164 -0
  50. package/src/base/typeahead.d.ts +105 -0
  51. package/src/base/typeahead.d.ts.map +1 -0
  52. package/src/base/typeahead.js +411 -0
  53. package/src/base/typeahead.ts +500 -0
  54. package/src/base/utils.d.ts +56 -0
  55. package/src/base/utils.d.ts.map +1 -0
  56. package/src/base/utils.js +88 -0
  57. package/src/base/utils.ts +98 -0
  58. package/src/base/zazz-element.d.ts +44 -0
  59. package/src/base/zazz-element.d.ts.map +1 -0
  60. package/src/base/zazz-element.js +59 -0
  61. package/src/base/zazz-element.ts +73 -0
  62. package/src/globals.d.ts +148 -0
  63. package/src/head.d.ts +95 -0
  64. package/src/head.d.ts.map +1 -0
  65. package/src/head.js +311 -0
  66. package/src/head.ts +433 -0
  67. package/src/index.css +71 -0
  68. package/src/index.d.ts +36 -0
  69. package/src/index.d.ts.map +1 -0
  70. package/src/index.js +35 -0
  71. package/src/index.ts +36 -0
  72. package/src/manifest.d.ts +90 -0
  73. package/src/manifest.d.ts.map +1 -0
  74. package/src/manifest.js +614 -0
  75. package/src/manifest.ts +667 -0
  76. package/src/primitives/accordion/accordion.css +104 -0
  77. package/src/primitives/accordion/accordion.html +69 -0
  78. package/src/primitives/alert-dialog/alert-dialog.css +38 -0
  79. package/src/primitives/alert-dialog/alert-dialog.html +53 -0
  80. package/src/primitives/autocomplete/autocomplete-groups.html +77 -0
  81. package/src/primitives/autocomplete/autocomplete.css +114 -0
  82. package/src/primitives/autocomplete/autocomplete.d.ts +30 -0
  83. package/src/primitives/autocomplete/autocomplete.d.ts.map +1 -0
  84. package/src/primitives/autocomplete/autocomplete.html +66 -0
  85. package/src/primitives/autocomplete/autocomplete.js +41 -0
  86. package/src/primitives/autocomplete/autocomplete.ts +46 -0
  87. package/src/primitives/avatar/avatar.html +44 -0
  88. package/src/primitives/badge/badge-icon.html +36 -0
  89. package/src/primitives/badge/badge.css +251 -0
  90. package/src/primitives/badge/badge.html +7 -0
  91. package/src/primitives/breadcrumbs/breadcrumbs.html +9 -0
  92. package/src/primitives/button/button-icon-only.html +84 -0
  93. package/src/primitives/button/button-icon.html +82 -0
  94. package/src/primitives/button/button.css +319 -0
  95. package/src/primitives/button/button.html +8 -0
  96. package/src/primitives/button-group/button-group-vertical.html +6 -0
  97. package/src/primitives/button-group/button-group.css +94 -0
  98. package/src/primitives/button-group/button-group.html +64 -0
  99. package/src/primitives/card/card-subgrid.html +118 -0
  100. package/src/primitives/card/card.html +53 -0
  101. package/src/primitives/carousel/carousel.css +61 -0
  102. package/src/primitives/carousel/carousel.d.ts +16 -0
  103. package/src/primitives/carousel/carousel.d.ts.map +1 -0
  104. package/src/primitives/carousel/carousel.html +315 -0
  105. package/src/primitives/carousel/carousel.js +73 -0
  106. package/src/primitives/carousel/carousel.ts +84 -0
  107. package/src/primitives/checkbox/checkbox-tasklist.html +71 -0
  108. package/src/primitives/checkbox/checkbox.css +82 -0
  109. package/src/primitives/checkbox/checkbox.d.ts +21 -0
  110. package/src/primitives/checkbox/checkbox.d.ts.map +1 -0
  111. package/src/primitives/checkbox/checkbox.html +17 -0
  112. package/src/primitives/checkbox/checkbox.js +190 -0
  113. package/src/primitives/checkbox/checkbox.ts +219 -0
  114. package/src/primitives/combobox/combobox-multiselect.html +91 -0
  115. package/src/primitives/combobox/combobox.css +307 -0
  116. package/src/primitives/combobox/combobox.d.ts +100 -0
  117. package/src/primitives/combobox/combobox.d.ts.map +1 -0
  118. package/src/primitives/combobox/combobox.html +73 -0
  119. package/src/primitives/combobox/combobox.js +503 -0
  120. package/src/primitives/combobox/combobox.ts +559 -0
  121. package/src/primitives/command/command-actions.d.ts +31 -0
  122. package/src/primitives/command/command-actions.d.ts.map +1 -0
  123. package/src/primitives/command/command-actions.html +89 -0
  124. package/src/primitives/command/command-actions.js +70 -0
  125. package/src/primitives/command/command-actions.ts +77 -0
  126. package/src/primitives/command/command-dialog.html +77 -0
  127. package/src/primitives/command/command.css +151 -0
  128. package/src/primitives/command/command.d.ts +52 -0
  129. package/src/primitives/command/command.d.ts.map +1 -0
  130. package/src/primitives/command/command.html +119 -0
  131. package/src/primitives/command/command.js +146 -0
  132. package/src/primitives/command/command.ts +157 -0
  133. package/src/primitives/dialog/dialog-with-form.html +200 -0
  134. package/src/primitives/dialog/dialog.css +229 -0
  135. package/src/primitives/dialog/dialog.html +90 -0
  136. package/src/primitives/fields/fields.css +278 -0
  137. package/src/primitives/input/input-icon-leading.html +19 -0
  138. package/src/primitives/input/input-icon-trailing.html +18 -0
  139. package/src/primitives/input/input.css +114 -0
  140. package/src/primitives/input/input.html +61 -0
  141. package/src/primitives/input-group/input-group-password-group.html +108 -0
  142. package/src/primitives/input-group/input-group.css +215 -0
  143. package/src/primitives/input-group/input-group.html +132 -0
  144. package/src/primitives/kbd/kbd-group.html +18 -0
  145. package/src/primitives/kbd/kbd.css +87 -0
  146. package/src/primitives/kbd/kbd.html +7 -0
  147. package/src/primitives/lightbox/lightbox.css +459 -0
  148. package/src/primitives/lightbox/lightbox.d.ts +7 -0
  149. package/src/primitives/lightbox/lightbox.d.ts.map +1 -0
  150. package/src/primitives/lightbox/lightbox.html +665 -0
  151. package/src/primitives/lightbox/lightbox.js +96 -0
  152. package/src/primitives/lightbox/lightbox.ts +113 -0
  153. package/src/primitives/menu/menu-interest.html +52 -0
  154. package/src/primitives/menu/menu.css +55 -0
  155. package/src/primitives/menu/menu.d.ts +38 -0
  156. package/src/primitives/menu/menu.d.ts.map +1 -0
  157. package/src/primitives/menu/menu.html +70 -0
  158. package/src/primitives/menu/menu.js +117 -0
  159. package/src/primitives/menu/menu.ts +131 -0
  160. package/src/primitives/menubar/menubar-help-search.html +103 -0
  161. package/src/primitives/menubar/menubar.html +166 -0
  162. package/src/primitives/meter/meter.css +178 -0
  163. package/src/primitives/meter/meter.html +38 -0
  164. package/src/primitives/mobile-menu/mobile-menu.css +145 -0
  165. package/src/primitives/mobile-menu/mobile-menu.html +614 -0
  166. package/src/primitives/navigation-menu/navigation-menu-featured.html +76 -0
  167. package/src/primitives/navigation-menu/navigation-menu-icon-grid.html +158 -0
  168. package/src/primitives/navigation-menu/navigation-menu-interest.html +103 -0
  169. package/src/primitives/navigation-menu/navigation-menu-megamenu.html +141 -0
  170. package/src/primitives/navigation-menu/navigation-menu-simple.html +96 -0
  171. package/src/primitives/navigation-menu/navigation-menu.css +295 -0
  172. package/src/primitives/navigation-menu/navigation-menu.html +576 -0
  173. package/src/primitives/otp/otp.css +126 -0
  174. package/src/primitives/otp/otp.d.ts +89 -0
  175. package/src/primitives/otp/otp.d.ts.map +1 -0
  176. package/src/primitives/otp/otp.html +22 -0
  177. package/src/primitives/otp/otp.js +226 -0
  178. package/src/primitives/otp/otp.ts +279 -0
  179. package/src/primitives/password-group/password-group.css +238 -0
  180. package/src/primitives/password-group/password-group.d.ts +25 -0
  181. package/src/primitives/password-group/password-group.d.ts.map +1 -0
  182. package/src/primitives/password-group/password-group.html +109 -0
  183. package/src/primitives/password-group/password-group.js +76 -0
  184. package/src/primitives/password-group/password-group.ts +95 -0
  185. package/src/primitives/popover/popover.css +233 -0
  186. package/src/primitives/progress/progress-indeterminate.html +1 -0
  187. package/src/primitives/progress/progress.css +105 -0
  188. package/src/primitives/progress/progress.html +7 -0
  189. package/src/primitives/prose/prose.html +65 -0
  190. package/src/primitives/radio/radio.css +84 -0
  191. package/src/primitives/radio/radio.html +17 -0
  192. package/src/primitives/reveal/reveal.css +167 -0
  193. package/src/primitives/select/multiselect.d.ts +43 -0
  194. package/src/primitives/select/multiselect.d.ts.map +1 -0
  195. package/src/primitives/select/multiselect.js +147 -0
  196. package/src/primitives/select/multiselect.ts +188 -0
  197. package/src/primitives/select/select-align.html +49 -0
  198. package/src/primitives/select/select-multiple.html +12 -0
  199. package/src/primitives/select/select-sides.html +65 -0
  200. package/src/primitives/select/select.css +481 -0
  201. package/src/primitives/select/select.html +16 -0
  202. package/src/primitives/separator/separator.css +49 -0
  203. package/src/primitives/separator/separator.html +16 -0
  204. package/src/primitives/slider/slider.css +122 -0
  205. package/src/primitives/slider/slider.html +1 -0
  206. package/src/primitives/switch/switch.css +126 -0
  207. package/src/primitives/switch/switch.html +13 -0
  208. package/src/primitives/table/table-alternating.html +36 -0
  209. package/src/primitives/table/table-grid.html +30 -0
  210. package/src/primitives/table/table-size-sm.html +26 -0
  211. package/src/primitives/table/table.css +158 -0
  212. package/src/primitives/table/table.html +45 -0
  213. package/src/primitives/tabs/tabs.css +328 -0
  214. package/src/primitives/tabs/tabs.d.ts +35 -0
  215. package/src/primitives/tabs/tabs.d.ts.map +1 -0
  216. package/src/primitives/tabs/tabs.html +75 -0
  217. package/src/primitives/tabs/tabs.js +84 -0
  218. package/src/primitives/tabs/tabs.ts +93 -0
  219. package/src/primitives/textarea/textarea.css +78 -0
  220. package/src/primitives/textarea/textarea.html +10 -0
  221. package/src/primitives/toaster/toaster.css +309 -0
  222. package/src/primitives/toaster/toaster.d.ts +138 -0
  223. package/src/primitives/toaster/toaster.d.ts.map +1 -0
  224. package/src/primitives/toaster/toaster.html +77 -0
  225. package/src/primitives/toaster/toaster.js +736 -0
  226. package/src/primitives/toaster/toaster.ts +823 -0
  227. package/src/primitives/toggle/toggle.css +295 -0
  228. package/src/primitives/toggle/toggle.html +68 -0
  229. package/src/primitives/toggle-group/toggle-group-multiple.html +14 -0
  230. package/src/primitives/toggle-group/toggle-group-vertical.html +14 -0
  231. package/src/primitives/toggle-group/toggle-group.css +73 -0
  232. package/src/primitives/toggle-group/toggle-group.html +53 -0
  233. package/src/primitives/toolbar/toolbar.html +45 -0
  234. package/src/primitives/tooltip/tooltip-disabled.html +19 -0
  235. package/src/primitives/tooltip/tooltip-sides.html +57 -0
  236. package/src/primitives/tooltip/tooltip-with-kbd.html +31 -0
  237. package/src/primitives/tooltip/tooltip.css +235 -0
  238. package/src/primitives/tooltip/tooltip.html +12 -0
  239. package/src/primitives/utilities/aspect-ratio.html +17 -0
  240. package/src/primitives/utilities/basis.html +27 -0
  241. package/src/primitives/utilities/col-span.html +23 -0
  242. package/src/primitives/utilities/compose.html +19 -0
  243. package/src/primitives/utilities/flex-align.html +26 -0
  244. package/src/primitives/utilities/flex-direction.html +36 -0
  245. package/src/primitives/utilities/flex-grow.html +17 -0
  246. package/src/primitives/utilities/gap.html +62 -0
  247. package/src/primitives/utilities/grid-cols.html +20 -0
  248. package/src/primitives/utilities/grid-pile.html +5 -0
  249. package/src/primitives/utilities/grid-responsive.html +31 -0
  250. package/src/primitives/utilities/margin-auto.html +13 -0
  251. package/src/primitives/utilities/object-fit.html +41 -0
  252. package/src/primitives/utilities/order.html +17 -0
  253. package/src/primitives/utilities/overflow.html +26 -0
  254. package/src/primitives/utilities/padding-compose.html +23 -0
  255. package/src/primitives/utilities/padding.html +30 -0
  256. package/src/primitives/utilities/position.html +13 -0
  257. package/src/primitives/utilities/responsive-flex.html +22 -0
  258. package/src/primitives/utilities/responsive-grid.html +31 -0
  259. package/src/primitives/utilities/size.html +22 -0
  260. package/src/primitives/utilities/width.html +22 -0
@@ -0,0 +1,503 @@
1
+ "use strict";
2
+ /**
3
+ * @fileoverview `<ui-combobox>`: an input restricted to a predefined list.
4
+ * @description Light-DOM custom element on the shared typeahead engine
5
+ * (`base/typeahead.ts`). A relative of select: the panel filters as you type,
6
+ * but free text can never submit. The form value lives in an authored hidden
7
+ * input (`data-slot="combobox-value"`), synced whenever the selection changes.
8
+ *
9
+ * The visible input carries a **display value, not the filter**. Committing
10
+ * writes the option label into it and clears the query, so reopening shows
11
+ * the full list with the committed row checked, matching `.ui-select`.
12
+ * Opening selects that label so the first keystroke replaces it, and focus
13
+ * alone never opens the panel (a click, the chevron, an arrow key or typing
14
+ * does). On blur, stray text reverts to the committed label and cleared text
15
+ * clears the selection.
16
+ *
17
+ * `data-variant="multiselect"` selects a set instead: committing toggles a row
18
+ * and keeps the panel open, and every selected value renders as a removable
19
+ * tag stamped inside the control. The tag markup is HTML, not script: author
20
+ * a `<template data-slot="combobox-tag-template">` to control its classes and
21
+ * structure, and this file fills in the label, the value, and the remove
22
+ * button accessible name. Without one, the ui-badge default below applies.
23
+ * Backspace on an empty input drops the last tag. Options carrying
24
+ * `aria-selected="true"` are the single source of truth: the tags and the
25
+ * hidden inputs are both derived from them in DOM order so nothing can drift.
26
+ * The form submits repeated `name` pairs like `<select multiple>`: the authored
27
+ * hidden input carries the first value, and stamped siblings carry the rest.
28
+ *
29
+ * Where a no-JS fallback matters, prefer `.ui-select` / `<ui-multiselect>`:
30
+ * this control is inert without its script (the hidden input still submits a
31
+ * server-set value, but no tags render).
32
+ *
33
+ * Attributes on the root: `data-variant="multiselect"`, `data-sort="score"`,
34
+ * `data-min-length="<n>"`, `data-label-remove="Remove {label}"`.
35
+ * Parts: `combobox-value` (hidden input), `combobox-control` (select-look
36
+ * shell), `combobox-tag-template` (authored `<template>`), `combobox-tag` /
37
+ * `combobox-tag-label` / `combobox-tag-remove` (cloned from it),
38
+ * `combobox-trigger` (chevron), `combobox-panel`, `combobox-list`,
39
+ * `combobox-item`, `combobox-group` / `combobox-group-label`, `combobox-empty`.
40
+ */
41
+ import { TypeaheadElement } from "../../base/typeahead.js";
42
+ import { defineZazzElement } from "../../base/zazz-element.js";
43
+ // --- Tag blueprint ---
44
+ /**
45
+ * The default tag markup, cloned once per selected value. Authors override it
46
+ * wholesale with a `<template data-slot="combobox-tag-template">`, so restyling
47
+ * a tag (other classes, an extra icon, a different component entirely) never
48
+ * means editing this script. Kept as markup rather than createElement calls so
49
+ * the default reads the same way the override is written.
50
+ */
51
+ const TAG_MARKUP = '<span class="ui-badge" data-slot="combobox-tag">' +
52
+ '<span data-slot="combobox-tag-label"></span>' +
53
+ '<button type="button" tabindex="-1" data-slot="combobox-tag-remove"></button>' +
54
+ "</span>";
55
+ let defaultTagTemplate = null;
56
+ /**
57
+ * @description Parses `TAG_MARKUP` once and shares the result.
58
+ *
59
+ * @returns The fallback tag template.
60
+ * @private
61
+ */
62
+ function defaultTagBlueprint() {
63
+ if (!defaultTagTemplate) {
64
+ defaultTagTemplate = document.createElement("template");
65
+ defaultTagTemplate.innerHTML = TAG_MARKUP;
66
+ }
67
+ return defaultTagTemplate;
68
+ }
69
+ /**
70
+ * @description Resolves a blur: free text never survives it. The multiselect
71
+ * variant always empties the filter (its selection lives in the tags), the
72
+ * single variant restores the committed label, and an emptied single-select
73
+ * input clears the selection outright.
74
+ *
75
+ * @param typed - The input's current text.
76
+ * @param committedLabel - The last committed label, empty for no selection.
77
+ * @param multiselect - Whether the multiselect variant is active.
78
+ * @returns The text to show and whether to clear the selection.
79
+ */
80
+ function resolveBlur(typed, committedLabel, multiselect) {
81
+ if (multiselect)
82
+ return { value: "", clear: false };
83
+ if (typed === "")
84
+ return { value: "", clear: true };
85
+ return { value: committedLabel, clear: false };
86
+ }
87
+ /**
88
+ * @description Fills the remove-button label template (`data-label-remove`).
89
+ *
90
+ * @param template - Template with a `{label}` placeholder.
91
+ * @param label - The tag's visible label.
92
+ * @returns The accessible name for that tag's remove button.
93
+ */
94
+ function resolveRemoveLabel(template, label) {
95
+ return template.replace("{label}", label);
96
+ }
97
+ // --- Element ---
98
+ class UiCombobox extends TypeaheadElement {
99
+ slotPrefix = "combobox";
100
+ /** The input holds a committed label, so focus alone must not open the list. */
101
+ openOnFocus = false;
102
+ #committedLabel = "";
103
+ #placeholder = "";
104
+ #defaultValues = [];
105
+ #serialized = "";
106
+ setup(signal) {
107
+ super.setup(signal);
108
+ const input = this.searchInput;
109
+ if (!input)
110
+ return;
111
+ const multiselect = this.#multiselect();
112
+ this.#placeholder = input.getAttribute("placeholder") ?? "";
113
+ if (multiselect) {
114
+ this.querySelector('[data-slot~="combobox-list"]')?.setAttribute("aria-multiselectable", "true");
115
+ }
116
+ // Adopt a server-rendered value no row claims yet, so `value` alone is
117
+ // enough markup to seed the selection, ensuring the sync below never
118
+ // silently discards it
119
+ const hidden = this.#valueInput();
120
+ if (hidden !== null && hidden.value !== "" && this.#selectedItems().length === 0) {
121
+ const match = this.items().find((item) => this.#itemFormValue(item) === hidden.value);
122
+ match?.setAttribute("aria-selected", "true");
123
+ }
124
+ // A multi-select listbox announces every row state, not just the picked ones
125
+ if (multiselect) {
126
+ for (const item of this.items()) {
127
+ if (item.getAttribute("aria-selected") !== "true") {
128
+ item.setAttribute("aria-selected", "false");
129
+ }
130
+ }
131
+ }
132
+ // Attribute state has no defaultValue: remember the markup selection so
133
+ // a form reset can restore it
134
+ this.#defaultValues = this.#selectedItems().map((item) => this.#itemFormValue(item));
135
+ this.#committedLabel = multiselect ? "" : (this.#selectedLabel() ?? "");
136
+ input.value = this.#committedLabel;
137
+ this.#syncSelection(false);
138
+ const control = input.closest('[data-slot~="combobox-control"]');
139
+ // A select-look control is one big hit target
140
+ input.addEventListener("click", () => this.#openPanel(), { signal });
141
+ control?.addEventListener("mousedown", (event) => {
142
+ if (!(event.target instanceof Element) || event.target === input)
143
+ return;
144
+ // Nothing in the shell may steal focus from the input: the chevron and
145
+ // the tag remove buttons would otherwise blur it and close the panel
146
+ event.preventDefault();
147
+ input.focus();
148
+ if (!event.target.closest('[data-slot~="combobox-trigger"], [data-slot~="combobox-tag"]')) {
149
+ this.#openPanel();
150
+ }
151
+ }, { signal });
152
+ // Tag removal is delegated: the tags are re-stamped on every change
153
+ control?.addEventListener("click", (event) => {
154
+ if (!(event.target instanceof Element))
155
+ return;
156
+ const remove = event.target.closest('[data-slot~="combobox-tag-remove"]');
157
+ if (!remove)
158
+ return;
159
+ // A template that forgets type="button" would otherwise submit the form
160
+ event.preventDefault();
161
+ const value = remove.closest('[data-slot~="combobox-tag"]')?.getAttribute("data-value");
162
+ this.#deselect(this.items().find((item) => this.#itemFormValue(item) === value));
163
+ input.focus();
164
+ }, { signal });
165
+ // Chevron toggles the full, unfiltered list
166
+ const trigger = this.querySelector('[data-slot~="combobox-trigger"]');
167
+ if (trigger instanceof HTMLElement) {
168
+ trigger.addEventListener("click", () => {
169
+ if (this.open.get())
170
+ this.open.set(false);
171
+ else
172
+ this.#openPanel();
173
+ }, { signal });
174
+ }
175
+ // Backspace on an empty filter drops the last tag
176
+ if (multiselect) {
177
+ input.addEventListener("keydown", (event) => {
178
+ if (event.key !== "Backspace" || input.value !== "")
179
+ return;
180
+ const last = this.#selectedItems().at(-1);
181
+ if (!last)
182
+ return;
183
+ event.preventDefault();
184
+ this.#deselect(last);
185
+ }, { signal });
186
+ }
187
+ // No free text: on leaving, revert stray text or clear the selection
188
+ this.addEventListener("focusout", (event) => {
189
+ const next = event.relatedTarget;
190
+ if (next instanceof Node && this.contains(next))
191
+ return;
192
+ const outcome = resolveBlur(input.value, this.#committedLabel, this.#multiselect());
193
+ input.value = outcome.value;
194
+ // Safe mid-close: the gated ranking effect ignores query writes while
195
+ // the panel is fading out
196
+ this.query.set("");
197
+ if (outcome.clear)
198
+ this.#clearSelection();
199
+ }, { signal });
200
+ // A form reset restores the selection the markup shipped with (reset
201
+ // applies after the event)
202
+ (hidden ?? input).form?.addEventListener("reset", () => queueMicrotask(() => this.#restoreDefaults()), { signal });
203
+ }
204
+ teardown() {
205
+ for (const node of this.querySelectorAll('[data-slot~="combobox-tag"], [data-combobox-stamped]')) {
206
+ node.remove();
207
+ }
208
+ if (this.searchInput)
209
+ this.searchInput.placeholder = this.#placeholder;
210
+ }
211
+ /**
212
+ * @description Combobox items match against what the user sees (the label)
213
+ * not the machine `data-value`.
214
+ *
215
+ * @param item - The item element.
216
+ * @returns The trimmed visible label.
217
+ */
218
+ itemValue(item) {
219
+ return item.textContent?.trim() ?? "";
220
+ }
221
+ /**
222
+ * @description Escape with the panel already closed restores the committed
223
+ * label (single) or drops the filter text (multiselect), avoiding a half-typed
224
+ * value that a later blur would read as a cleared selection.
225
+ */
226
+ clearQuery() {
227
+ const input = this.searchInput;
228
+ if (!input)
229
+ return;
230
+ input.value = this.#multiselect() ? "" : this.#committedLabel;
231
+ this.query.set("");
232
+ }
233
+ /**
234
+ * @description Committing shows the item's label, stores its `data-value` in
235
+ * the hidden input, and moves `aria-selected`. The multiselect variant
236
+ * toggles the row instead and keeps the panel open so picking can continue.
237
+ *
238
+ * @param item - The committed option.
239
+ */
240
+ commit(item, _source) {
241
+ const input = this.searchInput;
242
+ if (!input)
243
+ return;
244
+ if (this.#multiselect()) {
245
+ item.setAttribute("aria-selected", String(item.getAttribute("aria-selected") !== "true"));
246
+ input.value = "";
247
+ this.query.set("");
248
+ // Follow the row through the re-widened list so a second Enter toggles it
249
+ // back: with an empty query every item is visible, and visual order is
250
+ // DOM order (score ranking is stable at equal scores)
251
+ this.activeIndex.set(this.items().indexOf(item));
252
+ this.#syncSelection(true);
253
+ input.focus();
254
+ return;
255
+ }
256
+ this.#committedLabel = this.itemValue(item);
257
+ input.value = this.#committedLabel;
258
+ // The label is display text, not a filter: clearing the query is what
259
+ // makes the reopened panel show the full list with this row checked
260
+ this.query.set("");
261
+ for (const other of this.items())
262
+ other.removeAttribute("aria-selected");
263
+ item.setAttribute("aria-selected", "true");
264
+ this.#syncSelection(true);
265
+ input.focus();
266
+ // The inline variant has no popover to close, and closing it would gate its
267
+ // filtering off for good. It also never reopens, so commit is where it
268
+ // selects the label (the popover variants do that in #openPanel()).
269
+ if (this.panel?.hasAttribute("popover"))
270
+ this.open.set(false);
271
+ else
272
+ input.select();
273
+ }
274
+ /**
275
+ * @description Whether the multiselect variant is active.
276
+ *
277
+ * @returns True for `data-variant="multiselect"`.
278
+ * @private
279
+ */
280
+ #multiselect() {
281
+ return this.getAttribute("data-variant") === "multiselect";
282
+ }
283
+ /**
284
+ * @description Opens the panel the way a select does: the full list, the
285
+ * committed row highlighted, and its label selected so the first keystroke
286
+ * replaces it. `activeIndex` is written here rather than from an effect
287
+ * because effects are output adapters, and writes from inside one can be dropped
288
+ * until the next notification.
289
+ * @private
290
+ */
291
+ #openPanel() {
292
+ const input = this.searchInput;
293
+ if (!input || this.open.get())
294
+ return;
295
+ if (this.#multiselect()) {
296
+ input.value = "";
297
+ this.activeIndex.set(-1);
298
+ }
299
+ else {
300
+ input.value = this.#committedLabel;
301
+ const selected = this.#selectedItems()[0];
302
+ this.activeIndex.set(selected ? this.items().indexOf(selected) : -1);
303
+ if (input.value !== "")
304
+ input.select();
305
+ }
306
+ this.query.set("");
307
+ this.open.set(true);
308
+ }
309
+ /**
310
+ * @description The selection: items carrying `aria-selected="true"`, in DOM
311
+ * order. The single source of truth for tags and form values alike.
312
+ *
313
+ * @returns The selected items.
314
+ * @private
315
+ */
316
+ #selectedItems() {
317
+ return this.items().filter((item) => item.getAttribute("aria-selected") === "true");
318
+ }
319
+ /**
320
+ * @description The single-select committed label.
321
+ *
322
+ * @returns The selected item's label, or undefined when nothing is selected.
323
+ * @private
324
+ */
325
+ #selectedLabel() {
326
+ const selected = this.#selectedItems()[0];
327
+ return selected ? this.itemValue(selected) : undefined;
328
+ }
329
+ /**
330
+ * @description An item's machine value.
331
+ *
332
+ * @param item - The item element.
333
+ * @returns `data-value` when present, the visible label otherwise.
334
+ * @private
335
+ */
336
+ #itemFormValue(item) {
337
+ return item.getAttribute("data-value") ?? this.itemValue(item);
338
+ }
339
+ /**
340
+ * @description Drops one item from the selection.
341
+ *
342
+ * @param item - The item to deselect; a no-op when undefined.
343
+ * @private
344
+ */
345
+ #deselect(item) {
346
+ if (!item)
347
+ return;
348
+ item.setAttribute("aria-selected", "false");
349
+ this.#syncSelection(true);
350
+ }
351
+ /**
352
+ * @description The authored hidden input carrying the form value (never one
353
+ * of the stamped siblings).
354
+ *
355
+ * @returns The hidden input, or null when the author omitted it.
356
+ * @private
357
+ */
358
+ #valueInput() {
359
+ const hidden = this.querySelector('[data-slot~="combobox-value"]:not([data-combobox-stamped])');
360
+ return hidden instanceof HTMLInputElement ? hidden : null;
361
+ }
362
+ /**
363
+ * @description Mirrors the selection into the two things derived from it: the
364
+ * tags and the hidden inputs. The authored input carries the first value and
365
+ * stamped siblings carry the rest under the same `name`, so a multiselection
366
+ * submits as repeated pairs exactly like `<select multiple>`. DOM
367
+ * construction stays imperative: the DOM is the source of truth here, not a
368
+ * signal.
369
+ *
370
+ * @param notify - Whether to dispatch `change` (skipped while seeding).
371
+ * @private
372
+ */
373
+ #syncSelection(notify) {
374
+ const input = this.searchInput;
375
+ const values = this.#selectedItems().map((item) => this.#itemFormValue(item));
376
+ const serialized = JSON.stringify(values);
377
+ const changed = serialized !== this.#serialized;
378
+ this.#serialized = serialized;
379
+ if (input && this.#multiselect())
380
+ this.#renderTags(input);
381
+ const hidden = this.#valueInput();
382
+ if (!hidden)
383
+ return;
384
+ hidden.value = values[0] ?? "";
385
+ for (const stale of this.querySelectorAll("[data-combobox-stamped]"))
386
+ stale.remove();
387
+ let anchor = hidden;
388
+ for (const value of values.slice(1)) {
389
+ const extra = document.createElement("input");
390
+ extra.type = "hidden";
391
+ extra.setAttribute("data-slot", "combobox-value");
392
+ extra.setAttribute("data-combobox-stamped", "");
393
+ if (hidden.name)
394
+ extra.name = hidden.name;
395
+ extra.value = value;
396
+ anchor.after(extra);
397
+ anchor = extra;
398
+ }
399
+ if (notify && changed)
400
+ hidden.dispatchEvent(new Event("change", { bubbles: true }));
401
+ }
402
+ /**
403
+ * @description The element cloned per selected value: the first child of an
404
+ * authored `<template data-slot="combobox-tag-template">` when present, the
405
+ * default ui-badge otherwise. A template's content lives in a separate
406
+ * fragment, so a blueprint carrying `data-slot="combobox-tag"` is invisible to
407
+ * the stale-tag sweep and can never be mistaken for a rendered tag.
408
+ *
409
+ * @returns The blueprint element, or null when an authored template is empty.
410
+ * @private
411
+ */
412
+ #tagBlueprint() {
413
+ const authored = this.querySelector('[data-slot~="combobox-tag-template"]');
414
+ const template = authored instanceof HTMLTemplateElement ? authored : defaultTagBlueprint();
415
+ const root = template.content.firstElementChild;
416
+ return root instanceof HTMLElement ? root : null;
417
+ }
418
+ /**
419
+ * @description Re-stamps the selection tags inside the control, in DOM order.
420
+ * Rebuilt wholesale rather than diffed: the tags are pure output, nothing
421
+ * focusable ever lands inside one, and removal is delegated from the control.
422
+ *
423
+ * @param input - The search input the tags render before.
424
+ * @private
425
+ */
426
+ #renderTags(input) {
427
+ const control = input.closest('[data-slot~="combobox-control"]');
428
+ if (!control)
429
+ return;
430
+ for (const stale of control.querySelectorAll('[data-slot~="combobox-tag"]'))
431
+ stale.remove();
432
+ const blueprint = this.#tagBlueprint();
433
+ if (!blueprint)
434
+ return;
435
+ const removeLabel = this.getAttribute("data-label-remove") ?? "Remove {label}";
436
+ const tags = this.#selectedItems().map((item) => {
437
+ const label = this.itemValue(item);
438
+ const tag = blueprint.cloneNode(true);
439
+ tag.setAttribute("data-value", this.#itemFormValue(item));
440
+ // The slot is the contract every other moving part keys off (the CSS, the
441
+ // stale sweep above, and tag removal), so add the token if the template
442
+ // left it out rather than stamping an orphan
443
+ if (!tag.matches('[data-slot~="combobox-tag"]')) {
444
+ const slots = tag.getAttribute("data-slot");
445
+ tag.setAttribute("data-slot", slots ? `${slots} combobox-tag` : "combobox-tag");
446
+ }
447
+ // The label wants its own box: text-overflow ignores a flex container's
448
+ // own text, but a template without one still gets its text
449
+ const text = tag.querySelector('[data-slot~="combobox-tag-label"]');
450
+ if (text)
451
+ text.textContent = label;
452
+ else
453
+ tag.prepend(document.createTextNode(label));
454
+ tag
455
+ .querySelector('[data-slot~="combobox-tag-remove"]')
456
+ ?.setAttribute("aria-label", resolveRemoveLabel(removeLabel, label));
457
+ return tag;
458
+ });
459
+ input.before(...tags);
460
+ input.placeholder = tags.length > 0 ? "" : this.#placeholder;
461
+ }
462
+ /**
463
+ * @description Empties the committed state: label, hidden value, tags, and
464
+ * `aria-selected` all clear together.
465
+ * @private
466
+ */
467
+ #clearSelection() {
468
+ this.#committedLabel = "";
469
+ const multiselect = this.#multiselect();
470
+ for (const item of this.items()) {
471
+ if (multiselect)
472
+ item.setAttribute("aria-selected", "false");
473
+ else
474
+ item.removeAttribute("aria-selected");
475
+ }
476
+ this.#syncSelection(true);
477
+ }
478
+ /**
479
+ * @description Restores the selection the markup shipped with (form reset).
480
+ * @private
481
+ */
482
+ #restoreDefaults() {
483
+ const input = this.searchInput;
484
+ if (!input)
485
+ return;
486
+ const multiselect = this.#multiselect();
487
+ for (const item of this.items()) {
488
+ const selected = this.#defaultValues.includes(this.#itemFormValue(item));
489
+ if (multiselect)
490
+ item.setAttribute("aria-selected", String(selected));
491
+ else if (selected)
492
+ item.setAttribute("aria-selected", "true");
493
+ else
494
+ item.removeAttribute("aria-selected");
495
+ }
496
+ this.#committedLabel = multiselect ? "" : (this.#selectedLabel() ?? "");
497
+ input.value = this.#committedLabel;
498
+ this.query.set("");
499
+ this.#syncSelection(true);
500
+ }
501
+ }
502
+ defineZazzElement("ui-combobox", UiCombobox);
503
+ export { UiCombobox, resolveBlur, resolveRemoveLabel };