@chromvoid/uikit 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 (246) hide show
  1. package/LICENSE +8 -0
  2. package/README.md +96 -0
  3. package/dist/components/cv-accordion-item.d.ts +69 -0
  4. package/dist/components/cv-accordion-item.js +176 -0
  5. package/dist/components/cv-accordion.d.ts +79 -0
  6. package/dist/components/cv-accordion.js +310 -0
  7. package/dist/components/cv-alert-dialog.d.ts +86 -0
  8. package/dist/components/cv-alert-dialog.js +393 -0
  9. package/dist/components/cv-alert.d.ts +48 -0
  10. package/dist/components/cv-alert.js +156 -0
  11. package/dist/components/cv-badge.d.ts +56 -0
  12. package/dist/components/cv-badge.js +280 -0
  13. package/dist/components/cv-breadcrumb-item.d.ts +35 -0
  14. package/dist/components/cv-breadcrumb-item.js +64 -0
  15. package/dist/components/cv-breadcrumb.d.ts +39 -0
  16. package/dist/components/cv-breadcrumb.js +160 -0
  17. package/dist/components/cv-button.d.ts +83 -0
  18. package/dist/components/cv-button.js +541 -0
  19. package/dist/components/cv-callout.d.ts +32 -0
  20. package/dist/components/cv-callout.js +221 -0
  21. package/dist/components/cv-card.d.ts +48 -0
  22. package/dist/components/cv-card.js +269 -0
  23. package/dist/components/cv-carousel-slide.d.ts +25 -0
  24. package/dist/components/cv-carousel-slide.js +51 -0
  25. package/dist/components/cv-carousel.d.ts +96 -0
  26. package/dist/components/cv-carousel.js +457 -0
  27. package/dist/components/cv-checkbox.d.ts +84 -0
  28. package/dist/components/cv-checkbox.js +274 -0
  29. package/dist/components/cv-combobox-group.d.ts +15 -0
  30. package/dist/components/cv-combobox-group.js +34 -0
  31. package/dist/components/cv-combobox-option.d.ts +30 -0
  32. package/dist/components/cv-combobox-option.js +66 -0
  33. package/dist/components/cv-combobox.d.ts +135 -0
  34. package/dist/components/cv-combobox.js +834 -0
  35. package/dist/components/cv-command-item.d.ts +30 -0
  36. package/dist/components/cv-command-item.js +68 -0
  37. package/dist/components/cv-command-palette.d.ts +105 -0
  38. package/dist/components/cv-command-palette.js +578 -0
  39. package/dist/components/cv-context-menu.d.ts +98 -0
  40. package/dist/components/cv-context-menu.js +515 -0
  41. package/dist/components/cv-copy-button.d.ts +61 -0
  42. package/dist/components/cv-copy-button.js +318 -0
  43. package/dist/components/cv-date-picker.d.ts +161 -0
  44. package/dist/components/cv-date-picker.js +803 -0
  45. package/dist/components/cv-dialog.d.ts +89 -0
  46. package/dist/components/cv-dialog.js +459 -0
  47. package/dist/components/cv-disclosure.d.ts +57 -0
  48. package/dist/components/cv-disclosure.js +241 -0
  49. package/dist/components/cv-drawer.d.ts +102 -0
  50. package/dist/components/cv-drawer.js +595 -0
  51. package/dist/components/cv-feed-article.d.ts +26 -0
  52. package/dist/components/cv-feed-article.js +52 -0
  53. package/dist/components/cv-feed.d.ts +62 -0
  54. package/dist/components/cv-feed.js +310 -0
  55. package/dist/components/cv-grid-cell.d.ts +30 -0
  56. package/dist/components/cv-grid-cell.js +57 -0
  57. package/dist/components/cv-grid-column.d.ts +30 -0
  58. package/dist/components/cv-grid-column.js +43 -0
  59. package/dist/components/cv-grid-row.d.ts +30 -0
  60. package/dist/components/cv-grid-row.js +42 -0
  61. package/dist/components/cv-grid.d.ts +119 -0
  62. package/dist/components/cv-grid.js +567 -0
  63. package/dist/components/cv-icon.d.ts +57 -0
  64. package/dist/components/cv-icon.js +352 -0
  65. package/dist/components/cv-input.d.ts +127 -0
  66. package/dist/components/cv-input.js +482 -0
  67. package/dist/components/cv-landmark.d.ts +32 -0
  68. package/dist/components/cv-landmark.js +62 -0
  69. package/dist/components/cv-link.d.ts +22 -0
  70. package/dist/components/cv-link.js +99 -0
  71. package/dist/components/cv-listbox-group.d.ts +15 -0
  72. package/dist/components/cv-listbox-group.js +42 -0
  73. package/dist/components/cv-listbox.d.ts +81 -0
  74. package/dist/components/cv-listbox.js +388 -0
  75. package/dist/components/cv-menu-button.d.ts +118 -0
  76. package/dist/components/cv-menu-button.js +822 -0
  77. package/dist/components/cv-menu-group.d.ts +20 -0
  78. package/dist/components/cv-menu-group.js +48 -0
  79. package/dist/components/cv-menu-item.d.ts +52 -0
  80. package/dist/components/cv-menu-item.js +105 -0
  81. package/dist/components/cv-menu.d.ts +62 -0
  82. package/dist/components/cv-menu.js +414 -0
  83. package/dist/components/cv-meter.d.ts +66 -0
  84. package/dist/components/cv-meter.js +154 -0
  85. package/dist/components/cv-number.d.ts +139 -0
  86. package/dist/components/cv-number.js +553 -0
  87. package/dist/components/cv-option.d.ts +30 -0
  88. package/dist/components/cv-option.js +84 -0
  89. package/dist/components/cv-popover.d.ts +87 -0
  90. package/dist/components/cv-popover.js +373 -0
  91. package/dist/components/cv-progress-ring.d.ts +45 -0
  92. package/dist/components/cv-progress-ring.js +169 -0
  93. package/dist/components/cv-progress.d.ts +45 -0
  94. package/dist/components/cv-progress.js +148 -0
  95. package/dist/components/cv-radio-group.d.ts +79 -0
  96. package/dist/components/cv-radio-group.js +398 -0
  97. package/dist/components/cv-radio.d.ts +36 -0
  98. package/dist/components/cv-radio.js +123 -0
  99. package/dist/components/cv-select-group.d.ts +15 -0
  100. package/dist/components/cv-select-group.js +44 -0
  101. package/dist/components/cv-select-option.d.ts +30 -0
  102. package/dist/components/cv-select-option.js +66 -0
  103. package/dist/components/cv-select.d.ts +128 -0
  104. package/dist/components/cv-select.js +666 -0
  105. package/dist/components/cv-sidebar-item.d.ts +26 -0
  106. package/dist/components/cv-sidebar-item.js +142 -0
  107. package/dist/components/cv-sidebar.d.ts +171 -0
  108. package/dist/components/cv-sidebar.js +767 -0
  109. package/dist/components/cv-slider-multi-thumb.d.ts +73 -0
  110. package/dist/components/cv-slider-multi-thumb.js +374 -0
  111. package/dist/components/cv-slider.d.ts +84 -0
  112. package/dist/components/cv-slider.js +328 -0
  113. package/dist/components/cv-spinbutton.d.ts +121 -0
  114. package/dist/components/cv-spinbutton.js +486 -0
  115. package/dist/components/cv-spinner.d.ts +18 -0
  116. package/dist/components/cv-spinner.js +95 -0
  117. package/dist/components/cv-switch.d.ts +81 -0
  118. package/dist/components/cv-switch.js +285 -0
  119. package/dist/components/cv-tab-panel.d.ts +20 -0
  120. package/dist/components/cv-tab-panel.js +37 -0
  121. package/dist/components/cv-tab.d.ts +40 -0
  122. package/dist/components/cv-tab.js +132 -0
  123. package/dist/components/cv-table-cell.d.ts +31 -0
  124. package/dist/components/cv-table-cell.js +49 -0
  125. package/dist/components/cv-table-column.d.ts +37 -0
  126. package/dist/components/cv-table-column.js +63 -0
  127. package/dist/components/cv-table-row.d.ts +30 -0
  128. package/dist/components/cv-table-row.js +45 -0
  129. package/dist/components/cv-table.d.ts +147 -0
  130. package/dist/components/cv-table.js +607 -0
  131. package/dist/components/cv-tabs.d.ts +70 -0
  132. package/dist/components/cv-tabs.js +524 -0
  133. package/dist/components/cv-textarea.d.ts +108 -0
  134. package/dist/components/cv-textarea.js +328 -0
  135. package/dist/components/cv-toast-region.d.ts +39 -0
  136. package/dist/components/cv-toast-region.js +162 -0
  137. package/dist/components/cv-toast.d.ts +67 -0
  138. package/dist/components/cv-toast.js +315 -0
  139. package/dist/components/cv-toolbar-item.d.ts +25 -0
  140. package/dist/components/cv-toolbar-item.js +72 -0
  141. package/dist/components/cv-toolbar-separator.d.ts +25 -0
  142. package/dist/components/cv-toolbar-separator.js +45 -0
  143. package/dist/components/cv-toolbar.d.ts +63 -0
  144. package/dist/components/cv-toolbar.js +295 -0
  145. package/dist/components/cv-tooltip.d.ts +83 -0
  146. package/dist/components/cv-tooltip.js +455 -0
  147. package/dist/components/cv-treegrid-cell.d.ts +30 -0
  148. package/dist/components/cv-treegrid-cell.js +57 -0
  149. package/dist/components/cv-treegrid-column.d.ts +37 -0
  150. package/dist/components/cv-treegrid-column.js +53 -0
  151. package/dist/components/cv-treegrid-row.d.ts +55 -0
  152. package/dist/components/cv-treegrid-row.js +90 -0
  153. package/dist/components/cv-treegrid.d.ts +96 -0
  154. package/dist/components/cv-treegrid.js +632 -0
  155. package/dist/components/cv-treeitem.d.ts +58 -0
  156. package/dist/components/cv-treeitem.js +144 -0
  157. package/dist/components/cv-treeview.d.ts +70 -0
  158. package/dist/components/cv-treeview.js +396 -0
  159. package/dist/components/cv-window-splitter.d.ts +79 -0
  160. package/dist/components/cv-window-splitter.js +316 -0
  161. package/dist/components/index.d.ts +94 -0
  162. package/dist/components/index.js +79 -0
  163. package/dist/dialog/create-dialog-controller.d.ts +31 -0
  164. package/dist/dialog/create-dialog-controller.js +320 -0
  165. package/dist/dialog/index.d.ts +2 -0
  166. package/dist/dialog/index.js +1 -0
  167. package/dist/form-associated/FormAssociatedReatomElement.d.ts +25 -0
  168. package/dist/form-associated/FormAssociatedReatomElement.js +70 -0
  169. package/dist/form-associated/withFormAssociated.d.ts +5 -0
  170. package/dist/form-associated/withFormAssociated.js +1 -0
  171. package/dist/index.d.ts +10 -0
  172. package/dist/index.js +9 -0
  173. package/dist/reatom-lit/ReatomLitElement.d.ts +27 -0
  174. package/dist/reatom-lit/ReatomLitElement.js +118 -0
  175. package/dist/reatom-lit/html.d.ts +4 -0
  176. package/dist/reatom-lit/html.js +10 -0
  177. package/dist/reatom-lit/index.d.ts +4 -0
  178. package/dist/reatom-lit/index.js +4 -0
  179. package/dist/reatom-lit/watch.d.ts +15 -0
  180. package/dist/reatom-lit/watch.js +40 -0
  181. package/dist/reatom-lit/withReatomElement.d.ts +4 -0
  182. package/dist/reatom-lit/withReatomElement.js +57 -0
  183. package/dist/register.d.ts +1 -0
  184. package/dist/register.js +84 -0
  185. package/dist/styles/component-styles.d.ts +4 -0
  186. package/dist/styles/component-styles.js +78 -0
  187. package/dist/theme/cv-theme-provider.d.ts +32 -0
  188. package/dist/theme/cv-theme-provider.js +110 -0
  189. package/dist/theme/index.d.ts +4 -0
  190. package/dist/theme/index.js +2 -0
  191. package/dist/theme/theme-engine.d.ts +4 -0
  192. package/dist/theme/theme-engine.js +67 -0
  193. package/dist/theme/tokens.css +265 -0
  194. package/dist/theme/types.d.ts +7 -0
  195. package/dist/theme/types.js +1 -0
  196. package/dist/toast/create-toast-controller.d.ts +12 -0
  197. package/dist/toast/create-toast-controller.js +12 -0
  198. package/dist/toast/index.d.ts +2 -0
  199. package/dist/toast/index.js +1 -0
  200. package/package.json +146 -0
  201. package/specs/_template.md +110 -0
  202. package/specs/components/accordion.md +207 -0
  203. package/specs/components/alert.md +83 -0
  204. package/specs/components/badge.md +183 -0
  205. package/specs/components/breadcrumb.md +152 -0
  206. package/specs/components/button.md +227 -0
  207. package/specs/components/callout.md +153 -0
  208. package/specs/components/card.md +192 -0
  209. package/specs/components/carousel.md +232 -0
  210. package/specs/components/checkbox.md +141 -0
  211. package/specs/components/combobox.md +427 -0
  212. package/specs/components/context-menu.md +375 -0
  213. package/specs/components/copy-button.md +236 -0
  214. package/specs/components/date-picker.md +290 -0
  215. package/specs/components/dialog.md +184 -0
  216. package/specs/components/disclosure.md +151 -0
  217. package/specs/components/drawer.md +216 -0
  218. package/specs/components/feed.md +266 -0
  219. package/specs/components/grid.md +423 -0
  220. package/specs/components/input.md +237 -0
  221. package/specs/components/landmark.md +92 -0
  222. package/specs/components/link.md +117 -0
  223. package/specs/components/listbox.md +327 -0
  224. package/specs/components/menu.md +508 -0
  225. package/specs/components/meter.md +148 -0
  226. package/specs/components/number.md +268 -0
  227. package/specs/components/option.md +167 -0
  228. package/specs/components/popover.md +207 -0
  229. package/specs/components/progress-ring.md +134 -0
  230. package/specs/components/progress.md +110 -0
  231. package/specs/components/radio.md +208 -0
  232. package/specs/components/select.md +305 -0
  233. package/specs/components/sidebar.md +204 -0
  234. package/specs/components/spinbutton.md +157 -0
  235. package/specs/components/spinner.md +83 -0
  236. package/specs/components/switch.md +145 -0
  237. package/specs/components/table.md +372 -0
  238. package/specs/components/tabs.md +242 -0
  239. package/specs/components/textarea.md +166 -0
  240. package/specs/components/theme.md +364 -0
  241. package/specs/components/toast.md +198 -0
  242. package/specs/components/toolbar.md +258 -0
  243. package/specs/components/tooltip.md +152 -0
  244. package/specs/components/treegrid.md +363 -0
  245. package/specs/components/treeview.md +263 -0
  246. package/specs/components/window-splitter.md +225 -0
