basecoat-css 0.3.11 → 1.0.0-beta.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 (97) hide show
  1. package/README.md +8 -1
  2. package/dist/base/base.css +150 -0
  3. package/dist/basecoat-base.cdn.css +2942 -0
  4. package/dist/basecoat-base.cdn.min.css +2 -0
  5. package/dist/basecoat-base.css +2 -0
  6. package/dist/basecoat-components.css +32 -0
  7. package/dist/basecoat-luma.cdn.css +6073 -0
  8. package/dist/basecoat-luma.cdn.min.css +2 -0
  9. package/dist/basecoat-luma.css +2 -0
  10. package/dist/basecoat-lyra.cdn.css +6004 -0
  11. package/dist/basecoat-lyra.cdn.min.css +2 -0
  12. package/dist/basecoat-lyra.css +2 -0
  13. package/dist/basecoat-maia.cdn.css +6012 -0
  14. package/dist/basecoat-maia.cdn.min.css +2 -0
  15. package/dist/basecoat-maia.css +2 -0
  16. package/dist/basecoat-mira.cdn.css +6063 -0
  17. package/dist/basecoat-mira.cdn.min.css +2 -0
  18. package/dist/basecoat-mira.css +2 -0
  19. package/dist/basecoat-nova.cdn.css +6067 -0
  20. package/dist/basecoat-nova.cdn.min.css +2 -0
  21. package/dist/basecoat-nova.css +2 -0
  22. package/dist/basecoat-rhea.cdn.css +6092 -0
  23. package/dist/basecoat-rhea.cdn.min.css +2 -0
  24. package/dist/basecoat-rhea.css +2 -0
  25. package/dist/basecoat-sera.cdn.css +5840 -0
  26. package/dist/basecoat-sera.cdn.min.css +2 -0
  27. package/dist/basecoat-sera.css +2 -0
  28. package/dist/basecoat-vega.cdn.css +6023 -0
  29. package/dist/basecoat-vega.cdn.min.css +2 -0
  30. package/dist/basecoat-vega.css +2 -0
  31. package/dist/basecoat.all.css +1 -0
  32. package/dist/basecoat.cdn.css +5084 -2646
  33. package/dist/basecoat.cdn.min.css +1 -1
  34. package/dist/basecoat.css +1 -1311
  35. package/dist/components/alert.css +21 -0
  36. package/dist/components/badge.css +12 -0
  37. package/dist/components/button-group.css +54 -0
  38. package/dist/components/button.css +61 -0
  39. package/dist/components/card.css +22 -0
  40. package/dist/components/checkbox.css +12 -0
  41. package/dist/components/collapsible.css +21 -0
  42. package/dist/components/combobox.css +78 -0
  43. package/dist/components/command.css +133 -0
  44. package/dist/components/dialog.css +52 -0
  45. package/dist/components/dropdown-menu.css +48 -0
  46. package/dist/components/empty.css +26 -0
  47. package/dist/components/field.css +86 -0
  48. package/dist/components/form.css +9 -0
  49. package/dist/components/input-group.css +50 -0
  50. package/dist/components/input.css +32 -0
  51. package/dist/components/item.css +50 -0
  52. package/dist/components/kbd.css +10 -0
  53. package/dist/components/label.css +12 -0
  54. package/dist/components/native-select.css +12 -0
  55. package/dist/components/popover.css +81 -0
  56. package/dist/components/radio.css +11 -0
  57. package/dist/components/range.css +50 -0
  58. package/dist/components/select.css +64 -0
  59. package/dist/components/sidebar.css +101 -0
  60. package/dist/components/skeleton.css +6 -0
  61. package/dist/components/switch.css +8 -0
  62. package/dist/components/table.css +34 -0
  63. package/dist/components/tabs.css +31 -0
  64. package/dist/components/textarea.css +7 -0
  65. package/dist/components/toast.css +57 -0
  66. package/dist/components/tooltip.css +82 -0
  67. package/dist/js/all.js +1435 -755
  68. package/dist/js/all.min.js +1 -1
  69. package/dist/js/basecoat.js +95 -40
  70. package/dist/js/basecoat.min.js +1 -1
  71. package/dist/js/combobox.js +483 -0
  72. package/dist/js/combobox.min.js +1 -0
  73. package/dist/js/command.js +165 -126
  74. package/dist/js/command.min.js +1 -1
  75. package/dist/js/dropdown-menu.js +158 -133
  76. package/dist/js/dropdown-menu.min.js +1 -1
  77. package/dist/js/popover.js +22 -8
  78. package/dist/js/popover.min.js +1 -1
  79. package/dist/js/range.js +25 -0
  80. package/dist/js/range.min.js +1 -0
  81. package/dist/js/select.js +347 -359
  82. package/dist/js/select.min.js +1 -1
  83. package/dist/js/sidebar.js +27 -31
  84. package/dist/js/sidebar.min.js +1 -1
  85. package/dist/js/tabs.js +82 -38
  86. package/dist/js/tabs.min.js +1 -1
  87. package/dist/js/toast.js +42 -37
  88. package/dist/js/toast.min.js +1 -1
  89. package/dist/styles/luma.css +1308 -0
  90. package/dist/styles/lyra.css +1289 -0
  91. package/dist/styles/maia.css +1296 -0
  92. package/dist/styles/mira.css +1297 -0
  93. package/dist/styles/nova.css +1293 -0
  94. package/dist/styles/rhea.css +1305 -0
  95. package/dist/styles/sera.css +1316 -0
  96. package/dist/styles/vega.css +1293 -0
  97. package/package.json +47 -3
