@bwui/js 0.1.3 → 0.1.4

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.
@@ -14,6 +14,18 @@
14
14
  }
15
15
 
16
16
  /* ---- Combobox ---- */
17
+ // Wave-1 HTML delivery of docs/components/combobox.md (CMB-074): single mode,
18
+ // client filtering. Click opens (CMB-022), the input is named from the
19
+ // floor's label (CMB-050), the ARIA and active-descendant set is complete
20
+ // (CMB-010, CMB-051), a polite status region announces results (CMB-052)
21
+ // and a real selection change fires a bubbling `change` on the floor
22
+ // (CMB-012). The keyboard map of section 5.2 holds as well: arrows skip
23
+ // disabled options and wrap at the ends (CMB-016), Tab closes without
24
+ // choosing, Escape closes and then reverts an uncommitted edit, and focus
25
+ // leaving without a commit restores the committed label (CMB-013). Multiple,
26
+ // server filtering, create, groups and clear stay Django-first.
27
+ let comboboxSeq = 0;
28
+
17
29
  function initCombobox(root) {
18
30
  const floor = $(".bw-combobox__floor", root);
19
31
  const field = $(".bw-combobox__field", root);
@@ -21,26 +33,122 @@
21
33
  const listbox = $(".bw-combobox__listbox", root);
22
34
  if (!floor || !field || !input || !listbox) return;
23
35
 
36
+ comboboxSeq += 1;
37
+ const uid = "bw-combobox-" + comboboxSeq;
38
+ if (!listbox.id) listbox.id = uid + "-listbox";
39
+ // Strings for the live region are supplied by the markup (CMB-072), so no
40
+ // English originates here. The empty row's own text covers "No matches".
41
+ const status = $("[data-bw-combobox-status]", root);
42
+
24
43
  field.hidden = false;
25
44
  floor.hidden = true;
26
45
  floor.setAttribute("tabindex", "-1");
27
46
  floor.setAttribute("aria-hidden", "true");
28
47
 
29
48
  const options = () => $$(".bw-combobox__option", listbox);
49
+ const visibleOptions = () => options().filter((o) => !o.hidden);
30
50
  let active = -1;
31
51
 
52
+ // Every option needs a stable id for aria-activedescendant (CMB-051).
53
+ options().forEach((opt, i) => {
54
+ if (!opt.id) opt.id = listbox.id + "-opt-" + i;
55
+ });
56
+
57
+ // The label's `for` targets the hidden floor, so name the enhanced input
58
+ // from the floor's labels (or its aria-label) unless the markup did
59
+ // (CMB-050). A label without an id gets a stable generated one.
60
+ function nameInput() {
61
+ if (input.hasAttribute("aria-labelledby") || input.hasAttribute("aria-label")) return;
62
+ const ids = Array.from(floor.labels || []).map((label, i) => {
63
+ if (!label.id) label.id = (floor.id || uid) + "-label-" + i;
64
+ return label.id;
65
+ });
66
+ if (ids.length) {
67
+ input.setAttribute("aria-labelledby", ids.join(" "));
68
+ } else if (floor.hasAttribute("aria-label")) {
69
+ input.setAttribute("aria-label", floor.getAttribute("aria-label"));
70
+ }
71
+ }
72
+
73
+ function isOpen() {
74
+ return root.hasAttribute("data-bw-open");
75
+ }
32
76
  function open() {
77
+ if (isOpen()) return;
78
+ // Opening shows the full list; typing narrows it from here (the input
79
+ // handler re-applies the filter straight after open()).
80
+ filter("");
33
81
  root.setAttribute("data-bw-open", "");
34
82
  input.setAttribute("aria-expanded", "true");
35
83
  }
84
+ function isDisabled(opt) {
85
+ return opt.getAttribute("aria-disabled") === "true";
86
+ }
87
+ // The next enabled visible option from `from` in direction `step`,
88
+ // wrapping at the ends and skipping disabled rows (CMB-016); -1 when every
89
+ // visible option is disabled.
90
+ function nextEnabled(from, step) {
91
+ const visible = visibleOptions();
92
+ const count = visible.length;
93
+ let index = from;
94
+ for (let tried = 0; tried < count; tried += 1) {
95
+ index = (((index + step) % count) + count) % count;
96
+ if (!isDisabled(visible[index])) return index;
97
+ }
98
+ return -1;
99
+ }
100
+ function setActive(index, scroll) {
101
+ const visible = visibleOptions();
102
+ active = index >= 0 && index < visible.length ? index : -1;
103
+ options().forEach((o) => o.removeAttribute("data-bw-active"));
104
+ const current = visible[active];
105
+ if (current) {
106
+ current.setAttribute("data-bw-active", "");
107
+ if (scroll !== false) current.scrollIntoView({ block: "nearest" });
108
+ input.setAttribute("aria-activedescendant", current.id);
109
+ } else {
110
+ input.removeAttribute("aria-activedescendant");
111
+ }
112
+ }
36
113
  function close() {
37
114
  root.removeAttribute("data-bw-open");
38
115
  input.setAttribute("aria-expanded", "false");
39
- active = -1;
40
- options().forEach((o) => o.removeAttribute("data-bw-active"));
116
+ setActive(-1);
41
117
  }
42
- function filter() {
43
- const q = input.value.trim().toLowerCase();
118
+ // The committed option's label; "" when nothing, or the floor's blank
119
+ // option, is committed (CMB-013).
120
+ function committedLabel() {
121
+ const chosen = floor.options[floor.selectedIndex];
122
+ return chosen && chosen.value ? chosen.textContent.trim() : "";
123
+ }
124
+ // Put the committed label back in the input (CMB-013).
125
+ function restoreText() {
126
+ const label = committedLabel();
127
+ if (input.value !== label) input.value = label;
128
+ }
129
+ // Focus or the pointer leaving without a commit closes the list and
130
+ // restores the label.
131
+ function dismiss() {
132
+ close();
133
+ restoreText();
134
+ }
135
+ function announce(text) {
136
+ if (status && text) status.textContent = text;
137
+ }
138
+ function announceResults(count) {
139
+ if (!status) return;
140
+ if (count === 0) {
141
+ const empty = $(".bw-combobox__empty", listbox);
142
+ announce(empty ? (empty.textContent || "").trim() : "");
143
+ } else if (count === 1) {
144
+ announce(status.getAttribute("data-bw-status-one"));
145
+ } else {
146
+ const other = status.getAttribute("data-bw-status-other") || "";
147
+ announce(other.replace("{count}", String(count)));
148
+ }
149
+ }
150
+ function filter(query) {
151
+ const q = query.trim().toLowerCase();
44
152
  let visible = 0;
45
153
  options().forEach((opt) => {
46
154
  const match = !q || (opt.textContent || "").toLowerCase().includes(q);
@@ -49,67 +157,120 @@
49
157
  });
50
158
  const empty = $(".bw-combobox__empty", listbox);
51
159
  if (empty) empty.hidden = visible > 0;
160
+ return visible;
161
+ }
162
+ function syncSelected() {
163
+ options().forEach((o) => {
164
+ const value = o.getAttribute("data-bw-value") || o.textContent.trim();
165
+ o.setAttribute("aria-selected", String(value !== "" && value === floor.value));
166
+ });
52
167
  }
53
168
  function select(opt) {
54
- if (!opt || opt.getAttribute("aria-disabled") === "true") return;
169
+ if (!opt || isDisabled(opt)) return;
55
170
  const value = opt.getAttribute("data-bw-value") || opt.textContent.trim();
171
+ const before = floor.value;
56
172
  floor.value = value;
57
- floor.dispatchEvent(new Event("change", { bubbles: true }));
58
- options().forEach((o) => o.setAttribute("aria-selected", String(o === opt)));
173
+ // A bubbling native change, only when the selection really changed.
174
+ if (floor.value !== before) floor.dispatchEvent(new Event("change", { bubbles: true }));
175
+ syncSelected();
59
176
  input.value = opt.textContent.trim();
177
+ // A committed choice leaves every option available for the next open.
178
+ filter("");
60
179
  close();
61
180
  }
62
181
 
182
+ nameInput();
63
183
  input.setAttribute("role", "combobox");
64
184
  input.setAttribute("aria-expanded", "false");
65
- input.setAttribute("aria-controls", listbox.id || "bw-listbox");
185
+ input.setAttribute("aria-controls", listbox.id);
186
+ input.setAttribute("aria-autocomplete", "list");
66
187
  input.setAttribute("autocomplete", "off");
67
188
 
68
- input.addEventListener("focus", () => {
69
- open();
70
- filter();
71
- });
189
+ // A click or tap opens the list; keyboard focus alone never does
190
+ // (CMB-022). Opening does not filter: the full list shows until typing.
191
+ input.addEventListener("click", open);
72
192
  input.addEventListener("input", () => {
73
193
  open();
74
- filter();
194
+ const count = filter(input.value);
195
+ setActive(-1);
196
+ announceResults(count);
75
197
  });
76
198
  input.addEventListener("keydown", (e) => {
77
- const visible = options().filter((o) => !o.hidden);
78
199
  if (e.key === "ArrowDown") {
79
200
  e.preventDefault();
80
- open();
81
- active = Math.min(active + 1, visible.length - 1);
201
+ if (!isOpen()) {
202
+ open();
203
+ // Alt+ArrowDown opens and leaves the active option unchanged.
204
+ if (e.altKey) return;
205
+ // Read after open(), which resets the filter.
206
+ const visible = visibleOptions();
207
+ const selectedIndex = visible.findIndex((o) => o.getAttribute("aria-selected") === "true");
208
+ const current = visible[selectedIndex];
209
+ setActive(current && !isDisabled(current) ? selectedIndex : nextEnabled(-1, 1));
210
+ } else if (!e.altKey) {
211
+ const next = nextEnabled(active, 1);
212
+ if (next !== -1) setActive(next);
213
+ }
82
214
  } else if (e.key === "ArrowUp") {
83
215
  e.preventDefault();
84
- active = Math.max(active - 1, 0);
216
+ if (!isOpen()) {
217
+ open();
218
+ setActive(nextEnabled(0, -1));
219
+ } else {
220
+ const next = nextEnabled(active === -1 ? 0 : active, -1);
221
+ if (next !== -1) setActive(next);
222
+ }
85
223
  } else if (e.key === "Enter") {
86
- e.preventDefault();
87
- if (active >= 0) select(visible[active]);
224
+ // Enter only acts on an open list with an active option; otherwise the
225
+ // form submit proceeds.
226
+ if (isOpen() && active >= 0) {
227
+ e.preventDefault();
228
+ select(visibleOptions()[active]);
229
+ }
88
230
  } else if (e.key === "Escape") {
89
- close();
90
- } else {
91
- return;
92
- }
93
- visible.forEach((o, i) => {
94
- if (i === active) {
95
- o.setAttribute("data-bw-active", "");
96
- o.scrollIntoView({ block: "nearest" });
97
- input.setAttribute("aria-activedescendant", o.id || "");
98
- } else {
99
- o.removeAttribute("data-bw-active");
231
+ if (isOpen()) {
232
+ // Closes and leaves the text as typed; blur or Tab restores it.
233
+ e.preventDefault();
234
+ close();
235
+ } else if (input.value !== committedLabel()) {
236
+ // Closed with an uncommitted edit: revert it. An Escape with
237
+ // nothing to revert is left for the page (a modal still closes).
238
+ e.preventDefault();
239
+ restoreText();
100
240
  }
101
- });
241
+ } else if (e.key === "Tab") {
242
+ // Close without choosing; the default Tab continues the sequence.
243
+ dismiss();
244
+ }
245
+ });
246
+ input.addEventListener("blur", (e) => {
247
+ if (e.relatedTarget && root.contains(e.relatedTarget)) return;
248
+ dismiss();
249
+ });
250
+ // A press on an option must not blur the input: focus stays on the input
251
+ // and aria-activedescendant carries the active option.
252
+ listbox.addEventListener("mousedown", (e) => e.preventDefault());
253
+ // One highlight (CMB-023): moving the pointer over an enabled option makes
254
+ // it the active option. A synthetic mousemove from a keyboard scroll has a
255
+ // zero delta and is ignored.
256
+ listbox.addEventListener("mousemove", (e) => {
257
+ if (e.movementX === 0 && e.movementY === 0) return;
258
+ const opt = e.target.closest(".bw-combobox__option");
259
+ if (!opt || isDisabled(opt) || opt.hasAttribute("data-bw-active")) return;
260
+ const index = visibleOptions().indexOf(opt);
261
+ if (index !== -1) setActive(index, false);
102
262
  });
103
263
  listbox.addEventListener("click", (e) => {
104
264
  const opt = e.target.closest(".bw-combobox__option");
105
265
  if (opt) select(opt);
106
266
  });
107
267
  document.addEventListener("pointerdown", (e) => {
108
- if (!root.contains(e.target)) close();
268
+ if (!root.contains(e.target)) dismiss();
109
269
  });
110
270
 
111
271
  const selected = floor.options[floor.selectedIndex];
112
272
  if (selected && selected.value) input.value = selected.textContent.trim();
273
+ syncSelected();
113
274
  }
114
275
 
115
276
  /* ---- Date picker ---- */
@@ -378,7 +539,7 @@
378
539
  toast.setAttribute("data-bw-toast", "");
379
540
  toast.innerHTML =
380
541
  '<p class="bw-toast__message"></p>' +
381
- '<button type="button" class="bw-toast__close bw-btn bw-btn--ghost bw-btn--sm bw-btn--icon-only" aria-label="Dismiss">x</button>';
542
+ '<button type="button" class="bw-toast__close bw-btn bw-btn--ghost bw-btn--size-sm bw-btn--icon-only" aria-label="Dismiss">x</button>';
382
543
  $(".bw-toast__message", toast).textContent = message;
383
544
  region.prepend(toast);
384
545
  requestAnimationFrame(() => toast.setAttribute("data-bw-visible", ""));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@bwui/js",
3
- "version": "0.1.3",
3
+ "version": "0.1.4",
4
4
  "description": "Progressive-enhancement behaviours for Brickwork HTML gallery markup (combobox, date picker, CMDK, modal, toast).",
5
5
  "license": "MIT",
6
6
  "type": "module",
package/src/behaviours.js CHANGED
@@ -14,6 +14,18 @@
14
14
  }