@@ -0,0 +1,834 @@
1
+ import { createCombobox, } from '@chromvoid/headless-ui/combobox';
2
+ import { css, html, nothing } from 'lit';
3
+ import { ReatomLitElement } from '../reatom-lit/ReatomLitElement.js';
4
+ import { CVComboboxGroup } from './cv-combobox-group.js';
5
+ import { CVComboboxOption } from './cv-combobox-option.js';
6
+ const comboboxNavigationKeys = new Set(['ArrowUp', 'ArrowDown', 'Home', 'End', 'Enter', 'Escape']);
7
+ function isVisibleGroup(item) {
8
+ return 'options' in item && Array.isArray(item.options);
9
+ }
10
+ let cvComboboxNonce = 0;
11
+ export class CVCombobox extends ReatomLitElement {
12
+ static elementName = 'cv-combobox';
13
+ static hostDisplay = 'inline-block';
14
+ static get properties() {
15
+ return {
16
+ value: { type: String, reflect: true },
17
+ inputValue: { type: String, attribute: 'input-value' },
18
+ open: { type: Boolean, reflect: true },
19
+ type: { type: String, reflect: true },
20
+ multiple: { type: Boolean, reflect: true },
21
+ clearable: { type: Boolean, reflect: true },
22
+ maxTagsVisible: { type: Number, attribute: 'max-tags-visible' },
23
+ openOnFocus: { type: Boolean, attribute: 'open-on-focus', reflect: true },
24
+ openOnClick: { type: Boolean, attribute: 'open-on-click', reflect: true },
25
+ closeOnSelect: { type: Boolean, attribute: 'close-on-select', reflect: true },
26
+ matchMode: { type: String, attribute: 'match-mode', reflect: true },
27
+ placeholder: { type: String },
28
+ ariaLabel: { type: String, attribute: 'aria-label' },
29
+ };
30
+ }
31
+ idBase = `cv-combobox-${++cvComboboxNonce}`;
32
+ optionRecords = [];
33
+ groupRecords = [];
34
+ optionListeners = new WeakMap();
35
+ model;
36
+ constructor() {
37
+ super();
38
+ this.value = '';
39
+ this.inputValue = '';
40
+ this.open = false;
41
+ this.type = 'editable';
42
+ this.multiple = false;
43
+ this.clearable = false;
44
+ this.maxTagsVisible = 3;
45
+ this.openOnFocus = true;
46
+ this.openOnClick = true;
47
+ this.closeOnSelect = true;
48
+ this.matchMode = 'includes';
49
+ this.placeholder = '';
50
+ this.ariaLabel = '';
51
+ }
52
+ static styles = [
53
+ css `
54
+ :host {
55
+ inline-size: 260px;
56
+ }
57
+
58
+ [part='base'] {
59
+ display: grid;
60
+ gap: var(--cv-space-1, 4px);
61
+ }
62
+
63
+ [part='input-wrapper'] {
64
+ flex-wrap: wrap;
65
+ gap: var(--cv-space-1, 4px);
66
+ min-block-size: 36px;
67
+ padding: 0 var(--cv-space-3, 12px);
68
+ }
69
+
70
+ [part='input'] {
71
+ min-inline-size: 60px;
72
+ block-size: 100%;
73
+ min-block-size: 36px;
74
+ border: none;
75
+ background: transparent;
76
+ color: var(--cv-color-text, #e8ecf6);
77
+ outline: none;
78
+ padding: 0;
79
+ }
80
+
81
+ [part='input']:focus-visible {
82
+ outline: none;
83
+ }
84
+
85
+ [part='trigger'] {
86
+ gap: var(--cv-space-2, 8px);
87
+ min-block-size: 36px;
88
+ border: none;
89
+ background: transparent;
90
+ color: var(--cv-color-text, #e8ecf6);
91
+ cursor: pointer;
92
+ outline: none;
93
+ padding: 0;
94
+ }
95
+
96
+ [part='trigger']:focus-visible {
97
+ outline: none;
98
+ }
99
+
100
+ [part='label'] {
101
+ text-align: start;
102
+ }
103
+
104
+ [part='tags'] {
105
+ flex-wrap: wrap;
106
+ gap: var(--cv-space-1, 4px);
107
+ }
108
+
109
+ [part='tag'] {
110
+ gap: var(--cv-space-1, 4px);
111
+ padding: 2px var(--cv-space-2, 8px);
112
+ border-radius: var(--cv-radius-sm, 6px);
113
+ background: color-mix(in oklab, var(--cv-color-primary, #65d7ff) 24%, transparent);
114
+ font-size: 0.85em;
115
+ }
116
+
117
+ [part='tag-remove'] {
118
+ border: none;
119
+ background: transparent;
120
+ color: inherit;
121
+ cursor: pointer;
122
+ padding: 0;
123
+ font-size: 1em;
124
+ line-height: 1;
125
+ }
126
+
127
+ [part='tag-overflow'] {
128
+ font-size: 0.85em;
129
+ color: var(--cv-color-text-muted, #9aa6bf);
130
+ }
131
+
132
+ [part='clear-button'] {
133
+ border: none;
134
+ background: transparent;
135
+ color: var(--cv-color-text-muted, #9aa6bf);
136
+ cursor: pointer;
137
+ padding: 0 var(--cv-space-1, 4px);
138
+ }
139
+
140
+ [part='expand-icon'] {
141
+ color: var(--cv-color-text-muted, #9aa6bf);
142
+ }
143
+
144
+ [part='listbox'] {
145
+ gap: var(--cv-space-1, 4px);
146
+ max-block-size: 220px;
147
+ overflow: auto;
148
+ padding: var(--cv-space-1, 4px);
149
+ background: var(--cv-color-surface, #141923);
150
+ }
151
+
152
+ [part='group'] {
153
+ display: grid;
154
+ gap: var(--cv-space-1, 4px);
155
+ }
156
+
157
+ [part='group-label'] {
158
+ padding: var(--cv-space-1, 4px) var(--cv-space-2, 8px);
159
+ font-size: 0.75rem;
160
+ letter-spacing: 0.02em;
161
+ color: var(--cv-color-text-muted, #9aa6bf);
162
+ }
163
+ `,
164
+ ];
165
+ static define() {
166
+ if (!customElements.get(this.elementName)) {
167
+ customElements.define(this.elementName, this);
168
+ }
169
+ }
170
+ connectedCallback() {
171
+ super.connectedCallback();
172
+ if (!this.model) {
173
+ this.rebuildModelFromSlot(false, false);
174
+ }
175
+ this.syncOutsidePointerListener();
176
+ }
177
+ disconnectedCallback() {
178
+ super.disconnectedCallback();
179
+ this.detachOptionListeners();
180
+ this.syncOutsidePointerListener(true);
181
+ }
182
+ willUpdate(changedProperties) {
183
+ super.willUpdate(changedProperties);
184
+ if (changedProperties.has('ariaLabel') ||
185
+ changedProperties.has('closeOnSelect') ||
186
+ changedProperties.has('matchMode') ||
187
+ changedProperties.has('type') ||
188
+ changedProperties.has('multiple') ||
189
+ changedProperties.has('clearable')) {
190
+ this.rebuildModelFromSlot(true, false);
191
+ return;
192
+ }
193
+ if (!this.model)
194
+ return;
195
+ if (changedProperties.has('value')) {
196
+ const next = this.value.trim();
197
+ const previous = this.captureState();
198
+ if (next.length === 0) {
199
+ this.model.actions.clearSelection();
200
+ this.applyInteractionResult(previous);
201
+ }
202
+ else if (this.multiple) {
203
+ // Multi-mode: parse space-delimited ids
204
+ const ids = next.split(/\s+/).filter(Boolean);
205
+ const currentIds = this.model.state.selectedIds();
206
+ if (ids.join(' ') !== currentIds.join(' ')) {
207
+ // Clear and re-select each id
208
+ this.model.actions.clearSelection();
209
+ for (const id of ids) {
210
+ this.model.actions.toggleOption(id);
211
+ }
212
+ this.applyInteractionResult(previous);
213
+ }
214
+ }
215
+ else if (this.model.state.selectedId() !== next) {
216
+ this.model.actions.select(next);
217
+ this.applyInteractionResult(previous);
218
+ }
219
+ }
220
+ if (changedProperties.has('inputValue') && this.model.state.inputValue() !== this.inputValue) {
221
+ const previous = this.captureState();
222
+ this.model.actions.setInputValue(this.inputValue);
223
+ this.applyInteractionResult(previous);
224
+ }
225
+ if (changedProperties.has('open') && this.model.state.isOpen() !== this.open) {
226
+ const previous = this.captureState();
227
+ if (this.open) {
228
+ this.model.actions.open();
229
+ }
230
+ else {
231
+ this.model.actions.close();
232
+ }
233
+ this.applyInteractionResult(previous);
234
+ }
235
+ }
236
+ updated(changedProperties) {
237
+ super.updated(changedProperties);
238
+ this.syncOutsidePointerListener();
239
+ if (!changedProperties.has('value') &&
240
+ !changedProperties.has('inputValue') &&
241
+ !changedProperties.has('open')) {
242
+ this.syncOptionElements();
243
+ }
244
+ }
245
+ getOptionElements() {
246
+ const result = [];
247
+ for (const child of Array.from(this.children)) {
248
+ if (child.tagName.toLowerCase() === CVComboboxOption.elementName) {
249
+ result.push(child);
250
+ }
251
+ else if (child.tagName.toLowerCase() === CVComboboxGroup.elementName) {
252
+ for (const groupChild of Array.from(child.children)) {
253
+ if (groupChild.tagName.toLowerCase() === CVComboboxOption.elementName) {
254
+ result.push(groupChild);
255
+ }
256
+ }
257
+ }
258
+ }
259
+ return result;
260
+ }
261
+ getGroupElements() {
262
+ return Array.from(this.children).filter((element) => element.tagName.toLowerCase() === CVComboboxGroup.elementName);
263
+ }
264
+ ensureOptionValue(option, index) {
265
+ const normalized = option.value?.trim();
266
+ if (normalized)
267
+ return normalized;
268
+ const fallback = `option-${index + 1}`;
269
+ option.value = fallback;
270
+ return fallback;
271
+ }
272
+ resolveInitialSelected(optionElements) {
273
+ const fromProperty = this.value.trim();
274
+ if (fromProperty.length > 0)
275
+ return fromProperty;
276
+ for (const [index, option] of optionElements.entries()) {
277
+ if (option.selected && !option.disabled) {
278
+ return this.ensureOptionValue(option, index);
279
+ }
280
+ }
281
+ return null;
282
+ }
283
+ rebuildModelFromSlot(preserveState, requestRender = true) {
284
+ const optionElements = this.getOptionElements();
285
+ const groupElements = this.getGroupElements();
286
+ const previousState = preserveState
287
+ ? this.captureState()
288
+ : {
289
+ selectedId: this.resolveInitialSelected(optionElements),
290
+ selectedIds: this.multiple ? this.value.trim().split(/\s+/).filter(Boolean) : [],
291
+ inputValue: this.inputValue,
292
+ activeId: null,
293
+ isOpen: this.open,
294
+ };
295
+ this.detachOptionListeners();
296
+ // Build group records
297
+ let groupNonce = 0;
298
+ this.groupRecords = groupElements.map((element) => {
299
+ const id = `group-${++groupNonce}`;
300
+ const label = element.label || element.getAttribute('label') || '';
301
+ const childOptions = Array.from(element.children).filter((child) => child.tagName.toLowerCase() === CVComboboxOption.elementName);
302
+ const optionIds = [];
303
+ return { id, label, element, optionIds };
304
+ });
305
+ // Build option records with group assignment
306
+ let optionIndex = 0;
307
+ this.optionRecords = [];
308
+ // Map from group element to group record
309
+ const groupElementMap = new Map();
310
+ for (const gr of this.groupRecords) {
311
+ groupElementMap.set(gr.element, gr);
312
+ }
313
+ for (const child of Array.from(this.children)) {
314
+ if (child.tagName.toLowerCase() === CVComboboxOption.elementName) {
315
+ const element = child;
316
+ const id = this.ensureOptionValue(element, optionIndex);
317
+ const label = element.textContent?.trim() || id;
318
+ this.optionRecords.push({ id, label, disabled: element.disabled, element });
319
+ optionIndex++;
320
+ }
321
+ else if (child.tagName.toLowerCase() === CVComboboxGroup.elementName) {
322
+ const groupRecord = groupElementMap.get(child);
323
+ for (const groupChild of Array.from(child.children)) {
324
+ if (groupChild.tagName.toLowerCase() === CVComboboxOption.elementName) {
325
+ const element = groupChild;
326
+ const id = this.ensureOptionValue(element, optionIndex);
327
+ const label = element.textContent?.trim() || id;
328
+ const record = {
329
+ id,
330
+ label,
331
+ disabled: element.disabled,
332
+ element,
333
+ groupId: groupRecord?.id,
334
+ };
335
+ this.optionRecords.push(record);
336
+ groupRecord?.optionIds.push(id);
337
+ optionIndex++;
338
+ }
339
+ }
340
+ }
341
+ }
342
+ const enabledIds = new Set(this.optionRecords.filter((record) => !record.disabled).map((record) => record.id));
343
+ // Build headless options structure (with groups if any)
344
+ const hasGroups = this.groupRecords.length > 0;
345
+ let headlessOptions;
346
+ if (hasGroups) {
347
+ headlessOptions = [];
348
+ const usedGroupIds = new Set();
349
+ for (const child of Array.from(this.children)) {
350
+ if (child.tagName.toLowerCase() === CVComboboxOption.elementName) {
351
+ const record = this.optionRecords.find((r) => r.element === child);
352
+ if (record) {
353
+ headlessOptions.push({ id: record.id, label: record.label, disabled: record.disabled });
354
+ }
355
+ }
356
+ else if (child.tagName.toLowerCase() === CVComboboxGroup.elementName) {
357
+ const groupRecord = groupElementMap.get(child);
358
+ if (groupRecord && !usedGroupIds.has(groupRecord.id)) {
359
+ usedGroupIds.add(groupRecord.id);
360
+ headlessOptions.push({
361
+ id: groupRecord.id,
362
+ label: groupRecord.label,
363
+ options: groupRecord.optionIds.map((optId) => {
364
+ const rec = this.optionRecords.find((r) => r.id === optId);
365
+ return { id: rec.id, label: rec.label, disabled: rec.disabled };
366
+ }),
367
+ });
368
+ }
369
+ }
370
+ }
371
+ }
372
+ else {
373
+ headlessOptions = this.optionRecords.map((record) => ({
374
+ id: record.id,
375
+ label: record.label,
376
+ disabled: record.disabled,
377
+ }));
378
+ }
379
+ // Determine initial selection
380
+ let initialSelectedId = null;
381
+ let initialSelectedIds;
382
+ if (this.multiple) {
383
+ const prevIds = previousState.selectedIds ?? [];
384
+ initialSelectedIds = prevIds.filter((id) => enabledIds.has(id));
385
+ }
386
+ else {
387
+ initialSelectedId =
388
+ previousState.selectedId && enabledIds.has(previousState.selectedId) ? previousState.selectedId : null;
389
+ }
390
+ // Determine effective closeOnSelect
391
+ const effectiveCloseOnSelect = this.multiple ? false : this.closeOnSelect;
392
+ this.model = createCombobox({
393
+ idBase: this.idBase,
394
+ options: headlessOptions,
395
+ type: this.type,
396
+ multiple: this.multiple,
397
+ clearable: this.clearable,
398
+ ariaLabel: this.ariaLabel || undefined,
399
+ initialInputValue: previousState.inputValue,
400
+ initialSelectedId: this.multiple ? undefined : initialSelectedId,
401
+ initialSelectedIds: this.multiple ? initialSelectedIds : undefined,
402
+ initialOpen: previousState.isOpen,
403
+ closeOnSelect: effectiveCloseOnSelect,
404
+ matchMode: this.matchMode === 'startsWith' ? 'startsWith' : 'includes',
405
+ });
406
+ if (previousState.activeId && enabledIds.has(previousState.activeId)) {
407
+ this.model.actions.setActive(previousState.activeId);
408
+ }
409
+ this.attachOptionListeners();
410
+ this.syncOptionElements();
411
+ this.syncHostState();
412
+ if (requestRender) {
413
+ this.requestUpdate();
414
+ }
415
+ }
416
+ syncHostState() {
417
+ if (!this.model)
418
+ return;
419
+ if (this.multiple) {
420
+ const ids = this.model.state.selectedIds();
421
+ this.value = ids.join(' ');
422
+ }
423
+ else {
424
+ this.value = this.model.state.selectedId() ?? '';
425
+ }
426
+ this.inputValue = this.model.state.inputValue();
427
+ this.open = this.model.state.isOpen();
428
+ }
429
+ detachOptionListeners() {
430
+ for (const record of this.optionRecords) {
431
+ const listeners = this.optionListeners.get(record.element);
432
+ if (!listeners)
433
+ continue;
434
+ record.element.removeEventListener('click', listeners.click);
435
+ record.element.removeEventListener('mouseenter', listeners.mouseenter);
436
+ this.optionListeners.delete(record.element);
437
+ }
438
+ }
439
+ attachOptionListeners() {
440
+ if (!this.model)
441
+ return;
442
+ for (const record of this.optionRecords) {
443
+ const click = (event) => {
444
+ event.preventDefault();
445
+ this.handleOptionClick(record.id);
446
+ };
447
+ const mouseenter = () => {
448
+ this.handleOptionMouseEnter(record.id);
449
+ };
450
+ record.element.addEventListener('click', click);
451
+ record.element.addEventListener('mouseenter', mouseenter);
452
+ this.optionListeners.set(record.element, { click, mouseenter });
453
+ }
454
+ }
455
+ syncOptionElements() {
456
+ if (!this.model)
457
+ return;
458
+ const isOpen = this.model.state.isOpen();
459
+ const visibleOptions = this.model.contracts.getVisibleOptions();
460
+ // Build a set of visible option ids (flat)
461
+ const visibleIds = new Set();
462
+ const visibleGroupIds = new Set();
463
+ for (const item of visibleOptions) {
464
+ if (isVisibleGroup(item)) {
465
+ visibleGroupIds.add(item.id);
466
+ for (const opt of item.options) {
467
+ visibleIds.add(opt.id);
468
+ }
469
+ }
470
+ else {
471
+ visibleIds.add(item.id);
472
+ }
473
+ }
474
+ for (const record of this.optionRecords) {
475
+ const props = this.model.contracts.getOptionProps(record.id);
476
+ record.element.id = props.id;
477
+ record.element.setAttribute('role', props.role);
478
+ record.element.setAttribute('tabindex', props.tabindex);
479
+ record.element.setAttribute('aria-selected', props['aria-selected']);
480
+ if (props['aria-disabled']) {
481
+ record.element.setAttribute('aria-disabled', props['aria-disabled']);
482
+ }
483
+ else {
484
+ record.element.removeAttribute('aria-disabled');
485
+ }
486
+ if (props['data-active'] === 'true') {
487
+ record.element.setAttribute('data-active', 'true');
488
+ record.element.active = true;
489
+ }
490
+ else {
491
+ record.element.removeAttribute('data-active');
492
+ record.element.active = false;
493
+ }
494
+ record.element.selected = props['aria-selected'] === 'true';
495
+ record.element.disabled = props['aria-disabled'] === 'true';
496
+ record.element.hidden = !isOpen || !visibleIds.has(record.id);
497
+ }
498
+ // Sync group element visibility
499
+ for (const groupRecord of this.groupRecords) {
500
+ const allHidden = groupRecord.optionIds.every((id) => !visibleIds.has(id));
501
+ groupRecord.element.hidden = !isOpen || allHidden;
502
+ }
503
+ }
504
+ captureState() {
505
+ return {
506
+ selectedId: this.model?.state.selectedId() ?? (this.value.trim() || null),
507
+ selectedIds: this.model?.state.selectedIds() ?? [],
508
+ inputValue: this.model?.state.inputValue() ?? this.inputValue,
509
+ activeId: this.model?.state.activeId() ?? null,
510
+ isOpen: this.model?.state.isOpen() ?? this.open,
511
+ };
512
+ }
513
+ makeEventDetail() {
514
+ const state = this.captureState();
515
+ return {
516
+ value: this.multiple
517
+ ? state.selectedIds.length > 0
518
+ ? state.selectedIds.join(' ')
519
+ : null
520
+ : state.selectedId,
521
+ inputValue: state.inputValue,
522
+ activeId: state.activeId,
523
+ open: state.isOpen,
524
+ selectedIds: state.selectedIds,
525
+ };
526
+ }
527
+ dispatchInput(detail) {
528
+ this.dispatchEvent(new CustomEvent('cv-input', {
529
+ detail,
530
+ bubbles: true,
531
+ composed: true,
532
+ }));
533
+ }
534
+ dispatchChange(detail) {
535
+ this.dispatchEvent(new CustomEvent('cv-change', {
536
+ detail,
537
+ bubbles: true,
538
+ composed: true,
539
+ }));
540
+ }
541
+ applyInteractionResult(previousState) {
542
+ if (!this.model)
543
+ return;
544
+ this.syncOptionElements();
545
+ const nextState = this.captureState();
546
+ this.syncHostState();
547
+ const selectedChanged = this.multiple
548
+ ? previousState.selectedIds.join(' ') !== nextState.selectedIds.join(' ')
549
+ : previousState.selectedId !== nextState.selectedId;
550
+ const inputChanged = previousState.inputValue !== nextState.inputValue;
551
+ const activeChanged = previousState.activeId !== nextState.activeId;
552
+ const openChanged = previousState.isOpen !== nextState.isOpen;
553
+ if (!selectedChanged && !inputChanged && !activeChanged && !openChanged)
554
+ return;
555
+ const detail = this.makeEventDetail();
556
+ this.dispatchInput(detail);
557
+ if (selectedChanged) {
558
+ this.dispatchChange(detail);
559
+ }
560
+ }
561
+ syncOutsidePointerListener(forceOff = false) {
562
+ const shouldListen = !forceOff && this.open;
563
+ if (shouldListen) {
564
+ document.addEventListener('pointerdown', this.handleDocumentPointerDown);
565
+ }
566
+ else {
567
+ document.removeEventListener('pointerdown', this.handleDocumentPointerDown);
568
+ }
569
+ }
570
+ handleDocumentPointerDown = (event) => {
571
+ if (!this.model || !this.model.state.isOpen())
572
+ return;
573
+ const path = event.composedPath();
574
+ if (path.includes(this))
575
+ return;
576
+ const previous = this.captureState();
577
+ this.model.actions.close();
578
+ this.applyInteractionResult(previous);
579
+ };
580
+ handleInputChange(event) {
581
+ if (!this.model)
582
+ return;
583
+ const value = event.currentTarget.value;
584
+ const previous = this.captureState();
585
+ this.model.actions.setInputValue(value);
586
+ this.applyInteractionResult(previous);
587
+ }
588
+ handleInputFocus() {
589
+ if (!this.model || this.model.state.isOpen() || !this.openOnFocus)
590
+ return;
591
+ const previous = this.captureState();
592
+ this.model.actions.open();
593
+ this.applyInteractionResult(previous);
594
+ }
595
+ handleInputClick() {
596
+ if (!this.model || this.model.state.isOpen() || !this.openOnClick)
597
+ return;
598
+ const previous = this.captureState();
599
+ this.model.actions.open();
600
+ this.applyInteractionResult(previous);
601
+ }
602
+ handleKeyDown(event) {
603
+ if (!this.model)
604
+ return;
605
+ if (comboboxNavigationKeys.has(event.key) || event.key === ' ') {
606
+ event.preventDefault();
607
+ }
608
+ const previous = this.captureState();
609
+ this.model.actions.handleKeyDown({
610
+ key: event.key,
611
+ shiftKey: event.shiftKey,
612
+ ctrlKey: event.ctrlKey,
613
+ metaKey: event.metaKey,
614
+ altKey: event.altKey,
615
+ });
616
+ this.applyInteractionResult(previous);
617
+ }
618
+ handleOptionMouseEnter(id) {
619
+ if (!this.model)
620
+ return;
621
+ const previous = this.captureState();
622
+ this.model.actions.setActive(id);
623
+ this.applyInteractionResult(previous);
624
+ }
625
+ handleOptionClick(id) {
626
+ if (!this.model)
627
+ return;
628
+ const previous = this.captureState();
629
+ this.model.actions.select(id);
630
+ this.applyInteractionResult(previous);
631
+ }
632
+ handleClearClick(event) {
633
+ event.stopPropagation();
634
+ if (!this.model)
635
+ return;
636
+ const previous = this.captureState();
637
+ this.model.actions.clear();
638
+ this.applyInteractionResult(previous);
639
+ this.dispatchEvent(new CustomEvent('cv-clear', {
640
+ detail: {},
641
+ bubbles: true,
642
+ composed: true,
643
+ }));
644
+ }
645
+ handleTagRemove(id) {
646
+ if (!this.model)
647
+ return;
648
+ const previous = this.captureState();
649
+ this.model.actions.removeSelected(id);
650
+ this.applyInteractionResult(previous);
651
+ }
652
+ handleSlotChange() {
653
+ this.rebuildModelFromSlot(true, true);
654
+ }
655
+ getSelectedOptionLabel() {
656
+ if (!this.model)
657
+ return this.placeholder;
658
+ const selectedId = this.model.state.selectedId();
659
+ if (!selectedId)
660
+ return this.placeholder;
661
+ const record = this.optionRecords.find((r) => r.id === selectedId);
662
+ return record?.label ?? this.placeholder;
663
+ }
664
+ getSelectedRecords() {
665
+ if (!this.model)
666
+ return [];
667
+ const ids = this.model.state.selectedIds();
668
+ return ids
669
+ .map((id) => this.optionRecords.find((r) => r.id === id))
670
+ .filter((r) => r != null);
671
+ }
672
+ renderTags() {
673
+ if (!this.multiple)
674
+ return nothing;
675
+ const selectedRecords = this.getSelectedRecords();
676
+ if (selectedRecords.length === 0)
677
+ return nothing;
678
+ const maxVisible = this.maxTagsVisible > 0 ? this.maxTagsVisible : selectedRecords.length;
679
+ const visibleRecords = selectedRecords.slice(0, maxVisible);
680
+ const overflowCount = selectedRecords.length - maxVisible;
681
+ return html `
682
+ <div part="tags" class="cv-u-row">
683
+ ${visibleRecords.map((record) => html `
684
+ <span part="tag" class="cv-u-row">
685
+ <span part="tag-label">${record.label}</span>
686
+ <button
687
+ part="tag-remove"
688
+ class="cv-u-icon-slot"
689
+ aria-label="Remove ${record.label}"
690
+ @click=${(e) => {
691
+ e.stopPropagation();
692
+ this.handleTagRemove(record.id);
693
+ }}
694
+ >&times;</button>
695
+ </span>
696
+ `)}
697
+ ${overflowCount > 0 ? html `<span part="tag-overflow">+${overflowCount} more</span>` : nothing}
698
+ </div>
699
+ `;
700
+ }
701
+ renderClearButton() {
702
+ if (!this.clearable)
703
+ return nothing;
704
+ if (!this.model?.state.hasSelection())
705
+ return nothing;
706
+ return html `
707
+ <button part="clear-button" class="cv-u-icon-slot" aria-label="Clear" @click=${this.handleClearClick}>
708
+ &times;
709
+ </button>
710
+ `;
711
+ }
712
+ renderListboxContent() {
713
+ if (!this.model || this.groupRecords.length === 0) {
714
+ return html `<slot @slotchange=${this.handleSlotChange}></slot>`;
715
+ }
716
+ // Grouped rendering: render groups with their options inside the shadow listbox
717
+ const visibleOptions = this.model.contracts.getVisibleOptions();
718
+ const visibleGroupIds = new Set();
719
+ const visibleOptionIdsInGroup = new Map();
720
+ for (const item of visibleOptions) {
721
+ if (isVisibleGroup(item)) {
722
+ visibleGroupIds.add(item.id);
723
+ const optIds = new Set(item.options.map((o) => o.id));
724
+ visibleOptionIdsInGroup.set(item.id, optIds);
725
+ }
726
+ }
727
+ return html `
728
+ ${this.groupRecords.map((groupRecord) => {
729
+ const groupProps = this.model.contracts.getGroupProps(groupRecord.id);
730
+ const groupLabelProps = this.model.contracts.getGroupLabelProps(groupRecord.id);
731
+ const isGroupVisible = visibleGroupIds.has(groupRecord.id);
732
+ return html `
733
+ <div
734
+ part="group"
735
+ id=${groupProps.id}
736
+ role=${groupProps.role}
737
+ aria-labelledby=${groupProps['aria-labelledby']}
738
+ ?hidden=${!this.open || !isGroupVisible}
739
+ >
740
+ <div
741
+ part="group-label"
742
+ id=${groupLabelProps.id}
743
+ role=${groupLabelProps.role}
744
+ >${groupRecord.label}</div>
745
+ <slot name=${groupRecord.id}></slot>
746
+ </div>
747
+ `;
748
+ })}
749
+ <slot @slotchange=${this.handleSlotChange}></slot>
750
+ `;
751
+ }
752
+ render() {
753
+ const isSelectOnly = this.type === 'select-only';
754
+ const inputProps = this.model?.contracts.getInputProps() ?? {
755
+ id: `${this.idBase}-input`,
756
+ role: 'combobox',
757
+ tabindex: '0',
758
+ 'aria-haspopup': 'listbox',
759
+ 'aria-expanded': this.open ? 'true' : 'false',
760
+ 'aria-controls': `${this.idBase}-listbox`,
761
+ 'aria-autocomplete': isSelectOnly ? undefined : 'list',
762
+ 'aria-activedescendant': undefined,
763
+ 'aria-label': this.ariaLabel || undefined,
764
+ };
765
+ const listboxProps = this.model?.contracts.getListboxProps() ?? {
766
+ id: `${this.idBase}-listbox`,
767
+ role: 'listbox',
768
+ tabindex: '-1',
769
+ 'aria-label': this.ariaLabel || undefined,
770
+ };
771
+ const hasGroups = this.groupRecords.length > 0;
772
+ return html `
773
+ <div part="base">
774
+ <div part="input-wrapper" class="cv-u-control-shell">
775
+ ${this.renderTags()}
776
+ ${isSelectOnly
777
+ ? html `
778
+ <div
779
+ id=${inputProps.id}
780
+ role=${inputProps.role}
781
+ tabindex=${inputProps.tabindex}
782
+ aria-haspopup=${inputProps['aria-haspopup']}
783
+ aria-expanded=${inputProps['aria-expanded']}
784
+ aria-controls=${inputProps['aria-controls']}
785
+ aria-activedescendant=${inputProps['aria-activedescendant'] ?? nothing}
786
+ aria-label=${inputProps['aria-label'] ?? nothing}
787
+ part="trigger"
788
+ class="cv-u-row cv-u-fill"
789
+ @click=${this.handleInputClick}
790
+ @keydown=${this.handleKeyDown}
791
+ >
792
+ <span part="label" class="cv-u-fill">${this.getSelectedOptionLabel()}</span>
793
+ </div>
794
+ `
795
+ : html `
796
+ <input
797
+ id=${inputProps.id}
798
+ role=${inputProps.role}
799
+ tabindex=${inputProps.tabindex}
800
+ aria-haspopup=${inputProps['aria-haspopup']}
801
+ aria-expanded=${inputProps['aria-expanded']}
802
+ aria-controls=${inputProps['aria-controls']}
803
+ aria-autocomplete=${inputProps['aria-autocomplete'] ?? nothing}
804
+ aria-activedescendant=${inputProps['aria-activedescendant'] ?? nothing}
805
+ aria-label=${inputProps['aria-label'] ?? nothing}
806
+ .value=${this.inputValue}
807
+ placeholder=${this.placeholder}
808
+ part="input"
809
+ class="cv-u-fill"
810
+ @input=${this.handleInputChange}
811
+ @focus=${this.handleInputFocus}
812
+ @click=${this.handleInputClick}
813
+ @keydown=${this.handleKeyDown}
814
+ />
815
+ `}
816
+ ${this.renderClearButton()}
817
+ </div>
818
+
819
+ <div
820
+ id=${listboxProps.id}
821
+ role=${listboxProps.role}
822
+ tabindex=${listboxProps.tabindex}
823
+ aria-label=${listboxProps['aria-label'] ?? nothing}
824
+ aria-multiselectable=${listboxProps['aria-multiselectable'] ?? nothing}
825
+ ?hidden=${!this.open}
826
+ part="listbox"
827
+ class="cv-u-panel-shell"
828
+ >
829
+ ${hasGroups ? this.renderListboxContent() : html `<slot @slotchange=${this.handleSlotChange}></slot>`}
830
+ </div>
831
+ </div>
832
+ `;
833
+ }
834
+ }