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.
- 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 -755
- 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 -31
- 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/select.js
CHANGED
|
@@ -1,440 +1,428 @@
|
|
|
1
1
|
(() => {
|
|
2
|
-
const
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
const
|
|
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 =
|
|
8
|
-
|
|
9
|
+
const input = root.querySelector(':scope > input[type="hidden"]');
|
|
10
|
+
return { trigger, selectedLabel, popover, listbox, input };
|
|
11
|
+
};
|
|
9
12
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
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
|
-
|
|
22
|
-
|
|
23
|
-
|
|
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
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
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
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
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
|
-
|
|
85
|
-
|
|
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
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
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
|
|
102
|
-
|
|
58
|
+
const value = selected.map(item => item.value);
|
|
59
|
+
return state.isMultiple ? JSON.stringify(value) : (value[0] || '');
|
|
60
|
+
};
|
|
103
61
|
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
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
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
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
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
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
|
-
|
|
125
|
-
|
|
126
|
-
|
|
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
|
-
|
|
86
|
+
state.trigger.removeAttribute('aria-activedescendant');
|
|
129
87
|
}
|
|
130
|
-
|
|
131
|
-
|
|
88
|
+
} else {
|
|
89
|
+
state.trigger.removeAttribute('aria-activedescendant');
|
|
90
|
+
}
|
|
91
|
+
};
|
|
132
92
|
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
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
|
-
|
|
141
|
-
|
|
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
|
-
|
|
150
|
-
|
|
151
|
-
|
|
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
|
-
|
|
154
|
-
|
|
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
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
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 (
|
|
165
|
-
|
|
166
|
-
|
|
133
|
+
if (triggerEvent) {
|
|
134
|
+
root.dispatchEvent(new CustomEvent('change', {
|
|
135
|
+
detail: { value, selected: selectedDetail },
|
|
136
|
+
bubbles: true,
|
|
137
|
+
}));
|
|
138
|
+
}
|
|
139
|
+
};
|
|
167
140
|
|
|
168
|
-
|
|
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
|
-
|
|
171
|
-
|
|
172
|
-
|
|
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
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
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
|
-
|
|
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
|
-
|
|
198
|
-
|
|
199
|
-
|
|
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
|
-
|
|
217
|
-
|
|
218
|
-
|
|
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
|
-
|
|
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
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
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
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
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
|
-
|
|
226
|
+
const handleKeyNavigation = (event, root) => {
|
|
227
|
+
const state = states.get(root);
|
|
228
|
+
const isPopoverOpen = state.popover.getAttribute('aria-hidden') === 'false';
|
|
241
229
|
|
|
242
|
-
|
|
243
|
-
closePopover();
|
|
244
|
-
return;
|
|
245
|
-
}
|
|
230
|
+
if (!['ArrowDown', 'ArrowUp', 'Enter', 'Home', 'End', 'Escape'].includes(event.key)) return;
|
|
246
231
|
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
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
|
-
|
|
240
|
+
event.preventDefault();
|
|
266
241
|
|
|
267
|
-
|
|
268
|
-
|
|
242
|
+
if (event.key === 'Escape') {
|
|
243
|
+
root.close();
|
|
244
|
+
return;
|
|
245
|
+
}
|
|
269
246
|
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
if (
|
|
278
|
-
|
|
279
|
-
|
|
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
|
-
|
|
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
|
-
|
|
299
|
-
|
|
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
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
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
|
-
|
|
318
|
-
|
|
319
|
-
|
|
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
|
-
|
|
323
|
-
|
|
324
|
-
detail: { source: selectComponent }
|
|
325
|
-
}));
|
|
278
|
+
const initSelect = (root) => {
|
|
279
|
+
if (root.dataset.selectInitialized) return;
|
|
326
280
|
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
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
|
-
|
|
338
|
-
|
|
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
|
-
|
|
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
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
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
|
-
|
|
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
|
-
|
|
327
|
+
if (state.isMultiple) {
|
|
328
|
+
toggleMultipleValue(root, option);
|
|
329
|
+
if (state.closeOnSelect) {
|
|
330
|
+
root.close();
|
|
367
331
|
} else {
|
|
368
|
-
setActiveOption(options.indexOf(option));
|
|
369
|
-
|
|
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 (
|
|
377
|
-
|
|
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
|
-
|
|
390
|
-
|
|
391
|
-
|
|
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
|
-
|
|
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
|
-
|
|
398
|
-
|
|
399
|
-
get: () =>
|
|
400
|
-
|
|
401
|
-
|
|
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
|
-
|
|
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
|
-
|
|
386
|
+
updateValue(root, option);
|
|
387
|
+
root.close();
|
|
420
388
|
}
|
|
421
389
|
}
|
|
422
|
-
}
|
|
390
|
+
},
|
|
423
391
|
});
|
|
424
392
|
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
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
|
-
|
|
434
|
-
|
|
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',
|
|
422
|
+
window.basecoat.register('select', {
|
|
423
|
+
selector: 'div.select:not([data-select-initialized])',
|
|
424
|
+
init: initSelect,
|
|
425
|
+
refresh: refreshSelect,
|
|
426
|
+
});
|
|
439
427
|
}
|
|
440
428
|
})();
|