@bwui/js 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Nigel Copley
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -1,3 +1,15 @@
1
- # Temporary Holding Version
1
+ # `@bwui/js`
2
2
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
3
+ Progressive-enhancement pack for Brickwork HTML. Link `@bwui/css`, paste
4
+ gallery markup, then load this script. React and Vue adapters replace these
5
+ behaviours with framework components; Django ships equivalent JS in
6
+ `django-brickwork`.
7
+
8
+ ```html
9
+ <link rel="stylesheet" href="node_modules/@bwui/css/dist/brickwork.css" />
10
+ <script defer src="node_modules/@bwui/js/dist/behaviours.js"></script>
11
+ ```
12
+
13
+ ```bash
14
+ npm install @bwui/css @bwui/js
15
+ ```
@@ -0,0 +1,512 @@
1
+ /*!
2
+ * Brickwork gallery HTML behaviours (Wave B).
3
+ * Progressive enhancement for combobox, date picker, CMDK, modal, toast, tags, OTP.
4
+ * Copy with gallery pages; stack deliveries may replace with framework adapters.
5
+ */
6
+ (function () {
7
+ "use strict";
8
+
9
+ function $(sel, root) {
10
+ return (root || document).querySelector(sel);
11
+ }
12
+ function $$(sel, root) {
13
+ return Array.from((root || document).querySelectorAll(sel));
14
+ }
15
+
16
+ /* ---- Combobox ---- */
17
+ function initCombobox(root) {
18
+ const floor = $(".bw-combobox__floor", root);
19
+ const field = $(".bw-combobox__field", root);
20
+ const input = $(".bw-combobox__input", root);
21
+ const listbox = $(".bw-combobox__listbox", root);
22
+ if (!floor || !field || !input || !listbox) return;
23
+
24
+ field.hidden = false;
25
+ floor.hidden = true;
26
+ floor.setAttribute("tabindex", "-1");
27
+ floor.setAttribute("aria-hidden", "true");
28
+
29
+ const options = () => $$(".bw-combobox__option", listbox);
30
+ let active = -1;
31
+
32
+ function open() {
33
+ root.setAttribute("data-bw-open", "");
34
+ input.setAttribute("aria-expanded", "true");
35
+ }
36
+ function close() {
37
+ root.removeAttribute("data-bw-open");
38
+ input.setAttribute("aria-expanded", "false");
39
+ active = -1;
40
+ options().forEach((o) => o.removeAttribute("data-bw-active"));
41
+ }
42
+ function filter() {
43
+ const q = input.value.trim().toLowerCase();
44
+ let visible = 0;
45
+ options().forEach((opt) => {
46
+ const match = !q || (opt.textContent || "").toLowerCase().includes(q);
47
+ opt.hidden = !match;
48
+ if (match) visible += 1;
49
+ });
50
+ const empty = $(".bw-combobox__empty", listbox);
51
+ if (empty) empty.hidden = visible > 0;
52
+ }
53
+ function select(opt) {
54
+ if (!opt || opt.getAttribute("aria-disabled") === "true") return;
55
+ const value = opt.getAttribute("data-bw-value") || opt.textContent.trim();
56
+ floor.value = value;
57
+ floor.dispatchEvent(new Event("change", { bubbles: true }));
58
+ options().forEach((o) => o.setAttribute("aria-selected", String(o === opt)));
59
+ input.value = opt.textContent.trim();
60
+ close();
61
+ }
62
+
63
+ input.setAttribute("role", "combobox");
64
+ input.setAttribute("aria-expanded", "false");
65
+ input.setAttribute("aria-controls", listbox.id || "bw-listbox");
66
+ input.setAttribute("autocomplete", "off");
67
+
68
+ input.addEventListener("focus", () => {
69
+ open();
70
+ filter();
71
+ });
72
+ input.addEventListener("input", () => {
73
+ open();
74
+ filter();
75
+ });
76
+ input.addEventListener("keydown", (e) => {
77
+ const visible = options().filter((o) => !o.hidden);
78
+ if (e.key === "ArrowDown") {
79
+ e.preventDefault();
80
+ open();
81
+ active = Math.min(active + 1, visible.length - 1);
82
+ } else if (e.key === "ArrowUp") {
83
+ e.preventDefault();
84
+ active = Math.max(active - 1, 0);
85
+ } else if (e.key === "Enter") {
86
+ e.preventDefault();
87
+ if (active >= 0) select(visible[active]);
88
+ } 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");
100
+ }
101
+ });
102
+ });
103
+ listbox.addEventListener("click", (e) => {
104
+ const opt = e.target.closest(".bw-combobox__option");
105
+ if (opt) select(opt);
106
+ });
107
+ document.addEventListener("pointerdown", (e) => {
108
+ if (!root.contains(e.target)) close();
109
+ });
110
+
111
+ const selected = floor.options[floor.selectedIndex];
112
+ if (selected && selected.value) input.value = selected.textContent.trim();
113
+ }
114
+
115
+ /* ---- Date picker ---- */
116
+ const MONTHS = [
117
+ "January", "February", "March", "April", "May", "June",
118
+ "July", "August", "September", "October", "November", "December",
119
+ ];
120
+
121
+ function parseISO(s) {
122
+ if (!s) return null;
123
+ const m = /^(\d{4})-(\d{2})-(\d{2})$/.exec(s);
124
+ if (!m) return null;
125
+ return new Date(Number(m[1]), Number(m[2]) - 1, Number(m[3]));
126
+ }
127
+ function toISO(d) {
128
+ const y = d.getFullYear();
129
+ const m = String(d.getMonth() + 1).padStart(2, "0");
130
+ const day = String(d.getDate()).padStart(2, "0");
131
+ return y + "-" + m + "-" + day;
132
+ }
133
+ function sameDay(a, b) {
134
+ return a && b && a.getFullYear() === b.getFullYear() && a.getMonth() === b.getMonth() && a.getDate() === b.getDate();
135
+ }
136
+
137
+ function initDatePicker(root) {
138
+ const input = $(".bw-date-picker-chrome__field input, input[data-bw-date-input]", root);
139
+ const trigger = $(".bw-date-picker-chrome__trigger", root);
140
+ const panel = $(".bw-date-picker-chrome__panel", root);
141
+ const calendar = $(".bw-calendar", root);
142
+ if (!input || !panel || !calendar) return;
143
+
144
+ let view = parseISO(input.value) || new Date();
145
+ view = new Date(view.getFullYear(), view.getMonth(), 1);
146
+ const rangeMode = root.getAttribute("data-bw-range") === "true";
147
+ let rangeStart = parseISO(input.value);
148
+ let rangeEnd = parseISO(root.getAttribute("data-bw-end") || "");
149
+
150
+ function render() {
151
+ const title = $(".bw-calendar__title", calendar);
152
+ const grid = $(".bw-calendar__grid", calendar);
153
+ if (!title || !grid) return;
154
+ title.textContent = MONTHS[view.getMonth()] + " " + view.getFullYear();
155
+ grid.innerHTML = "";
156
+ ["Mo", "Tu", "We", "Th", "Fr", "Sa", "Su"].forEach((d) => {
157
+ const el = document.createElement("div");
158
+ el.className = "bw-calendar__dow";
159
+ el.textContent = d;
160
+ grid.appendChild(el);
161
+ });
162
+ const first = new Date(view.getFullYear(), view.getMonth(), 1);
163
+ let startDow = (first.getDay() + 6) % 7;
164
+ for (let i = 0; i < startDow; i++) {
165
+ const blank = document.createElement("div");
166
+ grid.appendChild(blank);
167
+ }
168
+ const days = new Date(view.getFullYear(), view.getMonth() + 1, 0).getDate();
169
+ for (let day = 1; day <= days; day++) {
170
+ const date = new Date(view.getFullYear(), view.getMonth(), day);
171
+ const btn = document.createElement("button");
172
+ btn.type = "button";
173
+ btn.className = "bw-calendar__day";
174
+ btn.textContent = String(day);
175
+ btn.setAttribute("aria-label", toISO(date));
176
+ if (!rangeMode && sameDay(date, parseISO(input.value))) {
177
+ btn.setAttribute("aria-selected", "true");
178
+ }
179
+ if (rangeMode && rangeStart && rangeEnd) {
180
+ if (sameDay(date, rangeStart) || sameDay(date, rangeEnd)) btn.setAttribute("aria-selected", "true");
181
+ else if (date > rangeStart && date < rangeEnd) btn.setAttribute("data-bw-in-range", "");
182
+ } else if (rangeMode && rangeStart && sameDay(date, rangeStart)) {
183
+ btn.setAttribute("aria-selected", "true");
184
+ }
185
+ btn.addEventListener("click", () => {
186
+ if (!rangeMode) {
187
+ input.value = toISO(date);
188
+ input.dispatchEvent(new Event("change", { bubbles: true }));
189
+ panel.hidden = true;
190
+ render();
191
+ return;
192
+ }
193
+ if (!rangeStart || (rangeStart && rangeEnd)) {
194
+ rangeStart = date;
195
+ rangeEnd = null;
196
+ } else if (date < rangeStart) {
197
+ rangeEnd = rangeStart;
198
+ rangeStart = date;
199
+ } else {
200
+ rangeEnd = date;
201
+ }
202
+ input.value = toISO(rangeStart);
203
+ const endInput = $("[data-bw-date-end]", root);
204
+ if (endInput) endInput.value = rangeEnd ? toISO(rangeEnd) : "";
205
+ if (rangeEnd) panel.hidden = true;
206
+ render();
207
+ });
208
+ grid.appendChild(btn);
209
+ }
210
+ }
211
+
212
+ function open() {
213
+ panel.hidden = false;
214
+ render();
215
+ }
216
+ function close() {
217
+ panel.hidden = true;
218
+ }
219
+
220
+ if (trigger) trigger.addEventListener("click", () => (panel.hidden ? open() : close()));
221
+ input.addEventListener("focus", open);
222
+ calendar.addEventListener("click", (e) => {
223
+ const prev = e.target.closest("[data-bw-cal-prev]");
224
+ const next = e.target.closest("[data-bw-cal-next]");
225
+ if (prev) {
226
+ view = new Date(view.getFullYear(), view.getMonth() - 1, 1);
227
+ render();
228
+ }
229
+ if (next) {
230
+ view = new Date(view.getFullYear(), view.getMonth() + 1, 1);
231
+ render();
232
+ }
233
+ });
234
+ document.addEventListener("pointerdown", (e) => {
235
+ if (!root.contains(e.target)) close();
236
+ });
237
+ document.addEventListener("keydown", (e) => {
238
+ if (e.key === "Escape" && !panel.hidden) close();
239
+ });
240
+ panel.hidden = true;
241
+ render();
242
+ }
243
+
244
+ /* ---- CMDK ---- */
245
+ function initCmdk(root) {
246
+ const input = $(".bw-cmdk__input", root);
247
+ const items = () => $$(".bw-cmdk__item", root).filter((el) => !el.hidden);
248
+ let active = 0;
249
+
250
+ function setActive(i) {
251
+ const list = items();
252
+ active = Math.max(0, Math.min(i, list.length - 1));
253
+ list.forEach((el, idx) => {
254
+ if (idx === active) {
255
+ el.setAttribute("data-bw-active", "");
256
+ el.scrollIntoView({ block: "nearest" });
257
+ } else el.removeAttribute("data-bw-active");
258
+ });
259
+ }
260
+ function open() {
261
+ root.setAttribute("data-bw-open", "");
262
+ root.setAttribute("aria-hidden", "false");
263
+ if (input) {
264
+ input.value = "";
265
+ filter();
266
+ input.focus();
267
+ }
268
+ setActive(0);
269
+ }
270
+ function close() {
271
+ root.removeAttribute("data-bw-open");
272
+ root.setAttribute("aria-hidden", "true");
273
+ }
274
+ function filter() {
275
+ const q = (input && input.value.trim().toLowerCase()) || "";
276
+ $$(".bw-cmdk__item", root).forEach((el) => {
277
+ const text = (el.textContent || "").toLowerCase();
278
+ el.hidden = Boolean(q) && !text.includes(q);
279
+ });
280
+ const empty = $(".bw-cmdk__empty", root);
281
+ if (empty) empty.hidden = items().length > 0;
282
+ setActive(0);
283
+ }
284
+ function run() {
285
+ const list = items();
286
+ const el = list[active];
287
+ if (!el) return;
288
+ const href = el.getAttribute("data-bw-href") || el.getAttribute("href");
289
+ close();
290
+ if (href) window.location.href = href;
291
+ el.dispatchEvent(new CustomEvent("bw-cmdk-select", { bubbles: true, detail: { id: el.id } }));
292
+ }
293
+
294
+ root.setAttribute("aria-hidden", "true");
295
+ $$(".bw-cmdk__scrim", root).forEach((s) => s.addEventListener("click", close));
296
+ if (input) {
297
+ input.addEventListener("input", filter);
298
+ input.addEventListener("keydown", (e) => {
299
+ if (e.key === "ArrowDown") {
300
+ e.preventDefault();
301
+ setActive(active + 1);
302
+ } else if (e.key === "ArrowUp") {
303
+ e.preventDefault();
304
+ setActive(active - 1);
305
+ } else if (e.key === "Enter") {
306
+ e.preventDefault();
307
+ run();
308
+ } else if (e.key === "Escape") {
309
+ e.preventDefault();
310
+ close();
311
+ }
312
+ });
313
+ }
314
+ $$(".bw-cmdk__item", root).forEach((el, idx) => {
315
+ el.setAttribute("role", "option");
316
+ el.addEventListener("click", () => {
317
+ active = items().indexOf(el);
318
+ run();
319
+ });
320
+ el.addEventListener("mousemove", () => setActive(items().indexOf(el)));
321
+ });
322
+
323
+ document.addEventListener("keydown", (e) => {
324
+ if ((e.metaKey || e.ctrlKey) && e.key.toLowerCase() === "k") {
325
+ e.preventDefault();
326
+ root.hasAttribute("data-bw-open") ? close() : open();
327
+ }
328
+ });
329
+ $$("[data-bw-cmdk-open]", document).forEach((btn) =>
330
+ btn.addEventListener("click", (e) => {
331
+ e.preventDefault();
332
+ open();
333
+ }),
334
+ );
335
+
336
+ window.bwCmdk = { open, close };
337
+ }
338
+
339
+ /* ---- Modal ---- */
340
+ function initModal(root) {
341
+ function open() {
342
+ root.setAttribute("data-bw-open", "");
343
+ root.classList.add("bw-modal--open");
344
+ }
345
+ function close() {
346
+ root.removeAttribute("data-bw-open");
347
+ root.classList.remove("bw-modal--open");
348
+ }
349
+ $$(".bw-modal__scrim, .bw-modal__close, [data-bw-modal-close]", root).forEach((el) =>
350
+ el.addEventListener("click", close),
351
+ );
352
+ const id = root.id;
353
+ if (id) {
354
+ $$('[data-bw-modal-open="' + id + '"]', document).forEach((btn) =>
355
+ btn.addEventListener("click", (e) => {
356
+ e.preventDefault();
357
+ open();
358
+ }),
359
+ );
360
+ }
361
+ document.addEventListener("keydown", (e) => {
362
+ if (e.key === "Escape" && root.hasAttribute("data-bw-open")) close();
363
+ });
364
+ }
365
+
366
+ /* ---- Toast ---- */
367
+ function showToast(message, variant) {
368
+ const region = $("#bw-toast-region") || (function () {
369
+ const el = document.createElement("div");
370
+ el.id = "bw-toast-region";
371
+ el.className = "bw-toast-region bw-toast-region--top-end";
372
+ el.setAttribute("aria-live", "polite");
373
+ document.body.appendChild(el);
374
+ return el;
375
+ })();
376
+ const toast = document.createElement("div");
377
+ toast.className = "bw-toast" + (variant ? " bw-toast--" + variant : "");
378
+ toast.setAttribute("data-bw-toast", "");
379
+ toast.innerHTML =
380
+ '<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>';
382
+ $(".bw-toast__message", toast).textContent = message;
383
+ region.prepend(toast);
384
+ requestAnimationFrame(() => toast.setAttribute("data-bw-visible", ""));
385
+ const dismiss = () => {
386
+ toast.removeAttribute("data-bw-visible");
387
+ setTimeout(() => toast.remove(), 200);
388
+ };
389
+ $(".bw-toast__close", toast).addEventListener("click", dismiss);
390
+ setTimeout(dismiss, 4000);
391
+ }
392
+ window.bwToast = showToast;
393
+
394
+ /* ---- Tags ---- */
395
+ function initTags(root) {
396
+ const input = $(".bw-tags__input", root);
397
+ if (!input) return;
398
+ function add(value) {
399
+ const v = value.trim();
400
+ if (!v) return;
401
+ const chip = document.createElement("span");
402
+ chip.className = "bw-tags__chip";
403
+ chip.innerHTML =
404
+ '<span></span><button type="button" class="bw-tags__chip-remove" aria-label="Remove">x</button>';
405
+ chip.firstElementChild.textContent = v;
406
+ $(".bw-tags__chip-remove", chip).addEventListener("click", () => chip.remove());
407
+ root.insertBefore(chip, input);
408
+ input.value = "";
409
+ }
410
+ input.addEventListener("keydown", (e) => {
411
+ if (e.key === "Enter" || e.key === ",") {
412
+ e.preventDefault();
413
+ add(input.value.replace(/,/g, ""));
414
+ } else if (e.key === "Backspace" && !input.value) {
415
+ const chips = $$(".bw-tags__chip", root);
416
+ const last = chips[chips.length - 1];
417
+ if (last) last.remove();
418
+ }
419
+ });
420
+ }
421
+
422
+ /* ---- OTP ---- */
423
+ function initOtp(root) {
424
+ const digits = $$(".bw-otp__digit", root);
425
+ digits.forEach((el, i) => {
426
+ el.addEventListener("input", () => {
427
+ el.value = el.value.replace(/\D/g, "").slice(-1);
428
+ if (el.value && digits[i + 1]) digits[i + 1].focus();
429
+ });
430
+ el.addEventListener("keydown", (e) => {
431
+ if (e.key === "Backspace" && !el.value && digits[i - 1]) digits[i - 1].focus();
432
+ });
433
+ el.addEventListener("paste", (e) => {
434
+ const text = (e.clipboardData || window.clipboardData).getData("text").replace(/\D/g, "");
435
+ if (!text) return;
436
+ e.preventDefault();
437
+ digits.forEach((d, idx) => {
438
+ d.value = text[idx] || "";
439
+ });
440
+ digits[Math.min(text.length, digits.length - 1)].focus();
441
+ });
442
+ });
443
+ }
444
+
445
+ /* ---- Password reveal ---- */
446
+ function initPassword(root) {
447
+ const input = $("input", root);
448
+ const toggle = $(".bw-password__toggle", root);
449
+ if (!input || !toggle) return;
450
+ toggle.addEventListener("click", () => {
451
+ const show = input.type === "password";
452
+ input.type = show ? "text" : "password";
453
+ toggle.setAttribute("aria-pressed", String(show));
454
+ toggle.textContent = show ? "Hide" : "Show";
455
+ });
456
+ }
457
+
458
+ /* ---- Segmented ---- */
459
+ function initSegmented(root) {
460
+ const items = $$(".bw-segmented__item", root);
461
+ items.forEach((btn) => {
462
+ btn.addEventListener("click", () => {
463
+ items.forEach((b) => {
464
+ b.setAttribute("aria-pressed", "false");
465
+ b.removeAttribute("data-bw-active");
466
+ });
467
+ btn.setAttribute("aria-pressed", "true");
468
+ btn.setAttribute("data-bw-active", "");
469
+ });
470
+ });
471
+ }
472
+
473
+
474
+ function initThemeToggle(btn) {
475
+ btn.addEventListener("click", () => {
476
+ const root = document.body;
477
+ const next = root.getAttribute("data-theme") === "dark" ? "light" : "dark";
478
+ root.setAttribute("data-theme", next);
479
+ btn.setAttribute("aria-pressed", String(next === "dark"));
480
+ });
481
+ }
482
+
483
+ function initSidebarToggle(btn) {
484
+ const rail = btn.closest(".bw-sidebar-rail");
485
+ if (!rail) return;
486
+ btn.addEventListener("click", () => {
487
+ const collapsed = rail.getAttribute("data-bw-collapsed") === "true";
488
+ rail.setAttribute("data-bw-collapsed", String(!collapsed));
489
+ btn.setAttribute("aria-expanded", String(collapsed));
490
+ btn.textContent = collapsed ? "Collapse" : "Expand";
491
+ });
492
+ }
493
+
494
+ document.addEventListener("DOMContentLoaded", () => {
495
+ $$("[data-bw-combobox], .bw-combobox").forEach(initCombobox);
496
+ $$(".bw-date-picker-chrome").forEach(initDatePicker);
497
+ $$(".bw-cmdk").forEach(initCmdk);
498
+ $$(".bw-modal").forEach(initModal);
499
+ $$(".bw-tags").forEach(initTags);
500
+ $$(".bw-otp").forEach(initOtp);
501
+ $$(".bw-password").forEach(initPassword);
502
+ $$(".bw-segmented").forEach(initSegmented);
503
+ $$("[data-bw-theme-toggle]").forEach(initThemeToggle);
504
+ $$("[data-bw-sidebar-toggle]").forEach(initSidebarToggle);
505
+ $$("[data-bw-toast]").forEach((btn) =>
506
+ btn.addEventListener("click", (e) => {
507
+ e.preventDefault();
508
+ showToast(btn.getAttribute("data-bw-toast") || "Saved", btn.getAttribute("data-bw-toast-variant") || "success");
509
+ }),
510
+ );
511
+ });
512
+ })();
package/package.json CHANGED
@@ -1,6 +1,28 @@
1
1
  {
2
2
  "name": "@bwui/js",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.1.0",
4
+ "description": "Progressive-enhancement behaviours for Brickwork HTML gallery markup (combobox, date picker, CMDK, modal, toast).",
5
+ "license": "MIT",
6
+ "type": "module",
7
+ "files": [
8
+ "dist",
9
+ "src",
10
+ "LICENSE",
11
+ "README.md"
12
+ ],
13
+ "main": "./dist/behaviours.js",
14
+ "exports": {
15
+ ".": "./dist/behaviours.js",
16
+ "./behaviours.js": "./dist/behaviours.js"
17
+ },
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "https://github.com/icvoss/brickwork.git"
21
+ },
22
+ "publishConfig": {
23
+ "access": "public"
24
+ },
25
+ "scripts": {
26
+ "build": "mkdir -p dist && cp src/behaviours.js dist/behaviours.js"
27
+ }
6
28
  }
