basecoat-css 0.3.10 → 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.
- package/README.md +8 -1
- package/dist/base/base.css +150 -0
- package/dist/basecoat-base.cdn.css +2942 -0
- package/dist/basecoat-base.cdn.min.css +2 -0
- package/dist/basecoat-base.css +2 -0
- package/dist/basecoat-components.css +32 -0
- package/dist/basecoat-luma.cdn.css +6073 -0
- package/dist/basecoat-luma.cdn.min.css +2 -0
- package/dist/basecoat-luma.css +2 -0
- package/dist/basecoat-lyra.cdn.css +6004 -0
- package/dist/basecoat-lyra.cdn.min.css +2 -0
- package/dist/basecoat-lyra.css +2 -0
- package/dist/basecoat-maia.cdn.css +6012 -0
- package/dist/basecoat-maia.cdn.min.css +2 -0
- package/dist/basecoat-maia.css +2 -0
- package/dist/basecoat-mira.cdn.css +6063 -0
- package/dist/basecoat-mira.cdn.min.css +2 -0
- package/dist/basecoat-mira.css +2 -0
- package/dist/basecoat-nova.cdn.css +6067 -0
- package/dist/basecoat-nova.cdn.min.css +2 -0
- package/dist/basecoat-nova.css +2 -0
- package/dist/basecoat-rhea.cdn.css +6092 -0
- package/dist/basecoat-rhea.cdn.min.css +2 -0
- package/dist/basecoat-rhea.css +2 -0
- package/dist/basecoat-sera.cdn.css +5840 -0
- package/dist/basecoat-sera.cdn.min.css +2 -0
- package/dist/basecoat-sera.css +2 -0
- package/dist/basecoat-vega.cdn.css +6023 -0
- package/dist/basecoat-vega.cdn.min.css +2 -0
- package/dist/basecoat-vega.css +2 -0
- package/dist/basecoat.all.css +1 -0
- package/dist/basecoat.cdn.css +5084 -2646
- package/dist/basecoat.cdn.min.css +1 -1
- package/dist/basecoat.css +1 -1311
- package/dist/components/alert.css +21 -0
- package/dist/components/badge.css +12 -0
- package/dist/components/button-group.css +54 -0
- package/dist/components/button.css +61 -0
- package/dist/components/card.css +22 -0
- package/dist/components/checkbox.css +12 -0
- package/dist/components/collapsible.css +21 -0
- package/dist/components/combobox.css +78 -0
- package/dist/components/command.css +133 -0
- package/dist/components/dialog.css +52 -0
- package/dist/components/dropdown-menu.css +48 -0
- package/dist/components/empty.css +26 -0
- package/dist/components/field.css +86 -0
- package/dist/components/form.css +9 -0
- package/dist/components/input-group.css +50 -0
- package/dist/components/input.css +32 -0
- package/dist/components/item.css +50 -0
- package/dist/components/kbd.css +10 -0
- package/dist/components/label.css +12 -0
- package/dist/components/native-select.css +12 -0
- package/dist/components/popover.css +81 -0
- package/dist/components/radio.css +11 -0
- package/dist/components/range.css +50 -0
- package/dist/components/select.css +64 -0
- package/dist/components/sidebar.css +101 -0
- package/dist/components/skeleton.css +6 -0
- package/dist/components/switch.css +8 -0
- package/dist/components/table.css +34 -0
- package/dist/components/tabs.css +31 -0
- package/dist/components/textarea.css +7 -0
- package/dist/components/toast.css +57 -0
- package/dist/components/tooltip.css +82 -0
- package/dist/js/all.js +1435 -789
- package/dist/js/all.min.js +1 -1
- package/dist/js/basecoat.js +95 -40
- package/dist/js/basecoat.min.js +1 -1
- package/dist/js/combobox.js +483 -0
- package/dist/js/combobox.min.js +1 -0
- package/dist/js/command.js +165 -126
- package/dist/js/command.min.js +1 -1
- package/dist/js/dropdown-menu.js +158 -133
- package/dist/js/dropdown-menu.min.js +1 -1
- package/dist/js/popover.js +22 -8
- package/dist/js/popover.min.js +1 -1
- package/dist/js/range.js +25 -0
- package/dist/js/range.min.js +1 -0
- package/dist/js/select.js +347 -359
- package/dist/js/select.min.js +1 -1
- package/dist/js/sidebar.js +27 -66
- package/dist/js/sidebar.min.js +1 -1
- package/dist/js/tabs.js +82 -38
- package/dist/js/tabs.min.js +1 -1
- package/dist/js/toast.js +42 -37
- package/dist/js/toast.min.js +1 -1
- package/dist/styles/luma.css +1308 -0
- package/dist/styles/lyra.css +1289 -0
- package/dist/styles/maia.css +1296 -0
- package/dist/styles/mira.css +1297 -0
- package/dist/styles/nova.css +1293 -0
- package/dist/styles/rhea.css +1305 -0
- package/dist/styles/sera.css +1316 -0
- package/dist/styles/vega.css +1293 -0
- package/package.json +47 -3
- package/dist/js/carousel.js +0 -192
- package/dist/js/carousel.min.js +0 -1
|
@@ -0,0 +1,483 @@
|
|
|
1
|
+
(() => {
|
|
2
|
+
const states = new WeakMap();
|
|
3
|
+
|
|
4
|
+
const getElements = (root) => {
|
|
5
|
+
const input = root.querySelector(':scope > input[role="combobox"], :scope > .combobox-chips input[role="combobox"]');
|
|
6
|
+
const chips = root.querySelector(':scope > .combobox-chips');
|
|
7
|
+
const popover = root.querySelector(':scope > [data-popover]');
|
|
8
|
+
const listbox = popover ? popover.querySelector('[role="listbox"]') : null;
|
|
9
|
+
const hiddenInput = root.querySelector(':scope > input[type="hidden"]');
|
|
10
|
+
return { input, chips, popover, listbox, hiddenInput };
|
|
11
|
+
};
|
|
12
|
+
|
|
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) });
|
|
18
|
+
const normalizeEntry = entry => {
|
|
19
|
+
if (entry && typeof entry === 'object') {
|
|
20
|
+
const value = entry.value == null ? '' : String(entry.value);
|
|
21
|
+
return value ? { value, label: String(entry.label ?? entry.value) } : null;
|
|
22
|
+
}
|
|
23
|
+
const value = entry == null ? '' : String(entry);
|
|
24
|
+
return value ? { value, label: value } : null;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
const getOptions = (listbox) => {
|
|
28
|
+
const allOptions = Array.from(listbox.querySelectorAll('[role="option"]'));
|
|
29
|
+
return {
|
|
30
|
+
allOptions,
|
|
31
|
+
options: allOptions.filter(option => !isDisabled(option)),
|
|
32
|
+
};
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
const getSelection = (state) => Array.from(state.selected.values());
|
|
36
|
+
const getCanonicalValue = (state) => state.isMultiple ? getSelection(state).map(item => item.value) : (getSelection(state)[0]?.value || '');
|
|
37
|
+
const getSelectedDetail = (state) => state.isMultiple ? getSelection(state) : (getSelection(state)[0] || null);
|
|
38
|
+
|
|
39
|
+
const serializeSelection = (state) => {
|
|
40
|
+
const selected = getSelection(state);
|
|
41
|
+
if (state.format === 'object') {
|
|
42
|
+
return JSON.stringify(state.isMultiple ? selected : (selected[0] || null));
|
|
43
|
+
}
|
|
44
|
+
const value = selected.map(item => item.value);
|
|
45
|
+
return state.isMultiple ? JSON.stringify(value) : (value[0] || '');
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
const parseStoredSelection = (storedValue, inputValue, state) => {
|
|
49
|
+
if (state.isMultiple) {
|
|
50
|
+
let parsed = [];
|
|
51
|
+
try {
|
|
52
|
+
parsed = JSON.parse(storedValue || '[]');
|
|
53
|
+
} catch (_) {
|
|
54
|
+
parsed = [];
|
|
55
|
+
}
|
|
56
|
+
if (!Array.isArray(parsed)) return [];
|
|
57
|
+
return parsed.map(item => {
|
|
58
|
+
const entry = normalizeEntry(state.format === 'object' ? item : { value: item, label: state.selected.get(String(item))?.label ?? item });
|
|
59
|
+
if (!entry) return null;
|
|
60
|
+
const option = state.options.find(opt => getValue(opt) === entry.value);
|
|
61
|
+
return option ? toSelected(option) : entry;
|
|
62
|
+
}).filter(Boolean);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (state.format === 'object') {
|
|
66
|
+
try {
|
|
67
|
+
const entry = normalizeEntry(JSON.parse(storedValue || 'null'));
|
|
68
|
+
if (!entry) return [];
|
|
69
|
+
const option = state.options.find(opt => getValue(opt) === entry.value);
|
|
70
|
+
return [option ? toSelected(option) : entry];
|
|
71
|
+
} catch (_) {
|
|
72
|
+
return [];
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const value = storedValue || '';
|
|
77
|
+
if (!value) return [];
|
|
78
|
+
const option = state.options.find(opt => getValue(opt) === value);
|
|
79
|
+
return [option ? toSelected(option) : { value, label: state.selected.get(value)?.label || inputValue || value }];
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
const scrollOptionIntoListbox = (state, option) => {
|
|
83
|
+
const optionRect = option.getBoundingClientRect();
|
|
84
|
+
const listboxRect = state.listbox.getBoundingClientRect();
|
|
85
|
+
|
|
86
|
+
if (optionRect.top < listboxRect.top) {
|
|
87
|
+
state.listbox.scrollTop -= listboxRect.top - optionRect.top;
|
|
88
|
+
} else if (optionRect.bottom > listboxRect.bottom) {
|
|
89
|
+
state.listbox.scrollTop += optionRect.bottom - listboxRect.bottom;
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
const setActiveOption = (state, index) => {
|
|
94
|
+
if (state.activeIndex > -1 && state.options[state.activeIndex]) {
|
|
95
|
+
state.options[state.activeIndex].classList.remove('active');
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
state.activeIndex = index;
|
|
99
|
+
|
|
100
|
+
if (state.activeIndex > -1) {
|
|
101
|
+
const activeOption = state.options[state.activeIndex];
|
|
102
|
+
activeOption.classList.add('active');
|
|
103
|
+
if (!activeOption.id) activeOption.id = `${state.listbox.id || state.root.id || 'combobox'}-option-${state.activeIndex}`;
|
|
104
|
+
state.input.setAttribute('aria-activedescendant', activeOption.id);
|
|
105
|
+
} else {
|
|
106
|
+
state.input.removeAttribute('aria-activedescendant');
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
const syncEmptyState = (state) => {
|
|
111
|
+
state.popover.dataset.empty = String(state.visibleOptions.length === 0);
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
const filterOptions = (state, { preserveActive = false } = {}) => {
|
|
115
|
+
const previousActive = state.activeIndex > -1 ? state.options[state.activeIndex] : null;
|
|
116
|
+
state.visibleOptions = [];
|
|
117
|
+
|
|
118
|
+
if (state.manualFilter) {
|
|
119
|
+
state.visibleOptions = state.options.filter(option => option.getAttribute('aria-hidden') !== 'true');
|
|
120
|
+
|
|
121
|
+
if (preserveActive && previousActive && state.visibleOptions.includes(previousActive)) {
|
|
122
|
+
setActiveOption(state, state.options.indexOf(previousActive));
|
|
123
|
+
} else {
|
|
124
|
+
setActiveOption(state, state.autoHighlight && state.visibleOptions.length > 0 ? state.options.indexOf(state.visibleOptions[0]) : -1);
|
|
125
|
+
}
|
|
126
|
+
syncEmptyState(state);
|
|
127
|
+
return;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
const search = state.input.value.trim().toLowerCase();
|
|
131
|
+
|
|
132
|
+
state.allOptions.forEach(option => {
|
|
133
|
+
if (option.hasAttribute('data-force')) {
|
|
134
|
+
option.setAttribute('aria-hidden', 'false');
|
|
135
|
+
if (state.options.includes(option)) state.visibleOptions.push(option);
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const optionText = (option.dataset.filter || option.dataset.label || option.textContent).trim().toLowerCase();
|
|
140
|
+
const keywords = (option.dataset.keywords || '').toLowerCase().split(/[\s,]+/).filter(Boolean);
|
|
141
|
+
const matches = !search || optionText.includes(search) || keywords.some(keyword => keyword.includes(search));
|
|
142
|
+
option.setAttribute('aria-hidden', String(!matches));
|
|
143
|
+
if (matches && state.options.includes(option)) state.visibleOptions.push(option);
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
if (preserveActive && previousActive && state.visibleOptions.includes(previousActive)) {
|
|
147
|
+
setActiveOption(state, state.options.indexOf(previousActive));
|
|
148
|
+
} else {
|
|
149
|
+
setActiveOption(state, state.autoHighlight && state.visibleOptions.length > 0 ? state.options.indexOf(state.visibleOptions[0]) : -1);
|
|
150
|
+
}
|
|
151
|
+
syncEmptyState(state);
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
const renderChips = (root) => {
|
|
155
|
+
const state = states.get(root);
|
|
156
|
+
if (!state.chips) return;
|
|
157
|
+
|
|
158
|
+
state.chips.querySelectorAll('.combobox-chip').forEach(chip => chip.remove());
|
|
159
|
+
|
|
160
|
+
getSelection(state).forEach(entry => {
|
|
161
|
+
const chip = document.createElement('span');
|
|
162
|
+
chip.className = 'combobox-chip';
|
|
163
|
+
chip.dataset.value = entry.value;
|
|
164
|
+
chip.textContent = entry.label;
|
|
165
|
+
|
|
166
|
+
const remove = document.createElement('button');
|
|
167
|
+
remove.type = 'button';
|
|
168
|
+
remove.className = 'combobox-chip-remove';
|
|
169
|
+
remove.setAttribute('aria-label', `Remove ${entry.label}`);
|
|
170
|
+
remove.innerHTML = '<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M18 6 6 18"/><path d="m6 6 12 12"/></svg>';
|
|
171
|
+
remove.addEventListener('click', () => root.deselect(entry.value));
|
|
172
|
+
|
|
173
|
+
chip.appendChild(remove);
|
|
174
|
+
state.chips.insertBefore(chip, state.input);
|
|
175
|
+
});
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
const syncSelectedOptions = (state) => {
|
|
179
|
+
state.options.forEach(option => {
|
|
180
|
+
if (state.selected.has(getValue(option))) {
|
|
181
|
+
option.setAttribute('aria-selected', 'true');
|
|
182
|
+
} else {
|
|
183
|
+
option.removeAttribute('aria-selected');
|
|
184
|
+
}
|
|
185
|
+
});
|
|
186
|
+
};
|
|
187
|
+
|
|
188
|
+
const setSelected = (root, entries, triggerEvent = true) => {
|
|
189
|
+
const state = states.get(root);
|
|
190
|
+
const normalized = (Array.isArray(entries) ? entries : [entries]).map(normalizeEntry).filter(Boolean);
|
|
191
|
+
|
|
192
|
+
state.selected.clear();
|
|
193
|
+
if (state.isMultiple) {
|
|
194
|
+
normalized.forEach(entry => state.selected.set(entry.value, entry));
|
|
195
|
+
state.input.value = '';
|
|
196
|
+
} else if (normalized[0]) {
|
|
197
|
+
state.selected.set(normalized[0].value, normalized[0]);
|
|
198
|
+
state.input.value = normalized[0].label;
|
|
199
|
+
} else {
|
|
200
|
+
state.input.value = '';
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
state.hiddenInput.value = serializeSelection(state);
|
|
204
|
+
syncSelectedOptions(state);
|
|
205
|
+
if (state.isMultiple) {
|
|
206
|
+
renderChips(root);
|
|
207
|
+
filterOptions(state, { preserveActive: true });
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
if (triggerEvent) {
|
|
211
|
+
root.dispatchEvent(new CustomEvent('change', {
|
|
212
|
+
detail: { value: getCanonicalValue(state), selected: getSelectedDetail(state) },
|
|
213
|
+
bubbles: true,
|
|
214
|
+
}));
|
|
215
|
+
}
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
const closePopover = (state, focusInput = false) => {
|
|
219
|
+
if (state.popover.getAttribute('aria-hidden') === 'true') return;
|
|
220
|
+
state.popover.setAttribute('aria-hidden', 'true');
|
|
221
|
+
state.input.setAttribute('aria-expanded', 'false');
|
|
222
|
+
setActiveOption(state, -1);
|
|
223
|
+
if (focusInput) state.input.focus();
|
|
224
|
+
};
|
|
225
|
+
|
|
226
|
+
const refreshCombobox = (root) => {
|
|
227
|
+
const state = states.get(root);
|
|
228
|
+
if (!state) return;
|
|
229
|
+
|
|
230
|
+
const elements = getElements(root);
|
|
231
|
+
if (!elements.input || !elements.popover || !elements.listbox || !elements.hiddenInput) {
|
|
232
|
+
const missing = [];
|
|
233
|
+
if (!elements.input) missing.push('input');
|
|
234
|
+
if (!elements.popover) missing.push('popover');
|
|
235
|
+
if (!elements.listbox) missing.push('listbox');
|
|
236
|
+
if (!elements.hiddenInput) missing.push('hidden input');
|
|
237
|
+
console.error(`Combobox refresh failed. Missing element(s): ${missing.join(', ')}`, root);
|
|
238
|
+
return;
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
const previousValue = elements.hiddenInput.value;
|
|
242
|
+
const previousInputValue = elements.input.value;
|
|
243
|
+
Object.assign(state, elements, getOptions(elements.listbox));
|
|
244
|
+
state.isMultiple = state.listbox.getAttribute('aria-multiselectable') === 'true';
|
|
245
|
+
state.closeOnSelect = root.dataset.closeOnSelect === 'true';
|
|
246
|
+
state.autoHighlight = root.dataset.autoHighlight === 'true';
|
|
247
|
+
state.manualFilter = root.dataset.filter === 'manual';
|
|
248
|
+
state.format = getFormat(root);
|
|
249
|
+
|
|
250
|
+
const stored = parseStoredSelection(previousValue, previousInputValue, state);
|
|
251
|
+
if (stored.length > 0) {
|
|
252
|
+
setSelected(root, stored, false);
|
|
253
|
+
} else {
|
|
254
|
+
const selectedOptions = state.options.filter(option => option.getAttribute('aria-selected') === 'true').map(toSelected);
|
|
255
|
+
if (selectedOptions.length > 0) {
|
|
256
|
+
setSelected(root, state.isMultiple ? selectedOptions : selectedOptions[0], false);
|
|
257
|
+
} else if (state.isMultiple) {
|
|
258
|
+
setSelected(root, [], false);
|
|
259
|
+
} else {
|
|
260
|
+
state.input.value = previousInputValue;
|
|
261
|
+
state.selected.clear();
|
|
262
|
+
state.hiddenInput.value = '';
|
|
263
|
+
syncSelectedOptions(state);
|
|
264
|
+
filterOptions(state, { preserveActive: true });
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
};
|
|
268
|
+
|
|
269
|
+
const resolveEntry = (state, value) => {
|
|
270
|
+
if (value && typeof value === 'object') {
|
|
271
|
+
const entry = normalizeEntry(value);
|
|
272
|
+
if (!entry) return null;
|
|
273
|
+
const option = state.options.find(opt => getValue(opt) === entry.value);
|
|
274
|
+
return option ? toSelected(option) : entry;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
const option = state.options.find(opt => getValue(opt) === value);
|
|
278
|
+
if (option) return toSelected(option);
|
|
279
|
+
const existing = state.selected.get(String(value));
|
|
280
|
+
return existing || null;
|
|
281
|
+
};
|
|
282
|
+
|
|
283
|
+
const selectValue = (root, value) => {
|
|
284
|
+
const state = states.get(root);
|
|
285
|
+
const entry = resolveEntry(state, value);
|
|
286
|
+
if (!entry) return;
|
|
287
|
+
|
|
288
|
+
if (state.isMultiple) {
|
|
289
|
+
setSelected(root, [...getSelection(state).filter(item => item.value !== entry.value), entry]);
|
|
290
|
+
if (state.closeOnSelect) root.close(true);
|
|
291
|
+
} else {
|
|
292
|
+
setSelected(root, entry);
|
|
293
|
+
root.close(true);
|
|
294
|
+
}
|
|
295
|
+
};
|
|
296
|
+
|
|
297
|
+
const deselectValue = (root, value) => {
|
|
298
|
+
const state = states.get(root);
|
|
299
|
+
if (!state.isMultiple) return;
|
|
300
|
+
const normalized = String(value);
|
|
301
|
+
if (!state.selected.has(normalized)) return;
|
|
302
|
+
setSelected(root, getSelection(state).filter(item => item.value !== normalized));
|
|
303
|
+
};
|
|
304
|
+
|
|
305
|
+
const handleKeydown = (event, root) => {
|
|
306
|
+
const state = states.get(root);
|
|
307
|
+
if (!['ArrowDown', 'ArrowUp', 'Enter', 'Home', 'End', 'Escape', 'Backspace'].includes(event.key)) return;
|
|
308
|
+
|
|
309
|
+
const isOpen = state.popover.getAttribute('aria-hidden') === 'false';
|
|
310
|
+
|
|
311
|
+
if (event.key === 'Backspace' && state.isMultiple && state.input.value === '') {
|
|
312
|
+
const selected = getSelection(state);
|
|
313
|
+
const last = selected[selected.length - 1];
|
|
314
|
+
if (last) root.deselect(last.value);
|
|
315
|
+
return;
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
if (event.key === 'Escape') {
|
|
319
|
+
root.close(true);
|
|
320
|
+
return;
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
if (!isOpen && ['ArrowDown', 'ArrowUp', 'Home', 'End'].includes(event.key)) {
|
|
324
|
+
event.preventDefault();
|
|
325
|
+
root.open();
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
if (state.popover.getAttribute('aria-hidden') === 'true') return;
|
|
329
|
+
|
|
330
|
+
if (event.key === 'Enter') {
|
|
331
|
+
if (state.activeIndex > -1) {
|
|
332
|
+
event.preventDefault();
|
|
333
|
+
const option = state.options[state.activeIndex];
|
|
334
|
+
state.isMultiple ? root.toggle(getValue(option)) : root.select(getValue(option));
|
|
335
|
+
}
|
|
336
|
+
return;
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
if (!['ArrowDown', 'ArrowUp', 'Home', 'End'].includes(event.key) || state.visibleOptions.length === 0) return;
|
|
340
|
+
event.preventDefault();
|
|
341
|
+
|
|
342
|
+
const currentVisibleIndex = state.activeIndex > -1 ? state.visibleOptions.indexOf(state.options[state.activeIndex]) : -1;
|
|
343
|
+
let nextVisibleIndex = currentVisibleIndex;
|
|
344
|
+
|
|
345
|
+
if (event.key === 'ArrowDown') nextVisibleIndex = Math.min(currentVisibleIndex + 1, state.visibleOptions.length - 1);
|
|
346
|
+
if (event.key === 'ArrowUp') nextVisibleIndex = currentVisibleIndex <= 0 ? 0 : currentVisibleIndex - 1;
|
|
347
|
+
if (event.key === 'Home') nextVisibleIndex = 0;
|
|
348
|
+
if (event.key === 'End') nextVisibleIndex = state.visibleOptions.length - 1;
|
|
349
|
+
|
|
350
|
+
const nextOption = state.visibleOptions[nextVisibleIndex];
|
|
351
|
+
setActiveOption(state, state.options.indexOf(nextOption));
|
|
352
|
+
scrollOptionIntoListbox(state, nextOption);
|
|
353
|
+
};
|
|
354
|
+
|
|
355
|
+
const initCombobox = (root) => {
|
|
356
|
+
if (root.dataset.comboboxInitialized) return;
|
|
357
|
+
|
|
358
|
+
const state = { root, activeIndex: -1, allOptions: [], options: [], visibleOptions: [], selected: new Map(), format: 'value', manualFilter: false };
|
|
359
|
+
states.set(root, state);
|
|
360
|
+
root.refresh = () => refreshCombobox(root);
|
|
361
|
+
|
|
362
|
+
refreshCombobox(root);
|
|
363
|
+
if (!state.input || !state.popover || !state.listbox || !state.hiddenInput) {
|
|
364
|
+
states.delete(root);
|
|
365
|
+
delete root.refresh;
|
|
366
|
+
return;
|
|
367
|
+
}
|
|
368
|
+
|
|
369
|
+
root.open = () => {
|
|
370
|
+
if (state.popover.getAttribute('aria-hidden') === 'false') return;
|
|
371
|
+
document.dispatchEvent(new CustomEvent('basecoat:popover', { detail: { source: root } }));
|
|
372
|
+
root.refresh();
|
|
373
|
+
filterOptions(state);
|
|
374
|
+
state.popover.setAttribute('aria-hidden', 'false');
|
|
375
|
+
state.input.setAttribute('aria-expanded', 'true');
|
|
376
|
+
};
|
|
377
|
+
root.close = (focusInput = false) => closePopover(state, focusInput);
|
|
378
|
+
|
|
379
|
+
root.select = (value) => selectValue(root, value);
|
|
380
|
+
root.selectByValue = root.select;
|
|
381
|
+
root.setValue = (value) => {
|
|
382
|
+
const entries = state.isMultiple ? (Array.isArray(value) ? value : (value == null ? [] : [value])) : [value];
|
|
383
|
+
const resolved = entries.map(entry => resolveEntry(state, entry) || normalizeEntry(entry)).filter(Boolean);
|
|
384
|
+
setSelected(root, state.isMultiple ? resolved : resolved[0]);
|
|
385
|
+
};
|
|
386
|
+
if (state.isMultiple) {
|
|
387
|
+
root.deselect = (value) => deselectValue(root, value);
|
|
388
|
+
root.toggle = (value) => {
|
|
389
|
+
const entry = resolveEntry(state, value);
|
|
390
|
+
if (!entry) return;
|
|
391
|
+
state.selected.has(entry.value) ? root.deselect(entry.value) : root.select(entry);
|
|
392
|
+
};
|
|
393
|
+
root.selectAll = () => setSelected(root, state.options.map(toSelected));
|
|
394
|
+
root.selectNone = () => setSelected(root, []);
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
const handleInputFocus = root.open;
|
|
398
|
+
const handleInputClick = root.open;
|
|
399
|
+
const handleInput = () => {
|
|
400
|
+
root.open();
|
|
401
|
+
filterOptions(state);
|
|
402
|
+
if (!state.isMultiple) {
|
|
403
|
+
state.hiddenInput.value = '';
|
|
404
|
+
state.selected.clear();
|
|
405
|
+
syncSelectedOptions(state);
|
|
406
|
+
}
|
|
407
|
+
};
|
|
408
|
+
const handleInputKeydown = (event) => handleKeydown(event, root);
|
|
409
|
+
const handleListboxMousemove = (event) => {
|
|
410
|
+
const option = event.target.closest('[role="option"]');
|
|
411
|
+
if (option && state.visibleOptions.includes(option)) setActiveOption(state, state.options.indexOf(option));
|
|
412
|
+
};
|
|
413
|
+
const handleListboxClick = (event) => {
|
|
414
|
+
const option = event.target.closest('[role="option"]');
|
|
415
|
+
if (!option || !state.options.includes(option)) return;
|
|
416
|
+
state.isMultiple ? root.toggle(getValue(option)) : root.select(getValue(option));
|
|
417
|
+
if (state.isMultiple && !state.closeOnSelect) state.input.focus();
|
|
418
|
+
};
|
|
419
|
+
const handleDocumentClick = (event) => {
|
|
420
|
+
if (!root.contains(event.target)) root.close(false);
|
|
421
|
+
};
|
|
422
|
+
const handleDocumentPopover = (event) => {
|
|
423
|
+
if (event.detail.source !== root) root.close(false);
|
|
424
|
+
};
|
|
425
|
+
|
|
426
|
+
state.input.addEventListener('focus', handleInputFocus);
|
|
427
|
+
state.input.addEventListener('click', handleInputClick);
|
|
428
|
+
state.input.addEventListener('input', handleInput);
|
|
429
|
+
state.input.addEventListener('keydown', handleInputKeydown);
|
|
430
|
+
state.listbox.addEventListener('mousemove', handleListboxMousemove);
|
|
431
|
+
state.listbox.addEventListener('click', handleListboxClick);
|
|
432
|
+
document.addEventListener('click', handleDocumentClick);
|
|
433
|
+
document.addEventListener('basecoat:popover', handleDocumentPopover);
|
|
434
|
+
|
|
435
|
+
root._destroy = () => {
|
|
436
|
+
state.input.removeEventListener('focus', handleInputFocus);
|
|
437
|
+
state.input.removeEventListener('click', handleInputClick);
|
|
438
|
+
state.input.removeEventListener('input', handleInput);
|
|
439
|
+
state.input.removeEventListener('keydown', handleInputKeydown);
|
|
440
|
+
state.listbox.removeEventListener('mousemove', handleListboxMousemove);
|
|
441
|
+
state.listbox.removeEventListener('click', handleListboxClick);
|
|
442
|
+
document.removeEventListener('click', handleDocumentClick);
|
|
443
|
+
document.removeEventListener('basecoat:popover', handleDocumentPopover);
|
|
444
|
+
state.chips?.querySelectorAll('.combobox-chip').forEach(chip => chip.remove());
|
|
445
|
+
states.delete(root);
|
|
446
|
+
delete root.refresh;
|
|
447
|
+
delete root.open;
|
|
448
|
+
delete root.close;
|
|
449
|
+
delete root.select;
|
|
450
|
+
delete root.selectByValue;
|
|
451
|
+
delete root.setValue;
|
|
452
|
+
delete root.deselect;
|
|
453
|
+
delete root.toggle;
|
|
454
|
+
delete root.selectAll;
|
|
455
|
+
delete root.selectNone;
|
|
456
|
+
};
|
|
457
|
+
|
|
458
|
+
state.popover.setAttribute('aria-hidden', 'true');
|
|
459
|
+
state.input.setAttribute('aria-expanded', 'false');
|
|
460
|
+
|
|
461
|
+
Object.defineProperty(root, 'value', {
|
|
462
|
+
configurable: true,
|
|
463
|
+
get: () => getCanonicalValue(state),
|
|
464
|
+
set: (value) => root.setValue(value),
|
|
465
|
+
});
|
|
466
|
+
|
|
467
|
+
Object.defineProperty(root, 'selected', {
|
|
468
|
+
configurable: true,
|
|
469
|
+
get: () => getSelectedDetail(state),
|
|
470
|
+
});
|
|
471
|
+
|
|
472
|
+
root.dataset.comboboxInitialized = 'true';
|
|
473
|
+
root.dispatchEvent(new CustomEvent('basecoat:initialized'));
|
|
474
|
+
};
|
|
475
|
+
|
|
476
|
+
if (window.basecoat) {
|
|
477
|
+
window.basecoat.register('combobox', {
|
|
478
|
+
selector: '.combobox:not([data-combobox-initialized])',
|
|
479
|
+
init: initCombobox,
|
|
480
|
+
refresh: refreshCombobox,
|
|
481
|
+
});
|
|
482
|
+
}
|
|
483
|
+
})();
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
(()=>{const e=new WeakMap,t=e=>e.dataset.value??e.textContent.trim(),i=e=>e.dataset.label||e.textContent.trim(),o=e=>({value:t(e),label:i(e)}),n=e=>{if(e&&"object"==typeof e){const t=null==e.value?"":String(e.value);return t?{value:t,label:String(e.label??e.value)}:null}const t=null==e?"":String(e);return t?{value:t,label:t}:null},l=e=>Array.from(e.selected.values()),s=e=>e.isMultiple?l(e).map((e=>e.value)):l(e)[0]?.value||"",r=e=>e.isMultiple?l(e):l(e)[0]||null,a=(e,t)=>{if(e.activeIndex>-1&&e.options[e.activeIndex]&&e.options[e.activeIndex].classList.remove("active"),e.activeIndex=t,e.activeIndex>-1){const t=e.options[e.activeIndex];t.classList.add("active"),t.id||(t.id=`${e.listbox.id||e.root.id||"combobox"}-option-${e.activeIndex}`),e.input.setAttribute("aria-activedescendant",t.id)}else e.input.removeAttribute("aria-activedescendant")},c=e=>{e.popover.dataset.empty=String(0===e.visibleOptions.length)},u=(e,{preserveActive:t=!1}={})=>{const i=e.activeIndex>-1?e.options[e.activeIndex]:null;if(e.visibleOptions=[],e.manualFilter)return e.visibleOptions=e.options.filter((e=>"true"!==e.getAttribute("aria-hidden"))),t&&i&&e.visibleOptions.includes(i)?a(e,e.options.indexOf(i)):a(e,e.autoHighlight&&e.visibleOptions.length>0?e.options.indexOf(e.visibleOptions[0]):-1),void c(e);const o=e.input.value.trim().toLowerCase();e.allOptions.forEach((t=>{if(t.hasAttribute("data-force"))return t.setAttribute("aria-hidden","false"),void(e.options.includes(t)&&e.visibleOptions.push(t));const i=(t.dataset.filter||t.dataset.label||t.textContent).trim().toLowerCase(),n=(t.dataset.keywords||"").toLowerCase().split(/[\s,]+/).filter(Boolean),l=!o||i.includes(o)||n.some((e=>e.includes(o)));t.setAttribute("aria-hidden",String(!l)),l&&e.options.includes(t)&&e.visibleOptions.push(t)})),t&&i&&e.visibleOptions.includes(i)?a(e,e.options.indexOf(i)):a(e,e.autoHighlight&&e.visibleOptions.length>0?e.options.indexOf(e.visibleOptions[0]):-1),c(e)},p=e=>{e.options.forEach((i=>{e.selected.has(t(i))?i.setAttribute("aria-selected","true"):i.removeAttribute("aria-selected")}))},d=(t,i,o=!0)=>{const a=e.get(t),c=(Array.isArray(i)?i:[i]).map(n).filter(Boolean);a.selected.clear(),a.isMultiple?(c.forEach((e=>a.selected.set(e.value,e))),a.input.value=""):c[0]?(a.selected.set(c[0].value,c[0]),a.input.value=c[0].label):a.input.value="",a.hiddenInput.value=(e=>{const t=l(e);if("object"===e.format)return JSON.stringify(e.isMultiple?t:t[0]||null);const i=t.map((e=>e.value));return e.isMultiple?JSON.stringify(i):i[0]||""})(a),p(a),a.isMultiple&&((t=>{const i=e.get(t);i.chips&&(i.chips.querySelectorAll(".combobox-chip").forEach((e=>e.remove())),l(i).forEach((e=>{const o=document.createElement("span");o.className="combobox-chip",o.dataset.value=e.value,o.textContent=e.label;const n=document.createElement("button");n.type="button",n.className="combobox-chip-remove",n.setAttribute("aria-label",`Remove ${e.label}`),n.innerHTML='<svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"><path d="M18 6 6 18"/><path d="m6 6 12 12"/></svg>',n.addEventListener("click",(()=>t.deselect(e.value))),o.appendChild(n),i.chips.insertBefore(o,i.input)})))})(t),u(a,{preserveActive:!0})),o&&t.dispatchEvent(new CustomEvent("change",{detail:{value:s(a),selected:r(a)},bubbles:!0}))},v=i=>{const l=e.get(i);if(!l)return;const s=(e=>{const t=e.querySelector(':scope > input[role="combobox"], :scope > .combobox-chips input[role="combobox"]'),i=e.querySelector(":scope > .combobox-chips"),o=e.querySelector(":scope > [data-popover]"),n=o?o.querySelector('[role="listbox"]'):null;return{input:t,chips:i,popover:o,listbox:n,hiddenInput:e.querySelector(':scope > input[type="hidden"]')}})(i);if(!(s.input&&s.popover&&s.listbox&&s.hiddenInput)){const e=[];return s.input||e.push("input"),s.popover||e.push("popover"),s.listbox||e.push("listbox"),s.hiddenInput||e.push("hidden input"),void console.error(`Combobox refresh failed. Missing element(s): ${e.join(", ")}`,i)}const r=s.hiddenInput.value,a=s.input.value;Object.assign(l,s,(e=>{const t=Array.from(e.querySelectorAll('[role="option"]'));return{allOptions:t,options:t.filter((e=>!(e=>"true"===e.getAttribute("aria-disabled"))(e)))}})(s.listbox)),l.isMultiple="true"===l.listbox.getAttribute("aria-multiselectable"),l.closeOnSelect="true"===i.dataset.closeOnSelect,l.autoHighlight="true"===i.dataset.autoHighlight,l.manualFilter="manual"===i.dataset.filter,l.format=(e=>"object"===e.dataset.format?"object":"value")(i);const c=((e,i,l)=>{if(l.isMultiple){let i=[];try{i=JSON.parse(e||"[]")}catch(e){i=[]}return Array.isArray(i)?i.map((e=>{const i=n("object"===l.format?e:{value:e,label:l.selected.get(String(e))?.label??e});if(!i)return null;const s=l.options.find((e=>t(e)===i.value));return s?o(s):i})).filter(Boolean):[]}if("object"===l.format)try{const i=n(JSON.parse(e||"null"));if(!i)return[];const s=l.options.find((e=>t(e)===i.value));return[s?o(s):i]}catch(e){return[]}const s=e||"";if(!s)return[];const r=l.options.find((e=>t(e)===s));return[r?o(r):{value:s,label:l.selected.get(s)?.label||i||s}]})(r,a,l);if(c.length>0)d(i,c,!1);else{const e=l.options.filter((e=>"true"===e.getAttribute("aria-selected"))).map(o);e.length>0?d(i,l.isMultiple?e:e[0],!1):l.isMultiple?d(i,[],!1):(l.input.value=a,l.selected.clear(),l.hiddenInput.value="",p(l),u(l,{preserveActive:!0}))}},b=(e,i)=>{if(i&&"object"==typeof i){const l=n(i);if(!l)return null;const s=e.options.find((e=>t(e)===l.value));return s?o(s):l}const l=e.options.find((e=>t(e)===i));if(l)return o(l);return e.selected.get(String(i))||null},f=i=>{if(i.dataset.comboboxInitialized)return;const c={root:i,activeIndex:-1,allOptions:[],options:[],visibleOptions:[],selected:new Map,format:"value",manualFilter:!1};if(e.set(i,c),i.refresh=()=>v(i),v(i),!(c.input&&c.popover&&c.listbox&&c.hiddenInput))return e.delete(i),void delete i.refresh;i.open=()=>{"false"!==c.popover.getAttribute("aria-hidden")&&(document.dispatchEvent(new CustomEvent("basecoat:popover",{detail:{source:i}})),i.refresh(),u(c),c.popover.setAttribute("aria-hidden","false"),c.input.setAttribute("aria-expanded","true"))},i.close=(e=!1)=>((e,t=!1)=>{"true"!==e.popover.getAttribute("aria-hidden")&&(e.popover.setAttribute("aria-hidden","true"),e.input.setAttribute("aria-expanded","false"),a(e,-1),t&&e.input.focus())})(c,e),i.select=t=>((t,i)=>{const o=e.get(t),n=b(o,i);n&&(o.isMultiple?(d(t,[...l(o).filter((e=>e.value!==n.value)),n]),o.closeOnSelect&&t.close(!0)):(d(t,n),t.close(!0)))})(i,t),i.selectByValue=i.select,i.setValue=e=>{const t=(c.isMultiple?Array.isArray(e)?e:null==e?[]:[e]:[e]).map((e=>b(c,e)||n(e))).filter(Boolean);d(i,c.isMultiple?t:t[0])},c.isMultiple&&(i.deselect=t=>((t,i)=>{const o=e.get(t);if(!o.isMultiple)return;const n=String(i);o.selected.has(n)&&d(t,l(o).filter((e=>e.value!==n)))})(i,t),i.toggle=e=>{const t=b(c,e);t&&(c.selected.has(t.value)?i.deselect(t.value):i.select(t))},i.selectAll=()=>d(i,c.options.map(o)),i.selectNone=()=>d(i,[]));const f=i.open,m=i.open,h=()=>{i.open(),u(c),c.isMultiple||(c.hiddenInput.value="",c.selected.clear(),p(c))},g=o=>((i,o)=>{const n=e.get(o);if(!["ArrowDown","ArrowUp","Enter","Home","End","Escape","Backspace"].includes(i.key))return;const s="false"===n.popover.getAttribute("aria-hidden");if("Backspace"===i.key&&n.isMultiple&&""===n.input.value){const e=l(n),t=e[e.length-1];return void(t&&o.deselect(t.value))}if("Escape"===i.key)return void o.close(!0);if(!s&&["ArrowDown","ArrowUp","Home","End"].includes(i.key)&&(i.preventDefault(),o.open()),"true"===n.popover.getAttribute("aria-hidden"))return;if("Enter"===i.key){if(n.activeIndex>-1){i.preventDefault();const e=n.options[n.activeIndex];n.isMultiple?o.toggle(t(e)):o.select(t(e))}return}if(!["ArrowDown","ArrowUp","Home","End"].includes(i.key)||0===n.visibleOptions.length)return;i.preventDefault();const r=n.activeIndex>-1?n.visibleOptions.indexOf(n.options[n.activeIndex]):-1;let c=r;"ArrowDown"===i.key&&(c=Math.min(r+1,n.visibleOptions.length-1)),"ArrowUp"===i.key&&(c=r<=0?0:r-1),"Home"===i.key&&(c=0),"End"===i.key&&(c=n.visibleOptions.length-1);const u=n.visibleOptions[c];a(n,n.options.indexOf(u)),((e,t)=>{const i=t.getBoundingClientRect(),o=e.listbox.getBoundingClientRect();i.top<o.top?e.listbox.scrollTop-=o.top-i.top:i.bottom>o.bottom&&(e.listbox.scrollTop+=i.bottom-o.bottom)})(n,u)})(o,i),x=e=>{const t=e.target.closest('[role="option"]');t&&c.visibleOptions.includes(t)&&a(c,c.options.indexOf(t))},A=e=>{const o=e.target.closest('[role="option"]');o&&c.options.includes(o)&&(c.isMultiple?i.toggle(t(o)):i.select(t(o)),c.isMultiple&&!c.closeOnSelect&&c.input.focus())},y=e=>{i.contains(e.target)||i.close(!1)},E=e=>{e.detail.source!==i&&i.close(!1)};c.input.addEventListener("focus",f),c.input.addEventListener("click",m),c.input.addEventListener("input",h),c.input.addEventListener("keydown",g),c.listbox.addEventListener("mousemove",x),c.listbox.addEventListener("click",A),document.addEventListener("click",y),document.addEventListener("basecoat:popover",E),i._destroy=()=>{c.input.removeEventListener("focus",f),c.input.removeEventListener("click",m),c.input.removeEventListener("input",h),c.input.removeEventListener("keydown",g),c.listbox.removeEventListener("mousemove",x),c.listbox.removeEventListener("click",A),document.removeEventListener("click",y),document.removeEventListener("basecoat:popover",E),c.chips?.querySelectorAll(".combobox-chip").forEach((e=>e.remove())),e.delete(i),delete i.refresh,delete i.open,delete i.close,delete i.select,delete i.selectByValue,delete i.setValue,delete i.deselect,delete i.toggle,delete i.selectAll,delete i.selectNone},c.popover.setAttribute("aria-hidden","true"),c.input.setAttribute("aria-expanded","false"),Object.defineProperty(i,"value",{configurable:!0,get:()=>s(c),set:e=>i.setValue(e)}),Object.defineProperty(i,"selected",{configurable:!0,get:()=>r(c)}),i.dataset.comboboxInitialized="true",i.dispatchEvent(new CustomEvent("basecoat:initialized"))};window.basecoat&&window.basecoat.register("combobox",{selector:".combobox:not([data-combobox-initialized])",init:f,refresh:v})})();
|