stimeo-ui 0.11.0 → 0.13.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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +107 -0
- data/dist/cable/index.js +461 -154
- data/dist/controllers/calendar_controller.js +10 -2
- data/dist/controllers/carousel_controller.js +1 -1
- data/dist/controllers/command_palette_controller.js +2 -2
- data/dist/controllers/count_up_controller.js +74 -35
- data/dist/controllers/date_range_picker_controller.js +10 -10
- data/dist/controllers/drawer_controller.js +2 -2
- data/dist/controllers/intersection_controller.js +55 -8
- data/dist/controllers/lazy_frame_controller.js +81 -19
- data/dist/controllers/optimistic_controller.js +141 -20
- data/dist/controllers/pointer_drag_controller.js +4 -0
- data/dist/controllers/reading_progress_controller.js +177 -6
- data/dist/controllers/scrollspy_controller.js +13 -2
- data/dist/controllers/smart_sticky_header_controller.js +52 -10
- data/dist/controllers/sortable_controller.js +241 -67
- data/dist/controllers/sticky_observer_controller.js +13 -2
- data/dist/controllers/toast_controller.js +1 -1
- data/dist/index.js +587 -183
- data/lib/stimeo/ui/version.rb +1 -1
- metadata +2 -2
|
@@ -51,6 +51,15 @@ function parseISOMonthString(monthStr) {
|
|
|
51
51
|
function toISOMonthString(date) {
|
|
52
52
|
return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, "0")}`;
|
|
53
53
|
}
|
|
54
|
+
function monthLabelFormatter(locale) {
|
|
55
|
+
const options = { month: "long", year: "numeric" };
|
|
56
|
+
try {
|
|
57
|
+
return new Intl.DateTimeFormat(locale, options);
|
|
58
|
+
} catch (error) {
|
|
59
|
+
if (!(error instanceof RangeError)) throw error;
|
|
60
|
+
return new Intl.DateTimeFormat("en", options);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
54
63
|
|
|
55
64
|
// src/utils/safe_timeout.ts
|
|
56
65
|
var TimerRegistry = class {
|
|
@@ -288,8 +297,7 @@ var CalendarController = class extends Controller {
|
|
|
288
297
|
const month = monthStart.getMonth() + 1;
|
|
289
298
|
if (this.hasLabelTarget) {
|
|
290
299
|
const lang = document.documentElement.lang || "en";
|
|
291
|
-
|
|
292
|
-
this.labelTarget.textContent = formatter.format(monthStart);
|
|
300
|
+
this.labelTarget.textContent = monthLabelFormatter(lang).format(monthStart);
|
|
293
301
|
}
|
|
294
302
|
const days = this.#calculateGridDays(year, month);
|
|
295
303
|
const dayElements = this.dayTargets;
|
|
@@ -252,7 +252,7 @@ var CarouselController = class extends Controller {
|
|
|
252
252
|
#handledEvents = /* @__PURE__ */ new WeakSet();
|
|
253
253
|
/**
|
|
254
254
|
* Whether `connect()` has run. Scheduling is already inert outside that window
|
|
255
|
-
* (
|
|
255
|
+
* (`MicrotaskCoalescer`), so this gates the Value callbacks Stimulus
|
|
256
256
|
* delivers ahead of `connect()`.
|
|
257
257
|
*/
|
|
258
258
|
#connected = false;
|
|
@@ -512,7 +512,7 @@ var CommandPaletteController = class _CommandPaletteController extends Controlle
|
|
|
512
512
|
* re-deriving from the declarative `open` Value (which would slam a user-opened
|
|
513
513
|
* palette shut). The `open` Value only seeds the initial state of a genuinely
|
|
514
514
|
* fresh render. We normalize to a clean closed baseline first so {@link open}
|
|
515
|
-
* runs its full setup — the
|
|
515
|
+
* runs its full setup — the `FocusTrap` is a fresh instance after a
|
|
516
516
|
* reconnect and must be re-activated.
|
|
517
517
|
*/
|
|
518
518
|
connect() {
|
|
@@ -617,7 +617,7 @@ var CommandPaletteController = class _CommandPaletteController extends Controlle
|
|
|
617
617
|
}
|
|
618
618
|
/**
|
|
619
619
|
* Combobox navigation keys (arrows / Home / End / Enter), bound to the input.
|
|
620
|
-
* `Tab` (focus trap) and `Escape` (close) are owned by the
|
|
620
|
+
* `Tab` (focus trap) and `Escape` (close) are owned by the shared `FocusTrap` at
|
|
621
621
|
* the document level, so they work no matter which element inside the dialog has
|
|
622
622
|
* focus — not only the input.
|
|
623
623
|
*/
|
|
@@ -2,29 +2,58 @@ import { Controller } from '@hotwired/stimulus';
|
|
|
2
2
|
|
|
3
3
|
// src/controllers/count_up_controller.ts
|
|
4
4
|
|
|
5
|
+
// src/utils/authored_integer.ts
|
|
6
|
+
var TOKEN_BODY = /\d[\d.,'’’ _]*\d|\d/;
|
|
7
|
+
var SIGN_OPENER = /[\s([]/;
|
|
8
|
+
function authoredInteger(text) {
|
|
9
|
+
const firstDigit = /\d/.exec(text);
|
|
10
|
+
if (firstDigit === null) return null;
|
|
11
|
+
const at = firstDigit.index;
|
|
12
|
+
const signed = at > 0 && text[at - 1] === "-" && (at === 1 || SIGN_OPENER.test(text[at - 2]));
|
|
13
|
+
const body = TOKEN_BODY.exec(text.slice(at));
|
|
14
|
+
const runs = body[0].split(/\D+/);
|
|
15
|
+
const separators = body[0].match(/\D+/g) ?? [];
|
|
16
|
+
const lead = runs[0];
|
|
17
|
+
const heads = lead.length <= 3 && !lead.startsWith("0");
|
|
18
|
+
let digits = lead;
|
|
19
|
+
for (let i = 1; heads && i < runs.length; i += 1) {
|
|
20
|
+
if (separators[i - 1]?.length !== 1 || runs[i]?.length !== 3) break;
|
|
21
|
+
digits += runs[i];
|
|
22
|
+
}
|
|
23
|
+
const magnitude = Number.parseInt(digits, 10);
|
|
24
|
+
return signed ? -magnitude || 0 : magnitude;
|
|
25
|
+
}
|
|
26
|
+
|
|
5
27
|
// src/utils/reduced_motion.ts
|
|
6
28
|
function prefersReducedMotion() {
|
|
7
29
|
return typeof window.matchMedia === "function" && window.matchMedia("(prefers-reduced-motion: reduce)").matches;
|
|
8
30
|
}
|
|
9
31
|
|
|
10
32
|
// src/controllers/count_up_controller.ts
|
|
33
|
+
var DEFAULT_DURATION = 1200;
|
|
11
34
|
var CountUpController = class extends Controller {
|
|
12
35
|
static values = {
|
|
13
|
-
duration: { type: Number, default:
|
|
36
|
+
duration: { type: Number, default: DEFAULT_DURATION },
|
|
14
37
|
from: { type: Number, default: 0 },
|
|
15
38
|
once: { type: Boolean, default: true }
|
|
16
39
|
};
|
|
17
40
|
static actions = ["start"];
|
|
18
41
|
static events = ["end"];
|
|
19
42
|
#frame = null;
|
|
20
|
-
/** The
|
|
21
|
-
#
|
|
43
|
+
/** The animation length, with a declaration outside its domain read as the default. */
|
|
44
|
+
get #duration() {
|
|
45
|
+
return Number.isFinite(this.durationValue) && this.durationValue > 0 ? this.durationValue : DEFAULT_DURATION;
|
|
46
|
+
}
|
|
47
|
+
/** The starting value, with a declaration that is not a finite number read as zero. */
|
|
48
|
+
get #from() {
|
|
49
|
+
return Number.isFinite(this.fromValue) ? this.fromValue : 0;
|
|
50
|
+
}
|
|
22
51
|
connect() {
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
52
|
+
const ticker = this.#ownedTicker();
|
|
53
|
+
if (ticker === null) return;
|
|
54
|
+
const authored = ticker.getAttribute("aria-label");
|
|
55
|
+
this.#unwrap(ticker, authored);
|
|
56
|
+
if (authored !== null) this.element.setAttribute("data-count-up-done", "true");
|
|
28
57
|
}
|
|
29
58
|
disconnect() {
|
|
30
59
|
if (this.#frame !== null) this.#settle();
|
|
@@ -36,27 +65,22 @@ var CountUpController = class extends Controller {
|
|
|
36
65
|
start() {
|
|
37
66
|
if (this.#frame !== null) return;
|
|
38
67
|
if (this.onceValue && this.element.hasAttribute("data-count-up-done")) return;
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
68
|
+
const node = this.#numericNode();
|
|
69
|
+
if (node === null) return;
|
|
70
|
+
const authored = node.data;
|
|
71
|
+
const target = authoredInteger(authored);
|
|
42
72
|
if (prefersReducedMotion()) {
|
|
43
73
|
this.element.setAttribute("data-count-up-done", "true");
|
|
44
74
|
this.dispatch("end", { detail: { value: target } });
|
|
45
75
|
return;
|
|
46
76
|
}
|
|
47
|
-
const
|
|
48
|
-
|
|
49
|
-
this.element.setAttribute("data-count-up-original-label", authored);
|
|
50
|
-
}
|
|
51
|
-
this.element.setAttribute("data-count-up-label", "true");
|
|
52
|
-
this.element.setAttribute("aria-label", this.#finalText);
|
|
77
|
+
const ticker = this.#wrap(node, authored);
|
|
78
|
+
const from = this.#from;
|
|
53
79
|
const started = performance.now();
|
|
54
80
|
const step = (now) => {
|
|
55
|
-
const t = Math.min((now - started) / this
|
|
81
|
+
const t = Math.min((now - started) / this.#duration, 1);
|
|
56
82
|
const eased = 1 - (1 - t) ** 3;
|
|
57
|
-
|
|
58
|
-
Math.round(this.fromValue + (target - this.fromValue) * eased)
|
|
59
|
-
);
|
|
83
|
+
ticker.textContent = String(Math.round(from + (target - from) * eased));
|
|
60
84
|
if (t < 1) {
|
|
61
85
|
this.#frame = requestAnimationFrame(step);
|
|
62
86
|
} else {
|
|
@@ -66,26 +90,41 @@ var CountUpController = class extends Controller {
|
|
|
66
90
|
};
|
|
67
91
|
this.#frame = requestAnimationFrame(step);
|
|
68
92
|
}
|
|
93
|
+
/** The first text node that displays a number, or null when the host has none. */
|
|
94
|
+
#numericNode() {
|
|
95
|
+
const walker = document.createTreeWalker(this.element, NodeFilter.SHOW_TEXT);
|
|
96
|
+
let node = walker.nextNode();
|
|
97
|
+
while (node !== null && authoredInteger(node.data) === null) {
|
|
98
|
+
node = walker.nextNode();
|
|
99
|
+
}
|
|
100
|
+
return node;
|
|
101
|
+
}
|
|
102
|
+
/** The wrapper this controller published, if one outlived its run. */
|
|
103
|
+
#ownedTicker() {
|
|
104
|
+
return this.element.querySelector("[data-count-up-label]");
|
|
105
|
+
}
|
|
106
|
+
/** Publishes the ticking number inside a named wrapper, replacing `node`. */
|
|
107
|
+
#wrap(node, authored) {
|
|
108
|
+
const ticker = document.createElement("span");
|
|
109
|
+
ticker.setAttribute("data-count-up-label", "true");
|
|
110
|
+
ticker.setAttribute("role", "img");
|
|
111
|
+
ticker.setAttribute("aria-label", authored);
|
|
112
|
+
node.replaceWith(ticker);
|
|
113
|
+
ticker.append(node);
|
|
114
|
+
return ticker;
|
|
115
|
+
}
|
|
116
|
+
/** Puts `text` back where the wrapper stood, leaving the rest of the host alone. */
|
|
117
|
+
#unwrap(ticker, text) {
|
|
118
|
+
ticker.replaceWith(document.createTextNode(text ?? ticker.textContent ?? ""));
|
|
119
|
+
}
|
|
69
120
|
/** Ends the run: cancels the frame and restores the authored presentation. */
|
|
70
121
|
#settle() {
|
|
71
122
|
if (this.#frame !== null) cancelAnimationFrame(this.#frame);
|
|
72
123
|
this.#frame = null;
|
|
73
|
-
|
|
74
|
-
this.#
|
|
124
|
+
const ticker = this.#ownedTicker();
|
|
125
|
+
if (ticker !== null) this.#unwrap(ticker, ticker.getAttribute("aria-label"));
|
|
75
126
|
this.element.setAttribute("data-count-up-done", "true");
|
|
76
127
|
}
|
|
77
|
-
/** Releases the marker-owned aria-label, restoring any parked authored value. */
|
|
78
|
-
#restoreLabel() {
|
|
79
|
-
if (!this.element.hasAttribute("data-count-up-label")) return;
|
|
80
|
-
const original = this.element.getAttribute("data-count-up-original-label");
|
|
81
|
-
if (original !== null) {
|
|
82
|
-
this.element.setAttribute("aria-label", original);
|
|
83
|
-
this.element.removeAttribute("data-count-up-original-label");
|
|
84
|
-
} else {
|
|
85
|
-
this.element.removeAttribute("aria-label");
|
|
86
|
-
}
|
|
87
|
-
this.element.removeAttribute("data-count-up-label");
|
|
88
|
-
}
|
|
89
128
|
};
|
|
90
129
|
|
|
91
130
|
export { CountUpController };
|
|
@@ -80,6 +80,15 @@ function parseISOMonthString(monthStr) {
|
|
|
80
80
|
function toISOMonthString(date) {
|
|
81
81
|
return `${date.getFullYear()}-${String(date.getMonth() + 1).padStart(2, "0")}`;
|
|
82
82
|
}
|
|
83
|
+
function monthLabelFormatter(locale) {
|
|
84
|
+
const options = { month: "long", year: "numeric" };
|
|
85
|
+
try {
|
|
86
|
+
return new Intl.DateTimeFormat(locale, options);
|
|
87
|
+
} catch (error) {
|
|
88
|
+
if (!(error instanceof RangeError)) throw error;
|
|
89
|
+
return new Intl.DateTimeFormat("en", options);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
83
92
|
|
|
84
93
|
// src/utils/microtask_coalescer.ts
|
|
85
94
|
var MicrotaskCoalescer = class {
|
|
@@ -433,7 +442,7 @@ var DateRangePickerController = class extends Controller {
|
|
|
433
442
|
if (this.#previewDate && !this.#isSelectable(this.#previewDate)) this.#previewDate = "";
|
|
434
443
|
if (this.hasMonthLabelTarget) {
|
|
435
444
|
const lang = document.documentElement.lang || "en";
|
|
436
|
-
const formatter =
|
|
445
|
+
const formatter = monthLabelFormatter(lang);
|
|
437
446
|
this.monthLabelTarget.textContent = formatter.format(new Date(year, month - 1, 1));
|
|
438
447
|
}
|
|
439
448
|
const [rangeStart, rangeEnd] = this.#visualRange();
|
|
@@ -588,15 +597,6 @@ function normalizeISO(value) {
|
|
|
588
597
|
function orderRange(start, end) {
|
|
589
598
|
return start && end && end < start ? [end, start] : [start, end];
|
|
590
599
|
}
|
|
591
|
-
function monthFormatter(locale) {
|
|
592
|
-
const options = { month: "long", year: "numeric" };
|
|
593
|
-
try {
|
|
594
|
-
return new Intl.DateTimeFormat(locale, options);
|
|
595
|
-
} catch (error) {
|
|
596
|
-
if (!(error instanceof RangeError)) throw error;
|
|
597
|
-
return new Intl.DateTimeFormat("en", options);
|
|
598
|
-
}
|
|
599
|
-
}
|
|
600
600
|
function computePreset(name) {
|
|
601
601
|
const today = /* @__PURE__ */ new Date();
|
|
602
602
|
const todayStr = toISODateString(today);
|
|
@@ -586,7 +586,7 @@ var DrawerController = class extends Controller {
|
|
|
586
586
|
* rather than being re-derived from the declarative `open` Value (which would
|
|
587
587
|
* close a user-opened drawer). The `open` Value only seeds a genuinely fresh
|
|
588
588
|
* render. We normalize to a clean closed baseline first so {@link open} runs its
|
|
589
|
-
* full reveal + trap activation — the
|
|
589
|
+
* full reveal + trap activation — the `FocusTrap` is inactive after a
|
|
590
590
|
* disconnect and must be re-activated.
|
|
591
591
|
*/
|
|
592
592
|
connect() {
|
|
@@ -719,7 +719,7 @@ var DrawerController = class extends Controller {
|
|
|
719
719
|
* Runs once the close transition has finished: applies `hidden` to the panel
|
|
720
720
|
* and overlay, then reverts the modal side effects (scroll lock, background
|
|
721
721
|
* `inert`, keydown listener) and restores focus to the opener. Deferring the
|
|
722
|
-
*
|
|
722
|
+
* `FocusTrap` teardown to here — rather than at {@link close} time — keeps
|
|
723
723
|
* the background unreachable and focus trapped for the whole exit animation.
|
|
724
724
|
*/
|
|
725
725
|
#applyHidden(panel) {
|
|
@@ -9,6 +9,14 @@ function isBeforeRootStart(entry) {
|
|
|
9
9
|
const rootTop = entry.rootBounds?.top ?? 0;
|
|
10
10
|
return rect.bottom <= rootTop;
|
|
11
11
|
}
|
|
12
|
+
function queryRoot(selector) {
|
|
13
|
+
if (!selector) return null;
|
|
14
|
+
try {
|
|
15
|
+
return document.querySelector(selector);
|
|
16
|
+
} catch {
|
|
17
|
+
}
|
|
18
|
+
return null;
|
|
19
|
+
}
|
|
12
20
|
var IntersectionWatcher = class {
|
|
13
21
|
#onEntries;
|
|
14
22
|
#observer = null;
|
|
@@ -30,7 +38,10 @@ var IntersectionWatcher = class {
|
|
|
30
38
|
* the watcher inert — without `IntersectionObserver` support (very old
|
|
31
39
|
* browsers; the caller's no-JS fallback stays in charge) or with no targets.
|
|
32
40
|
* If initial construction with the configured options fails, the watcher
|
|
33
|
-
* warns and retries once with the same root and platform defaults.
|
|
41
|
+
* warns and retries once with the same root and platform defaults. A
|
|
42
|
+
* `rootSelector` that does not parse resolves to the viewport (see
|
|
43
|
+
* {@link IntersectionWatchOptions.rootSelector}), so a typo never fails the
|
|
44
|
+
* call.
|
|
34
45
|
*
|
|
35
46
|
* @throws The fallback constructor error if both construction attempts fail,
|
|
36
47
|
* or whatever the platform throws from `observe()`. The exception is passed
|
|
@@ -43,7 +54,7 @@ var IntersectionWatcher = class {
|
|
|
43
54
|
if (typeof IntersectionObserver === "undefined") return false;
|
|
44
55
|
const list = Array.isArray(targets) ? targets : [targets];
|
|
45
56
|
if (list.length === 0) return false;
|
|
46
|
-
const root = "root" in options ? options.root ?? null :
|
|
57
|
+
const root = "root" in options ? options.root ?? null : queryRoot(options.rootSelector);
|
|
47
58
|
let observer = null;
|
|
48
59
|
try {
|
|
49
60
|
const onEntries = (entries) => {
|
|
@@ -136,6 +147,25 @@ var IntersectionController = class extends Controller {
|
|
|
136
147
|
}
|
|
137
148
|
connect() {
|
|
138
149
|
if (this.onceValue && this.element.getAttribute("data-intersecting") === "true") return;
|
|
150
|
+
this.#observe();
|
|
151
|
+
}
|
|
152
|
+
disconnect() {
|
|
153
|
+
this.#watcher.stop();
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Re-reads the visibility line and rebuilds the observer. Turbo 8 morphing
|
|
157
|
+
* rewrites the attribute in place without a reconnect, and the line is what
|
|
158
|
+
* the intersection callback compares every ratio against, so a value frozen at
|
|
159
|
+
* connect time would decide `data-intersecting` wrongly for the rest of the
|
|
160
|
+
* page's life. Nothing to rebuild before the first `connect()`; after a spent
|
|
161
|
+
* one-shot the watcher is deliberately stopped, and re-observing would deliver
|
|
162
|
+
* the current state and fire `enter` a second time.
|
|
163
|
+
*/
|
|
164
|
+
thresholdValueChanged() {
|
|
165
|
+
if (this.#watcher.active) this.#observe();
|
|
166
|
+
}
|
|
167
|
+
/** (Re)installs the observer from the current Values. */
|
|
168
|
+
#observe() {
|
|
139
169
|
this.#effectiveThreshold = this.#clampedThreshold();
|
|
140
170
|
this.#watcher.start(this.element, {
|
|
141
171
|
rootSelector: this.rootSelectorValue,
|
|
@@ -144,9 +174,6 @@ var IntersectionController = class extends Controller {
|
|
|
144
174
|
});
|
|
145
175
|
if (this.#watcher.usingPlatformDefaults) this.#effectiveThreshold = 0;
|
|
146
176
|
}
|
|
147
|
-
disconnect() {
|
|
148
|
-
this.#watcher.stop();
|
|
149
|
-
}
|
|
150
177
|
/**
|
|
151
178
|
* Re-delivers the current intersection state as a fresh transition. Bound via
|
|
152
179
|
* `data-action` (e.g. `my-feed:appended@window->stimeo--intersection#refresh`).
|
|
@@ -177,14 +204,29 @@ var IntersectionController = class extends Controller {
|
|
|
177
204
|
const previous = this.element.getAttribute("data-intersecting");
|
|
178
205
|
this.element.setAttribute("data-intersecting", intersecting ? "true" : "false");
|
|
179
206
|
if (intersecting && previous !== "true") {
|
|
180
|
-
this.dispatch("enter", { detail: { ratio } });
|
|
181
207
|
if (this.onceValue) this.#watcher.stop();
|
|
208
|
+
this.dispatch("enter", { detail: { ratio } });
|
|
182
209
|
} else if (!intersecting && previous === "true") {
|
|
183
210
|
this.dispatch("exit", {
|
|
184
|
-
detail: { ratio, position:
|
|
211
|
+
detail: { ratio, position: this.#leftViaStartEdge(entry) ? "before" : "after" }
|
|
185
212
|
});
|
|
186
213
|
}
|
|
187
214
|
}
|
|
215
|
+
/**
|
|
216
|
+
* Which edge the element left across, for the `exit` detail. A non-zero
|
|
217
|
+
* `threshold` withdraws visibility while the element still overlaps the root,
|
|
218
|
+
* so the leaving rect can straddle the start edge — the direction is the
|
|
219
|
+
* element's own top against that edge, not whether it has cleared the root
|
|
220
|
+
* entirely (that is what `passed` reports). An element with no layout box
|
|
221
|
+
* (`display: none`, a collapsed `<details>`) is reported with an empty rect
|
|
222
|
+
* that carries no position at all, so it is deliberately neither direction
|
|
223
|
+
* and takes the "still ahead" reading.
|
|
224
|
+
*/
|
|
225
|
+
#leftViaStartEdge(entry) {
|
|
226
|
+
const rect = entry.boundingClientRect;
|
|
227
|
+
if (rect.width === 0 && rect.height === 0) return false;
|
|
228
|
+
return rect.top < (entry.rootBounds?.top ?? 0);
|
|
229
|
+
}
|
|
188
230
|
/**
|
|
189
231
|
* Reflects the "scrolled past" state onto `data-passed` and fires `passed` on
|
|
190
232
|
* transitions — the line sticky headers and reading progress key off. Like
|
|
@@ -204,9 +246,14 @@ var IntersectionController = class extends Controller {
|
|
|
204
246
|
/**
|
|
205
247
|
* Observer thresholds: the `threshold` line itself, plus `ratioSteps` evenly
|
|
206
248
|
* spaced steps when fine-grained `change` ratios are wanted (progress bars).
|
|
249
|
+
*
|
|
250
|
+
* 0 is always observed. An observer notifies only at the lines it was given,
|
|
251
|
+
* so a non-zero `threshold` on its own delivers its last callback while the
|
|
252
|
+
* element is still partly visible: the element leaving for good would never be
|
|
253
|
+
* reported, freezing the ratio and `data-passed` mid-departure.
|
|
207
254
|
*/
|
|
208
255
|
#thresholds() {
|
|
209
|
-
const thresholds = /* @__PURE__ */ new Set([this.#clampedThreshold()]);
|
|
256
|
+
const thresholds = /* @__PURE__ */ new Set([0, this.#clampedThreshold()]);
|
|
210
257
|
if (this.ratioStepsValue > 0) {
|
|
211
258
|
for (let i = 0; i <= this.ratioStepsValue; i += 1) {
|
|
212
259
|
thresholds.add(i / this.ratioStepsValue);
|
|
@@ -3,6 +3,14 @@ import { Controller } from '@hotwired/stimulus';
|
|
|
3
3
|
// src/controllers/lazy_frame_controller.ts
|
|
4
4
|
|
|
5
5
|
// src/utils/intersection_watcher.ts
|
|
6
|
+
function queryRoot(selector) {
|
|
7
|
+
if (!selector) return null;
|
|
8
|
+
try {
|
|
9
|
+
return document.querySelector(selector);
|
|
10
|
+
} catch {
|
|
11
|
+
}
|
|
12
|
+
return null;
|
|
13
|
+
}
|
|
6
14
|
var IntersectionWatcher = class {
|
|
7
15
|
#onEntries;
|
|
8
16
|
#observer = null;
|
|
@@ -24,7 +32,10 @@ var IntersectionWatcher = class {
|
|
|
24
32
|
* the watcher inert — without `IntersectionObserver` support (very old
|
|
25
33
|
* browsers; the caller's no-JS fallback stays in charge) or with no targets.
|
|
26
34
|
* If initial construction with the configured options fails, the watcher
|
|
27
|
-
* warns and retries once with the same root and platform defaults.
|
|
35
|
+
* warns and retries once with the same root and platform defaults. A
|
|
36
|
+
* `rootSelector` that does not parse resolves to the viewport (see
|
|
37
|
+
* {@link IntersectionWatchOptions.rootSelector}), so a typo never fails the
|
|
38
|
+
* call.
|
|
28
39
|
*
|
|
29
40
|
* @throws The fallback constructor error if both construction attempts fail,
|
|
30
41
|
* or whatever the platform throws from `observe()`. The exception is passed
|
|
@@ -37,7 +48,7 @@ var IntersectionWatcher = class {
|
|
|
37
48
|
if (typeof IntersectionObserver === "undefined") return false;
|
|
38
49
|
const list = Array.isArray(targets) ? targets : [targets];
|
|
39
50
|
if (list.length === 0) return false;
|
|
40
|
-
const root = "root" in options ? options.root ?? null :
|
|
51
|
+
const root = "root" in options ? options.root ?? null : queryRoot(options.rootSelector);
|
|
41
52
|
let observer = null;
|
|
42
53
|
try {
|
|
43
54
|
const onEntries = (entries) => {
|
|
@@ -106,47 +117,98 @@ var LazyFrameController = class extends Controller {
|
|
|
106
117
|
static events = ["load"];
|
|
107
118
|
/** Shared IO plumbing (support guard, active guard, teardown). */
|
|
108
119
|
#watcher = new IntersectionWatcher((entries) => this.#onIntersect(entries));
|
|
120
|
+
#connected = false;
|
|
109
121
|
#loaded = false;
|
|
110
|
-
/**
|
|
111
|
-
|
|
122
|
+
/**
|
|
123
|
+
* Whether the observer last saw the frame inside the observed area — `null`
|
|
124
|
+
* until it has reported. A re-entry needs a frame that was seen inside and
|
|
125
|
+
* then left, so an unknown position is never a departure.
|
|
126
|
+
*/
|
|
127
|
+
#inside = null;
|
|
128
|
+
/**
|
|
129
|
+
* Focus reaching the frame starts the FIRST load before it intersects (keyboard / AT).
|
|
130
|
+
* `#load` releases this listener, so focus never reaches an already-loaded frame.
|
|
131
|
+
*/
|
|
132
|
+
#onFocus = () => this.#load();
|
|
133
|
+
/**
|
|
134
|
+
* Follows the held URL at runtime: one that arrives arms the frame, one that is
|
|
135
|
+
* taken away disarms it, so an empty `url` holds no triggers either way.
|
|
136
|
+
*
|
|
137
|
+
* Stimulus runs value callbacks before `connect()`, so the connected guard keeps
|
|
138
|
+
* arming in one place. A frame that already loaded keeps its held URL for the next
|
|
139
|
+
* re-entry instead of fetching on the spot.
|
|
140
|
+
*/
|
|
141
|
+
urlValueChanged() {
|
|
142
|
+
if (!this.#connected || this.#loaded) return;
|
|
143
|
+
if (this.urlValue) this.#arm();
|
|
144
|
+
else this.#stopObserving();
|
|
145
|
+
}
|
|
146
|
+
/** Rebuilds the observer when the early-load margin changes at runtime. */
|
|
147
|
+
rootMarginValueChanged() {
|
|
148
|
+
if (this.#connected && this.#watcher.active) this.#observe();
|
|
149
|
+
}
|
|
112
150
|
connect() {
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
this.element.addEventListener("focusin", this.#onFocus);
|
|
119
|
-
this.#watcher.start(this.element, { rootMargin: this.rootMarginValue });
|
|
151
|
+
this.#loaded = this.element.hasAttribute("data-lazy-loaded");
|
|
152
|
+
this.#inside = null;
|
|
153
|
+
this.#connected = true;
|
|
154
|
+
if (this.#loaded && this.onceValue) return;
|
|
155
|
+
this.#arm();
|
|
120
156
|
}
|
|
121
157
|
disconnect() {
|
|
158
|
+
this.#connected = false;
|
|
122
159
|
this.#stopObserving();
|
|
123
160
|
}
|
|
161
|
+
/** Attaches the triggers that still have a job to do. */
|
|
162
|
+
#arm() {
|
|
163
|
+
if (!this.urlValue) return;
|
|
164
|
+
if (!this.#loaded) this.element.addEventListener("focusin", this.#onFocus);
|
|
165
|
+
this.#observe();
|
|
166
|
+
}
|
|
167
|
+
#observe() {
|
|
168
|
+
this.#watcher.start(this.element, { rootMargin: this.rootMarginValue });
|
|
169
|
+
}
|
|
124
170
|
#onIntersect(entries) {
|
|
125
171
|
for (const entry of entries) {
|
|
126
172
|
if (entry.isIntersecting) {
|
|
127
|
-
this.#
|
|
128
|
-
|
|
173
|
+
const returning = this.#inside === false;
|
|
174
|
+
this.#inside = true;
|
|
175
|
+
this.#enter(returning);
|
|
176
|
+
} else if (this.#inside) {
|
|
177
|
+
this.#inside = false;
|
|
129
178
|
}
|
|
130
179
|
}
|
|
131
180
|
}
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
181
|
+
/** Applies an intersecting entry: the first one loads, a return re-fetches. */
|
|
182
|
+
#enter(returning) {
|
|
183
|
+
if (!this.#loaded) {
|
|
184
|
+
this.#load();
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
if (this.onceValue || !returning) return;
|
|
188
|
+
this.#reload();
|
|
135
189
|
}
|
|
136
190
|
/** Starts the load by writing the held URL to `src`. */
|
|
137
191
|
#load() {
|
|
192
|
+
if (!this.urlValue) return;
|
|
138
193
|
this.#loaded = true;
|
|
139
194
|
this.element.setAttribute("src", this.urlValue);
|
|
140
195
|
this.element.setAttribute("data-lazy-loaded", "true");
|
|
141
196
|
this.dispatch("load", { detail: { url: this.urlValue } });
|
|
142
|
-
|
|
197
|
+
this.element.removeEventListener("focusin", this.#onFocus);
|
|
198
|
+
if (this.onceValue) this.#watcher.stop();
|
|
143
199
|
}
|
|
144
|
-
/** Re-entry while `once` is off:
|
|
200
|
+
/** Re-entry while `once` is off: fetch the held URL again. */
|
|
145
201
|
#reload() {
|
|
202
|
+
const src = this.element.getAttribute("src");
|
|
203
|
+
if (this.urlValue && this.urlValue !== src) {
|
|
204
|
+
this.element.setAttribute("src", this.urlValue);
|
|
205
|
+
this.dispatch("load", { detail: { url: this.urlValue } });
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
146
208
|
const frame = this.element;
|
|
147
209
|
if (typeof frame.reload !== "function") return;
|
|
148
210
|
frame.reload();
|
|
149
|
-
this.dispatch("load", { detail: { url:
|
|
211
|
+
this.dispatch("load", { detail: { url: src ?? "" } });
|
|
150
212
|
}
|
|
151
213
|
#stopObserving() {
|
|
152
214
|
this.#watcher.stop();
|