package/dist/js/select.js CHANGED
@@ -1,440 +1,428 @@
1
1
  (() => {
2
- const initSelect = (selectComponent) => {
3
- const trigger = selectComponent.querySelector(':scope > button');
4
- const selectedLabel = trigger.querySelector(':scope > span');
5
- const popover = selectComponent.querySelector(':scope > [data-popover]');
2
+ const states = new WeakMap();
3
+
4
+ const getElements = (root) => {
5
+ const trigger = root.querySelector(':scope > button');
6
+ const selectedLabel = trigger?.querySelector(':scope > span') || null;
7
+ const popover = root.querySelector(':scope > [data-popover]');
6
8
  const listbox = popover ? popover.querySelector('[role="listbox"]') : null;
7
- const input = selectComponent.querySelector(':scope > input[type="hidden"]');
8
- const filter = selectComponent.querySelector('header input[type="text"]');
9
+ const input = root.querySelector(':scope > input[type="hidden"]');
10
+ return { trigger, selectedLabel, popover, listbox, input };
11
+ };
9
12
 
10
- if (!trigger || !popover || !listbox || !input) {
11
- const missing = [];
12
- if (!trigger) missing.push('trigger');
13
- if (!popover) missing.push('popover');
14
- if (!listbox) missing.push('listbox');
15
- if (!input) missing.push('input');
16
- console.error(`Select component initialisation failed. Missing element(s): ${missing.join(', ')}`, selectComponent);
17
- return;
18
- }
13
+ const getValue = (option) => option.dataset.value ?? option.textContent.trim();
14
+ const getLabel = (option) => option.dataset.label || option.textContent.trim();
15
+ const getFormat = (root) => root.dataset.format === 'object' ? 'object' : 'value';
16
+ const isDisabled = (option) => option.getAttribute('aria-disabled') === 'true';
17
+ const toSelected = (option) => ({ value: getValue(option), label: getLabel(option) });
19
18
 
19
+ const getOptions = (listbox) => {
20
20
  const allOptions = Array.from(listbox.querySelectorAll('[role="option"]'));
21
- const options = allOptions.filter(opt => opt.getAttribute('aria-disabled') !== 'true');
22
- let visibleOptions = [...options];
23
- let activeIndex = -1;
24
- const isMultiple = listbox.getAttribute('aria-multiselectable') === 'true';
25
- const selectedOptions = isMultiple ? new Set() : null;
26
- const placeholder = isMultiple ? (selectComponent.dataset.placeholder || '') : null;
27
- const closeOnSelect = selectComponent.dataset.closeOnSelect === 'true';
28
-
29
- const getValue = (opt) => opt.dataset.value ?? opt.textContent.trim();
30
-
31
- const setActiveOption = (index) => {
32
- if (activeIndex > -1 && options[activeIndex]) {
33
- options[activeIndex].classList.remove('active');
34
- }
35
-
36
- activeIndex = index;
37
-
38
- if (activeIndex > -1) {
39
- const activeOption = options[activeIndex];
40
- activeOption.classList.add('active');
41
- if (activeOption.id) {
42
- trigger.setAttribute('aria-activedescendant', activeOption.id);
43
- } else {
44
- trigger.removeAttribute('aria-activedescendant');
45
- }
46
- } else {
47
- trigger.removeAttribute('aria-activedescendant');
48
- }
21
+ return {
22
+ allOptions,
23
+ options: allOptions.filter(option => !isDisabled(option)),
49
24
  };
25
+ };
50
26
 
51
- const hasTransition = () => {
52
- const style = getComputedStyle(popover);
53
- return parseFloat(style.transitionDuration) > 0 || parseFloat(style.transitionDelay) > 0;
54
- };
55
-
56
- const updateValue = (optionOrOptions, triggerEvent = true) => {
57
- let value;
58
-
59
- if (isMultiple) {
60
- const opts = Array.isArray(optionOrOptions) ? optionOrOptions : [];
61
- selectedOptions.clear();
62
- opts.forEach(opt => selectedOptions.add(opt));
63
-
64
- // Get selected options in DOM order
65
- const selected = options.filter(opt => selectedOptions.has(opt));
66
- if (selected.length === 0) {
67
- selectedLabel.textContent = placeholder;
68
- selectedLabel.classList.add('text-muted-foreground');
69
- } else {
70
- selectedLabel.textContent = selected.map(opt => opt.dataset.label || opt.textContent.trim()).join(', ');
71
- selectedLabel.classList.remove('text-muted-foreground');
72
- }
27
+ const parseStoredValues = (storedValue, { isMultiple, format }) => {
28
+ if (isMultiple) {
29
+ try {
30
+ const parsed = JSON.parse(storedValue || '[]');
31
+ if (!Array.isArray(parsed)) return [];
32
+ return parsed
33
+ .map(item => format === 'object' && item && typeof item === 'object' ? item.value : item)
34
+ .filter(value => value != null)
35
+ .map(String);
36
+ } catch (_) {
37
+ return [];
38
+ }
39
+ }
73
40
 
74
- value = selected.map(getValue);
75
- input.value = JSON.stringify(value);
76
- } else {
77
- const option = optionOrOptions;
78
- if (!option) return;
79
- selectedLabel.innerHTML = option.innerHTML;
80
- value = getValue(option);
81
- input.value = value;
41
+ if (format === 'object') {
42
+ try {
43
+ const parsed = JSON.parse(storedValue || 'null');
44
+ return parsed && typeof parsed === 'object' && parsed.value != null ? String(parsed.value) : '';
45
+ } catch (_) {
46
+ return '';
82
47
  }
48
+ }
83
49
 
84
- options.forEach(opt => {
85
- const isSelected = isMultiple ? selectedOptions.has(opt) : opt === optionOrOptions;
86
- if (isSelected) {
87
- opt.setAttribute('aria-selected', 'true');
88
- } else {
89
- opt.removeAttribute('aria-selected');
90
- }
91
- });
50
+ return storedValue || '';
51
+ };
92
52
 
93
- if (triggerEvent) {
94
- selectComponent.dispatchEvent(new CustomEvent('change', {
95
- detail: { value },
96
- bubbles: true
97
- }));
98
- }
99
- };
53
+ const serializeSelection = (state, selected) => {
54
+ if (state.format === 'object') {
55
+ return JSON.stringify(state.isMultiple ? selected : (selected[0] || null));
56
+ }
100
57
 
101
- const closePopover = (focusOnTrigger = true) => {
102
- if (popover.getAttribute('aria-hidden') === 'true') return;
58
+ const value = selected.map(item => item.value);
59
+ return state.isMultiple ? JSON.stringify(value) : (value[0] || '');
60
+ };
103
61
 
104
- if (filter) {
105
- const resetFilter = () => {
106
- filter.value = '';
107
- visibleOptions = [...options];
108
- allOptions.forEach(opt => opt.setAttribute('aria-hidden', 'false'));
109
- };
62
+ const scrollOptionIntoListbox = (state, option) => {
63
+ const optionRect = option.getBoundingClientRect();
64
+ const listboxRect = state.listbox.getBoundingClientRect();
110
65
 
111
- if (hasTransition()) {
112
- popover.addEventListener('transitionend', resetFilter, { once: true });
113
- } else {
114
- resetFilter();
115
- }
116
- }
66
+ if (optionRect.top < listboxRect.top) {
67
+ state.listbox.scrollTop -= listboxRect.top - optionRect.top;
68
+ } else if (optionRect.bottom > listboxRect.bottom) {
69
+ state.listbox.scrollTop += optionRect.bottom - listboxRect.bottom;
70
+ }
71
+ };
117
72
 
118
- if (focusOnTrigger) trigger.focus();
119
- popover.setAttribute('aria-hidden', 'true');
120
- trigger.setAttribute('aria-expanded', 'false');
121
- setActiveOption(-1);
122
- };
73
+ const setActiveOption = (state, index) => {
74
+ if (state.activeIndex > -1 && state.options[state.activeIndex]) {
75
+ state.options[state.activeIndex].classList.remove('active');
76
+ }
123
77
 
124
- const toggleMultipleValue = (option) => {
125
- if (selectedOptions.has(option)) {
126
- selectedOptions.delete(option);
78
+ state.activeIndex = index;
79
+
80
+ if (state.activeIndex > -1) {
81
+ const activeOption = state.options[state.activeIndex];
82
+ activeOption.classList.add('active');
83
+ if (activeOption.id) {
84
+ state.trigger.setAttribute('aria-activedescendant', activeOption.id);
127
85
  } else {
128
- selectedOptions.add(option);
86
+ state.trigger.removeAttribute('aria-activedescendant');
129
87
  }
130
- updateValue(options.filter(opt => selectedOptions.has(opt)));
131
- };
88
+ } else {
89
+ state.trigger.removeAttribute('aria-activedescendant');
90
+ }
91
+ };
132
92
 
133
- const select = (value) => {
134
- if (isMultiple) {
135
- const option = options.find(opt => getValue(opt) === value && !selectedOptions.has(opt));
136
- if (!option) return;
137
- selectedOptions.add(option);
138
- updateValue(options.filter(opt => selectedOptions.has(opt)));
93
+ const updateValue = (root, optionOrOptions, triggerEvent = true) => {
94
+ const state = states.get(root);
95
+ let value;
96
+ let selectedDetail;
97
+
98
+ if (state.isMultiple) {
99
+ const selected = Array.isArray(optionOrOptions) ? optionOrOptions : [];
100
+ state.selectedOptions.clear();
101
+ selected.forEach(option => state.selectedOptions.add(option));
102
+
103
+ const selectedInOrder = state.options.filter(option => state.selectedOptions.has(option));
104
+ selectedDetail = selectedInOrder.map(toSelected);
105
+ if (selectedInOrder.length === 0) {
106
+ state.selectedLabel.textContent = state.placeholder;
107
+ state.selectedLabel.classList.add('text-muted-foreground');
139
108
  } else {
140
- const option = options.find(opt => getValue(opt) === value);
141
- if (!option) return;
142
- if (input.value !== value) {
143
- updateValue(option);
144
- }
145
- closePopover();
109
+ state.selectedLabel.textContent = selectedDetail.map(item => item.label).join(', ');
110
+ state.selectedLabel.classList.remove('text-muted-foreground');
146
111
  }
147
- };
148
112
 
149
- const deselect = (value) => {
150
- if (!isMultiple) return;
151
- const option = options.find(opt => getValue(opt) === value && selectedOptions.has(opt));
113
+ value = selectedDetail.map(item => item.value);
114
+ state.input.value = serializeSelection(state, selectedDetail);
115
+ } else {
116
+ const option = optionOrOptions;
152
117
  if (!option) return;
153
- selectedOptions.delete(option);
154
- updateValue(options.filter(opt => selectedOptions.has(opt)));
155
- };
118
+ state.selectedLabel.innerHTML = option.innerHTML;
119
+ selectedDetail = toSelected(option);
120
+ value = selectedDetail.value;
121
+ state.input.value = serializeSelection(state, [selectedDetail]);
122
+ }
156
123
 
157
- const toggle = (value) => {
158
- if (!isMultiple) return;
159
- const option = options.find(opt => getValue(opt) === value);
160
- if (!option) return;
161
- toggleMultipleValue(option);
162
- };
124
+ state.options.forEach(option => {
125
+ const isSelected = state.isMultiple ? state.selectedOptions.has(option) : option === optionOrOptions;
126
+ if (isSelected) {
127
+ option.setAttribute('aria-selected', 'true');
128
+ } else {
129
+ option.removeAttribute('aria-selected');
130
+ }
131
+ });
163
132
 
164
- if (filter) {
165
- const filterOptions = () => {
166
- const searchTerm = filter.value.trim().toLowerCase();
133
+ if (triggerEvent) {
134
+ root.dispatchEvent(new CustomEvent('change', {
135
+ detail: { value, selected: selectedDetail },
136
+ bubbles: true,
137
+ }));
138
+ }
139
+ };
167
140
 
168
- setActiveOption(-1);
141
+ const closePopover = (state, focusOnTrigger = true) => {
142
+ if (state.popover.getAttribute('aria-hidden') === 'true') return;
143
+ if (focusOnTrigger) state.trigger.focus();
144
+ state.popover.setAttribute('aria-hidden', 'true');
145
+ state.trigger.setAttribute('aria-expanded', 'false');
146
+ setActiveOption(state, -1);
147
+ };
169
148
 
170
- visibleOptions = [];
171
- allOptions.forEach(option => {
172
- if (option.hasAttribute('data-force')) {
173
- option.setAttribute('aria-hidden', 'false');
174
- if (options.includes(option)) {
175
- visibleOptions.push(option);
176
- }
177
- return;
178
- }
149
+ const refreshSelect = (root) => {
150
+ const state = states.get(root);
151
+ if (!state) return;
179
152
 
180
- const optionText = (option.dataset.filter || option.textContent).trim().toLowerCase();
181
- const keywordList = (option.dataset.keywords || '')
182
- .toLowerCase()
183
- .split(/[\s,]+/)
184
- .filter(Boolean);
185
- const matchesKeyword = keywordList.some(keyword => keyword.includes(searchTerm));
186
- const matches = optionText.includes(searchTerm) || matchesKeyword;
187
- option.setAttribute('aria-hidden', String(!matches));
188
- if (matches && options.includes(option)) {
189
- visibleOptions.push(option);
190
- }
191
- });
192
- };
153
+ const elements = getElements(root);
154
+ if (!elements.trigger || !elements.selectedLabel || !elements.popover || !elements.listbox || !elements.input) {
155
+ const missing = [];
156
+ if (!elements.trigger) missing.push('trigger');
157
+ if (!elements.selectedLabel) missing.push('selected label');
158
+ if (!elements.popover) missing.push('popover');
159
+ if (!elements.listbox) missing.push('listbox');
160
+ if (!elements.input) missing.push('input');
161
+ console.error(`Select component refresh failed. Missing element(s): ${missing.join(', ')}`, root);
162
+ return;
163
+ }
193
164
 
194
- filter.addEventListener('input', filterOptions);
165
+ const previousValue = elements.input.value;
166
+ Object.assign(state, elements, getOptions(elements.listbox));
167
+ state.visibleOptions = [...state.options];
168
+ state.isMultiple = state.listbox.getAttribute('aria-multiselectable') === 'true';
169
+ state.format = getFormat(root);
170
+ state.placeholder = state.isMultiple ? (root.dataset.placeholder || '') : null;
171
+ state.closeOnSelect = root.dataset.closeOnSelect === 'true';
172
+
173
+ if (state.isMultiple) {
174
+ if (!state.selectedOptions) state.selectedOptions = new Set();
175
+ const values = parseStoredValues(previousValue, state);
176
+ const selected = values.length
177
+ ? values.map(value => state.options.find(option => getValue(option) === value)).filter(Boolean)
178
+ : state.options.filter(option => option.getAttribute('aria-selected') === 'true');
179
+ updateValue(root, selected, false);
180
+ } else {
181
+ const value = parseStoredValues(previousValue, state);
182
+ const selected = state.options.find(option => getValue(option) === value)
183
+ || state.options.find(option => option.getAttribute('aria-selected') === 'true');
184
+ state.options.forEach(option => option.removeAttribute('aria-selected'));
185
+ if (selected) updateValue(root, selected, false);
195
186
  }
196
187
 
197
- // Initialization
198
- if (isMultiple) {
199
- const ariaSelected = options.filter(opt => opt.getAttribute('aria-selected') === 'true');
200
- try {
201
- const parsed = JSON.parse(input.value || '[]');
202
- const validValues = new Set(options.map(getValue));
203
- const initialValues = Array.isArray(parsed) ? parsed.filter(v => validValues.has(v)) : [];
204
-
205
- const initialOptions = [];
206
- if (initialValues.length > 0) {
207
- // Match values to options in order, allowing duplicates
208
- initialValues.forEach(val => {
209
- const opt = options.find(o => getValue(o) === val && !initialOptions.includes(o));
210
- if (opt) initialOptions.push(opt);
211
- });
212
- } else {
213
- initialOptions.push(...ariaSelected);
214
- }
188
+ const selectedOption = state.listbox.querySelector('[role="option"][aria-selected="true"]');
189
+ setActiveOption(state, selectedOption ? state.options.indexOf(selectedOption) : -1);
190
+ };
215
191
 
216
- updateValue(initialOptions, false);
217
- } catch (e) {
218
- updateValue(ariaSelected, false);
219
- }
192
+ const toggleMultipleValue = (root, option) => {
193
+ const state = states.get(root);
194
+ if (state.selectedOptions.has(option)) {
195
+ state.selectedOptions.delete(option);
220
196
  } else {
221
- const initialOption = options.find(opt => getValue(opt) === input.value) || options[0];
222
- if (initialOption) updateValue(initialOption, false);
197
+ state.selectedOptions.add(option);
223
198
  }
199
+ updateValue(root, state.options.filter(opt => state.selectedOptions.has(opt)));
200
+ };
224
201
 
225
- const handleKeyNavigation = (event) => {
226
- const isPopoverOpen = popover.getAttribute('aria-hidden') === 'false';
227
-
228
- if (!['ArrowDown', 'ArrowUp', 'Enter', 'Home', 'End', 'Escape'].includes(event.key)) {
229
- return;
230
- }
202
+ const selectValue = (root, value) => {
203
+ const state = states.get(root);
204
+ if (state.isMultiple) {
205
+ const option = state.options.find(opt => getValue(opt) === value && !state.selectedOptions.has(opt));
206
+ if (!option) return;
207
+ state.selectedOptions.add(option);
208
+ updateValue(root, state.options.filter(opt => state.selectedOptions.has(opt)));
209
+ } else {
210
+ const option = state.options.find(opt => getValue(opt) === value);
211
+ if (!option) return;
212
+ if (root.value !== value) updateValue(root, option);
213
+ closePopover(state);
214
+ }
215
+ };
231
216
 
232
- if (!isPopoverOpen) {
233
- if (event.key !== 'Enter' && event.key !== 'Escape') {
234
- event.preventDefault();
235
- trigger.click();
236
- }
237
- return;
238
- }
217
+ const deselectValue = (root, value) => {
218
+ const state = states.get(root);
219
+ if (!state.isMultiple) return;
220
+ const option = state.options.find(opt => getValue(opt) === value && state.selectedOptions.has(opt));
221
+ if (!option) return;
222
+ state.selectedOptions.delete(option);
223
+ updateValue(root, state.options.filter(opt => state.selectedOptions.has(opt)));
224
+ };
239
225
 
240
- event.preventDefault();
226
+ const handleKeyNavigation = (event, root) => {
227
+ const state = states.get(root);
228
+ const isPopoverOpen = state.popover.getAttribute('aria-hidden') === 'false';
241
229
 
242
- if (event.key === 'Escape') {
243
- closePopover();
244
- return;
245
- }
230
+ if (!['ArrowDown', 'ArrowUp', 'Enter', 'Home', 'End', 'Escape'].includes(event.key)) return;
246
231
 
247
- if (event.key === 'Enter') {
248
- if (activeIndex > -1) {
249
- const option = options[activeIndex];
250
- if (isMultiple) {
251
- toggleMultipleValue(option);
252
- if (closeOnSelect) {
253
- closePopover();
254
- }
255
- } else {
256
- if (input.value !== getValue(option)) {
257
- updateValue(option);
258
- }
259
- closePopover();
260
- }
261
- }
262
- return;
232
+ if (!isPopoverOpen) {
233
+ if (event.key !== 'Enter' && event.key !== 'Escape') {
234
+ event.preventDefault();
235
+ root.open();
263
236
  }
237
+ return;
238
+ }
264
239
 
265
- if (visibleOptions.length === 0) return;
240
+ event.preventDefault();
266
241
 
267
- const currentVisibleIndex = activeIndex > -1 ? visibleOptions.indexOf(options[activeIndex]) : -1;
268
- let nextVisibleIndex = currentVisibleIndex;
242
+ if (event.key === 'Escape') {
243
+ root.close();
244
+ return;
245
+ }
269
246
 
270
- switch (event.key) {
271
- case 'ArrowDown':
272
- if (currentVisibleIndex < visibleOptions.length - 1) {
273
- nextVisibleIndex = currentVisibleIndex + 1;
274
- }
275
- break;
276
- case 'ArrowUp':
277
- if (currentVisibleIndex > 0) {
278
- nextVisibleIndex = currentVisibleIndex - 1;
279
- } else if (currentVisibleIndex === -1) {
280
- nextVisibleIndex = 0;
281
- }
282
- break;
283
- case 'Home':
284
- nextVisibleIndex = 0;
285
- break;
286
- case 'End':
287
- nextVisibleIndex = visibleOptions.length - 1;
288
- break;
247
+ if (event.key === 'Enter') {
248
+ if (state.activeIndex > -1) {
249
+ const option = state.options[state.activeIndex];
250
+ if (state.isMultiple) {
251
+ toggleMultipleValue(root, option);
252
+ if (state.closeOnSelect) root.close();
253
+ } else {
254
+ if (root.value !== getValue(option)) updateValue(root, option);
255
+ root.close();
256
+ }
289
257
  }
258
+ return;
259
+ }
290
260
 
291
- if (nextVisibleIndex !== currentVisibleIndex) {
292
- const newActiveOption = visibleOptions[nextVisibleIndex];
293
- setActiveOption(options.indexOf(newActiveOption));
294
- newActiveOption.scrollIntoView({ block: 'nearest', behavior: 'smooth' });
295
- }
296
- };
261
+ if (state.visibleOptions.length === 0) return;
297
262
 
298
- listbox.addEventListener('mousemove', (event) => {
299
- const option = event.target.closest('[role="option"]');
300
- if (option && visibleOptions.includes(option)) {
301
- const index = options.indexOf(option);
302
- if (index !== activeIndex) {
303
- setActiveOption(index);
304
- }
305
- }
306
- });
263
+ const currentVisibleIndex = state.activeIndex > -1 ? state.visibleOptions.indexOf(state.options[state.activeIndex]) : -1;
264
+ let nextVisibleIndex = currentVisibleIndex;
307
265
 
308
- listbox.addEventListener('mouseleave', () => {
309
- const selectedOption = listbox.querySelector('[role="option"][aria-selected="true"]');
310
- if (selectedOption) {
311
- setActiveOption(options.indexOf(selectedOption));
312
- } else {
313
- setActiveOption(-1);
314
- }
315
- });
266
+ if (event.key === 'ArrowDown' && currentVisibleIndex < state.visibleOptions.length - 1) nextVisibleIndex = currentVisibleIndex + 1;
267
+ if (event.key === 'ArrowUp') nextVisibleIndex = currentVisibleIndex > 0 ? currentVisibleIndex - 1 : 0;
268
+ if (event.key === 'Home') nextVisibleIndex = 0;
269
+ if (event.key === 'End') nextVisibleIndex = state.visibleOptions.length - 1;
316
270
 
317
- trigger.addEventListener('keydown', handleKeyNavigation);
318
- if (filter) {
319
- filter.addEventListener('keydown', handleKeyNavigation);
271
+ if (nextVisibleIndex !== currentVisibleIndex) {
272
+ const newActiveOption = state.visibleOptions[nextVisibleIndex];
273
+ setActiveOption(state, state.options.indexOf(newActiveOption));
274
+ scrollOptionIntoListbox(state, newActiveOption);
320
275
  }
276
+ };
321
277
 
322
- const openPopover = () => {
323
- document.dispatchEvent(new CustomEvent('basecoat:popover', {
324
- detail: { source: selectComponent }
325
- }));
278
+ const initSelect = (root) => {
279
+ if (root.dataset.selectInitialized) return;
326
280
 
327
- if (filter) {
328
- if (hasTransition()) {
329
- popover.addEventListener('transitionend', () => {
330
- filter.focus();
331
- }, { once: true });
332
- } else {
333
- filter.focus();
334
- }
335
- }
281
+ const state = { activeIndex: -1, selectedOptions: null, options: [], allOptions: [], visibleOptions: [], format: 'value' };
282
+ states.set(root, state);
283
+ root.refresh = () => refreshSelect(root);
336
284
 
337
- popover.setAttribute('aria-hidden', 'false');
338
- trigger.setAttribute('aria-expanded', 'true');
285
+ refreshSelect(root);
286
+ if (!state.trigger || !state.selectedLabel || !state.popover || !state.listbox || !state.input) {
287
+ states.delete(root);
288
+ delete root.refresh;
289
+ return;
290
+ }
291
+
292
+ root.open = () => {
293
+ document.dispatchEvent(new CustomEvent('basecoat:popover', { detail: { source: root } }));
294
+ root.refresh();
295
+ state.popover.setAttribute('aria-hidden', 'false');
296
+ state.trigger.setAttribute('aria-expanded', 'true');
339
297
 
340
- const selectedOption = listbox.querySelector('[role="option"][aria-selected="true"]');
298
+ const selectedOption = state.listbox.querySelector('[role="option"][aria-selected="true"]');
341
299
  if (selectedOption) {
342
- setActiveOption(options.indexOf(selectedOption));
343
- selectedOption.scrollIntoView({ block: 'nearest' });
300
+ setActiveOption(state, state.options.indexOf(selectedOption));
301
+ scrollOptionIntoListbox(state, selectedOption);
344
302
  }
345
303
  };
304
+ root.close = (focusOnTrigger = true) => closePopover(state, focusOnTrigger);
305
+ root.togglePopover = () => state.trigger.getAttribute('aria-expanded') === 'true' ? root.close() : root.open();
346
306
 
347
- trigger.addEventListener('click', () => {
348
- const isExpanded = trigger.getAttribute('aria-expanded') === 'true';
349
- if (isExpanded) {
350
- closePopover();
351
- } else {
352
- openPopover();
307
+ const handleTriggerKeydown = (event) => handleKeyNavigation(event, root);
308
+ const handleTriggerClick = root.togglePopover;
309
+ const handleListboxMousemove = (event) => {
310
+ const option = event.target.closest('[role="option"]');
311
+ if (option && state.visibleOptions.includes(option)) {
312
+ const index = state.options.indexOf(option);
313
+ if (index !== state.activeIndex) setActiveOption(state, index);
353
314
  }
354
- });
355
-
356
- listbox.addEventListener('click', (event) => {
315
+ };
316
+ const handleListboxMouseleave = () => {
317
+ const selectedOption = state.listbox.querySelector('[role="option"][aria-selected="true"]');
318
+ setActiveOption(state, selectedOption ? state.options.indexOf(selectedOption) : -1);
319
+ };
320
+ const handleListboxClick = (event) => {
357
321
  const clickedOption = event.target.closest('[role="option"]');
358
322
  if (!clickedOption) return;
359
323
 
360
- const option = options.find(opt => opt === clickedOption);
324
+ const option = state.options.find(opt => opt === clickedOption);
361
325
  if (!option) return;
362
326
 
363
- if (isMultiple) {
364
- toggleMultipleValue(option);
365
- if (closeOnSelect) {
366
- closePopover();
327
+ if (state.isMultiple) {
328
+ toggleMultipleValue(root, option);
329
+ if (state.closeOnSelect) {
330
+ root.close();
367
331
  } else {
368
- setActiveOption(options.indexOf(option));
369
- if (filter) {
370
- filter.focus();
371
- } else {
372
- trigger.focus();
373
- }
332
+ setActiveOption(state, state.options.indexOf(option));
333
+ state.trigger.focus();
374
334
  }
375
335
  } else {
376
- if (input.value !== getValue(option)) {
377
- updateValue(option);
378
- }
379
- closePopover();
380
- }
381
- });
382
-
383
- document.addEventListener('click', (event) => {
384
- if (!selectComponent.contains(event.target)) {
385
- closePopover(false);
336
+ if (root.value !== getValue(option)) updateValue(root, option);
337
+ root.close();
386
338
  }
387
- });
388
-
389
- document.addEventListener('basecoat:popover', (event) => {
390
- if (event.detail.source !== selectComponent) {
391
- closePopover(false);
392
- }
393
- });
339
+ };
340
+ const handleDocumentClick = (event) => {
341
+ if (!root.contains(event.target)) root.close(false);
342
+ };
343
+ const handleDocumentPopover = (event) => {
344
+ if (event.detail.source !== root) root.close(false);
345
+ };
394
346
 
395
- popover.setAttribute('aria-hidden', 'true');
347
+ state.trigger.addEventListener('keydown', handleTriggerKeydown);
348
+ state.trigger.addEventListener('click', handleTriggerClick);
349
+ state.listbox.addEventListener('mousemove', handleListboxMousemove);
350
+ state.listbox.addEventListener('mouseleave', handleListboxMouseleave);
351
+ state.listbox.addEventListener('click', handleListboxClick);
352
+ document.addEventListener('click', handleDocumentClick);
353
+ document.addEventListener('basecoat:popover', handleDocumentPopover);
354
+
355
+ root._destroy = () => {
356
+ state.trigger.removeEventListener('keydown', handleTriggerKeydown);
357
+ state.trigger.removeEventListener('click', handleTriggerClick);
358
+ state.listbox.removeEventListener('mousemove', handleListboxMousemove);
359
+ state.listbox.removeEventListener('mouseleave', handleListboxMouseleave);
360
+ state.listbox.removeEventListener('click', handleListboxClick);
361
+ document.removeEventListener('click', handleDocumentClick);
362
+ document.removeEventListener('basecoat:popover', handleDocumentPopover);
363
+ states.delete(root);
364
+ delete root.refresh;
365
+ delete root.open;
366
+ delete root.close;
367
+ delete root.togglePopover;
368
+ delete root.select;
369
+ delete root.selectByValue;
370
+ delete root.deselect;
371
+ delete root.toggle;
372
+ delete root.selectAll;
373
+ delete root.selectNone;
374
+ };
396
375
 
397
- // Public API
398
- Object.defineProperty(selectComponent, 'value', {
399
- get: () => {
400
- if (isMultiple) {
401
- return options.filter(opt => selectedOptions.has(opt)).map(getValue);
376
+ Object.defineProperty(root, 'value', {
377
+ configurable: true,
378
+ get: () => state.isMultiple ? state.options.filter(option => state.selectedOptions.has(option)).map(getValue) : parseStoredValues(state.input.value, state),
379
+ set: (value) => {
380
+ if (state.isMultiple) {
381
+ const values = Array.isArray(value) ? value : (value != null ? [value] : []);
382
+ updateValue(root, values.map(v => state.options.find(option => getValue(option) === v)).filter(Boolean));
402
383
  } else {
403
- return input.value;
404
- }
405
- },
406
- set: (val) => {
407
- if (isMultiple) {
408
- const values = Array.isArray(val) ? val : (val != null ? [val] : []);
409
- const opts = [];
410
- values.forEach(v => {
411
- const opt = options.find(o => getValue(o) === v && !opts.includes(o));
412
- if (opt) opts.push(opt);
413
- });
414
- updateValue(opts);
415
- } else {
416
- const option = options.find(opt => getValue(opt) === val);
384
+ const option = state.options.find(opt => getValue(opt) === value);
417
385
  if (option) {
418
- updateValue(option);
419
- closePopover();
386
+ updateValue(root, option);
387
+ root.close();
420
388
  }
421
389
  }
422
- }
390
+ },
423
391
  });
424
392
 
425
- selectComponent.select = select;
426
- selectComponent.selectByValue = select; // Backward compatibility alias
427
- if (isMultiple) {
428
- selectComponent.deselect = deselect;
429
- selectComponent.toggle = toggle;
430
- selectComponent.selectAll = () => updateValue(options);
431
- selectComponent.selectNone = () => updateValue([]);
393
+ Object.defineProperty(root, 'selected', {
394
+ configurable: true,
395
+ get: () => {
396
+ if (state.isMultiple) return state.options.filter(option => state.selectedOptions.has(option)).map(toSelected);
397
+ const value = root.value;
398
+ const option = state.options.find(opt => getValue(opt) === value);
399
+ return option ? toSelected(option) : null;
400
+ },
401
+ });
402
+
403
+ root.select = (value) => selectValue(root, value);
404
+ root.selectByValue = root.select;
405
+ if (state.isMultiple) {
406
+ root.deselect = (value) => deselectValue(root, value);
407
+ root.toggle = (value) => {
408
+ const option = state.options.find(opt => getValue(opt) === value);
409
+ if (option) toggleMultipleValue(root, option);
410
+ };
411
+ root.selectAll = () => updateValue(root, state.options);
412
+ root.selectNone = () => updateValue(root, []);
432
413
  }
433
- selectComponent.dataset.selectInitialized = true;
434
- selectComponent.dispatchEvent(new CustomEvent('basecoat:initialized'));
414
+
415
+ state.popover.setAttribute('aria-hidden', 'true');
416
+ state.trigger.setAttribute('aria-expanded', 'false');
417
+ root.dataset.selectInitialized = 'true';
418
+ root.dispatchEvent(new CustomEvent('basecoat:initialized'));
435
419
  };
436
420
 
437
421
  if (window.basecoat) {
438
- window.basecoat.register('select', 'div.select:not([data-select-initialized])', initSelect);
422
+ window.basecoat.register('select', {
423
+ selector: 'div.select:not([data-select-initialized])',
424
+ init: initSelect,
425
+ refresh: refreshSelect,
426
+ });
439
427
  }
440
428
  })();