@feitio/vanilla 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 +21 -0
- package/README.md +28 -2
- package/dist/dialog.js +46 -0
- package/dist/feitio.js +416 -0
- package/dist/forms.js +111 -0
- package/dist/menu.js +65 -0
- package/dist/notice.js +143 -0
- package/dist/scroll.js +55 -0
- package/dist/status.js +16 -0
- package/dist/types/dialog/actions/wire-dialogs.d.ts +7 -0
- package/dist/types/dialog/calculations/confirm-word.d.ts +2 -0
- package/dist/types/dialog/index.d.ts +3 -0
- package/dist/types/forms/actions/wire-forms.d.ts +6 -0
- package/dist/types/forms/calculations/form-values.d.ts +6 -0
- package/dist/types/forms/index.d.ts +3 -0
- package/dist/types/index.d.ts +7 -0
- package/dist/types/menu/actions/wire-menus.d.ts +6 -0
- package/dist/types/menu/calculations/menu-focus.d.ts +21 -0
- package/dist/types/menu/index.d.ts +3 -0
- package/dist/types/notice/actions/close-notice.d.ts +3 -0
- package/dist/types/notice/actions/show-notice.d.ts +6 -0
- package/dist/types/notice/actions/start-notice-timer.d.ts +7 -0
- package/dist/types/notice/actions/wire-notices.d.ts +6 -0
- package/dist/types/notice/calculations/notice-timer.d.ts +16 -0
- package/dist/types/notice/data/notice.d.ts +8 -0
- package/dist/types/notice/index.d.ts +10 -0
- package/dist/types/scroll/actions/wire-scrolls.d.ts +7 -0
- package/dist/types/scroll/calculations/scroll-visibility.d.ts +4 -0
- package/dist/types/scroll/index.d.ts +3 -0
- package/dist/types/status/actions/set-status.d.ts +5 -0
- package/dist/types/status/index.d.ts +1 -0
- package/package.json +77 -4
- package/src/dialog/actions/wire-dialogs.ts +39 -0
- package/src/dialog/calculations/confirm-word.ts +4 -0
- package/src/dialog/index.ts +8 -0
- package/src/forms/actions/wire-forms.ts +92 -0
- package/src/forms/calculations/form-values.ts +19 -0
- package/src/forms/index.ts +8 -0
- package/src/index.ts +7 -0
- package/src/menu/actions/wire-menus.ts +44 -0
- package/src/menu/calculations/menu-focus.ts +33 -0
- package/src/menu/index.ts +8 -0
- package/src/notice/actions/close-notice.ts +17 -0
- package/src/notice/actions/show-notice.ts +17 -0
- package/src/notice/actions/start-notice-timer.ts +54 -0
- package/src/notice/actions/wire-notices.ts +40 -0
- package/src/notice/calculations/notice-timer.ts +29 -0
- package/src/notice/data/notice.ts +9 -0
- package/src/notice/index.ts +27 -0
- package/src/scroll/actions/wire-scrolls.ts +46 -0
- package/src/scroll/calculations/scroll-visibility.ts +9 -0
- package/src/scroll/index.ts +8 -0
- package/src/status/actions/set-status.ts +18 -0
- package/src/status/index.ts +1 -0
package/dist/notice.js
ADDED
|
@@ -0,0 +1,143 @@
|
|
|
1
|
+
// src/notice/data/notice.ts
|
|
2
|
+
var LEAVE_FALLBACK_MS = 600;
|
|
3
|
+
|
|
4
|
+
// src/notice/actions/close-notice.ts
|
|
5
|
+
function closeNotice(host, notice) {
|
|
6
|
+
if (notice.hidden || notice.classList.contains("leaving"))
|
|
7
|
+
return;
|
|
8
|
+
host.clocks.get(notice)?.();
|
|
9
|
+
host.clocks.delete(notice);
|
|
10
|
+
notice.classList.add("leaving");
|
|
11
|
+
function done() {
|
|
12
|
+
notice.hidden = true;
|
|
13
|
+
notice.classList.remove("leaving");
|
|
14
|
+
}
|
|
15
|
+
notice.addEventListener("animationend", done, { once: true });
|
|
16
|
+
host.window.setTimeout(() => {
|
|
17
|
+
if (!notice.hidden)
|
|
18
|
+
done();
|
|
19
|
+
}, LEAVE_FALLBACK_MS);
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// src/notice/calculations/notice-timer.ts
|
|
23
|
+
function closesByItself(status, timeoutMs) {
|
|
24
|
+
return (status === "info" || status === "ok") && Number.isFinite(timeoutMs) && timeoutMs > 0;
|
|
25
|
+
}
|
|
26
|
+
function noticeStatus(attribute) {
|
|
27
|
+
return attribute === "ok" || attribute === "warn" || attribute === "error" ? attribute : "info";
|
|
28
|
+
}
|
|
29
|
+
function noticeProgress(usedMs, timeoutMs) {
|
|
30
|
+
if (timeoutMs <= 0)
|
|
31
|
+
return 1;
|
|
32
|
+
return Math.min(Math.max(usedMs / timeoutMs, 0), 1);
|
|
33
|
+
}
|
|
34
|
+
function countFrame(usedMs, elapsedMs, paused) {
|
|
35
|
+
return paused ? usedMs : usedMs + Math.max(elapsedMs, 0);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// src/notice/actions/start-notice-timer.ts
|
|
39
|
+
function startNoticeTimer(host, notice) {
|
|
40
|
+
host.clocks.get(notice)?.();
|
|
41
|
+
const total = Number(notice.dataset.timeout);
|
|
42
|
+
if (!closesByItself(noticeStatus(notice.dataset.status), total))
|
|
43
|
+
return;
|
|
44
|
+
const { window } = host;
|
|
45
|
+
let used = 0;
|
|
46
|
+
let last = window.performance.now();
|
|
47
|
+
let paused = notice.matches(":hover, :focus-within");
|
|
48
|
+
let frame = 0;
|
|
49
|
+
function pause() {
|
|
50
|
+
paused = true;
|
|
51
|
+
}
|
|
52
|
+
function resume() {
|
|
53
|
+
window.setTimeout(() => {
|
|
54
|
+
if (notice.matches(":hover, :focus-within"))
|
|
55
|
+
return;
|
|
56
|
+
paused = false;
|
|
57
|
+
last = window.performance.now();
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
const listeners = [
|
|
61
|
+
["mouseenter", pause],
|
|
62
|
+
["focusin", pause],
|
|
63
|
+
["mouseleave", resume],
|
|
64
|
+
["focusout", resume]
|
|
65
|
+
];
|
|
66
|
+
for (const [event, listener] of listeners)
|
|
67
|
+
notice.addEventListener(event, listener);
|
|
68
|
+
function tick(now) {
|
|
69
|
+
used = countFrame(used, now - last, paused);
|
|
70
|
+
last = now;
|
|
71
|
+
const progress = noticeProgress(used, total);
|
|
72
|
+
notice.style.setProperty("--notice-p", progress.toFixed(4));
|
|
73
|
+
if (progress >= 1)
|
|
74
|
+
closeNotice(host, notice);
|
|
75
|
+
else
|
|
76
|
+
frame = window.requestAnimationFrame(tick);
|
|
77
|
+
}
|
|
78
|
+
host.clocks.set(notice, () => {
|
|
79
|
+
window.cancelAnimationFrame(frame);
|
|
80
|
+
for (const [event, listener] of listeners)
|
|
81
|
+
notice.removeEventListener(event, listener);
|
|
82
|
+
});
|
|
83
|
+
frame = window.requestAnimationFrame(tick);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// src/notice/actions/show-notice.ts
|
|
87
|
+
function showNotice(host, notice) {
|
|
88
|
+
if (notice.hidden) {
|
|
89
|
+
notice.hidden = false;
|
|
90
|
+
notice.classList.remove("feitio-place");
|
|
91
|
+
notice.offsetWidth;
|
|
92
|
+
notice.classList.add("feitio-place");
|
|
93
|
+
}
|
|
94
|
+
notice.style.setProperty("--notice-p", "0");
|
|
95
|
+
startNoticeTimer(host, notice);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// src/notice/actions/wire-notices.ts
|
|
99
|
+
var wired = new WeakSet;
|
|
100
|
+
function wireNotices(host, root) {
|
|
101
|
+
const own = "matches" in root && root.matches(".feitio-notice");
|
|
102
|
+
const notices = [
|
|
103
|
+
...own ? [root] : [],
|
|
104
|
+
...root.querySelectorAll(".feitio-notice")
|
|
105
|
+
];
|
|
106
|
+
const Observer = host.window.IntersectionObserver;
|
|
107
|
+
const seen = Observer ? new Observer((entries) => {
|
|
108
|
+
for (const entry of entries) {
|
|
109
|
+
if (!entry.isIntersecting)
|
|
110
|
+
continue;
|
|
111
|
+
seen?.unobserve(entry.target);
|
|
112
|
+
startNoticeTimer(host, entry.target);
|
|
113
|
+
}
|
|
114
|
+
}, { threshold: 0.9 }) : undefined;
|
|
115
|
+
for (const notice of notices) {
|
|
116
|
+
if (wired.has(notice))
|
|
117
|
+
continue;
|
|
118
|
+
wired.add(notice);
|
|
119
|
+
notice.querySelector(".close")?.addEventListener("click", () => closeNotice(host, notice));
|
|
120
|
+
if (!notice.dataset.timeout || notice.hidden)
|
|
121
|
+
continue;
|
|
122
|
+
if (seen)
|
|
123
|
+
seen.observe(notice);
|
|
124
|
+
else
|
|
125
|
+
startNoticeTimer(host, notice);
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
// src/notice/index.ts
|
|
130
|
+
function notices(window, root = window.document) {
|
|
131
|
+
const host = { window, clocks: new WeakMap };
|
|
132
|
+
wireNotices(host, root);
|
|
133
|
+
return {
|
|
134
|
+
show: (notice) => showNotice(host, notice),
|
|
135
|
+
close: (notice) => closeNotice(host, notice),
|
|
136
|
+
wire: (more) => wireNotices(host, more)
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
export {
|
|
140
|
+
closesByItself,
|
|
141
|
+
noticeProgress,
|
|
142
|
+
notices
|
|
143
|
+
};
|
package/dist/scroll.js
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
// src/scroll/calculations/scroll-visibility.ts
|
|
2
|
+
function fadeVisible(scrollTop, viewHeight, scrollHeight) {
|
|
3
|
+
return scrollTop + viewHeight < scrollHeight - 8;
|
|
4
|
+
}
|
|
5
|
+
function backToTopVisible(scrollTop, viewHeight) {
|
|
6
|
+
return scrollTop > viewHeight * 0.5;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
// src/scroll/actions/wire-scrolls.ts
|
|
10
|
+
function wireScrolls(window, root) {
|
|
11
|
+
const document = window.document;
|
|
12
|
+
function scrollerOf(element) {
|
|
13
|
+
for (let parent = element.parentElement;parent; parent = parent.parentElement) {
|
|
14
|
+
const overflow = window.getComputedStyle(parent).overflowY;
|
|
15
|
+
if (overflow === "auto" || overflow === "scroll")
|
|
16
|
+
return parent;
|
|
17
|
+
}
|
|
18
|
+
return null;
|
|
19
|
+
}
|
|
20
|
+
for (const element of root.querySelectorAll(".feitio-scroll-fade, .feitio-back-to-top")) {
|
|
21
|
+
if (element.dataset.wired !== undefined)
|
|
22
|
+
continue;
|
|
23
|
+
element.dataset.wired = "";
|
|
24
|
+
const box = scrollerOf(element);
|
|
25
|
+
const area = box ?? document.documentElement;
|
|
26
|
+
const fade = element.classList.contains("feitio-scroll-fade");
|
|
27
|
+
const update = () => {
|
|
28
|
+
const visible = fade ? fadeVisible(area.scrollTop, area.clientHeight, area.scrollHeight) : backToTopVisible(area.scrollTop, area.clientHeight);
|
|
29
|
+
element.toggleAttribute("data-visible", visible);
|
|
30
|
+
};
|
|
31
|
+
(box ?? window).addEventListener("scroll", update, { passive: true });
|
|
32
|
+
window.addEventListener("resize", update);
|
|
33
|
+
update();
|
|
34
|
+
if (!fade) {
|
|
35
|
+
element.addEventListener("click", (event) => {
|
|
36
|
+
event.preventDefault();
|
|
37
|
+
const still = window.matchMedia("(prefers-reduced-motion: reduce)").matches;
|
|
38
|
+
area.scrollTo({ top: 0, behavior: still ? "auto" : "smooth" });
|
|
39
|
+
const target = element.getAttribute("href");
|
|
40
|
+
if (target?.startsWith("#"))
|
|
41
|
+
document.querySelector(target)?.focus({ preventScroll: true });
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// src/scroll/index.ts
|
|
48
|
+
function scrolls(window, root = window.document) {
|
|
49
|
+
wireScrolls(window, root);
|
|
50
|
+
}
|
|
51
|
+
export {
|
|
52
|
+
backToTopVisible,
|
|
53
|
+
fadeVisible,
|
|
54
|
+
scrolls
|
|
55
|
+
};
|
package/dist/status.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
// src/status/actions/set-status.ts
|
|
2
|
+
function setStatus(host, state, label = "") {
|
|
3
|
+
const words = host.querySelector(".feitio-status .label");
|
|
4
|
+
if (state === null) {
|
|
5
|
+
delete host.dataset.state;
|
|
6
|
+
if (words)
|
|
7
|
+
words.textContent = "";
|
|
8
|
+
return;
|
|
9
|
+
}
|
|
10
|
+
host.dataset.state = state;
|
|
11
|
+
if (words)
|
|
12
|
+
words.textContent = label;
|
|
13
|
+
}
|
|
14
|
+
export {
|
|
15
|
+
setStatus as status
|
|
16
|
+
};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Wires the Dialogs of a page by delegation: a button with data-open-dialog="id" opens that Dialog
|
|
3
|
+
* (modal, centered, closing only with an answer), the focus goes to its field when it asks for a
|
|
4
|
+
* typed word, and that form's danger button only turns on with the right word (data-type-to-confirm).
|
|
5
|
+
* Closing empties the field and turns the button off again.
|
|
6
|
+
*/
|
|
7
|
+
export declare function wireDialogs(window: Window, root: Document | HTMLElement): void;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Wires the form controls of a page by delegation, so controls added later (a playground re-rendering)
|
|
3
|
+
* work too: the Switch says its state in words, the SegmentedControl marks the chosen option, the
|
|
4
|
+
* TimeInput's AM/PM swaps, and a FilterField filters its list and counts what is left.
|
|
5
|
+
*/
|
|
6
|
+
export declare function wireForms(window: Window, root: Document | HTMLElement): void;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/** The other half of the day: AM becomes PM and back. */
|
|
2
|
+
export declare function togglePeriod(period: string): 'AM' | 'PM';
|
|
3
|
+
/** Takes accents and case away, so "acao" finds "Ação". */
|
|
4
|
+
export declare function plainText(text: string): string;
|
|
5
|
+
/** Whether a list item's search words hold every word typed, in any order. */
|
|
6
|
+
export declare function matchesFilter(itemWords: string, typed: string): boolean;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/** Feitio's component behaviors in plain JavaScript. Each one also has its own entry point. */
|
|
2
|
+
export { dialogs } from './dialog/index.js';
|
|
3
|
+
export { forms } from './forms/index.js';
|
|
4
|
+
export { menus } from './menu/index.js';
|
|
5
|
+
export { notices } from './notice/index.js';
|
|
6
|
+
export { scrolls } from './scroll/index.js';
|
|
7
|
+
export { status } from './status/index.js';
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Wires every Feitio Menu under root, each only once: the button opens its list (the popover API does the opening,
|
|
3
|
+
* Esc and the click outside); this places it next to the button, puts the focus on the current item,
|
|
4
|
+
* and moves it with the arrows, Home and End.
|
|
5
|
+
*/
|
|
6
|
+
export declare function wireMenus(window: Window, root: ParentNode): void;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/** Which item gets the focus after a key, in a menu of `count` items where `current` has it (-1: none). */
|
|
2
|
+
export declare function nextMenuIndex(key: string, current: number, count: number): number | undefined;
|
|
3
|
+
/**
|
|
4
|
+
* Where the open menu goes, in the viewport: under its button, aligned to the button's end when it
|
|
5
|
+
* would pass the window's edge, and above it when there is no room below.
|
|
6
|
+
*/
|
|
7
|
+
export declare function menuPosition(button: {
|
|
8
|
+
left: number;
|
|
9
|
+
right: number;
|
|
10
|
+
top: number;
|
|
11
|
+
bottom: number;
|
|
12
|
+
}, menu: {
|
|
13
|
+
width: number;
|
|
14
|
+
height: number;
|
|
15
|
+
}, viewport: {
|
|
16
|
+
width: number;
|
|
17
|
+
height: number;
|
|
18
|
+
}, gap: number): {
|
|
19
|
+
left: number;
|
|
20
|
+
top: number;
|
|
21
|
+
};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { NoticeHost } from '../data/notice.js';
|
|
2
|
+
/**
|
|
3
|
+
* Shows a notice after an action. Hidden, it arrives as placed paper; already on screen, it does not
|
|
4
|
+
* land again: only its time starts over (one paper at a time).
|
|
5
|
+
*/
|
|
6
|
+
export declare function showNotice(host: NoticeHost, notice: HTMLElement): void;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { NoticeHost } from '../data/notice.js';
|
|
2
|
+
/**
|
|
3
|
+
* Starts a timed notice's clock: it counts in JavaScript, frame by frame, not in a CSS animation, so
|
|
4
|
+
* it still runs with reduced motion. The time stops while the pointer or focus is on the notice, and
|
|
5
|
+
* --notice-p carries how far it has run, for the thread around the close button.
|
|
6
|
+
*/
|
|
7
|
+
export declare function startNoticeTimer(host: NoticeHost, notice: HTMLElement): void;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { NoticeHost } from '../data/notice.js';
|
|
2
|
+
/**
|
|
3
|
+
* Wires every notice under root (root itself too, when it is one), each only once: the close button closes it, and a timed notice already on the page
|
|
4
|
+
* (after saving and coming back, say) only starts counting once it shows in the window.
|
|
5
|
+
*/
|
|
6
|
+
export declare function wireNotices(host: NoticeHost, root: ParentNode): void;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
/** A notice's status, as its data-status says: none is information. */
|
|
2
|
+
export type NoticeStatus = 'info' | 'ok' | 'warn' | 'error';
|
|
3
|
+
/**
|
|
4
|
+
* Whether a notice may close by itself: only success and information, and only with a time.
|
|
5
|
+
* Warnings and errors stay until the person closes them.
|
|
6
|
+
*/
|
|
7
|
+
export declare function closesByItself(status: NoticeStatus, timeoutMs: number): boolean;
|
|
8
|
+
/** Reads data-status, where no attribute (or an unknown one) is information. */
|
|
9
|
+
export declare function noticeStatus(attribute: string | null | undefined): NoticeStatus;
|
|
10
|
+
/** How far the time has run, from 0 to 1, given the milliseconds already counted. */
|
|
11
|
+
export declare function noticeProgress(usedMs: number, timeoutMs: number): number;
|
|
12
|
+
/**
|
|
13
|
+
* The time counted after one frame: it only grows while the notice is not paused (no pointer
|
|
14
|
+
* or focus on it), so hovering stops the clock instead of hiding the time it had left.
|
|
15
|
+
*/
|
|
16
|
+
export declare function countFrame(usedMs: number, elapsedMs: number, paused: boolean): number;
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** What the notice actions need from the page: passed in, so a test can hand in its own window. */
|
|
2
|
+
export type NoticeHost = {
|
|
3
|
+
readonly window: Window;
|
|
4
|
+
/** The running clock of each timed notice, to stop it when it closes or starts again. */
|
|
5
|
+
readonly clocks: WeakMap<HTMLElement, () => void>;
|
|
6
|
+
};
|
|
7
|
+
/** How long a leaving notice may take before it is hidden anyway (no animation with reduced motion). */
|
|
8
|
+
export declare const LEAVE_FALLBACK_MS = 600;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export { closesByItself, noticeProgress } from './calculations/notice-timer.js';
|
|
2
|
+
/**
|
|
3
|
+
* The Notice for a page: wires every notice in it and hands back show and close, and wire, for a
|
|
4
|
+
* notice added later (a React component mounting).
|
|
5
|
+
*/
|
|
6
|
+
export declare function notices(window: Window, root?: ParentNode): {
|
|
7
|
+
show: (notice: HTMLElement) => void;
|
|
8
|
+
close: (notice: HTMLElement) => void;
|
|
9
|
+
wire: (root: ParentNode) => void;
|
|
10
|
+
};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Wires every ScrollFade and BackToTop under root to the box that scrolls around it (or the window):
|
|
3
|
+
* the fade shows while there is more below, BackToTop after half a screen, and its click scrolls
|
|
4
|
+
* back up (smoothly, unless motion is reduced) and moves the focus to its target. Each element is
|
|
5
|
+
* wired once, so calling it again after new HTML arrives is safe.
|
|
6
|
+
*/
|
|
7
|
+
export declare function wireScrolls(window: Window, root: ParentNode): void;
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
/** Whether the ScrollFade shows: there is more than a few pixels left below what is in view. */
|
|
2
|
+
export declare function fadeVisible(scrollTop: number, viewHeight: number, scrollHeight: number): boolean;
|
|
3
|
+
/** Whether BackToTop shows: the person has scrolled past half a screen. */
|
|
4
|
+
export declare function backToTopVisible(scrollTop: number, viewHeight: number): boolean;
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shows the feedback on a StatusOverlay's container: "loading", "ok" or "error", with the words for
|
|
3
|
+
* it; null clears it. The status region announces the words to screen readers.
|
|
4
|
+
*/
|
|
5
|
+
export declare function setStatus(host: HTMLElement, state: 'loading' | 'ok' | 'error' | null, label?: string): void;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { setStatus as status } from './actions/set-status.js';
|
package/package.json
CHANGED
|
@@ -1,6 +1,79 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@feitio/vanilla",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Feitio's component behaviors in plain JavaScript, for any HTML: Notice, Menu, Dialog, form controls, scroll and status.",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"feitio",
|
|
7
|
+
"design-system",
|
|
8
|
+
"accessibility",
|
|
9
|
+
"a11y",
|
|
10
|
+
"handmade",
|
|
11
|
+
"vanilla-js",
|
|
12
|
+
"components",
|
|
13
|
+
"dialog",
|
|
14
|
+
"menu",
|
|
15
|
+
"notification"
|
|
16
|
+
],
|
|
17
|
+
"homepage": "https://made-by-maid.github.io/feitio/",
|
|
18
|
+
"bugs": {
|
|
19
|
+
"url": "https://github.com/Made-by-MAID/feitio/issues"
|
|
20
|
+
},
|
|
21
|
+
"license": "MIT",
|
|
22
|
+
"type": "module",
|
|
23
|
+
"repository": {
|
|
24
|
+
"type": "git",
|
|
25
|
+
"url": "git+https://github.com/Made-by-MAID/feitio.git",
|
|
26
|
+
"directory": "packages/vanilla"
|
|
27
|
+
},
|
|
28
|
+
"files": [
|
|
29
|
+
"dist",
|
|
30
|
+
"src",
|
|
31
|
+
"!src/**/*.spec.ts"
|
|
32
|
+
],
|
|
33
|
+
"exports": {
|
|
34
|
+
".": {
|
|
35
|
+
"types": "./dist/types/index.d.ts",
|
|
36
|
+
"default": "./dist/feitio.js"
|
|
37
|
+
},
|
|
38
|
+
"./notice": {
|
|
39
|
+
"types": "./dist/types/notice/index.d.ts",
|
|
40
|
+
"default": "./dist/notice.js"
|
|
41
|
+
},
|
|
42
|
+
"./notice/source": "./src/notice/index.ts",
|
|
43
|
+
"./menu": {
|
|
44
|
+
"types": "./dist/types/menu/index.d.ts",
|
|
45
|
+
"default": "./dist/menu.js"
|
|
46
|
+
},
|
|
47
|
+
"./menu/source": "./src/menu/index.ts",
|
|
48
|
+
"./forms": {
|
|
49
|
+
"types": "./dist/types/forms/index.d.ts",
|
|
50
|
+
"default": "./dist/forms.js"
|
|
51
|
+
},
|
|
52
|
+
"./forms/source": "./src/forms/index.ts",
|
|
53
|
+
"./status": {
|
|
54
|
+
"types": "./dist/types/status/index.d.ts",
|
|
55
|
+
"default": "./dist/status.js"
|
|
56
|
+
},
|
|
57
|
+
"./status/source": "./src/status/index.ts",
|
|
58
|
+
"./scroll": {
|
|
59
|
+
"types": "./dist/types/scroll/index.d.ts",
|
|
60
|
+
"default": "./dist/scroll.js"
|
|
61
|
+
},
|
|
62
|
+
"./scroll/source": "./src/scroll/index.ts",
|
|
63
|
+
"./dialog": {
|
|
64
|
+
"types": "./dist/types/dialog/index.d.ts",
|
|
65
|
+
"default": "./dist/dialog.js"
|
|
66
|
+
},
|
|
67
|
+
"./dialog/source": "./src/dialog/index.ts"
|
|
68
|
+
},
|
|
69
|
+
"scripts": {
|
|
70
|
+
"build": "rm -rf dist && bun build src/index.ts src/notice/index.ts src/menu/index.ts src/forms/index.ts src/status/index.ts src/scroll/index.ts src/dialog/index.ts --outdir dist --format esm --entry-naming [dir]/[name].[ext] --root src && bun run rename && tsc -p tsconfig.build.json && bun scripts/fix-declarations.ts",
|
|
71
|
+
"rename": "mv dist/index.js dist/feitio.js && for m in notice menu forms status scroll dialog; do mv dist/$m/index.js dist/$m.js && rmdir dist/$m; done"
|
|
72
|
+
},
|
|
73
|
+
"dependencies": {
|
|
74
|
+
"@feitio/core": "0.1.0"
|
|
75
|
+
},
|
|
76
|
+
"publishConfig": {
|
|
77
|
+
"access": "public"
|
|
78
|
+
}
|
|
79
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { typedTheWord } from '../calculations/confirm-word.ts';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Wires the Dialogs of a page by delegation: a button with data-open-dialog="id" opens that Dialog
|
|
5
|
+
* (modal, centered, closing only with an answer), the focus goes to its field when it asks for a
|
|
6
|
+
* typed word, and that form's danger button only turns on with the right word (data-type-to-confirm).
|
|
7
|
+
* Closing empties the field and turns the button off again.
|
|
8
|
+
*/
|
|
9
|
+
export function wireDialogs(window: Window, root: Document | HTMLElement): void {
|
|
10
|
+
const document = window.document;
|
|
11
|
+
root.addEventListener('click', (event) => {
|
|
12
|
+
const opener = (event.target as HTMLElement).closest<HTMLElement>('[data-open-dialog]');
|
|
13
|
+
if (!opener) return;
|
|
14
|
+
const dialog = document.getElementById(
|
|
15
|
+
opener.dataset.openDialog ?? '',
|
|
16
|
+
) as HTMLDialogElement | null;
|
|
17
|
+
if (!dialog || dialog.open) return;
|
|
18
|
+
dialog.showModal();
|
|
19
|
+
dialog.querySelector<HTMLInputElement>('[data-type-to-confirm] input')?.focus();
|
|
20
|
+
dialog.addEventListener(
|
|
21
|
+
'close',
|
|
22
|
+
() => {
|
|
23
|
+
const form = dialog.querySelector<HTMLElement>('[data-type-to-confirm]');
|
|
24
|
+
const input = form?.querySelector<HTMLInputElement>('input');
|
|
25
|
+
const danger = form?.querySelector<HTMLButtonElement>('[data-variant="danger"]');
|
|
26
|
+
if (input) input.value = '';
|
|
27
|
+
if (danger) danger.disabled = true;
|
|
28
|
+
},
|
|
29
|
+
{ once: true },
|
|
30
|
+
);
|
|
31
|
+
});
|
|
32
|
+
root.addEventListener('input', (event) => {
|
|
33
|
+
const input = event.target as HTMLInputElement;
|
|
34
|
+
const form = input.closest<HTMLElement>('[data-type-to-confirm]');
|
|
35
|
+
if (!form) return;
|
|
36
|
+
const danger = form.querySelector<HTMLButtonElement>('[data-variant="danger"]');
|
|
37
|
+
if (danger) danger.disabled = !typedTheWord(input.value, form.dataset.typeToConfirm ?? '');
|
|
38
|
+
});
|
|
39
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { wireDialogs } from './actions/wire-dialogs.ts';
|
|
2
|
+
|
|
3
|
+
export { typedTheWord } from './calculations/confirm-word.ts';
|
|
4
|
+
|
|
5
|
+
/** The Dialogs of a page: openers, the typed confirmation, the reset on close. */
|
|
6
|
+
export function dialogs(window: Window, root: Document | HTMLElement = window.document): void {
|
|
7
|
+
wireDialogs(window, root);
|
|
8
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import { matchesFilter, togglePeriod } from '../calculations/form-values.ts';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Wires the form controls of a page by delegation, so controls added later (a playground re-rendering)
|
|
5
|
+
* work too: the Switch says its state in words, the SegmentedControl marks the chosen option, the
|
|
6
|
+
* TimeInput's AM/PM swaps, and a FilterField filters its list and counts what is left.
|
|
7
|
+
*/
|
|
8
|
+
export function wireForms(window: Window, root: Document | HTMLElement): void {
|
|
9
|
+
root.addEventListener('change', (event) => {
|
|
10
|
+
const input = event.target as HTMLInputElement;
|
|
11
|
+
const label = input.closest<HTMLElement>('.feitio-switch');
|
|
12
|
+
if (!label || input.type !== 'checkbox') return;
|
|
13
|
+
const state = label.querySelector('.state');
|
|
14
|
+
if (state)
|
|
15
|
+
state.textContent = input.checked
|
|
16
|
+
? (label.dataset.on ?? 'Ligado')
|
|
17
|
+
: (label.dataset.off ?? 'Desligado');
|
|
18
|
+
});
|
|
19
|
+
root.addEventListener('click', (event) => {
|
|
20
|
+
const target = event.target as HTMLElement;
|
|
21
|
+
const option = target.closest<HTMLButtonElement>('.feitio-segmented button[aria-pressed]');
|
|
22
|
+
if (option && !option.closest('[data-playground] .controls')) {
|
|
23
|
+
for (const sibling of option.parentElement?.querySelectorAll('button[aria-pressed]') ?? [])
|
|
24
|
+
sibling.setAttribute('aria-pressed', String(sibling === option));
|
|
25
|
+
}
|
|
26
|
+
const period = target.closest<HTMLButtonElement>('.feitio-time-field .period');
|
|
27
|
+
if (period && !period.disabled) period.textContent = togglePeriod(period.textContent ?? 'AM');
|
|
28
|
+
});
|
|
29
|
+
const timers = new WeakMap<HTMLElement, number>();
|
|
30
|
+
function filterNow(field: HTMLInputElement, list: HTMLElement): void {
|
|
31
|
+
const document = window.document;
|
|
32
|
+
const listId = list.id;
|
|
33
|
+
const items = [...list.querySelectorAll<HTMLElement>('[data-search]')];
|
|
34
|
+
list.dataset.filtering = '';
|
|
35
|
+
let shown = 0;
|
|
36
|
+
for (const item of items) {
|
|
37
|
+
const match = matchesFilter(item.dataset.search ?? item.textContent ?? '', field.value);
|
|
38
|
+
item.hidden = !match;
|
|
39
|
+
if (match) shown += 1;
|
|
40
|
+
}
|
|
41
|
+
const count = document.querySelector(`[data-count="${listId}"]`);
|
|
42
|
+
if (count) count.textContent = field.value.trim() ? `${shown} / ${items.length}` : '';
|
|
43
|
+
const empty = document.querySelector<HTMLElement>(`[data-empty="${listId}"]`);
|
|
44
|
+
if (empty) empty.hidden = shown > 0;
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Runs the search: on the list here, or, with data-async, by telling the page (a feitio-filter event
|
|
48
|
+
* with the query and a done() to call when its results are in) while the field shows it is busy.
|
|
49
|
+
*/
|
|
50
|
+
function run(field: HTMLInputElement, box: HTMLElement | null): void {
|
|
51
|
+
const list = window.document.getElementById(field.dataset.filter ?? '');
|
|
52
|
+
if (field.dataset.async === undefined) {
|
|
53
|
+
if (box) delete box.dataset.state;
|
|
54
|
+
if (list) filterNow(field, list);
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
if (box) box.dataset.state = 'busy';
|
|
58
|
+
const busy = window.document.querySelector(`[data-count="${field.dataset.filter}"]`);
|
|
59
|
+
if (busy && field.dataset.busyLabel) busy.textContent = field.dataset.busyLabel;
|
|
60
|
+
const query = field.value;
|
|
61
|
+
const done = (): void => {
|
|
62
|
+
if (field.value !== query) return;
|
|
63
|
+
if (box) delete box.dataset.state;
|
|
64
|
+
if (list) filterNow(field, list);
|
|
65
|
+
};
|
|
66
|
+
field.dispatchEvent(
|
|
67
|
+
new CustomEvent('feitio-filter', { bubbles: true, detail: { query, done } }),
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
root.addEventListener('input', (event) => {
|
|
71
|
+
const field = event.target as HTMLInputElement;
|
|
72
|
+
if (!field.dataset?.filter) return;
|
|
73
|
+
const box = field.closest<HTMLElement>('.feitio-filter');
|
|
74
|
+
const wait = Number(field.dataset.debounce ?? 0);
|
|
75
|
+
window.clearTimeout(timers.get(field));
|
|
76
|
+
if (!(wait > 0)) {
|
|
77
|
+
run(field, box);
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
// Debounce: the line under the field fills while the search waits for the typing to stop.
|
|
81
|
+
if (box) {
|
|
82
|
+
box.style.setProperty('--feitio-filter-debounce', `${wait}ms`);
|
|
83
|
+
delete box.dataset.state;
|
|
84
|
+
void box.offsetWidth;
|
|
85
|
+
box.dataset.state = 'waiting';
|
|
86
|
+
}
|
|
87
|
+
timers.set(
|
|
88
|
+
field,
|
|
89
|
+
window.setTimeout(() => run(field, box), wait),
|
|
90
|
+
);
|
|
91
|
+
});
|
|
92
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/** The other half of the day: AM becomes PM and back. */
|
|
2
|
+
export function togglePeriod(period: string): 'AM' | 'PM' {
|
|
3
|
+
return period.trim().toUpperCase() === 'AM' ? 'PM' : 'AM';
|
|
4
|
+
}
|
|
5
|
+
|
|
6
|
+
/** Takes accents and case away, so "acao" finds "Ação". */
|
|
7
|
+
export function plainText(text: string): string {
|
|
8
|
+
return text
|
|
9
|
+
.normalize('NFD')
|
|
10
|
+
.replace(/\p{Diacritic}/gu, '')
|
|
11
|
+
.toLowerCase();
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/** Whether a list item's search words hold every word typed, in any order. */
|
|
15
|
+
export function matchesFilter(itemWords: string, typed: string): boolean {
|
|
16
|
+
const words = plainText(typed).split(/\s+/).filter(Boolean);
|
|
17
|
+
const target = plainText(itemWords);
|
|
18
|
+
return words.every((word) => target.includes(word));
|
|
19
|
+
}
|