@feitio/vanilla 0.0.0-stage → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +28 -2
  3. package/dist/dialog.js +46 -0
  4. package/dist/feitio.js +416 -0
  5. package/dist/forms.js +111 -0
  6. package/dist/menu.js +65 -0
  7. package/dist/notice.js +143 -0
  8. package/dist/scroll.js +55 -0
  9. package/dist/status.js +16 -0
  10. package/dist/types/dialog/actions/wire-dialogs.d.ts +7 -0
  11. package/dist/types/dialog/calculations/confirm-word.d.ts +2 -0
  12. package/dist/types/dialog/index.d.ts +3 -0
  13. package/dist/types/forms/actions/wire-forms.d.ts +6 -0
  14. package/dist/types/forms/calculations/form-values.d.ts +6 -0
  15. package/dist/types/forms/index.d.ts +3 -0
  16. package/dist/types/index.d.ts +7 -0
  17. package/dist/types/menu/actions/wire-menus.d.ts +6 -0
  18. package/dist/types/menu/calculations/menu-focus.d.ts +21 -0
  19. package/dist/types/menu/index.d.ts +3 -0
  20. package/dist/types/notice/actions/close-notice.d.ts +3 -0
  21. package/dist/types/notice/actions/show-notice.d.ts +6 -0
  22. package/dist/types/notice/actions/start-notice-timer.d.ts +7 -0
  23. package/dist/types/notice/actions/wire-notices.d.ts +6 -0
  24. package/dist/types/notice/calculations/notice-timer.d.ts +16 -0
  25. package/dist/types/notice/data/notice.d.ts +8 -0
  26. package/dist/types/notice/index.d.ts +10 -0
  27. package/dist/types/scroll/actions/wire-scrolls.d.ts +7 -0
  28. package/dist/types/scroll/calculations/scroll-visibility.d.ts +4 -0
  29. package/dist/types/scroll/index.d.ts +3 -0
  30. package/dist/types/status/actions/set-status.d.ts +5 -0
  31. package/dist/types/status/index.d.ts +1 -0
  32. package/package.json +77 -4
  33. package/src/dialog/actions/wire-dialogs.ts +39 -0
  34. package/src/dialog/calculations/confirm-word.ts +4 -0
  35. package/src/dialog/index.ts +8 -0
  36. package/src/forms/actions/wire-forms.ts +92 -0
  37. package/src/forms/calculations/form-values.ts +19 -0
  38. package/src/forms/index.ts +8 -0
  39. package/src/index.ts +7 -0
  40. package/src/menu/actions/wire-menus.ts +44 -0
  41. package/src/menu/calculations/menu-focus.ts +33 -0
  42. package/src/menu/index.ts +8 -0
  43. package/src/notice/actions/close-notice.ts +17 -0
  44. package/src/notice/actions/show-notice.ts +17 -0
  45. package/src/notice/actions/start-notice-timer.ts +54 -0
  46. package/src/notice/actions/wire-notices.ts +40 -0
  47. package/src/notice/calculations/notice-timer.ts +29 -0
  48. package/src/notice/data/notice.ts +9 -0
  49. package/src/notice/index.ts +27 -0
  50. package/src/scroll/actions/wire-scrolls.ts +46 -0
  51. package/src/scroll/calculations/scroll-visibility.ts +9 -0
  52. package/src/scroll/index.ts +8 -0
  53. package/src/status/actions/set-status.ts +18 -0
  54. package/src/status/index.ts +1 -0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Henrique Artur
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,29 @@
1
- # Temporary Holding Version
1
+ # @feitio/vanilla
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
+ The little JavaScript some Feitio components need, for any HTML: no framework, no build step
4
+ required. Each behavior takes the page (`window`) as a parameter and works by delegation, so HTML
5
+ added later is covered too.
6
+
7
+ ```sh
8
+ npm install @feitio/core @feitio/vanilla
9
+ ```
10
+
11
+ ```js
12
+ import { dialogs, forms, menus, notices, scrolls } from '@feitio/vanilla';
13
+
14
+ const notice = notices(window); // close buttons, and timed notices that pause under the pointer
15
+ notice.show(document.getElementById('saved'));
16
+ menus(window); // Menu: position, focus, arrow keys
17
+ forms(window); // Switch, SegmentedControl, TimeInput, FilterField
18
+ dialogs(window); // openers, typed confirmation, reset on close
19
+ scrolls(window); // ScrollFade and BackToTop
20
+ ```
21
+
22
+ Each behavior also has its own entry point (`@feitio/vanilla/notice`, `/menu`, `/forms`,
23
+ `/status`, `/scroll`, `/dialog`). The markup comes from Feitio's components
24
+ ([`@feitio/astro`](https://www.npmjs.com/package/@feitio/astro), or the HTML in the docs), and the
25
+ styles from [`@feitio/core`](https://www.npmjs.com/package/@feitio/core).
26
+
27
+ Docs: https://made-by-maid.github.io/feitio/
28
+
29
+ MIT.
package/dist/dialog.js ADDED
@@ -0,0 +1,46 @@
1
+ // src/dialog/calculations/confirm-word.ts
2
+ function typedTheWord(typed, word) {
3
+ return typed.trim().toUpperCase() === word.trim().toUpperCase() && word.trim() !== "";
4
+ }
5
+
6
+ // src/dialog/actions/wire-dialogs.ts
7
+ function wireDialogs(window, root) {
8
+ const document = window.document;
9
+ root.addEventListener("click", (event) => {
10
+ const opener = event.target.closest("[data-open-dialog]");
11
+ if (!opener)
12
+ return;
13
+ const dialog = document.getElementById(opener.dataset.openDialog ?? "");
14
+ if (!dialog || dialog.open)
15
+ return;
16
+ dialog.showModal();
17
+ dialog.querySelector("[data-type-to-confirm] input")?.focus();
18
+ dialog.addEventListener("close", () => {
19
+ const form = dialog.querySelector("[data-type-to-confirm]");
20
+ const input = form?.querySelector("input");
21
+ const danger = form?.querySelector('[data-variant="danger"]');
22
+ if (input)
23
+ input.value = "";
24
+ if (danger)
25
+ danger.disabled = true;
26
+ }, { once: true });
27
+ });
28
+ root.addEventListener("input", (event) => {
29
+ const input = event.target;
30
+ const form = input.closest("[data-type-to-confirm]");
31
+ if (!form)
32
+ return;
33
+ const danger = form.querySelector('[data-variant="danger"]');
34
+ if (danger)
35
+ danger.disabled = !typedTheWord(input.value, form.dataset.typeToConfirm ?? "");
36
+ });
37
+ }
38
+
39
+ // src/dialog/index.ts
40
+ function dialogs(window, root = window.document) {
41
+ wireDialogs(window, root);
42
+ }
43
+ export {
44
+ dialogs,
45
+ typedTheWord
46
+ };
package/dist/feitio.js ADDED
@@ -0,0 +1,416 @@
1
+ // src/dialog/calculations/confirm-word.ts
2
+ function typedTheWord(typed, word) {
3
+ return typed.trim().toUpperCase() === word.trim().toUpperCase() && word.trim() !== "";
4
+ }
5
+
6
+ // src/dialog/actions/wire-dialogs.ts
7
+ function wireDialogs(window, root) {
8
+ const document = window.document;
9
+ root.addEventListener("click", (event) => {
10
+ const opener = event.target.closest("[data-open-dialog]");
11
+ if (!opener)
12
+ return;
13
+ const dialog = document.getElementById(opener.dataset.openDialog ?? "");
14
+ if (!dialog || dialog.open)
15
+ return;
16
+ dialog.showModal();
17
+ dialog.querySelector("[data-type-to-confirm] input")?.focus();
18
+ dialog.addEventListener("close", () => {
19
+ const form = dialog.querySelector("[data-type-to-confirm]");
20
+ const input = form?.querySelector("input");
21
+ const danger = form?.querySelector('[data-variant="danger"]');
22
+ if (input)
23
+ input.value = "";
24
+ if (danger)
25
+ danger.disabled = true;
26
+ }, { once: true });
27
+ });
28
+ root.addEventListener("input", (event) => {
29
+ const input = event.target;
30
+ const form = input.closest("[data-type-to-confirm]");
31
+ if (!form)
32
+ return;
33
+ const danger = form.querySelector('[data-variant="danger"]');
34
+ if (danger)
35
+ danger.disabled = !typedTheWord(input.value, form.dataset.typeToConfirm ?? "");
36
+ });
37
+ }
38
+
39
+ // src/dialog/index.ts
40
+ function dialogs(window, root = window.document) {
41
+ wireDialogs(window, root);
42
+ }
43
+ // src/forms/calculations/form-values.ts
44
+ function togglePeriod(period) {
45
+ return period.trim().toUpperCase() === "AM" ? "PM" : "AM";
46
+ }
47
+ function plainText(text) {
48
+ return text.normalize("NFD").replace(/\p{Diacritic}/gu, "").toLowerCase();
49
+ }
50
+ function matchesFilter(itemWords, typed) {
51
+ const words = plainText(typed).split(/\s+/).filter(Boolean);
52
+ const target = plainText(itemWords);
53
+ return words.every((word) => target.includes(word));
54
+ }
55
+
56
+ // src/forms/actions/wire-forms.ts
57
+ function wireForms(window, root) {
58
+ root.addEventListener("change", (event) => {
59
+ const input = event.target;
60
+ const label = input.closest(".feitio-switch");
61
+ if (!label || input.type !== "checkbox")
62
+ return;
63
+ const state = label.querySelector(".state");
64
+ if (state)
65
+ state.textContent = input.checked ? label.dataset.on ?? "Ligado" : label.dataset.off ?? "Desligado";
66
+ });
67
+ root.addEventListener("click", (event) => {
68
+ const target = event.target;
69
+ const option = target.closest(".feitio-segmented button[aria-pressed]");
70
+ if (option && !option.closest("[data-playground] .controls")) {
71
+ for (const sibling of option.parentElement?.querySelectorAll("button[aria-pressed]") ?? [])
72
+ sibling.setAttribute("aria-pressed", String(sibling === option));
73
+ }
74
+ const period = target.closest(".feitio-time-field .period");
75
+ if (period && !period.disabled)
76
+ period.textContent = togglePeriod(period.textContent ?? "AM");
77
+ });
78
+ const timers = new WeakMap;
79
+ function filterNow(field, list) {
80
+ const document = window.document;
81
+ const listId = list.id;
82
+ const items = [...list.querySelectorAll("[data-search]")];
83
+ list.dataset.filtering = "";
84
+ let shown = 0;
85
+ for (const item of items) {
86
+ const match = matchesFilter(item.dataset.search ?? item.textContent ?? "", field.value);
87
+ item.hidden = !match;
88
+ if (match)
89
+ shown += 1;
90
+ }
91
+ const count = document.querySelector(`[data-count="${listId}"]`);
92
+ if (count)
93
+ count.textContent = field.value.trim() ? `${shown} / ${items.length}` : "";
94
+ const empty = document.querySelector(`[data-empty="${listId}"]`);
95
+ if (empty)
96
+ empty.hidden = shown > 0;
97
+ }
98
+ function run(field, box) {
99
+ const list = window.document.getElementById(field.dataset.filter ?? "");
100
+ if (field.dataset.async === undefined) {
101
+ if (box)
102
+ delete box.dataset.state;
103
+ if (list)
104
+ filterNow(field, list);
105
+ return;
106
+ }
107
+ if (box)
108
+ box.dataset.state = "busy";
109
+ const busy = window.document.querySelector(`[data-count="${field.dataset.filter}"]`);
110
+ if (busy && field.dataset.busyLabel)
111
+ busy.textContent = field.dataset.busyLabel;
112
+ const query = field.value;
113
+ const done = () => {
114
+ if (field.value !== query)
115
+ return;
116
+ if (box)
117
+ delete box.dataset.state;
118
+ if (list)
119
+ filterNow(field, list);
120
+ };
121
+ field.dispatchEvent(new CustomEvent("feitio-filter", { bubbles: true, detail: { query, done } }));
122
+ }
123
+ root.addEventListener("input", (event) => {
124
+ const field = event.target;
125
+ if (!field.dataset?.filter)
126
+ return;
127
+ const box = field.closest(".feitio-filter");
128
+ const wait = Number(field.dataset.debounce ?? 0);
129
+ window.clearTimeout(timers.get(field));
130
+ if (!(wait > 0)) {
131
+ run(field, box);
132
+ return;
133
+ }
134
+ if (box) {
135
+ box.style.setProperty("--feitio-filter-debounce", `${wait}ms`);
136
+ delete box.dataset.state;
137
+ box.offsetWidth;
138
+ box.dataset.state = "waiting";
139
+ }
140
+ timers.set(field, window.setTimeout(() => run(field, box), wait));
141
+ });
142
+ }
143
+
144
+ // src/forms/index.ts
145
+ function forms(window, root = window.document) {
146
+ wireForms(window, root);
147
+ }
148
+ // src/menu/calculations/menu-focus.ts
149
+ function nextMenuIndex(key, current, count) {
150
+ if (count === 0)
151
+ return;
152
+ switch (key) {
153
+ case "ArrowDown":
154
+ return current < 0 ? 0 : (current + 1) % count;
155
+ case "ArrowUp":
156
+ return current <= 0 ? count - 1 : current - 1;
157
+ case "Home":
158
+ return 0;
159
+ case "End":
160
+ return count - 1;
161
+ default:
162
+ return;
163
+ }
164
+ }
165
+ function menuPosition(button, menu, viewport, gap) {
166
+ const fitsRight = button.left + menu.width <= viewport.width - gap;
167
+ const left = Math.max(gap, fitsRight ? button.left : button.right - menu.width);
168
+ const fitsBelow = button.bottom + gap + menu.height <= viewport.height - gap;
169
+ const top = fitsBelow ? button.bottom + gap : Math.max(gap, button.top - gap - menu.height);
170
+ return { left, top };
171
+ }
172
+
173
+ // src/menu/actions/wire-menus.ts
174
+ var wired = new WeakSet;
175
+ function wireMenus(window, root) {
176
+ for (const list of root.querySelectorAll(".feitio-menu-list[popover]")) {
177
+ const button = root.querySelector(`[popovertarget="${list.id}"]`);
178
+ if (!button || wired.has(list))
179
+ continue;
180
+ wired.add(list);
181
+ const items = () => [...list.querySelectorAll("a, button")];
182
+ list.addEventListener("toggle", (event) => {
183
+ const open = event.newState === "open";
184
+ button.setAttribute("aria-expanded", String(open));
185
+ if (!open)
186
+ return;
187
+ const place = menuPosition(button.getBoundingClientRect(), list.getBoundingClientRect(), { width: window.innerWidth, height: window.innerHeight }, 8);
188
+ list.style.left = `${place.left}px`;
189
+ list.style.top = `${place.top}px`;
190
+ const current = items().find((item) => item.getAttribute("aria-current")) ?? items()[0];
191
+ current?.focus();
192
+ });
193
+ list.addEventListener("keydown", (event) => {
194
+ const all = items();
195
+ const next = nextMenuIndex(event.key, all.indexOf(window.document.activeElement), all.length);
196
+ if (next === undefined)
197
+ return;
198
+ event.preventDefault();
199
+ all[next]?.focus();
200
+ });
201
+ }
202
+ }
203
+
204
+ // src/menu/index.ts
205
+ function menus(window, root = window.document) {
206
+ wireMenus(window, root);
207
+ }
208
+ // src/notice/data/notice.ts
209
+ var LEAVE_FALLBACK_MS = 600;
210
+
211
+ // src/notice/actions/close-notice.ts
212
+ function closeNotice(host, notice) {
213
+ if (notice.hidden || notice.classList.contains("leaving"))
214
+ return;
215
+ host.clocks.get(notice)?.();
216
+ host.clocks.delete(notice);
217
+ notice.classList.add("leaving");
218
+ function done() {
219
+ notice.hidden = true;
220
+ notice.classList.remove("leaving");
221
+ }
222
+ notice.addEventListener("animationend", done, { once: true });
223
+ host.window.setTimeout(() => {
224
+ if (!notice.hidden)
225
+ done();
226
+ }, LEAVE_FALLBACK_MS);
227
+ }
228
+
229
+ // src/notice/calculations/notice-timer.ts
230
+ function closesByItself(status, timeoutMs) {
231
+ return (status === "info" || status === "ok") && Number.isFinite(timeoutMs) && timeoutMs > 0;
232
+ }
233
+ function noticeStatus(attribute) {
234
+ return attribute === "ok" || attribute === "warn" || attribute === "error" ? attribute : "info";
235
+ }
236
+ function noticeProgress(usedMs, timeoutMs) {
237
+ if (timeoutMs <= 0)
238
+ return 1;
239
+ return Math.min(Math.max(usedMs / timeoutMs, 0), 1);
240
+ }
241
+ function countFrame(usedMs, elapsedMs, paused) {
242
+ return paused ? usedMs : usedMs + Math.max(elapsedMs, 0);
243
+ }
244
+
245
+ // src/notice/actions/start-notice-timer.ts
246
+ function startNoticeTimer(host, notice) {
247
+ host.clocks.get(notice)?.();
248
+ const total = Number(notice.dataset.timeout);
249
+ if (!closesByItself(noticeStatus(notice.dataset.status), total))
250
+ return;
251
+ const { window } = host;
252
+ let used = 0;
253
+ let last = window.performance.now();
254
+ let paused = notice.matches(":hover, :focus-within");
255
+ let frame = 0;
256
+ function pause() {
257
+ paused = true;
258
+ }
259
+ function resume() {
260
+ window.setTimeout(() => {
261
+ if (notice.matches(":hover, :focus-within"))
262
+ return;
263
+ paused = false;
264
+ last = window.performance.now();
265
+ });
266
+ }
267
+ const listeners = [
268
+ ["mouseenter", pause],
269
+ ["focusin", pause],
270
+ ["mouseleave", resume],
271
+ ["focusout", resume]
272
+ ];
273
+ for (const [event, listener] of listeners)
274
+ notice.addEventListener(event, listener);
275
+ function tick(now) {
276
+ used = countFrame(used, now - last, paused);
277
+ last = now;
278
+ const progress = noticeProgress(used, total);
279
+ notice.style.setProperty("--notice-p", progress.toFixed(4));
280
+ if (progress >= 1)
281
+ closeNotice(host, notice);
282
+ else
283
+ frame = window.requestAnimationFrame(tick);
284
+ }
285
+ host.clocks.set(notice, () => {
286
+ window.cancelAnimationFrame(frame);
287
+ for (const [event, listener] of listeners)
288
+ notice.removeEventListener(event, listener);
289
+ });
290
+ frame = window.requestAnimationFrame(tick);
291
+ }
292
+
293
+ // src/notice/actions/show-notice.ts
294
+ function showNotice(host, notice) {
295
+ if (notice.hidden) {
296
+ notice.hidden = false;
297
+ notice.classList.remove("feitio-place");
298
+ notice.offsetWidth;
299
+ notice.classList.add("feitio-place");
300
+ }
301
+ notice.style.setProperty("--notice-p", "0");
302
+ startNoticeTimer(host, notice);
303
+ }
304
+
305
+ // src/notice/actions/wire-notices.ts
306
+ var wired2 = new WeakSet;
307
+ function wireNotices(host, root) {
308
+ const own = "matches" in root && root.matches(".feitio-notice");
309
+ const notices = [
310
+ ...own ? [root] : [],
311
+ ...root.querySelectorAll(".feitio-notice")
312
+ ];
313
+ const Observer = host.window.IntersectionObserver;
314
+ const seen = Observer ? new Observer((entries) => {
315
+ for (const entry of entries) {
316
+ if (!entry.isIntersecting)
317
+ continue;
318
+ seen?.unobserve(entry.target);
319
+ startNoticeTimer(host, entry.target);
320
+ }
321
+ }, { threshold: 0.9 }) : undefined;
322
+ for (const notice of notices) {
323
+ if (wired2.has(notice))
324
+ continue;
325
+ wired2.add(notice);
326
+ notice.querySelector(".close")?.addEventListener("click", () => closeNotice(host, notice));
327
+ if (!notice.dataset.timeout || notice.hidden)
328
+ continue;
329
+ if (seen)
330
+ seen.observe(notice);
331
+ else
332
+ startNoticeTimer(host, notice);
333
+ }
334
+ }
335
+
336
+ // src/notice/index.ts
337
+ function notices(window, root = window.document) {
338
+ const host = { window, clocks: new WeakMap };
339
+ wireNotices(host, root);
340
+ return {
341
+ show: (notice) => showNotice(host, notice),
342
+ close: (notice) => closeNotice(host, notice),
343
+ wire: (more) => wireNotices(host, more)
344
+ };
345
+ }
346
+ // src/scroll/calculations/scroll-visibility.ts
347
+ function fadeVisible(scrollTop, viewHeight, scrollHeight) {
348
+ return scrollTop + viewHeight < scrollHeight - 8;
349
+ }
350
+ function backToTopVisible(scrollTop, viewHeight) {
351
+ return scrollTop > viewHeight * 0.5;
352
+ }
353
+
354
+ // src/scroll/actions/wire-scrolls.ts
355
+ function wireScrolls(window, root) {
356
+ const document = window.document;
357
+ function scrollerOf(element) {
358
+ for (let parent = element.parentElement;parent; parent = parent.parentElement) {
359
+ const overflow = window.getComputedStyle(parent).overflowY;
360
+ if (overflow === "auto" || overflow === "scroll")
361
+ return parent;
362
+ }
363
+ return null;
364
+ }
365
+ for (const element of root.querySelectorAll(".feitio-scroll-fade, .feitio-back-to-top")) {
366
+ if (element.dataset.wired !== undefined)
367
+ continue;
368
+ element.dataset.wired = "";
369
+ const box = scrollerOf(element);
370
+ const area = box ?? document.documentElement;
371
+ const fade = element.classList.contains("feitio-scroll-fade");
372
+ const update = () => {
373
+ const visible = fade ? fadeVisible(area.scrollTop, area.clientHeight, area.scrollHeight) : backToTopVisible(area.scrollTop, area.clientHeight);
374
+ element.toggleAttribute("data-visible", visible);
375
+ };
376
+ (box ?? window).addEventListener("scroll", update, { passive: true });
377
+ window.addEventListener("resize", update);
378
+ update();
379
+ if (!fade) {
380
+ element.addEventListener("click", (event) => {
381
+ event.preventDefault();
382
+ const still = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
383
+ area.scrollTo({ top: 0, behavior: still ? "auto" : "smooth" });
384
+ const target = element.getAttribute("href");
385
+ if (target?.startsWith("#"))
386
+ document.querySelector(target)?.focus({ preventScroll: true });
387
+ });
388
+ }
389
+ }
390
+ }
391
+
392
+ // src/scroll/index.ts
393
+ function scrolls(window, root = window.document) {
394
+ wireScrolls(window, root);
395
+ }
396
+ // src/status/actions/set-status.ts
397
+ function setStatus(host, state, label = "") {
398
+ const words = host.querySelector(".feitio-status .label");
399
+ if (state === null) {
400
+ delete host.dataset.state;
401
+ if (words)
402
+ words.textContent = "";
403
+ return;
404
+ }
405
+ host.dataset.state = state;
406
+ if (words)
407
+ words.textContent = label;
408
+ }
409
+ export {
410
+ dialogs,
411
+ forms,
412
+ menus,
413
+ notices,
414
+ scrolls,
415
+ setStatus as status
416
+ };
package/dist/forms.js ADDED
@@ -0,0 +1,111 @@
1
+ // src/forms/calculations/form-values.ts
2
+ function togglePeriod(period) {
3
+ return period.trim().toUpperCase() === "AM" ? "PM" : "AM";
4
+ }
5
+ function plainText(text) {
6
+ return text.normalize("NFD").replace(/\p{Diacritic}/gu, "").toLowerCase();
7
+ }
8
+ function matchesFilter(itemWords, typed) {
9
+ const words = plainText(typed).split(/\s+/).filter(Boolean);
10
+ const target = plainText(itemWords);
11
+ return words.every((word) => target.includes(word));
12
+ }
13
+
14
+ // src/forms/actions/wire-forms.ts
15
+ function wireForms(window, root) {
16
+ root.addEventListener("change", (event) => {
17
+ const input = event.target;
18
+ const label = input.closest(".feitio-switch");
19
+ if (!label || input.type !== "checkbox")
20
+ return;
21
+ const state = label.querySelector(".state");
22
+ if (state)
23
+ state.textContent = input.checked ? label.dataset.on ?? "Ligado" : label.dataset.off ?? "Desligado";
24
+ });
25
+ root.addEventListener("click", (event) => {
26
+ const target = event.target;
27
+ const option = target.closest(".feitio-segmented button[aria-pressed]");
28
+ if (option && !option.closest("[data-playground] .controls")) {
29
+ for (const sibling of option.parentElement?.querySelectorAll("button[aria-pressed]") ?? [])
30
+ sibling.setAttribute("aria-pressed", String(sibling === option));
31
+ }
32
+ const period = target.closest(".feitio-time-field .period");
33
+ if (period && !period.disabled)
34
+ period.textContent = togglePeriod(period.textContent ?? "AM");
35
+ });
36
+ const timers = new WeakMap;
37
+ function filterNow(field, list) {
38
+ const document = window.document;
39
+ const listId = list.id;
40
+ const items = [...list.querySelectorAll("[data-search]")];
41
+ list.dataset.filtering = "";
42
+ let shown = 0;
43
+ for (const item of items) {
44
+ const match = matchesFilter(item.dataset.search ?? item.textContent ?? "", field.value);
45
+ item.hidden = !match;
46
+ if (match)
47
+ shown += 1;
48
+ }
49
+ const count = document.querySelector(`[data-count="${listId}"]`);
50
+ if (count)
51
+ count.textContent = field.value.trim() ? `${shown} / ${items.length}` : "";
52
+ const empty = document.querySelector(`[data-empty="${listId}"]`);
53
+ if (empty)
54
+ empty.hidden = shown > 0;
55
+ }
56
+ function run(field, box) {
57
+ const list = window.document.getElementById(field.dataset.filter ?? "");
58
+ if (field.dataset.async === undefined) {
59
+ if (box)
60
+ delete box.dataset.state;
61
+ if (list)
62
+ filterNow(field, list);
63
+ return;
64
+ }
65
+ if (box)
66
+ box.dataset.state = "busy";
67
+ const busy = window.document.querySelector(`[data-count="${field.dataset.filter}"]`);
68
+ if (busy && field.dataset.busyLabel)
69
+ busy.textContent = field.dataset.busyLabel;
70
+ const query = field.value;
71
+ const done = () => {
72
+ if (field.value !== query)
73
+ return;
74
+ if (box)
75
+ delete box.dataset.state;
76
+ if (list)
77
+ filterNow(field, list);
78
+ };
79
+ field.dispatchEvent(new CustomEvent("feitio-filter", { bubbles: true, detail: { query, done } }));
80
+ }
81
+ root.addEventListener("input", (event) => {
82
+ const field = event.target;
83
+ if (!field.dataset?.filter)
84
+ return;
85
+ const box = field.closest(".feitio-filter");
86
+ const wait = Number(field.dataset.debounce ?? 0);
87
+ window.clearTimeout(timers.get(field));
88
+ if (!(wait > 0)) {
89
+ run(field, box);
90
+ return;
91
+ }
92
+ if (box) {
93
+ box.style.setProperty("--feitio-filter-debounce", `${wait}ms`);
94
+ delete box.dataset.state;
95
+ box.offsetWidth;
96
+ box.dataset.state = "waiting";
97
+ }
98
+ timers.set(field, window.setTimeout(() => run(field, box), wait));
99
+ });
100
+ }
101
+
102
+ // src/forms/index.ts
103
+ function forms(window, root = window.document) {
104
+ wireForms(window, root);
105
+ }
106
+ export {
107
+ forms,
108
+ matchesFilter,
109
+ plainText,
110
+ togglePeriod
111
+ };
package/dist/menu.js ADDED
@@ -0,0 +1,65 @@
1
+ // src/menu/calculations/menu-focus.ts
2
+ function nextMenuIndex(key, current, count) {
3
+ if (count === 0)
4
+ return;
5
+ switch (key) {
6
+ case "ArrowDown":
7
+ return current < 0 ? 0 : (current + 1) % count;
8
+ case "ArrowUp":
9
+ return current <= 0 ? count - 1 : current - 1;
10
+ case "Home":
11
+ return 0;
12
+ case "End":
13
+ return count - 1;
14
+ default:
15
+ return;
16
+ }
17
+ }
18
+ function menuPosition(button, menu, viewport, gap) {
19
+ const fitsRight = button.left + menu.width <= viewport.width - gap;
20
+ const left = Math.max(gap, fitsRight ? button.left : button.right - menu.width);
21
+ const fitsBelow = button.bottom + gap + menu.height <= viewport.height - gap;
22
+ const top = fitsBelow ? button.bottom + gap : Math.max(gap, button.top - gap - menu.height);
23
+ return { left, top };
24
+ }
25
+
26
+ // src/menu/actions/wire-menus.ts
27
+ var wired = new WeakSet;
28
+ function wireMenus(window, root) {
29
+ for (const list of root.querySelectorAll(".feitio-menu-list[popover]")) {
30
+ const button = root.querySelector(`[popovertarget="${list.id}"]`);
31
+ if (!button || wired.has(list))
32
+ continue;
33
+ wired.add(list);
34
+ const items = () => [...list.querySelectorAll("a, button")];
35
+ list.addEventListener("toggle", (event) => {
36
+ const open = event.newState === "open";
37
+ button.setAttribute("aria-expanded", String(open));
38
+ if (!open)
39
+ return;
40
+ const place = menuPosition(button.getBoundingClientRect(), list.getBoundingClientRect(), { width: window.innerWidth, height: window.innerHeight }, 8);
41
+ list.style.left = `${place.left}px`;
42
+ list.style.top = `${place.top}px`;
43
+ const current = items().find((item) => item.getAttribute("aria-current")) ?? items()[0];
44
+ current?.focus();
45
+ });
46
+ list.addEventListener("keydown", (event) => {
47
+ const all = items();
48
+ const next = nextMenuIndex(event.key, all.indexOf(window.document.activeElement), all.length);
49
+ if (next === undefined)
50
+ return;
51
+ event.preventDefault();
52
+ all[next]?.focus();
53
+ });
54
+ }
55
+ }
56
+
57
+ // src/menu/index.ts
58
+ function menus(window, root = window.document) {
59
+ wireMenus(window, root);
60
+ }
61
+ export {
62
+ menuPosition,
63
+ menus,
64
+ nextMenuIndex
65
+ };