loseta 0.18.0 → 0.19.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -1
- package/package.json +1 -1
- package/registry/behaviors/calendar.js +855 -0
- package/registry/behaviors/carousel.js +280 -0
- package/registry/behaviors/hover-overlay.js +180 -0
- package/registry/behaviors/message-scroller.js +147 -0
- package/registry/behaviors/questionnaire.js +91 -0
- package/registry/behaviors/resizable.js +463 -0
- package/registry/components/carousel.css +1 -0
- package/registry/components/hover-card.css +2 -2
- package/registry/components/resizable.css +49 -0
- package/registry/components/tooltip.css +4 -1
- package/registry/examples/calendar.html +387 -326
- package/registry/examples/carousel.html +80 -2
- package/registry/examples/questionnaire.html +2 -2
- package/registry/examples/resizable.html +88 -15
- package/registry/examples/sidebar.html +10 -10
- package/registry/registry.json +85 -1
- package/registry/styles/luma/resizable.css +10 -0
- package/registry/styles/lyra/resizable.css +10 -0
- package/registry/styles/maia/resizable.css +10 -0
- package/registry/styles/mira/resizable.css +10 -0
- package/registry/styles/nova/resizable.css +10 -0
- package/registry/styles/rhea/resizable.css +10 -0
- package/registry/styles/sera/resizable.css +10 -0
- package/registry/styles/vega/resizable.css +10 -0
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
|
|
3
|
+
import { emit, init, isRTL, on } from "./core.js";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* @typedef {object} CarouselState
|
|
7
|
+
* @property {number} index
|
|
8
|
+
* @property {number} target
|
|
9
|
+
* @property {boolean} settling
|
|
10
|
+
* @property {number} timer
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/** @type {WeakMap<HTMLElement, CarouselState>} */
|
|
14
|
+
const states = new WeakMap();
|
|
15
|
+
/** @type {WeakSet<HTMLElement>} */
|
|
16
|
+
const scheduled = new WeakSet();
|
|
17
|
+
|
|
18
|
+
/** @param {HTMLElement} carousel */
|
|
19
|
+
function parts(carousel) {
|
|
20
|
+
const viewport = carousel.querySelector(".carousel-viewport");
|
|
21
|
+
const items = [...carousel.querySelectorAll(".carousel-item")].filter(
|
|
22
|
+
(item) => item instanceof HTMLElement
|
|
23
|
+
);
|
|
24
|
+
const indicators = [...carousel.querySelectorAll(".carousel-indicator")].filter(
|
|
25
|
+
(indicator) => indicator instanceof HTMLElement
|
|
26
|
+
);
|
|
27
|
+
return {
|
|
28
|
+
viewport: viewport instanceof HTMLElement ? viewport : null,
|
|
29
|
+
items,
|
|
30
|
+
indicators
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/** @param {HTMLElement} carousel */
|
|
35
|
+
function horizontal(carousel) {
|
|
36
|
+
return carousel.getAttribute("data-orientation") !== "vertical";
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function reducedMotion() {
|
|
40
|
+
return (
|
|
41
|
+
typeof window.matchMedia === "function" &&
|
|
42
|
+
window.matchMedia("(prefers-reduced-motion: reduce)").matches
|
|
43
|
+
);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* @param {HTMLElement} carousel
|
|
48
|
+
* @param {HTMLElement} viewport
|
|
49
|
+
* @param {HTMLElement[]} items
|
|
50
|
+
*/
|
|
51
|
+
function indexFromScroll(carousel, viewport, items) {
|
|
52
|
+
if (items.length === 0) return 0;
|
|
53
|
+
const viewportRect = viewport.getBoundingClientRect();
|
|
54
|
+
const horizontalScroll = horizontal(carousel);
|
|
55
|
+
let best = 0;
|
|
56
|
+
let distance = Number.POSITIVE_INFINITY;
|
|
57
|
+
|
|
58
|
+
for (let index = 0; index < items.length; index += 1) {
|
|
59
|
+
const item = items[index];
|
|
60
|
+
if (!item) continue;
|
|
61
|
+
const rect = item.getBoundingClientRect();
|
|
62
|
+
const value = horizontalScroll
|
|
63
|
+
? Math.abs(rect.left - viewportRect.left) +
|
|
64
|
+
Math.abs(rect.right - viewportRect.right)
|
|
65
|
+
: Math.abs(rect.top - viewportRect.top) +
|
|
66
|
+
Math.abs(rect.bottom - viewportRect.bottom);
|
|
67
|
+
if (value < distance) {
|
|
68
|
+
distance = value;
|
|
69
|
+
best = index;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
return best;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* @param {HTMLElement} carousel
|
|
78
|
+
* @param {number} index
|
|
79
|
+
*/
|
|
80
|
+
function updateIndicators(carousel, index) {
|
|
81
|
+
const { indicators } = parts(carousel);
|
|
82
|
+
for (let cursor = 0; cursor < indicators.length; cursor += 1) {
|
|
83
|
+
const indicator = indicators[cursor];
|
|
84
|
+
if (!indicator) continue;
|
|
85
|
+
indicator.setAttribute("aria-current", String(cursor === index));
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* @param {HTMLElement} carousel
|
|
91
|
+
* @param {HTMLElement} viewport
|
|
92
|
+
* @param {HTMLElement[]} items
|
|
93
|
+
*/
|
|
94
|
+
function updateOffscreen(carousel, viewport, items) {
|
|
95
|
+
const viewportRect = viewport.getBoundingClientRect();
|
|
96
|
+
const horizontalScroll = horizontal(carousel);
|
|
97
|
+
|
|
98
|
+
for (const item of items) {
|
|
99
|
+
const rect = item.getBoundingClientRect();
|
|
100
|
+
const visible = horizontalScroll
|
|
101
|
+
? rect.right > viewportRect.left + 1 && rect.left < viewportRect.right - 1
|
|
102
|
+
: rect.bottom > viewportRect.top + 1 && rect.top < viewportRect.bottom - 1;
|
|
103
|
+
item.setAttribute("aria-hidden", String(!visible));
|
|
104
|
+
item.toggleAttribute("inert", !visible);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/** @param {HTMLElement} carousel */
|
|
109
|
+
function ensure(carousel) {
|
|
110
|
+
const existing = states.get(carousel);
|
|
111
|
+
if (existing) return existing;
|
|
112
|
+
|
|
113
|
+
const { viewport, items, indicators } = parts(carousel);
|
|
114
|
+
const current = indicators.findIndex(
|
|
115
|
+
(indicator) => indicator.getAttribute("aria-current") === "true"
|
|
116
|
+
);
|
|
117
|
+
const index =
|
|
118
|
+
current >= 0
|
|
119
|
+
? current
|
|
120
|
+
: viewport
|
|
121
|
+
? indexFromScroll(carousel, viewport, items)
|
|
122
|
+
: 0;
|
|
123
|
+
const state = { index, target: index, settling: false, timer: 0 };
|
|
124
|
+
states.set(carousel, state);
|
|
125
|
+
|
|
126
|
+
if (viewport) updateOffscreen(carousel, viewport, items);
|
|
127
|
+
updateIndicators(carousel, index);
|
|
128
|
+
return state;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* @param {HTMLElement} carousel
|
|
133
|
+
* @param {CarouselState} state
|
|
134
|
+
*/
|
|
135
|
+
function update(carousel, state) {
|
|
136
|
+
const { viewport, items } = parts(carousel);
|
|
137
|
+
if (!viewport) return;
|
|
138
|
+
const index = indexFromScroll(carousel, viewport, items);
|
|
139
|
+
updateOffscreen(carousel, viewport, items);
|
|
140
|
+
|
|
141
|
+
if (state.settling) {
|
|
142
|
+
if (index !== state.target) return;
|
|
143
|
+
state.settling = false;
|
|
144
|
+
if (state.timer !== 0) {
|
|
145
|
+
window.clearTimeout(state.timer);
|
|
146
|
+
state.timer = 0;
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
const changed = index !== state.index;
|
|
151
|
+
state.index = index;
|
|
152
|
+
updateIndicators(carousel, index);
|
|
153
|
+
if (changed) emit(carousel, "change", { index });
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* @param {HTMLElement} carousel
|
|
158
|
+
* @param {CarouselState} state
|
|
159
|
+
* @param {number} index
|
|
160
|
+
*/
|
|
161
|
+
function go(carousel, state, index) {
|
|
162
|
+
const { items } = parts(carousel);
|
|
163
|
+
if (items.length === 0) return;
|
|
164
|
+
|
|
165
|
+
const clamped = Math.max(0, Math.min(index, items.length - 1));
|
|
166
|
+
const item = items[clamped];
|
|
167
|
+
if (!item) return;
|
|
168
|
+
|
|
169
|
+
const changed = clamped !== state.index;
|
|
170
|
+
state.index = clamped;
|
|
171
|
+
state.target = clamped;
|
|
172
|
+
state.settling = true;
|
|
173
|
+
updateIndicators(carousel, clamped);
|
|
174
|
+
|
|
175
|
+
if (state.timer !== 0) window.clearTimeout(state.timer);
|
|
176
|
+
state.timer = window.setTimeout(() => {
|
|
177
|
+
state.settling = false;
|
|
178
|
+
state.target = state.index;
|
|
179
|
+
state.timer = 0;
|
|
180
|
+
}, 600);
|
|
181
|
+
|
|
182
|
+
const horizontalScroll = horizontal(carousel);
|
|
183
|
+
item.scrollIntoView({
|
|
184
|
+
behavior: reducedMotion() ? "auto" : "smooth",
|
|
185
|
+
block: horizontalScroll ? "nearest" : "start",
|
|
186
|
+
inline: horizontalScroll ? "start" : "nearest"
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
if (changed) emit(carousel, "change", { index: clamped });
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
on(
|
|
193
|
+
"click",
|
|
194
|
+
".carousel [data-slot='previous'], .carousel [data-slot='next']",
|
|
195
|
+
(event, element) => {
|
|
196
|
+
if (!(element instanceof HTMLElement)) return;
|
|
197
|
+
const carousel = element.closest(".carousel");
|
|
198
|
+
if (!(carousel instanceof HTMLElement)) return;
|
|
199
|
+
const state = ensure(carousel);
|
|
200
|
+
go(
|
|
201
|
+
carousel,
|
|
202
|
+
state,
|
|
203
|
+
state.index + (element.getAttribute("data-slot") === "next" ? 1 : -1)
|
|
204
|
+
);
|
|
205
|
+
event.preventDefault();
|
|
206
|
+
return true;
|
|
207
|
+
}
|
|
208
|
+
);
|
|
209
|
+
|
|
210
|
+
on("click", ".carousel .carousel-indicator", (event, element) => {
|
|
211
|
+
if (!(element instanceof HTMLElement)) return;
|
|
212
|
+
const carousel = element.closest(".carousel");
|
|
213
|
+
if (!(carousel instanceof HTMLElement)) return;
|
|
214
|
+
const { indicators } = parts(carousel);
|
|
215
|
+
const index = indicators.indexOf(element);
|
|
216
|
+
if (index === -1) return;
|
|
217
|
+
const state = ensure(carousel);
|
|
218
|
+
go(carousel, state, index);
|
|
219
|
+
event.preventDefault();
|
|
220
|
+
return true;
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
on("keydown", ".carousel-viewport", (event, element) => {
|
|
224
|
+
if (!(event instanceof KeyboardEvent)) return;
|
|
225
|
+
if (!(element instanceof HTMLElement)) return;
|
|
226
|
+
if (event.target !== element) return;
|
|
227
|
+
|
|
228
|
+
const carousel = element.closest(".carousel");
|
|
229
|
+
if (!(carousel instanceof HTMLElement)) return;
|
|
230
|
+
const state = ensure(carousel);
|
|
231
|
+
const { items } = parts(carousel);
|
|
232
|
+
if (items.length === 0) return;
|
|
233
|
+
|
|
234
|
+
const horizontalScroll = horizontal(carousel);
|
|
235
|
+
const rtl = horizontalScroll && isRTL(carousel);
|
|
236
|
+
const nextKey = horizontalScroll
|
|
237
|
+
? rtl
|
|
238
|
+
? "ArrowLeft"
|
|
239
|
+
: "ArrowRight"
|
|
240
|
+
: "ArrowDown";
|
|
241
|
+
const previousKey = horizontalScroll
|
|
242
|
+
? rtl
|
|
243
|
+
? "ArrowRight"
|
|
244
|
+
: "ArrowLeft"
|
|
245
|
+
: "ArrowUp";
|
|
246
|
+
|
|
247
|
+
if (event.key === nextKey) {
|
|
248
|
+
go(carousel, state, state.index + 1);
|
|
249
|
+
event.preventDefault();
|
|
250
|
+
return true;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
if (event.key === previousKey) {
|
|
254
|
+
go(carousel, state, state.index - 1);
|
|
255
|
+
event.preventDefault();
|
|
256
|
+
return true;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
if (event.key === "Home" || event.key === "End") {
|
|
260
|
+
go(carousel, state, event.key === "Home" ? 0 : items.length - 1);
|
|
261
|
+
event.preventDefault();
|
|
262
|
+
return true;
|
|
263
|
+
}
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
on("scroll", ".carousel-viewport", (_event, element) => {
|
|
267
|
+
if (!(element instanceof HTMLElement)) return;
|
|
268
|
+
const carousel = element.closest(".carousel");
|
|
269
|
+
if (!(carousel instanceof HTMLElement)) return;
|
|
270
|
+
if (scheduled.has(carousel)) return;
|
|
271
|
+
|
|
272
|
+
scheduled.add(carousel);
|
|
273
|
+
requestAnimationFrame(() => {
|
|
274
|
+
scheduled.delete(carousel);
|
|
275
|
+
if (!carousel.isConnected) return;
|
|
276
|
+
update(carousel, ensure(carousel));
|
|
277
|
+
});
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
init();
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
|
|
3
|
+
import { emit, init, on, onDocument } from "./core.js";
|
|
4
|
+
|
|
5
|
+
const WRAPPER_SELECTOR = ".tooltip, .hover-card";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @typedef {object} OverlayState
|
|
9
|
+
* @property {boolean} hovered
|
|
10
|
+
* @property {boolean} focused
|
|
11
|
+
* @property {boolean} dismissed
|
|
12
|
+
*/
|
|
13
|
+
|
|
14
|
+
/** @type {WeakMap<HTMLElement, OverlayState>} */
|
|
15
|
+
const states = new WeakMap();
|
|
16
|
+
/** @type {WeakSet<HTMLElement>} */
|
|
17
|
+
const ignoreFocus = new WeakSet();
|
|
18
|
+
/** @type {HTMLElement[]} */
|
|
19
|
+
const open = [];
|
|
20
|
+
|
|
21
|
+
/** @param {HTMLElement} wrapper */
|
|
22
|
+
function panelFor(wrapper) {
|
|
23
|
+
const selector = wrapper.matches(".tooltip")
|
|
24
|
+
? ":scope > [data-slot='content']"
|
|
25
|
+
: ":scope > [data-popover]";
|
|
26
|
+
const panel = wrapper.querySelector(selector);
|
|
27
|
+
return panel instanceof HTMLElement ? panel : null;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
/** @param {HTMLElement} wrapper @param {HTMLElement} panel */
|
|
31
|
+
function triggerFor(wrapper, panel) {
|
|
32
|
+
for (const element of wrapper.querySelectorAll(
|
|
33
|
+
"button, a[href], input, select, textarea, [tabindex]"
|
|
34
|
+
)) {
|
|
35
|
+
if (panel.contains(element)) continue;
|
|
36
|
+
if (element instanceof HTMLElement) return element;
|
|
37
|
+
}
|
|
38
|
+
return null;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/** @param {HTMLElement} wrapper */
|
|
42
|
+
function stateFor(wrapper) {
|
|
43
|
+
const existing = states.get(wrapper);
|
|
44
|
+
if (existing) return existing;
|
|
45
|
+
const state = { hovered: false, focused: false, dismissed: false };
|
|
46
|
+
states.set(wrapper, state);
|
|
47
|
+
return state;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/** @param {HTMLElement} wrapper */
|
|
51
|
+
function openOverlay(wrapper) {
|
|
52
|
+
const panel = panelFor(wrapper);
|
|
53
|
+
if (!panel) return;
|
|
54
|
+
if (states.get(wrapper)?.dismissed) return;
|
|
55
|
+
if (wrapper.matches(".tooltip") && (panel.textContent ?? "").trim() === "") {
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
if (open.includes(wrapper)) return;
|
|
59
|
+
|
|
60
|
+
for (const other of [...open]) {
|
|
61
|
+
if (other !== wrapper) closeOverlay(other, false);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
panel.setAttribute("aria-hidden", "false");
|
|
65
|
+
open.push(wrapper);
|
|
66
|
+
emit(wrapper, "toggle", { open: true });
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* @param {HTMLElement} wrapper
|
|
71
|
+
* @param {boolean} focusTrigger
|
|
72
|
+
*/
|
|
73
|
+
function closeOverlay(wrapper, focusTrigger) {
|
|
74
|
+
const panel = panelFor(wrapper);
|
|
75
|
+
if (panel) panel.setAttribute("aria-hidden", "true");
|
|
76
|
+
|
|
77
|
+
if (focusTrigger) {
|
|
78
|
+
const state = states.get(wrapper);
|
|
79
|
+
if (state) {
|
|
80
|
+
state.dismissed = true;
|
|
81
|
+
state.focused = false;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const index = open.indexOf(wrapper);
|
|
86
|
+
if (index !== -1) {
|
|
87
|
+
open.splice(index, 1);
|
|
88
|
+
emit(wrapper, "toggle", { open: false });
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if (focusTrigger && panel) {
|
|
92
|
+
const trigger = triggerFor(wrapper, panel);
|
|
93
|
+
if (trigger) {
|
|
94
|
+
if (document.activeElement !== trigger) ignoreFocus.add(wrapper);
|
|
95
|
+
trigger.focus({ preventScroll: true });
|
|
96
|
+
queueMicrotask(() => ignoreFocus.delete(wrapper));
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** @param {HTMLElement} wrapper */
|
|
102
|
+
function sync(wrapper) {
|
|
103
|
+
const state = stateFor(wrapper);
|
|
104
|
+
if (state.hovered || state.focused) {
|
|
105
|
+
openOverlay(wrapper);
|
|
106
|
+
} else {
|
|
107
|
+
closeOverlay(wrapper, false);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
on("pointerover", WRAPPER_SELECTOR, (_event, element) => {
|
|
112
|
+
if (!(element instanceof HTMLElement)) return;
|
|
113
|
+
const state = stateFor(element);
|
|
114
|
+
if (!state.hovered) state.dismissed = false;
|
|
115
|
+
state.hovered = true;
|
|
116
|
+
openOverlay(element);
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
on("pointerout", WRAPPER_SELECTOR, (event, element) => {
|
|
120
|
+
if (!(element instanceof HTMLElement)) return;
|
|
121
|
+
const related = event instanceof PointerEvent ? event.relatedTarget : null;
|
|
122
|
+
if (related instanceof Node && element.contains(related)) return;
|
|
123
|
+
const state = states.get(element);
|
|
124
|
+
if (!state) return;
|
|
125
|
+
state.hovered = false;
|
|
126
|
+
state.dismissed = false;
|
|
127
|
+
sync(element);
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
on("focusin", WRAPPER_SELECTOR, (_event, element) => {
|
|
131
|
+
if (!(element instanceof HTMLElement)) return;
|
|
132
|
+
const state = stateFor(element);
|
|
133
|
+
if (ignoreFocus.has(element)) {
|
|
134
|
+
ignoreFocus.delete(element);
|
|
135
|
+
state.focused = false;
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
138
|
+
if (!state.focused) state.dismissed = false;
|
|
139
|
+
state.focused = true;
|
|
140
|
+
openOverlay(element);
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
on("focusout", WRAPPER_SELECTOR, (event, element) => {
|
|
144
|
+
if (!(element instanceof HTMLElement) || !(event instanceof FocusEvent)) {
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
const related = event.relatedTarget;
|
|
148
|
+
if (related instanceof Node && element.contains(related)) return;
|
|
149
|
+
const state = states.get(element);
|
|
150
|
+
if (!state) return;
|
|
151
|
+
state.focused = false;
|
|
152
|
+
sync(element);
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
onDocument(
|
|
156
|
+
"keydown",
|
|
157
|
+
(event) => {
|
|
158
|
+
if (!(event instanceof KeyboardEvent) || event.key !== "Escape") return;
|
|
159
|
+
|
|
160
|
+
let wrapper = open[open.length - 1] ?? null;
|
|
161
|
+
if (!wrapper) {
|
|
162
|
+
for (const element of document.querySelectorAll(WRAPPER_SELECTOR)) {
|
|
163
|
+
if (!(element instanceof HTMLElement)) continue;
|
|
164
|
+
if (!(element.matches(":hover") || element.matches(":focus-within"))) {
|
|
165
|
+
continue;
|
|
166
|
+
}
|
|
167
|
+
if (!panelFor(element)) continue;
|
|
168
|
+
wrapper = element;
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
if (!wrapper) return;
|
|
172
|
+
|
|
173
|
+
closeOverlay(wrapper, true);
|
|
174
|
+
event.preventDefault();
|
|
175
|
+
return true;
|
|
176
|
+
},
|
|
177
|
+
10
|
|
178
|
+
);
|
|
179
|
+
|
|
180
|
+
init();
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
|
|
3
|
+
import { emit, init, on } from "./core.js";
|
|
4
|
+
|
|
5
|
+
const FOLLOW_THRESHOLD = 24;
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @typedef {object} ScrollerState
|
|
9
|
+
* @property {boolean} following
|
|
10
|
+
* @property {MutationObserver} observer
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/** @type {WeakMap<HTMLElement, ScrollerState>} */
|
|
14
|
+
const states = new WeakMap();
|
|
15
|
+
|
|
16
|
+
/** @param {HTMLElement} viewport */
|
|
17
|
+
function distance(viewport) {
|
|
18
|
+
return viewport.scrollHeight - viewport.scrollTop - viewport.clientHeight;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/** @param {HTMLElement} viewport */
|
|
22
|
+
function atBottom(viewport) {
|
|
23
|
+
return distance(viewport) <= FOLLOW_THRESHOLD;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** @param {HTMLElement} scroller */
|
|
27
|
+
function parts(scroller) {
|
|
28
|
+
const viewport = scroller.querySelector(".message-scroller-viewport");
|
|
29
|
+
const content = scroller.querySelector("[data-slot='content']");
|
|
30
|
+
const jump = scroller.querySelector("[data-slot='jump']");
|
|
31
|
+
return {
|
|
32
|
+
viewport: viewport instanceof HTMLElement ? viewport : null,
|
|
33
|
+
content: content instanceof HTMLElement ? content : null,
|
|
34
|
+
jump: jump instanceof HTMLElement ? jump : null
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* @param {HTMLElement} scroller
|
|
40
|
+
* @param {ScrollerState} state
|
|
41
|
+
* @param {HTMLElement} viewport
|
|
42
|
+
*/
|
|
43
|
+
function update(scroller, state, viewport) {
|
|
44
|
+
const following = atBottom(viewport);
|
|
45
|
+
const { jump } = parts(scroller);
|
|
46
|
+
if (jump) jump.hidden = following;
|
|
47
|
+
if (following === state.following) return;
|
|
48
|
+
|
|
49
|
+
state.following = following;
|
|
50
|
+
emit(scroller, "change", { following });
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* @param {HTMLElement} scroller
|
|
55
|
+
* @param {ScrollerState} state
|
|
56
|
+
* @param {HTMLElement} viewport
|
|
57
|
+
*/
|
|
58
|
+
function handleMutation(scroller, state, viewport) {
|
|
59
|
+
// Appended messages do not move scrollTop, so the reading position is kept
|
|
60
|
+
// when the user is not following. Native scroll anchoring handles prepends.
|
|
61
|
+
if (state.following) {
|
|
62
|
+
requestAnimationFrame(() => {
|
|
63
|
+
if (scroller.isConnected && state.following) {
|
|
64
|
+
viewport.scrollTop = viewport.scrollHeight;
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/** @param {HTMLElement} scroller */
|
|
71
|
+
function ensure(scroller) {
|
|
72
|
+
const existing = states.get(scroller);
|
|
73
|
+
if (existing) return existing;
|
|
74
|
+
|
|
75
|
+
const { viewport, content, jump } = parts(scroller);
|
|
76
|
+
if (!viewport || !content) return null;
|
|
77
|
+
|
|
78
|
+
/** @type {ScrollerState} */
|
|
79
|
+
const state = {
|
|
80
|
+
following: atBottom(viewport),
|
|
81
|
+
observer: new MutationObserver(() => {
|
|
82
|
+
if (!scroller.isConnected) {
|
|
83
|
+
state.observer.disconnect();
|
|
84
|
+
states.delete(scroller);
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
handleMutation(scroller, state, viewport);
|
|
88
|
+
})
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
state.observer.observe(content, { childList: true, subtree: true });
|
|
92
|
+
states.set(scroller, state);
|
|
93
|
+
if (jump) jump.hidden = state.following;
|
|
94
|
+
return state;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Mounts every message scroller under a root. The entry module calls this for
|
|
99
|
+
* the initial document; consumers can call it for a late-mounted subtree.
|
|
100
|
+
* @param {Document | Element} root
|
|
101
|
+
*/
|
|
102
|
+
export function mount(root = document) {
|
|
103
|
+
for (const scroller of root.querySelectorAll(".message-scroller")) {
|
|
104
|
+
if (scroller instanceof HTMLElement) ensure(scroller);
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
on("scroll", ".message-scroller-viewport", (_event, element) => {
|
|
109
|
+
if (!(element instanceof HTMLElement)) return;
|
|
110
|
+
const scroller = element.closest(".message-scroller");
|
|
111
|
+
if (!(scroller instanceof HTMLElement)) return;
|
|
112
|
+
const state = ensure(scroller);
|
|
113
|
+
if (state) update(scroller, state, element);
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
on("click", ".message-scroller [data-slot='jump']", (event, element) => {
|
|
117
|
+
if (!(element instanceof HTMLElement)) return;
|
|
118
|
+
const scroller = element.closest(".message-scroller");
|
|
119
|
+
if (!(scroller instanceof HTMLElement)) return;
|
|
120
|
+
const { viewport } = parts(scroller);
|
|
121
|
+
if (!viewport) return;
|
|
122
|
+
|
|
123
|
+
const state = ensure(scroller);
|
|
124
|
+
if (!state) return;
|
|
125
|
+
|
|
126
|
+
element.hidden = true;
|
|
127
|
+
viewport.scrollTo({ top: viewport.scrollHeight, behavior: "auto" });
|
|
128
|
+
if (!state.following) {
|
|
129
|
+
state.following = true;
|
|
130
|
+
emit(scroller, "change", { following: true });
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
event.preventDefault();
|
|
134
|
+
return true;
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
if (typeof document !== "undefined") {
|
|
138
|
+
if (document.readyState === "loading") {
|
|
139
|
+
document.addEventListener("DOMContentLoaded", () => mount(document), {
|
|
140
|
+
once: true
|
|
141
|
+
});
|
|
142
|
+
} else {
|
|
143
|
+
mount(document);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
init();
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
|
|
3
|
+
import { emit, focusWhenVisible, init, on } from "./core.js";
|
|
4
|
+
|
|
5
|
+
/** @param {HTMLElement} root */
|
|
6
|
+
function readSteps(root) {
|
|
7
|
+
const steps = [...root.querySelectorAll("[data-slot='item']")].filter(
|
|
8
|
+
(step) => step instanceof HTMLElement
|
|
9
|
+
);
|
|
10
|
+
let index = steps.findIndex(
|
|
11
|
+
(step) => step.getAttribute("aria-current") === "step"
|
|
12
|
+
);
|
|
13
|
+
if (index === -1) {
|
|
14
|
+
index = steps.findIndex((step) => !step.hasAttribute("hidden"));
|
|
15
|
+
}
|
|
16
|
+
return { steps, index: index >= 0 ? index : 0 };
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* @param {HTMLElement} root
|
|
21
|
+
* @param {HTMLElement[]} steps
|
|
22
|
+
* @param {number} index
|
|
23
|
+
*/
|
|
24
|
+
function go(root, steps, index) {
|
|
25
|
+
if (steps.length === 0) return;
|
|
26
|
+
const next = Math.max(0, Math.min(index, steps.length - 1));
|
|
27
|
+
|
|
28
|
+
steps.forEach((step, cursor) => {
|
|
29
|
+
step.hidden = cursor !== next;
|
|
30
|
+
if (cursor === next) {
|
|
31
|
+
step.setAttribute("aria-current", "step");
|
|
32
|
+
} else {
|
|
33
|
+
step.removeAttribute("aria-current");
|
|
34
|
+
}
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
const progress = root.querySelector("progress");
|
|
38
|
+
if (progress instanceof HTMLProgressElement) {
|
|
39
|
+
progress.max = steps.length;
|
|
40
|
+
progress.value = next + 1;
|
|
41
|
+
progress.textContent = `${next + 1} of ${steps.length}`;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const label = root.querySelector("[data-slot='progress-label']");
|
|
45
|
+
if (label) {
|
|
46
|
+
label.textContent = `Question ${next + 1} of ${steps.length}`;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const previous = root.querySelector("[data-slot='previous']");
|
|
50
|
+
if (previous instanceof HTMLButtonElement) {
|
|
51
|
+
previous.disabled = next === 0;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const forward = root.querySelector("[data-slot='next']");
|
|
55
|
+
if (forward instanceof HTMLButtonElement) {
|
|
56
|
+
forward.disabled = next === steps.length - 1;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
const control = steps[next]?.querySelector(
|
|
60
|
+
"input, button, select, textarea"
|
|
61
|
+
);
|
|
62
|
+
if (control instanceof HTMLElement && !control.matches(":disabled")) {
|
|
63
|
+
focusWhenVisible(control);
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
emit(root, "change", { step: next + 1, total: steps.length });
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
on(
|
|
70
|
+
"click",
|
|
71
|
+
".questionnaire [data-slot='previous'], .questionnaire [data-slot='next']",
|
|
72
|
+
(event, element) => {
|
|
73
|
+
if (!(element instanceof HTMLElement)) return;
|
|
74
|
+
const root = element.closest(".questionnaire");
|
|
75
|
+
if (!(root instanceof HTMLElement)) return;
|
|
76
|
+
|
|
77
|
+
const { steps, index } = readSteps(root);
|
|
78
|
+
const direction = element.getAttribute("data-slot") === "next" ? 1 : -1;
|
|
79
|
+
const next = index + direction;
|
|
80
|
+
if (next < 0 || next >= steps.length) {
|
|
81
|
+
event.preventDefault();
|
|
82
|
+
return true;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
go(root, steps, next);
|
|
86
|
+
event.preventDefault();
|
|
87
|
+
return true;
|
|
88
|
+
}
|
|
89
|
+
);
|
|
90
|
+
|
|
91
|
+
init();
|