15
15
 
16
16
  /* ---- Combobox ---- */
17
+ // Wave-1 HTML delivery of docs/components/combobox.md (CMB-074): single mode,
18
+ // client filtering. Click opens (CMB-022), the input is named from the
19
+ // floor's label (CMB-050), the ARIA and active-descendant set is complete
20
+ // (CMB-010, CMB-051), a polite status region announces results (CMB-052)
21
+ // and a real selection change fires a bubbling `change` on the floor
22
+ // (CMB-012). The keyboard map of section 5.2 holds as well: arrows skip
23
+ // disabled options and wrap at the ends (CMB-016), Tab closes without
24
+ // choosing, Escape closes and then reverts an uncommitted edit, and focus
25
+ // leaving without a commit restores the committed label (CMB-013). Multiple,
26
+ // server filtering, create, groups and clear stay Django-first.
27
+ let comboboxSeq = 0;
28
+
17
29
  function initCombobox(root) {
18
30
  const floor = $(".bw-combobox__floor", root);
19
31
  const field = $(".bw-combobox__field", root);
@@ -21,26 +33,122 @@
21
33
  const listbox = $(".bw-combobox__listbox", root);
22
34
  if (!floor || !field || !input || !listbox) return;
23
35
 
36
+ comboboxSeq += 1;
37
+ const uid = "bw-combobox-" + comboboxSeq;
38
+ if (!listbox.id) listbox.id = uid + "-listbox";
39
+ // Strings for the live region are supplied by the markup (CMB-072), so no
40
+ // English originates here. The empty row's own text covers "No matches".
41
+ const status = $("[data-bw-combobox-status]", root);
42
+
24
43
  field.hidden = false;
25
44
  floor.hidden = true;
26
45
  floor.setAttribute("tabindex", "-1");
27
46
  floor.setAttribute("aria-hidden", "true");
28
47
 
29
48
  const options = () => $$(".bw-combobox__option", listbox);
49
+ const visibleOptions = () => options().filter((o) => !o.hidden);
30
50
  let active = -1;
31
51
 
52
+ // Every option needs a stable id for aria-activedescendant (CMB-051).
53
+ options().forEach((opt, i) => {
54
+ if (!opt.id) opt.id = listbox.id + "-opt-" + i;
55
+ });
56
+
57
+ // The label's `for` targets the hidden floor, so name the enhanced input
58
+ // from the floor's labels (or its aria-label) unless the markup did
59
+ // (CMB-050). A label without an id gets a stable generated one.
60
+ function nameInput() {
61
+ if (input.hasAttribute("aria-labelledby") || input.hasAttribute("aria-label")) return;
62
+ const ids = Array.from(floor.labels || []).map((label, i) => {
63
+ if (!label.id) label.id = (floor.id || uid) + "-label-" + i;
64
+ return label.id;
65
+ });
66
+ if (ids.length) {
67
+ input.setAttribute("aria-labelledby", ids.join(" "));
68
+ } else if (floor.hasAttribute("aria-label")) {
69
+ input.setAttribute("aria-label", floor.getAttribute("aria-label"));
70
+ }
71
+ }
72
+
73
+ function isOpen() {
74
+ return root.hasAttribute("data-bw-open");
75
+ }
32
76
  function open() {
77
+ if (isOpen()) return;
78
+ // Opening shows the full list; typing narrows it from here (the input
79
+ // handler re-applies the filter straight after open()).
80
+ filter("");
33
81
  root.setAttribute("data-bw-open", "");
34
82
  input.setAttribute("aria-expanded", "true");
35
83
  }
84
+ function isDisabled(opt) {
85
+ return opt.getAttribute("aria-disabled") === "true";
86
+ }
87
+ // The next enabled visible option from `from` in direction `step`,
88
+ // wrapping at the ends and skipping disabled rows (CMB-016); -1 when every
89
+ // visible option is disabled.
90
+ function nextEnabled(from, step) {
91
+ const visible = visibleOptions();
92
+ const count = visible.length;
93
+ let index = from;
94
+ for (let tried = 0; tried < count; tried += 1) {
95
+ index = (((index + step) % count) + count) % count;
96
+ if (!isDisabled(visible[index])) return index;
97
+ }
98
+ return -1;
99
+ }
100
+ function setActive(index, scroll) {
101
+ const visible = visibleOptions();
102
+ active = index >= 0 && index < visible.length ? index : -1;
103
+ options().forEach((o) => o.removeAttribute("data-bw-active"));
104
+ const current = visible[active];
105
+ if (current) {
106
+ current.setAttribute("data-bw-active", "");
107
+ if (scroll !== false) current.scrollIntoView({ block: "nearest" });
108
+ input.setAttribute("aria-activedescendant", current.id);
109
+ } else {
110
+ input.removeAttribute("aria-activedescendant");
111
+ }
112
+ }
36
113
  function close() {
37
114
  root.removeAttribute("data-bw-open");
38
115
  input.setAttribute("aria-expanded", "false");
39
- active = -1;
40
- options().forEach((o) => o.removeAttribute("data-bw-active"));
116
+ setActive(-1);
41
117
  }
42
- function filter() {
43
- const q = input.value.trim().toLowerCase();
118
+ // The committed option's label; "" when nothing, or the floor's blank
119
+ // option, is committed (CMB-013).
120
+ function committedLabel() {
121
+ const chosen = floor.options[floor.selectedIndex];
122
+ return chosen && chosen.value ? chosen.textContent.trim() : "";
123
+ }
124
+ // Put the committed label back in the input (CMB-013).
125
+ function restoreText() {
126
+ const label = committedLabel();
127
+ if (input.value !== label) input.value = label;
128
+ }
129
+ // Focus or the pointer leaving without a commit closes the list and
130
+ // restores the label.
131
+ function dismiss() {
132
+ close();
133
+ restoreText();
134
+ }
135
+ function announce(text) {
136
+ if (status && text) status.textContent = text;
137
+ }
138
+ function announceResults(count) {
139
+ if (!status) return;
140
+ if (count === 0) {
141
+ const empty = $(".bw-combobox__empty", listbox);
142
+ announce(empty ? (empty.textContent || "").trim() : "");
143
+ } else if (count === 1) {
144
+ announce(status.getAttribute("data-bw-status-one"));
145
+ } else {
146
+ const other = status.getAttribute("data-bw-status-other") || "";
147
+ announce(other.replace("{count}", String(count)));
148
+ }
149
+ }
150
+ function filter(query) {
151
+ const q = query.trim().toLowerCase();
44
152
  let visible = 0;
45
153
  options().forEach((opt) => {
46
154
  const match = !q || (opt.textContent || "").toLowerCase().includes(q);
@@ -49,67 +157,120 @@
49
157
  });
50
158
  const empty = $(".bw-combobox__empty", listbox);
51
159
  if (empty) empty.hidden = visible > 0;
160
+ return visible;
161
+ }
162
+ function syncSelected() {
163
+ options().forEach((o) => {
164
+ const value = o.getAttribute("data-bw-value") || o.textContent.trim();
165
+ o.setAttribute("aria-selected", String(value !== "" && value === floor.value));
166
+ });
52
167
  }
53
168
  function select(opt) {
54
- if (!opt || opt.getAttribute("aria-disabled") === "true") return;
169
+ if (!opt || isDisabled(opt)) return;
55
170
  const value = opt.getAttribute("data-bw-value") || opt.textContent.trim();
171
+ const before = floor.value;
56
172
  floor.value = value;
57
- floor.dispatchEvent(new Event("change", { bubbles: true }));
58
- options().forEach((o) => o.setAttribute("aria-selected", String(o === opt)));
173
+ // A bubbling native change, only when the selection really changed.
174
+ if (floor.value !== before) floor.dispatchEvent(new Event("change", { bubbles: true }));
175
+ syncSelected();
59
176
  input.value = opt.textContent.trim();
177
+ // A committed choice leaves every option available for the next open.
178
+ filter("");
60
179
  close();
61
180
  }
62
181
 
182
+ nameInput();
63
183
  input.setAttribute("role", "combobox");
64
184
  input.setAttribute("aria-expanded", "false");
65
- input.setAttribute("aria-controls", listbox.id || "bw-listbox");
185
+ input.setAttribute("aria-controls", listbox.id);
186
+ input.setAttribute("aria-autocomplete", "list");
66
187
  input.setAttribute("autocomplete", "off");
67
188
 
68
- input.addEventListener("focus", () => {
69
- open();
70
- filter();
71
- });
189
+ // A click or tap opens the list; keyboard focus alone never does
190
+ // (CMB-022). Opening does not filter: the full list shows until typing.
191
+ input.addEventListener("click", open);
72
192
  input.addEventListener("input", () => {
73
193
  open();
74
- filter();
194
+ const count = filter(input.value);
195
+ setActive(-1);
196
+ announceResults(count);
75
197
  });
76
198
  input.addEventListener("keydown", (e) => {
77
- const visible = options().filter((o) => !o.hidden);
78
199
  if (e.key === "ArrowDown") {
79
200
  e.preventDefault();
80
- open();
81
- active = Math.min(active + 1, visible.length - 1);
201
+ if (!isOpen()) {
202
+ open();
203
+ // Alt+ArrowDown opens and leaves the active option unchanged.
204
+ if (e.altKey) return;
205
+ // Read after open(), which resets the filter.
206
+ const visible = visibleOptions();
207
+ const selectedIndex = visible.findIndex((o) => o.getAttribute("aria-selected") === "true");
208
+ const current = visible[selectedIndex];
209
+ setActive(current && !isDisabled(current) ? selectedIndex : nextEnabled(-1, 1));
210
+ } else if (!e.altKey) {
211
+ const next = nextEnabled(active, 1);
212
+ if (next !== -1) setActive(next);
213
+ }
82
214
  } else if (e.key === "ArrowUp") {
83
215
  e.preventDefault();
84
- active = Math.max(active - 1, 0);
216
+ if (!isOpen()) {
217
+ open();
218
+ setActive(nextEnabled(0, -1));
219
+ } else {
220
+ const next = nextEnabled(active === -1 ? 0 : active, -1);
221
+ if (next !== -1) setActive(next);
222
+ }
85
223
  } else if (e.key === "Enter") {
86
- e.preventDefault();
87
- if (active >= 0) select(visible[active]);
224
+ // Enter only acts on an open list with an active option; otherwise the
225
+ // form submit proceeds.
226
+ if (isOpen() && active >= 0) {
227
+ e.preventDefault();
228
+ select(visibleOptions()[active]);
229
+ }
88
230
  } else if (e.key === "Escape") {
89
- close();
90
- } else {
91
- return;
92
- }
93
- visible.forEach((o, i) => {
94
- if (i === active) {
95
- o.setAttribute("data-bw-active", "");
96
- o.scrollIntoView({ block: "nearest" });
97
- input.setAttribute("aria-activedescendant", o.id || "");
98
- } else {
99
- o.removeAttribute("data-bw-active");
231
+ if (isOpen()) {
232
+ // Closes and leaves the text as typed; blur or Tab restores it.
233
+ e.preventDefault();
234
+ close();
235
+ } else if (input.value !== committedLabel()) {
236
+ // Closed with an uncommitted edit: revert it. An Escape with
237
+ // nothing to revert is left for the page (a modal still closes).
238
+ e.preventDefault();
239
+ restoreText();
100
240
  }
101
- });
241
+ } else if (e.key === "Tab") {
242
+ // Close without choosing; the default Tab continues the sequence.
243
+ dismiss();
244
+ }
245
+ });
246
+ input.addEventListener("blur", (e) => {
247
+ if (e.relatedTarget && root.contains(e.relatedTarget)) return;
248
+ dismiss();
249
+ });
250
+ // A press on an option must not blur the input: focus stays on the input
251
+ // and aria-activedescendant carries the active option.
252
+ listbox.addEventListener("mousedown", (e) => e.preventDefault());
253
+ // One highlight (CMB-023): moving the pointer over an enabled option makes
254
+ // it the active option. A synthetic mousemove from a keyboard scroll has a
255
+ // zero delta and is ignored.
256
+ listbox.addEventListener("mousemove", (e) => {
257
+ if (e.movementX === 0 && e.movementY === 0) return;
258
+ const opt = e.target.closest(".bw-combobox__option");
259
+ if (!opt || isDisabled(opt) || opt.hasAttribute("data-bw-active")) return;
260
+ const index = visibleOptions().indexOf(opt);
261
+ if (index !== -1) setActive(index, false);
102
262
  });
103
263
  listbox.addEventListener("click", (e) => {
104
264
  const opt = e.target.closest(".bw-combobox__option");
105
265
  if (opt) select(opt);
106
266
  });
107
267
  document.addEventListener("pointerdown", (e) => {
108
- if (!root.contains(e.target)) close();
268
+ if (!root.contains(e.target)) dismiss();
109
269
  });
110
270
 
111
271
  const selected = floor.options[floor.selectedIndex];
112
272
  if (selected && selected.value) input.value = selected.textContent.trim();
273
+ syncSelected();
113
274
  }
114
275
 
115
276
  /* ---- Date picker ---- */
@@ -378,7 +539,7 @@
378
539
  toast.setAttribute("data-bw-toast", "");
379
540
  toast.innerHTML =
380
541
  '<p class="bw-toast__message"></p>' +
381
- '<button type="button" class="bw-toast__close bw-btn bw-btn--ghost bw-btn--sm bw-btn--icon-only" aria-label="Dismiss">x</button>';
542
+ '<button type="button" class="bw-toast__close bw-btn bw-btn--ghost bw-btn--size-sm bw-btn--icon-only" aria-label="Dismiss">x</button>';
382
543
  $(".bw-toast__message", toast).textContent = message;
383
544
  region.prepend(toast);
384
545
  requestAnimationFrame(() => toast.setAttribute("data-bw-visible", ""));