@@ -0,0 +1,512 @@
1
+ /*!
2
+ * Brickwork gallery HTML behaviours (Wave B).
3
+ * Progressive enhancement for combobox, date picker, CMDK, modal, toast, tags, OTP.
4
+ * Copy with gallery pages; stack deliveries may replace with framework adapters.
5
+ */
6
+ (function () {
7
+ "use strict";
8
+
9
+ function $(sel, root) {
10
+ return (root || document).querySelector(sel);
11
+ }
12
+ function $$(sel, root) {
13
+ return Array.from((root || document).querySelectorAll(sel));
14
+ }
15
+
16
+ /* ---- Combobox ---- */
17
+ function initCombobox(root) {
18
+ const floor = $(".bw-combobox__floor", root);
19
+ const field = $(".bw-combobox__field", root);
20
+ const input = $(".bw-combobox__input", root);
21
+ const listbox = $(".bw-combobox__listbox", root);
22
+ if (!floor || !field || !input || !listbox) return;
23
+
24
+ field.hidden = false;
25
+ floor.hidden = true;
26
+ floor.setAttribute("tabindex", "-1");
27
+ floor.setAttribute("aria-hidden", "true");
28
+
29
+ const options = () => $$(".bw-combobox__option", listbox);
30
+ let active = -1;
31
+
32
+ function open() {
33
+ root.setAttribute("data-bw-open", "");
34
+ input.setAttribute("aria-expanded", "true");
35
+ }
36
+ function close() {
37
+ root.removeAttribute("data-bw-open");
38
+ input.setAttribute("aria-expanded", "false");
39
+ active = -1;
40
+ options().forEach((o) => o.removeAttribute("data-bw-active"));
41
+ }
42
+ function filter() {
43
+ const q = input.value.trim().toLowerCase();
44
+ let visible = 0;
45
+ options().forEach((opt) => {
46
+ const match = !q || (opt.textContent || "").toLowerCase().includes(q);
47
+ opt.hidden = !match;
48
+ if (match) visible += 1;
49
+ });
50
+ const empty = $(".bw-combobox__empty", listbox);
51
+ if (empty) empty.hidden = visible > 0;
52
+ }
53
+ function select(opt) {
54
+ if (!opt || opt.getAttribute("aria-disabled") === "true") return;
55
+ const value = opt.getAttribute("data-bw-value") || opt.textContent.trim();
56
+ floor.value = value;
57
+ floor.dispatchEvent(new Event("change", { bubbles: true }));
58
+ options().forEach((o) => o.setAttribute("aria-selected", String(o === opt)));
59
+ input.value = opt.textContent.trim();
60
+ close();
61
+ }
62
+
63
+ input.setAttribute("role", "combobox");
64
+ input.setAttribute("aria-expanded", "false");
65
+ input.setAttribute("aria-controls", listbox.id || "bw-listbox");
66
+ input.setAttribute("autocomplete", "off");
67
+
68
+ input.addEventListener("focus", () => {
69
+ open();
70
+ filter();
71
+ });
72
+ input.addEventListener("input", () => {
73
+ open();
74
+ filter();
75
+ });
76
+ input.addEventListener("keydown", (e) => {
77
+ const visible = options().filter((o) => !o.hidden);
78
+ if (e.key === "ArrowDown") {
79
+ e.preventDefault();
80
+ open();
81
+ active = Math.min(active + 1, visible.length - 1);
82
+ } else if (e.key === "ArrowUp") {
83
+ e.preventDefault();
84
+ active = Math.max(active - 1, 0);
85
+ } else if (e.key === "Enter") {
86
+ e.preventDefault();
87
+ if (active >= 0) select(visible[active]);
88
+ } 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");
100
+ }
101
+ });
102
+ });
103
+ listbox.addEventListener("click", (e) => {
104
+ const opt = e.target.closest(".bw-combobox__option");
105
+ if (opt) select(opt);
106
+ });
107
+ document.addEventListener("pointerdown", (e) => {
108
+ if (!root.contains(e.target)) close();
109
+ });
110
+
111
+ const selected = floor.options[floor.selectedIndex];
112
+ if (selected && selected.value) input.value = selected.textContent.trim();
113
+ }
114
+
115
+ /* ---- Date picker ---- */
116
+ const MONTHS = [
117
+ "January", "February", "March", "April", "May", "June",
118
+ "July", "August", "September", "October", "November", "December",
119
+ ];
120
+
121
+ function parseISO(s) {
122
+ if (!s) return null;
123
+ const m = /^(\d{4})-(\d{2})-(\d{2})$/.exec(s);
124
+ if (!m) return null;
125
+ return new Date(Number(m[1]), Number(m[2]) - 1, Number(m[3]));
126
+ }
127
+ function toISO(d) {
128
+ const y = d.getFullYear();
129
+ const m = String(d.getMonth() + 1).padStart(2, "0");
130
+ const day = String(d.getDate()).padStart(2, "0");
131
+ return y + "-" + m + "-" + day;
132
+ }
133
+ function sameDay(a, b) {
134
+ return a && b && a.getFullYear() === b.getFullYear() && a.getMonth() === b.getMonth() && a.getDate() === b.getDate();
135
+ }
136
+
137
+ function initDatePicker(root) {
138
+ const input = $(".bw-date-picker-chrome__field input, input[data-bw-date-input]", root);
139
+ const trigger = $(".bw-date-picker-chrome__trigger", root);
140
+ const panel = $(".bw-date-picker-chrome__panel", root);
141
+ const calendar = $(".bw-calendar", root);
142
+ if (!input || !panel || !calendar) return;
143
+
144
+ let view = parseISO(input.value) || new Date();
145
+ view = new Date(view.getFullYear(), view.getMonth(), 1);
146
+ const rangeMode = root.getAttribute("data-bw-range") === "true";
147
+ let rangeStart = parseISO(input.value);
148
+ let rangeEnd = parseISO(root.getAttribute("data-bw-end") || "");
149
+
150
+ function render() {
151
+ const title = $(".bw-calendar__title", calendar);
152
+ const grid = $(".bw-calendar__grid", calendar);
153
+ if (!title || !grid) return;
154
+ title.textContent = MONTHS[view.getMonth()] + " " + view.getFullYear();
155
+ grid.innerHTML = "";
156
+ ["Mo", "Tu", "We", "Th", "Fr", "Sa", "Su"].forEach((d) => {
157
+ const el = document.createElement("div");
158
+ el.className = "bw-calendar__dow";
159
+ el.textContent = d;
160
+ grid.appendChild(el);
161
+ });
162
+ const first = new Date(view.getFullYear(), view.getMonth(), 1);
163
+ let startDow = (first.getDay() + 6) % 7;
164
+ for (let i = 0; i < startDow; i++) {
165
+ const blank = document.createElement("div");
166
+ grid.appendChild(blank);
167
+ }
168
+ const days = new Date(view.getFullYear(), view.getMonth() + 1, 0).getDate();
169
+ for (let day = 1; day <= days; day++) {
170
+ const date = new Date(view.getFullYear(), view.getMonth(), day);
171
+ const btn = document.createElement("button");
172
+ btn.type = "button";
173
+ btn.className = "bw-calendar__day";
174
+ btn.textContent = String(day);
175
+ btn.setAttribute("aria-label", toISO(date));
176
+ if (!rangeMode && sameDay(date, parseISO(input.value))) {
177
+ btn.setAttribute("aria-selected", "true");
178
+ }
179
+ if (rangeMode && rangeStart && rangeEnd) {
180
+ if (sameDay(date, rangeStart) || sameDay(date, rangeEnd)) btn.setAttribute("aria-selected", "true");
181
+ else if (date > rangeStart && date < rangeEnd) btn.setAttribute("data-bw-in-range", "");
182
+ } else if (rangeMode && rangeStart && sameDay(date, rangeStart)) {
183
+ btn.setAttribute("aria-selected", "true");
184
+ }
185
+ btn.addEventListener("click", () => {
186
+ if (!rangeMode) {
187
+ input.value = toISO(date);
188
+ input.dispatchEvent(new Event("change", { bubbles: true }));
189
+ panel.hidden = true;
190
+ render();
191
+ return;
192
+ }
193
+ if (!rangeStart || (rangeStart && rangeEnd)) {
194
+ rangeStart = date;
195
+ rangeEnd = null;
196
+ } else if (date < rangeStart) {
197
+ rangeEnd = rangeStart;
198
+ rangeStart = date;
199
+ } else {
200
+ rangeEnd = date;
201
+ }
202
+ input.value = toISO(rangeStart);
203
+ const endInput = $("[data-bw-date-end]", root);
204
+ if (endInput) endInput.value = rangeEnd ? toISO(rangeEnd) : "";
205
+ if (rangeEnd) panel.hidden = true;
206
+ render();
207
+ });
208
+ grid.appendChild(btn);
209
+ }
210
+ }
211
+
212
+ function open() {
213
+ panel.hidden = false;
214
+ render();
215
+ }
216
+ function close() {
217
+ panel.hidden = true;
218
+ }
219
+
220
+ if (trigger) trigger.addEventListener("click", () => (panel.hidden ? open() : close()));
221
+ input.addEventListener("focus", open);
222
+ calendar.addEventListener("click", (e) => {
223
+ const prev = e.target.closest("[data-bw-cal-prev]");
224
+ const next = e.target.closest("[data-bw-cal-next]");
225
+ if (prev) {
226
+ view = new Date(view.getFullYear(), view.getMonth() - 1, 1);
227
+ render();
228
+ }
229
+ if (next) {
230
+ view = new Date(view.getFullYear(), view.getMonth() + 1, 1);
231
+ render();
232
+ }
233
+ });
234
+ document.addEventListener("pointerdown", (e) => {
235
+ if (!root.contains(e.target)) close();
236
+ });
237
+ document.addEventListener("keydown", (e) => {
238
+ if (e.key === "Escape" && !panel.hidden) close();
239
+ });
240
+ panel.hidden = true;
241
+ render();
242
+ }
243
+
244
+ /* ---- CMDK ---- */
245
+ function initCmdk(root) {
246
+ const input = $(".bw-cmdk__input", root);
247
+ const items = () => $$(".bw-cmdk__item", root).filter((el) => !el.hidden);
248
+ let active = 0;
249
+
250
+ function setActive(i) {
251
+ const list = items();
252
+ active = Math.max(0, Math.min(i, list.length - 1));
253
+ list.forEach((el, idx) => {
254
+ if (idx === active) {
255
+ el.setAttribute("data-bw-active", "");
256
+ el.scrollIntoView({ block: "nearest" });
257
+ } else el.removeAttribute("data-bw-active");
258
+ });
259
+ }
260
+ function open() {
261
+ root.setAttribute("data-bw-open", "");
262
+ root.setAttribute("aria-hidden", "false");
263
+ if (input) {
264
+ input.value = "";
265
+ filter();
266
+ input.focus();
267
+ }
268
+ setActive(0);
269
+ }
270
+ function close() {
271
+ root.removeAttribute("data-bw-open");
272
+ root.setAttribute("aria-hidden", "true");
273
+ }
274
+ function filter() {
275
+ const q = (input && input.value.trim().toLowerCase()) || "";
276
+ $$(".bw-cmdk__item", root).forEach((el) => {
277
+ const text = (el.textContent || "").toLowerCase();
278
+ el.hidden = Boolean(q) && !text.includes(q);
279
+ });
280
+ const empty = $(".bw-cmdk__empty", root);
281
+ if (empty) empty.hidden = items().length > 0;
282
+ setActive(0);
283
+ }
284
+ function run() {
285
+ const list = items();
286
+ const el = list[active];
287
+ if (!el) return;
288
+ const href = el.getAttribute("data-bw-href") || el.getAttribute("href");
289
+ close();
290
+ if (href) window.location.href = href;
291
+ el.dispatchEvent(new CustomEvent("bw-cmdk-select", { bubbles: true, detail: { id: el.id } }));
292
+ }
293
+
294
+ root.setAttribute("aria-hidden", "true");
295
+ $$(".bw-cmdk__scrim", root).forEach((s) => s.addEventListener("click", close));
296
+ if (input) {
297
+ input.addEventListener("input", filter);
298
+ input.addEventListener("keydown", (e) => {
299
+ if (e.key === "ArrowDown") {
300
+ e.preventDefault();
301
+ setActive(active + 1);
302
+ } else if (e.key === "ArrowUp") {
303
+ e.preventDefault();
304
+ setActive(active - 1);
305
+ } else if (e.key === "Enter") {
306
+ e.preventDefault();
307
+ run();
308
+ } else if (e.key === "Escape") {
309
+ e.preventDefault();
310
+ close();
311
+ }
312
+ });
313
+ }
314
+ $$(".bw-cmdk__item", root).forEach((el, idx) => {
315
+ el.setAttribute("role", "option");
316
+ el.addEventListener("click", () => {
317
+ active = items().indexOf(el);
318
+ run();
319
+ });
320
+ el.addEventListener("mousemove", () => setActive(items().indexOf(el)));
321
+ });
322
+
323
+ document.addEventListener("keydown", (e) => {
324
+ if ((e.metaKey || e.ctrlKey) && e.key.toLowerCase() === "k") {
325
+ e.preventDefault();
326
+ root.hasAttribute("data-bw-open") ? close() : open();
327
+ }
328
+ });
329
+ $$("[data-bw-cmdk-open]", document).forEach((btn) =>
330
+ btn.addEventListener("click", (e) => {
331
+ e.preventDefault();
332
+ open();
333
+ }),
334
+ );
335
+
336
+ window.bwCmdk = { open, close };
337
+ }
338
+
339
+ /* ---- Modal ---- */
340
+ function initModal(root) {
341
+ function open() {
342
+ root.setAttribute("data-bw-open", "");
343
+ root.classList.add("bw-modal--open");
344
+ }
345
+ function close() {
346
+ root.removeAttribute("data-bw-open");
347
+ root.classList.remove("bw-modal--open");
348
+ }
349
+ $$(".bw-modal__scrim, .bw-modal__close, [data-bw-modal-close]", root).forEach((el) =>
350
+ el.addEventListener("click", close),
351
+ );
352
+ const id = root.id;
353
+ if (id) {
354
+ $$('[data-bw-modal-open="' + id + '"]', document).forEach((btn) =>
355
+ btn.addEventListener("click", (e) => {
356
+ e.preventDefault();
357
+ open();
358
+ }),
359
+ );
360
+ }
361
+ document.addEventListener("keydown", (e) => {
362
+ if (e.key === "Escape" && root.hasAttribute("data-bw-open")) close();
363
+ });
364
+ }
365
+
366
+ /* ---- Toast ---- */
367
+ function showToast(message, variant) {
368
+ const region = $("#bw-toast-region") || (function () {
369
+ const el = document.createElement("div");
370
+ el.id = "bw-toast-region";
371
+ el.className = "bw-toast-region bw-toast-region--top-end";
372
+ el.setAttribute("aria-live", "polite");
373
+ document.body.appendChild(el);
374
+ return el;
375
+ })();
376
+ const toast = document.createElement("div");
377
+ toast.className = "bw-toast" + (variant ? " bw-toast--" + variant : "");
378
+ toast.setAttribute("data-bw-toast", "");
379
+ toast.innerHTML =
380
+ '<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>';
382
+ $(".bw-toast__message", toast).textContent = message;
383
+ region.prepend(toast);
384
+ requestAnimationFrame(() => toast.setAttribute("data-bw-visible", ""));
385
+ const dismiss = () => {
386
+ toast.removeAttribute("data-bw-visible");
387
+ setTimeout(() => toast.remove(), 200);
388
+ };
389
+ $(".bw-toast__close", toast).addEventListener("click", dismiss);
390
+ setTimeout(dismiss, 4000);
391
+ }
392
+ window.bwToast = showToast;
393
+
394
+ /* ---- Tags ---- */
395
+ function initTags(root) {
396
+ const input = $(".bw-tags__input", root);
397
+ if (!input) return;
398
+ function add(value) {
399
+ const v = value.trim();
400
+ if (!v) return;
401
+ const chip = document.createElement("span");
402
+ chip.className = "bw-tags__chip";
403
+ chip.innerHTML =
404
+ '<span></span><button type="button" class="bw-tags__chip-remove" aria-label="Remove">x</button>';
405
+ chip.firstElementChild.textContent = v;
406
+ $(".bw-tags__chip-remove", chip).addEventListener("click", () => chip.remove());
407
+ root.insertBefore(chip, input);
408
+ input.value = "";
409
+ }
410
+ input.addEventListener("keydown", (e) => {
411
+ if (e.key === "Enter" || e.key === ",") {
412
+ e.preventDefault();
413
+ add(input.value.replace(/,/g, ""));
414
+ } else if (e.key === "Backspace" && !input.value) {
415
+ const chips = $$(".bw-tags__chip", root);
416
+ const last = chips[chips.length - 1];
417
+ if (last) last.remove();
418
+ }
419
+ });
420
+ }
421
+
422
+ /* ---- OTP ---- */
423
+ function initOtp(root) {
424
+ const digits = $$(".bw-otp__digit", root);
425
+ digits.forEach((el, i) => {
426
+ el.addEventListener("input", () => {
427
+ el.value = el.value.replace(/\D/g, "").slice(-1);
428
+ if (el.value && digits[i + 1]) digits[i + 1].focus();
429
+ });
430
+ el.addEventListener("keydown", (e) => {
431
+ if (e.key === "Backspace" && !el.value && digits[i - 1]) digits[i - 1].focus();
432
+ });
433
+ el.addEventListener("paste", (e) => {
434
+ const text = (e.clipboardData || window.clipboardData).getData("text").replace(/\D/g, "");
435
+ if (!text) return;
436
+ e.preventDefault();
437
+ digits.forEach((d, idx) => {
438
+ d.value = text[idx] || "";
439
+ });
440
+ digits[Math.min(text.length, digits.length - 1)].focus();
441
+ });
442
+ });
443
+ }
444
+
445
+ /* ---- Password reveal ---- */
446
+ function initPassword(root) {
447
+ const input = $("input", root);
448
+ const toggle = $(".bw-password__toggle", root);
449
+ if (!input || !toggle) return;
450
+ toggle.addEventListener("click", () => {
451
+ const show = input.type === "password";
452
+ input.type = show ? "text" : "password";
453
+ toggle.setAttribute("aria-pressed", String(show));
454
+ toggle.textContent = show ? "Hide" : "Show";
455
+ });
456
+ }
457
+
458
+ /* ---- Segmented ---- */
459
+ function initSegmented(root) {
460
+ const items = $$(".bw-segmented__item", root);
461
+ items.forEach((btn) => {
462
+ btn.addEventListener("click", () => {
463
+ items.forEach((b) => {
464
+ b.setAttribute("aria-pressed", "false");
465
+ b.removeAttribute("data-bw-active");
466
+ });
467
+ btn.setAttribute("aria-pressed", "true");
468
+ btn.setAttribute("data-bw-active", "");
469
+ });
470
+ });
471
+ }
472
+
473
+
474
+ function initThemeToggle(btn) {
475
+ btn.addEventListener("click", () => {
476
+ const root = document.body;
477
+ const next = root.getAttribute("data-theme") === "dark" ? "light" : "dark";
478
+ root.setAttribute("data-theme", next);
479
+ btn.setAttribute("aria-pressed", String(next === "dark"));
480
+ });
481
+ }
482
+
483
+ function initSidebarToggle(btn) {
484
+ const rail = btn.closest(".bw-sidebar-rail");
485
+ if (!rail) return;
486
+ btn.addEventListener("click", () => {
487
+ const collapsed = rail.getAttribute("data-bw-collapsed") === "true";
488
+ rail.setAttribute("data-bw-collapsed", String(!collapsed));
489
+ btn.setAttribute("aria-expanded", String(collapsed));
490
+ btn.textContent = collapsed ? "Collapse" : "Expand";
491
+ });
492
+ }
493
+
494
+ document.addEventListener("DOMContentLoaded", () => {
495
+ $$("[data-bw-combobox], .bw-combobox").forEach(initCombobox);
496
+ $$(".bw-date-picker-chrome").forEach(initDatePicker);
497
+ $$(".bw-cmdk").forEach(initCmdk);
498
+ $$(".bw-modal").forEach(initModal);
499
+ $$(".bw-tags").forEach(initTags);
500
+ $$(".bw-otp").forEach(initOtp);
501
+ $$(".bw-password").forEach(initPassword);
502
+ $$(".bw-segmented").forEach(initSegmented);
503
+ $$("[data-bw-theme-toggle]").forEach(initThemeToggle);
504
+ $$("[data-bw-sidebar-toggle]").forEach(initSidebarToggle);
505
+ $$("[data-bw-toast]").forEach((btn) =>
506
+ btn.addEventListener("click", (e) => {
507
+ e.preventDefault();
508
+ showToast(btn.getAttribute("data-bw-toast") || "Saved", btn.getAttribute("data-bw-toast-variant") || "success");
509
+ }),
510
+ );
511
+ });
512
+ })();