stimeo-ui 0.3.0 → 0.4.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 +56 -0
- data/dist/controllers/alert_dialog_controller.js +32 -5
- data/dist/controllers/announcer_controller.js +255 -20
- data/dist/controllers/color_picker_controller.js +46 -0
- data/dist/controllers/command_palette_controller.js +32 -5
- data/dist/controllers/confirm_controller.js +32 -5
- data/dist/controllers/countdown_controller.js +112 -12
- data/dist/controllers/date_range_picker_controller.js +50 -0
- data/dist/controllers/dialog_controller.js +32 -5
- data/dist/controllers/drawer_controller.js +32 -5
- data/dist/controllers/empty_state_controller.js +24 -10
- data/dist/controllers/focus_controller.js +32 -5
- data/dist/controllers/frame_loading_controller.js +177 -15
- data/dist/controllers/local_time_controller.js +100 -6
- data/dist/controllers/meter_controller.js +145 -26
- data/dist/controllers/network_status_controller.js +28 -8
- data/dist/controllers/overflow_menu_controller.js +33 -6
- data/dist/controllers/progress_controller.js +116 -9
- data/dist/controllers/range_slider_controller.js +68 -3
- data/dist/controllers/rating_controller.js +53 -0
- data/dist/controllers/relative_time_controller.js +133 -12
- data/dist/controllers/sidebar_controller.js +37 -8
- data/dist/controllers/skeleton_controller.js +73 -20
- data/dist/controllers/slider_controller.js +17 -2
- data/dist/controllers/spinner_controller.js +228 -27
- data/dist/controllers/step_indicator_controller.js +82 -5
- data/dist/controllers/stick_to_bottom_controller.js +60 -10
- data/dist/index.js +1056 -281
- data/lib/stimeo/ui/version.rb +1 -1
- metadata +2 -2
|
@@ -2,6 +2,145 @@ import { Controller } from '@hotwired/stimulus';
|
|
|
2
2
|
|
|
3
3
|
// src/controllers/spinner_controller.ts
|
|
4
4
|
|
|
5
|
+
// src/utils/announce.ts
|
|
6
|
+
function announce(message, options = {}) {
|
|
7
|
+
const text = message.trim();
|
|
8
|
+
if (text.length === 0) return;
|
|
9
|
+
window.dispatchEvent(
|
|
10
|
+
new CustomEvent("stimeo--announcer:announce", {
|
|
11
|
+
detail: { message: text, assertive: options.assertive === true }
|
|
12
|
+
})
|
|
13
|
+
);
|
|
14
|
+
}
|
|
15
|
+
function fillTemplate(template, values) {
|
|
16
|
+
return template.replace(/\{([a-zA-Z][a-zA-Z0-9]*)\}/g, (match, name) => {
|
|
17
|
+
const replacement = values[name];
|
|
18
|
+
return replacement === void 0 ? match : String(replacement);
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// src/utils/before_cache_reset.ts
|
|
23
|
+
var BeforeCacheReset = class _BeforeCacheReset {
|
|
24
|
+
/** Every subscribed instance, iterated by the one shared document listener. */
|
|
25
|
+
static #subscribers = /* @__PURE__ */ new Set();
|
|
26
|
+
/** The shared listener; installed while at least one instance is subscribed. */
|
|
27
|
+
static #onBeforeCache = () => {
|
|
28
|
+
for (const subscriber of _BeforeCacheReset.#subscribers) subscriber.#rewind();
|
|
29
|
+
};
|
|
30
|
+
#rewind;
|
|
31
|
+
/** @param rewind - the pass that returns this controller's state to its initial form. */
|
|
32
|
+
constructor(rewind) {
|
|
33
|
+
this.#rewind = rewind;
|
|
34
|
+
}
|
|
35
|
+
/** Subscribes to `turbo:before-cache`; call from `connect()`. Idempotent. */
|
|
36
|
+
activate() {
|
|
37
|
+
const first = _BeforeCacheReset.#subscribers.size === 0;
|
|
38
|
+
_BeforeCacheReset.#subscribers.add(this);
|
|
39
|
+
if (first) {
|
|
40
|
+
document.addEventListener("turbo:before-cache", _BeforeCacheReset.#onBeforeCache);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
/** Unsubscribes; call from `disconnect()`. Safe when never subscribed. */
|
|
44
|
+
deactivate() {
|
|
45
|
+
_BeforeCacheReset.#subscribers.delete(this);
|
|
46
|
+
if (_BeforeCacheReset.#subscribers.size > 0) return;
|
|
47
|
+
document.removeEventListener("turbo:before-cache", _BeforeCacheReset.#onBeforeCache);
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
// src/utils/detach_gate.ts
|
|
52
|
+
var DetachGate = class _DetachGate {
|
|
53
|
+
/** Set while a probe is queued, waiting for a reconnect to cancel it. */
|
|
54
|
+
#pending = false;
|
|
55
|
+
/**
|
|
56
|
+
* True when the disconnect is definitely a real detach — the element left
|
|
57
|
+
* the document, or `data-controller` no longer lists the identifier. False
|
|
58
|
+
* means ambiguous (in-page move or observed-root exit), NOT "alive".
|
|
59
|
+
*/
|
|
60
|
+
static isDetached(host) {
|
|
61
|
+
if (!host.element.isConnected) return true;
|
|
62
|
+
const tokens = (host.element.getAttribute("data-controller") ?? "").split(/\s+/);
|
|
63
|
+
return !tokens.includes(host.identifier);
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* Call from `disconnect()`: runs `teardown` synchronously on a definite
|
|
67
|
+
* detach (fast path), otherwise defers it one microtask — a reconnect
|
|
68
|
+
* ({@link cancel} from `connect()`) keeps the state, no reconnect runs it.
|
|
69
|
+
* One microtask is the whole probe window: Stimulus reconnects a moved
|
|
70
|
+
* element within the same mutation batch, before the checkpoint drains.
|
|
71
|
+
*/
|
|
72
|
+
disconnected(host, teardown) {
|
|
73
|
+
if (_DetachGate.isDetached(host)) {
|
|
74
|
+
this.#pending = false;
|
|
75
|
+
teardown();
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
this.#pending = true;
|
|
79
|
+
queueMicrotask(() => {
|
|
80
|
+
if (!this.#pending) return;
|
|
81
|
+
this.#pending = false;
|
|
82
|
+
teardown();
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Disarms a pending probe. Call from `connect()` (the reconnect that proves
|
|
87
|
+
* an in-page move) and from the head of any teardown path not routed through
|
|
88
|
+
* {@link disconnected} (disabled-toggle, Escape), so an orphaned probe can
|
|
89
|
+
* never run the teardown a second time.
|
|
90
|
+
*/
|
|
91
|
+
cancel() {
|
|
92
|
+
this.#pending = false;
|
|
93
|
+
}
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
// src/utils/min_duration_floor.ts
|
|
97
|
+
var MinDurationFloor = class {
|
|
98
|
+
#timers;
|
|
99
|
+
/** Pending finish timer id, or `null` when nothing is held back. */
|
|
100
|
+
#timerId = null;
|
|
101
|
+
/** Epoch ms the floor is measured from. */
|
|
102
|
+
#since = 0;
|
|
103
|
+
/** @param timers - the controller's registry; the floor schedules into it. */
|
|
104
|
+
constructor(timers) {
|
|
105
|
+
this.#timers = timers;
|
|
106
|
+
}
|
|
107
|
+
/** Starts the floor: call when the state being held becomes visible. */
|
|
108
|
+
begin() {
|
|
109
|
+
this.#since = Date.now();
|
|
110
|
+
}
|
|
111
|
+
/** True while a finish is held back waiting for the floor to elapse. */
|
|
112
|
+
get pending() {
|
|
113
|
+
return this.#timerId !== null;
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Runs `finish` once the floor has elapsed, immediately when it already has.
|
|
117
|
+
*
|
|
118
|
+
* A held-back finish is **replaced**, never stacked: only the most recently
|
|
119
|
+
* queued id is cancellable, so a second timer would outlive every cancel and
|
|
120
|
+
* end a state that has since restarted. Controllers that want the first signal
|
|
121
|
+
* to win guard on {@link pending} before calling.
|
|
122
|
+
*/
|
|
123
|
+
schedule(minDuration, finish) {
|
|
124
|
+
this.cancel();
|
|
125
|
+
const remaining = minDuration - (Date.now() - this.#since);
|
|
126
|
+
if (remaining > 0) {
|
|
127
|
+
this.#timerId = this.#timers.set(() => {
|
|
128
|
+
this.#timerId = null;
|
|
129
|
+
finish();
|
|
130
|
+
}, remaining);
|
|
131
|
+
} else {
|
|
132
|
+
finish();
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
/** Drops a held-back finish. Safe when none is queued, or after a bulk clear. */
|
|
136
|
+
cancel() {
|
|
137
|
+
if (this.#timerId !== null) {
|
|
138
|
+
this.#timers.clear(this.#timerId);
|
|
139
|
+
this.#timerId = null;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
};
|
|
143
|
+
|
|
5
144
|
// src/utils/safe_timeout.ts
|
|
6
145
|
var TimerRegistry = class {
|
|
7
146
|
/** Live timer ids that have not yet been cleared (or, for timeouts, fired). */
|
|
@@ -59,38 +198,61 @@ var SafeTimeout = class extends TimerRegistry {
|
|
|
59
198
|
var SpinnerController = class extends Controller {
|
|
60
199
|
static targets = ["indicator", "region", "message"];
|
|
61
200
|
static values = {
|
|
201
|
+
announceText: { type: String, default: "" },
|
|
202
|
+
announceReadyText: { type: String, default: "" },
|
|
62
203
|
delay: { type: Number, default: 0 },
|
|
63
|
-
minDuration: { type: Number, default: 0 }
|
|
204
|
+
minDuration: { type: Number, default: 0 },
|
|
205
|
+
timeout: { type: Number, default: 0 }
|
|
64
206
|
};
|
|
65
207
|
static actions = ["start", "stop"];
|
|
66
|
-
static events = ["hide", "show"];
|
|
208
|
+
static events = ["hide", "show", "timeout"];
|
|
67
209
|
#timers = new SafeTimeout();
|
|
210
|
+
#floor = new MinDurationFloor(this.#timers);
|
|
211
|
+
#gate = new DetachGate();
|
|
212
|
+
#beforeCache = new BeforeCacheReset(() => this.#rewindForCache());
|
|
68
213
|
/** Pending show-delay timer id, or `null` when no start is awaiting its delay. */
|
|
69
214
|
#delayTimerId = null;
|
|
70
|
-
/** Pending
|
|
71
|
-
#
|
|
72
|
-
/** Epoch ms when the spinner became visible; `minDuration` is measured from it. */
|
|
73
|
-
#shownAt = 0;
|
|
215
|
+
/** Pending safety-net timer id, or `null` when `timeout` is off or not armed. */
|
|
216
|
+
#timeoutTimerId = null;
|
|
74
217
|
connect() {
|
|
218
|
+
this.#gate.cancel();
|
|
219
|
+
this.#beforeCache.activate();
|
|
220
|
+
if (this.#state === "pending" && this.#delayTimerId === null) {
|
|
221
|
+
this.#setBusy(false);
|
|
222
|
+
this.element.setAttribute("data-state", "idle");
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
75
225
|
if (!this.element.hasAttribute("data-state")) {
|
|
76
226
|
this.element.setAttribute("data-state", "idle");
|
|
77
227
|
}
|
|
78
228
|
}
|
|
229
|
+
/**
|
|
230
|
+
* Re-applies the current phase to an indicator that arrived after `connect()`.
|
|
231
|
+
*
|
|
232
|
+
* A Turbo Stream can swap the indicator for a fresh node mid-load, and that node
|
|
233
|
+
* carries the markup contract's `hidden`. Without this the spinner would vanish
|
|
234
|
+
* while `data-state` still says `loading`, and nothing but the next cycle would
|
|
235
|
+
* bring it back.
|
|
236
|
+
*/
|
|
237
|
+
indicatorTargetConnected(target) {
|
|
238
|
+
target.hidden = this.#state !== "loading";
|
|
239
|
+
}
|
|
79
240
|
disconnect() {
|
|
80
|
-
this.#
|
|
81
|
-
this.#
|
|
82
|
-
this.#hideTimerId = null;
|
|
241
|
+
this.#beforeCache.deactivate();
|
|
242
|
+
this.#gate.disconnected(this, () => this.#teardown());
|
|
83
243
|
}
|
|
84
244
|
/** Begins loading. Honors `delay` before the spinner actually appears. */
|
|
85
245
|
start() {
|
|
86
246
|
if (this.#state === "loading") {
|
|
87
247
|
this.#setBusy(true);
|
|
88
|
-
this.#
|
|
248
|
+
this.#floor.cancel();
|
|
249
|
+
this.#armTimeout();
|
|
89
250
|
return;
|
|
90
251
|
}
|
|
91
252
|
if (this.#state !== "idle") return;
|
|
92
253
|
this.#setBusy(true);
|
|
93
|
-
this.#
|
|
254
|
+
this.#floor.cancel();
|
|
255
|
+
this.#armTimeout();
|
|
94
256
|
if (this.delayValue > 0) {
|
|
95
257
|
this.element.setAttribute("data-state", "pending");
|
|
96
258
|
this.#delayTimerId = this.#timers.set(() => {
|
|
@@ -104,6 +266,7 @@ var SpinnerController = class extends Controller {
|
|
|
104
266
|
/** Ends loading. Honors `minDuration` so a shown spinner does not flicker. */
|
|
105
267
|
stop() {
|
|
106
268
|
const state = this.#state;
|
|
269
|
+
this.#cancelTimeout();
|
|
107
270
|
if (state === "pending") {
|
|
108
271
|
this.#cancelDelay();
|
|
109
272
|
this.#setBusy(false);
|
|
@@ -112,28 +275,52 @@ var SpinnerController = class extends Controller {
|
|
|
112
275
|
}
|
|
113
276
|
if (state !== "loading") return;
|
|
114
277
|
this.#setBusy(false);
|
|
115
|
-
|
|
116
|
-
if (remaining > 0) {
|
|
117
|
-
this.#hideTimerId = this.#timers.set(() => {
|
|
118
|
-
this.#hideTimerId = null;
|
|
119
|
-
this.#hide();
|
|
120
|
-
}, remaining);
|
|
121
|
-
} else {
|
|
122
|
-
this.#hide();
|
|
123
|
-
}
|
|
278
|
+
this.#floor.schedule(this.minDurationValue, () => this.#hide());
|
|
124
279
|
}
|
|
125
280
|
/** Reveals the indicator, marks the moment shown, and announces via the live region. */
|
|
126
281
|
#show() {
|
|
127
|
-
this.#
|
|
282
|
+
this.#floor.begin();
|
|
283
|
+
this.#setBusy(true);
|
|
128
284
|
if (this.hasIndicatorTarget) this.indicatorTarget.hidden = false;
|
|
129
285
|
this.element.setAttribute("data-state", "loading");
|
|
130
286
|
this.dispatch("show", { detail: {} });
|
|
287
|
+
announce(fillTemplate(this.announceTextValue, {}));
|
|
131
288
|
}
|
|
132
289
|
/** Hides the indicator and returns to the idle state. */
|
|
133
290
|
#hide() {
|
|
134
291
|
if (this.hasIndicatorTarget) this.indicatorTarget.hidden = true;
|
|
135
292
|
this.element.setAttribute("data-state", "idle");
|
|
136
293
|
this.dispatch("hide", { detail: {} });
|
|
294
|
+
announce(fillTemplate(this.announceReadyTextValue, {}));
|
|
295
|
+
}
|
|
296
|
+
/**
|
|
297
|
+
* Drops both timers on a real detach. The markup keeps whatever it last held: an
|
|
298
|
+
* element on its way out of the document has no reader left, and one whose
|
|
299
|
+
* `data-controller` dropped the identifier no longer resolves its own targets, so
|
|
300
|
+
* the rollback could only ever be partial. The snapshot is rewound where it is
|
|
301
|
+
* still whole, on `turbo:before-cache`.
|
|
302
|
+
*/
|
|
303
|
+
#teardown() {
|
|
304
|
+
this.#gate.cancel();
|
|
305
|
+
this.#timers.clearAll();
|
|
306
|
+
this.#delayTimerId = null;
|
|
307
|
+
this.#timeoutTimerId = null;
|
|
308
|
+
this.#floor.cancel();
|
|
309
|
+
}
|
|
310
|
+
/**
|
|
311
|
+
* Returns the loading state to idle for the snapshot Turbo is about to take,
|
|
312
|
+
* so a page reached with the Back button is not restored mid-load with a
|
|
313
|
+
* spinner nothing can stop. State only: `data-state`, the indicator's `hidden`,
|
|
314
|
+
* and `aria-busy`. No `hide` is dispatched — the load was never observed to
|
|
315
|
+
* finish, and a snapshot rewind is not a lifecycle event the consumer can act
|
|
316
|
+
* on. The live page keeps its timers, so a navigation that never completes
|
|
317
|
+
* leaves the running cycle intact.
|
|
318
|
+
*/
|
|
319
|
+
#rewindForCache() {
|
|
320
|
+
this.#cancelTimeout();
|
|
321
|
+
this.#setBusy(false);
|
|
322
|
+
if (this.hasIndicatorTarget) this.indicatorTarget.hidden = true;
|
|
323
|
+
this.element.setAttribute("data-state", "idle");
|
|
137
324
|
}
|
|
138
325
|
/** Reflects busy state onto the controlled region (if present). */
|
|
139
326
|
#setBusy(busy) {
|
|
@@ -141,18 +328,32 @@ var SpinnerController = class extends Controller {
|
|
|
141
328
|
this.regionTarget.setAttribute("aria-busy", String(busy));
|
|
142
329
|
}
|
|
143
330
|
}
|
|
331
|
+
/**
|
|
332
|
+
* Arms the safety net so a `stop` that never arrives cannot strand the spinner.
|
|
333
|
+
* Off by default: the consumer owns the async work, so only it knows whether a
|
|
334
|
+
* ceiling makes sense. Re-arming on a restart measures from the newest start.
|
|
335
|
+
*/
|
|
336
|
+
#armTimeout() {
|
|
337
|
+
this.#cancelTimeout();
|
|
338
|
+
if (this.timeoutValue <= 0) return;
|
|
339
|
+
this.#timeoutTimerId = this.#timers.set(() => {
|
|
340
|
+
this.#timeoutTimerId = null;
|
|
341
|
+
this.dispatch("timeout", { detail: {} });
|
|
342
|
+
this.stop();
|
|
343
|
+
}, this.timeoutValue);
|
|
344
|
+
}
|
|
345
|
+
#cancelTimeout() {
|
|
346
|
+
if (this.#timeoutTimerId !== null) {
|
|
347
|
+
this.#timers.clear(this.#timeoutTimerId);
|
|
348
|
+
this.#timeoutTimerId = null;
|
|
349
|
+
}
|
|
350
|
+
}
|
|
144
351
|
#cancelDelay() {
|
|
145
352
|
if (this.#delayTimerId !== null) {
|
|
146
353
|
this.#timers.clear(this.#delayTimerId);
|
|
147
354
|
this.#delayTimerId = null;
|
|
148
355
|
}
|
|
149
356
|
}
|
|
150
|
-
#cancelHide() {
|
|
151
|
-
if (this.#hideTimerId !== null) {
|
|
152
|
-
this.#timers.clear(this.#hideTimerId);
|
|
153
|
-
this.#hideTimerId = null;
|
|
154
|
-
}
|
|
155
|
-
}
|
|
156
357
|
/** Current lifecycle phase as reflected on `data-state`. */
|
|
157
358
|
get #state() {
|
|
158
359
|
return this.element.getAttribute("data-state") ?? "idle";
|
|
@@ -1,5 +1,40 @@
|
|
|
1
1
|
import { Controller } from '@hotwired/stimulus';
|
|
2
2
|
|
|
3
|
+
// src/controllers/step_indicator_controller.ts
|
|
4
|
+
|
|
5
|
+
// src/utils/microtask_coalescer.ts
|
|
6
|
+
var MicrotaskCoalescer = class {
|
|
7
|
+
#run;
|
|
8
|
+
#queued = false;
|
|
9
|
+
#active = false;
|
|
10
|
+
#generation = 0;
|
|
11
|
+
/** @param run - the single reconciliation pass, invoked at most once per batch. */
|
|
12
|
+
constructor(run) {
|
|
13
|
+
this.#run = run;
|
|
14
|
+
}
|
|
15
|
+
/** Opens the window in which {@link schedule} is honoured; call from `connect()`. */
|
|
16
|
+
activate() {
|
|
17
|
+
this.#active = true;
|
|
18
|
+
}
|
|
19
|
+
/** Closes the window and drops any pending pass; call from `disconnect()`. */
|
|
20
|
+
cancel() {
|
|
21
|
+
this.#active = false;
|
|
22
|
+
this.#queued = false;
|
|
23
|
+
this.#generation += 1;
|
|
24
|
+
}
|
|
25
|
+
/** Requests one pass after the batch settles. Idempotent; inert outside the window. */
|
|
26
|
+
schedule() {
|
|
27
|
+
if (!this.#active || this.#queued) return;
|
|
28
|
+
this.#queued = true;
|
|
29
|
+
const generation = this.#generation;
|
|
30
|
+
queueMicrotask(() => {
|
|
31
|
+
if (generation !== this.#generation || !this.#queued || !this.#active) return;
|
|
32
|
+
this.#queued = false;
|
|
33
|
+
this.#run();
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
};
|
|
37
|
+
|
|
3
38
|
// src/controllers/step_indicator_controller.ts
|
|
4
39
|
var StepIndicatorController = class extends Controller {
|
|
5
40
|
static targets = ["step"];
|
|
@@ -8,26 +43,63 @@ var StepIndicatorController = class extends Controller {
|
|
|
8
43
|
};
|
|
9
44
|
static actions = ["setCurrent"];
|
|
10
45
|
static events = ["change"];
|
|
46
|
+
/**
|
|
47
|
+
* Whether the target callbacks may render. Stimulus reports the authored steps
|
|
48
|
+
* as connected before `connect()` and the remaining ones as disconnected after
|
|
49
|
+
* `disconnect()`, so this keeps a connect at one render pass, not one per step.
|
|
50
|
+
*/
|
|
51
|
+
/**
|
|
52
|
+
* Collapses a batch of step callbacks — and a morph that swaps `current` with
|
|
53
|
+
* them — into one repaint. Replacing a list of N steps delivers N callbacks, and
|
|
54
|
+
* each one would otherwise rewrite every step's state.
|
|
55
|
+
*/
|
|
56
|
+
#repaint = new MicrotaskCoalescer(() => this.#render());
|
|
11
57
|
/** Renders the initial state from the `current` value. */
|
|
12
58
|
connect() {
|
|
59
|
+
this.#repaint.activate();
|
|
13
60
|
this.#render();
|
|
14
61
|
}
|
|
62
|
+
/** Closes the window in which a queued repaint may still run. */
|
|
63
|
+
disconnect() {
|
|
64
|
+
this.#repaint.cancel();
|
|
65
|
+
}
|
|
66
|
+
/** Syncs a step appended or replaced at runtime (the consumer owns the list). */
|
|
67
|
+
stepTargetConnected() {
|
|
68
|
+
this.#repaint.schedule();
|
|
69
|
+
}
|
|
70
|
+
/** Re-derives the remaining steps when one is removed at runtime. */
|
|
71
|
+
stepTargetDisconnected() {
|
|
72
|
+
this.#repaint.schedule();
|
|
73
|
+
}
|
|
74
|
+
/** Repaints when application code (or a Turbo morph) changes `current` at runtime. */
|
|
75
|
+
currentValueChanged() {
|
|
76
|
+
this.#repaint.schedule();
|
|
77
|
+
}
|
|
15
78
|
/**
|
|
16
79
|
* Updates the current step from an external event (`detail.current`, 0-based)
|
|
17
|
-
* and dispatches `change`. Out-of-range indices are clamped to the step set
|
|
80
|
+
* and dispatches `change`. Out-of-range indices are clamped to the step set,
|
|
81
|
+
* and both sides of the no-op test are clamped, so moving onto the step an
|
|
82
|
+
* out-of-range `current` already renders is not reported as a change.
|
|
18
83
|
*/
|
|
19
84
|
setCurrent(event) {
|
|
20
85
|
const next = event.detail?.current;
|
|
21
86
|
if (typeof next !== "number" || !Number.isFinite(next)) return;
|
|
22
87
|
const clamped = this.#clamp(next);
|
|
23
|
-
|
|
88
|
+
const moved = clamped !== this.#clamp(this.currentValue);
|
|
24
89
|
this.currentValue = clamped;
|
|
90
|
+
if (!moved) return;
|
|
25
91
|
this.#render();
|
|
26
92
|
this.dispatch("change", {
|
|
27
93
|
detail: { current: clamped, total: this.stepTargets.length }
|
|
28
94
|
});
|
|
29
95
|
}
|
|
30
|
-
/**
|
|
96
|
+
/**
|
|
97
|
+
* Applies `data-state`, `aria-current`, and the progress ratio custom property.
|
|
98
|
+
*
|
|
99
|
+
* A pure function of the step set and `current`, so running it again writes the
|
|
100
|
+
* same values — which is what lets the action path paint synchronously (the event
|
|
101
|
+
* goes out after the DOM is updated) while a coalesced pass may still follow.
|
|
102
|
+
*/
|
|
31
103
|
#render() {
|
|
32
104
|
const total = this.stepTargets.length;
|
|
33
105
|
const current = this.#clamp(this.currentValue);
|
|
@@ -42,10 +114,15 @@ var StepIndicatorController = class extends Controller {
|
|
|
42
114
|
const ratio = total > 1 ? current / (total - 1) : 0;
|
|
43
115
|
this.element.style.setProperty("--stimeo-step-indicator-ratio", String(ratio));
|
|
44
116
|
}
|
|
45
|
-
/**
|
|
117
|
+
/**
|
|
118
|
+
* Constrains an index to `[0, total-1]` (or `0` when there are no steps). A
|
|
119
|
+
* non-finite index falls back to the first step: `current` is read from markup,
|
|
120
|
+
* so an unparsable attribute arrives as `NaN` and would otherwise propagate
|
|
121
|
+
* into every state hook.
|
|
122
|
+
*/
|
|
46
123
|
#clamp(index) {
|
|
47
124
|
const last = this.stepTargets.length - 1;
|
|
48
|
-
if (last < 0) return 0;
|
|
125
|
+
if (last < 0 || !Number.isFinite(index)) return 0;
|
|
49
126
|
return Math.min(last, Math.max(0, Math.trunc(index)));
|
|
50
127
|
}
|
|
51
128
|
};
|
|
@@ -17,14 +17,18 @@ var StickToBottomController = class extends Controller {
|
|
|
17
17
|
static targets = ["content"];
|
|
18
18
|
static values = {
|
|
19
19
|
threshold: { type: Number, default: 80 },
|
|
20
|
-
behavior: { type: String, default: "auto" }
|
|
20
|
+
behavior: { type: String, default: "auto" },
|
|
21
|
+
pinOnConnect: { type: Boolean, default: false }
|
|
21
22
|
};
|
|
22
23
|
static actions = ["scrollToBottom"];
|
|
23
24
|
static events = ["pin", "new"];
|
|
24
25
|
#observer = null;
|
|
26
|
+
/** Watches for the box a deferred `pinOnConnect` jump is still waiting on. */
|
|
27
|
+
#layout = null;
|
|
25
28
|
#pinned = false;
|
|
26
29
|
#onScroll = () => this.#updatePinned();
|
|
27
30
|
connect() {
|
|
31
|
+
if (this.pinOnConnectValue && this.#measurable()) this.#scrollToBottom("instant");
|
|
28
32
|
this.#pinned = this.#isPinned();
|
|
29
33
|
this.#reflectPinned();
|
|
30
34
|
this.element.addEventListener("scroll", this.#onScroll, { passive: true });
|
|
@@ -32,21 +36,29 @@ var StickToBottomController = class extends Controller {
|
|
|
32
36
|
this.#observer = new MutationObserver((mutations) => this.#onMutations(mutations));
|
|
33
37
|
this.#observer.observe(this.#watched(), { childList: true });
|
|
34
38
|
}
|
|
39
|
+
if (this.pinOnConnectValue && !this.#measurable()) this.#pinWhenLaidOut();
|
|
35
40
|
}
|
|
36
41
|
disconnect() {
|
|
37
42
|
this.element.removeEventListener("scroll", this.#onScroll);
|
|
38
43
|
this.#observer?.disconnect();
|
|
39
44
|
this.#observer = null;
|
|
45
|
+
this.#stopWaitingForLayout();
|
|
40
46
|
}
|
|
41
|
-
/**
|
|
47
|
+
/**
|
|
48
|
+
* Jumps to the bottom and re-pins (wired to a "new messages" button).
|
|
49
|
+
*
|
|
50
|
+
* The has-new flag clears on request — the user has acknowledged the arrival — while
|
|
51
|
+
* pinned is read back from where the scroll landed: a jump that arrives by the time
|
|
52
|
+
* this returns pins immediately, an animated one settles from its own scroll events,
|
|
53
|
+
* and a jump the engine cannot honor leaves the container unpinned, so the next append
|
|
54
|
+
* flags it again instead of being swallowed by a pinned state that does not hold.
|
|
55
|
+
*
|
|
56
|
+
* Which of those happens is not this method's to decide — see {@link behaviorValue}.
|
|
57
|
+
*/
|
|
42
58
|
scrollToBottom() {
|
|
43
59
|
this.#scrollToBottom();
|
|
44
60
|
this.element.removeAttribute("data-has-new");
|
|
45
|
-
|
|
46
|
-
this.#pinned = true;
|
|
47
|
-
this.element.setAttribute("data-pinned", "true");
|
|
48
|
-
this.dispatch("pin", { detail: { pinned: true } });
|
|
49
|
-
}
|
|
61
|
+
this.#updatePinned();
|
|
50
62
|
}
|
|
51
63
|
/** Follows appended children while pinned; otherwise flags new content. */
|
|
52
64
|
#onMutations(mutations) {
|
|
@@ -81,10 +93,43 @@ var StickToBottomController = class extends Controller {
|
|
|
81
93
|
const el = this.element;
|
|
82
94
|
return el.scrollHeight - el.clientHeight - el.scrollTop <= this.thresholdValue;
|
|
83
95
|
}
|
|
84
|
-
|
|
96
|
+
/**
|
|
97
|
+
* Whether the container has a box to scroll and to measure. One that is not rendered
|
|
98
|
+
* (inside a closed panel) reports every metric as 0, which reads as "already at the
|
|
99
|
+
* bottom" — a position describing no layout the user will ever see.
|
|
100
|
+
*/
|
|
101
|
+
#measurable() {
|
|
102
|
+
return this.element.clientHeight > 0;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Holds the `pinOnConnect` jump until the container is laid out, then runs it and
|
|
106
|
+
* re-reads the state — otherwise the panel opens at the top still claiming the bottom.
|
|
107
|
+
*/
|
|
108
|
+
#pinWhenLaidOut() {
|
|
109
|
+
if (typeof ResizeObserver === "undefined") return;
|
|
110
|
+
this.#layout = new ResizeObserver(() => {
|
|
111
|
+
if (!this.#measurable()) return;
|
|
112
|
+
this.#stopWaitingForLayout();
|
|
113
|
+
this.#scrollToBottom("instant");
|
|
114
|
+
this.#updatePinned();
|
|
115
|
+
});
|
|
116
|
+
this.#layout.observe(this.element);
|
|
117
|
+
}
|
|
118
|
+
/** Releases the layout watch, whether or not the deferred jump ever ran. */
|
|
119
|
+
#stopWaitingForLayout() {
|
|
120
|
+
this.#layout?.disconnect();
|
|
121
|
+
this.#layout = null;
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Scrolls to the bottom, clamped by the engine to the maximum scroll offset — which is
|
|
125
|
+
* 0 for a container tall enough to hold its whole content, so the jump moves nothing
|
|
126
|
+
* there. `behavior` defaults to the configured follow behavior; pass `"instant"` for a
|
|
127
|
+
* jump that must not animate.
|
|
128
|
+
*/
|
|
129
|
+
#scrollToBottom(behavior = this.#behavior()) {
|
|
85
130
|
const top = this.element.scrollHeight;
|
|
86
131
|
if (typeof this.element.scrollTo === "function") {
|
|
87
|
-
this.element.scrollTo({ top, behavior
|
|
132
|
+
this.element.scrollTo({ top, behavior });
|
|
88
133
|
} else {
|
|
89
134
|
this.element.scrollTop = top;
|
|
90
135
|
}
|
|
@@ -93,7 +138,12 @@ var StickToBottomController = class extends Controller {
|
|
|
93
138
|
#watched() {
|
|
94
139
|
return this.hasContentTarget ? this.contentTarget : this.element;
|
|
95
140
|
}
|
|
96
|
-
/**
|
|
141
|
+
/**
|
|
142
|
+
* The behavior a follow-scroll runs with. `"auto"` is **not** a request to arrive at
|
|
143
|
+
* once: it hands the decision to the element's computed `scroll-behavior`, so a
|
|
144
|
+
* consumer stylesheet saying `smooth` animates these scrolls too. Only `"instant"`
|
|
145
|
+
* overrides that CSS, which is why reduced motion and the `pinOnConnect` jump name it.
|
|
146
|
+
*/
|
|
97
147
|
#behavior() {
|
|
98
148
|
if (prefersReducedMotion()) return "instant";
|
|
99
149
|
return this.behaviorValue === "smooth" ? "smooth" : "auto";
|