@real-router/svelte 0.13.12 → 0.13.14
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/dist/composables/useIsActiveRoute.svelte.js +31 -1
- package/dist/dom-utils/route-announcer.js +28 -1
- package/dist/dom-utils/scroll-restore.js +19 -2
- package/dist/dom-utils/scroll-spy.d.ts +7 -3
- package/dist/dom-utils/scroll-spy.js +51 -14
- package/dist/dom-utils/view-transitions.js +10 -1
- package/package.json +4 -4
|
@@ -1,8 +1,38 @@
|
|
|
1
|
-
import { createActiveRouteSource } from "@real-router/sources";
|
|
1
|
+
import { createActiveNameSelector, createActiveRouteSource, } from "@real-router/sources";
|
|
2
|
+
import { createSubscriber } from "svelte/reactivity";
|
|
2
3
|
import { createReactiveSource } from "../createReactiveSource.svelte";
|
|
3
4
|
import { useRouter } from "./useRouter.svelte";
|
|
4
5
|
export function useIsActiveRoute(routeName, params, strict, ignoreQueryParams, hash) {
|
|
5
6
|
const router = useRouter();
|
|
7
|
+
// Fast path (#1099) — the default-options `<Link>`: non-strict matching,
|
|
8
|
+
// query params ignored, no custom `params`, no `hash`. Use the per-router
|
|
9
|
+
// shared `ActiveNameSelector`: ONE `router.subscribe` handle serves any
|
|
10
|
+
// number of distinct-`routeName` links, instead of a per-link
|
|
11
|
+
// `createActiveRouteSource` (which allocates a `BaseSource` AND opens its own
|
|
12
|
+
// router subscription for every link). This mirrors the Solid adapter's
|
|
13
|
+
// `routeSelector` fast path and removes ~4 ms / 1000 links from the
|
|
14
|
+
// link-build mount — the per-link source setup was the bulk of the Svelte
|
|
15
|
+
// `<Link>`'s extra cost over the Solid adapter.
|
|
16
|
+
//
|
|
17
|
+
// Equivalence: the selector's `isActive` is exactly non-strict,
|
|
18
|
+
// query-ignoring, name-only matching — identical to
|
|
19
|
+
// `createActiveRouteSource(router, name, undefined, { strict: false,
|
|
20
|
+
// ignoreQueryParams: true })`. Any deviation from these defaults falls to the
|
|
21
|
+
// slow path below, whose cache handles the full argument surface (custom
|
|
22
|
+
// params, strict, `ignoreQueryParams: false`, hash-aware #532).
|
|
23
|
+
if (params === undefined &&
|
|
24
|
+
!strict &&
|
|
25
|
+
ignoreQueryParams &&
|
|
26
|
+
hash === undefined) {
|
|
27
|
+
const selector = createActiveNameSelector(router);
|
|
28
|
+
const subscribe = createSubscriber((update) => selector.subscribe(routeName, update));
|
|
29
|
+
return {
|
|
30
|
+
get current() {
|
|
31
|
+
subscribe();
|
|
32
|
+
return selector.isActive(routeName);
|
|
33
|
+
},
|
|
34
|
+
};
|
|
35
|
+
}
|
|
6
36
|
// The `hash` argument (#532) participates in the cache key when defined.
|
|
7
37
|
// exactOptionalPropertyTypes forbids `{ hash: undefined }` literally — we
|
|
8
38
|
// conditionally include the key only when a value is provided.
|
|
@@ -8,6 +8,14 @@ const NOOP_INSTANCE = Object.freeze({
|
|
|
8
8
|
/* no-op */
|
|
9
9
|
},
|
|
10
10
|
});
|
|
11
|
+
// Live (non-NOOP) instances sharing the single `[data-real-router-announcer]`
|
|
12
|
+
// aria-live element. The element is created once by the first instance
|
|
13
|
+
// (`getOrCreateAnnouncer`) and reused by the rest; it must be removed only when
|
|
14
|
+
// the LAST holder is destroyed. Without this count the first provider's
|
|
15
|
+
// destroy() would detach the shared node while sibling providers (micro-
|
|
16
|
+
// frontends — the same multi-provider scenario `scroll-restore`'s `storageKey`
|
|
17
|
+
// exists for) keep writing to the now-orphaned node → silent screen reader (#783).
|
|
18
|
+
let announcerRefCount = 0;
|
|
11
19
|
export function createRouteAnnouncer(router, options) {
|
|
12
20
|
// Defensive SSR / non-browser guard: in SSR (Node.js) or non-DOM
|
|
13
21
|
// environments, `document` is undefined and the announcer cannot
|
|
@@ -30,6 +38,7 @@ export function createRouteAnnouncer(router, options) {
|
|
|
30
38
|
let pendingText = null;
|
|
31
39
|
let clearTimeoutId;
|
|
32
40
|
const announcer = getOrCreateAnnouncer();
|
|
41
|
+
announcerRefCount += 1;
|
|
33
42
|
const doAnnounce = (text, h1) => {
|
|
34
43
|
lastAnnouncedText = text;
|
|
35
44
|
clearTimeout(clearTimeoutId);
|
|
@@ -83,11 +92,22 @@ export function createRouteAnnouncer(router, options) {
|
|
|
83
92
|
});
|
|
84
93
|
return {
|
|
85
94
|
destroy() {
|
|
95
|
+
// Idempotency guard — required so the ref-count is decremented EXACTLY
|
|
96
|
+
// once per instance. A double destroy() must not drop the count below the
|
|
97
|
+
// number of live holders (which would detach a sibling's element, or
|
|
98
|
+
// leave it attached forever).
|
|
99
|
+
if (isDestroyed) {
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
86
102
|
isDestroyed = true;
|
|
87
103
|
unsubscribe();
|
|
88
104
|
clearTimeout(clearTimeoutId);
|
|
89
105
|
clearTimeout(safariTimeoutId);
|
|
90
|
-
|
|
106
|
+
announcerRefCount -= 1;
|
|
107
|
+
// Only the last holder tears down the shared element.
|
|
108
|
+
if (announcerRefCount === 0) {
|
|
109
|
+
removeAnnouncer();
|
|
110
|
+
}
|
|
91
111
|
},
|
|
92
112
|
};
|
|
93
113
|
}
|
|
@@ -96,6 +116,13 @@ function getOrCreateAnnouncer() {
|
|
|
96
116
|
if (existing) {
|
|
97
117
|
return existing;
|
|
98
118
|
}
|
|
119
|
+
// Creating a FRESH element means no live instance is validly sharing one, so
|
|
120
|
+
// the ref-count restarts from zero (the caller increments immediately after).
|
|
121
|
+
// Without this, an element removed out from under live instances — a host
|
|
122
|
+
// wiping the subtree, or a consumer test whose teardown clears the DOM
|
|
123
|
+
// without calling every instance's destroy() — would leave a stale positive
|
|
124
|
+
// count that prevents the new element from ever being torn down (#783).
|
|
125
|
+
announcerRefCount = 0;
|
|
99
126
|
const element = document.createElement("div");
|
|
100
127
|
element.setAttribute("style", VISUALLY_HIDDEN);
|
|
101
128
|
element.setAttribute("aria-live", "assertive");
|
|
@@ -189,6 +189,15 @@ export function createScrollRestoration(router, options) {
|
|
|
189
189
|
};
|
|
190
190
|
let destroyed = false;
|
|
191
191
|
let unserializableWarned = false;
|
|
192
|
+
// Capture/effect seam guard (#782). previousRoute's position is captured
|
|
193
|
+
// synchronously in `subscribe`, but the snap/restore effect runs a frame
|
|
194
|
+
// later in rAF. Across that window the viewport still shows the route BEFORE
|
|
195
|
+
// previousRoute, so a second navigation landing in the same frame would
|
|
196
|
+
// capture that foreign position under previousRoute's key. `scrollSettled` is
|
|
197
|
+
// false across the window — capture is skipped (previousRoute's own stored
|
|
198
|
+
// value survives the transit). A real user scroll in this <16ms window is
|
|
199
|
+
// physically impossible.
|
|
200
|
+
let scrollSettled = true;
|
|
192
201
|
// `keyOf` defers to `canonicalJson` which calls `JSON.stringify`. Two
|
|
193
202
|
// realistic inputs blow up the serializer and would otherwise crash the
|
|
194
203
|
// subscribe callback (taking scroll-restore offline for the whole session):
|
|
@@ -214,17 +223,25 @@ export function createScrollRestoration(router, options) {
|
|
|
214
223
|
.navigation;
|
|
215
224
|
// Browsers dispatch reload as the initial navigation after refresh, so
|
|
216
225
|
// previousRoute is undefined and capture is naturally skipped. The
|
|
217
|
-
// pre-refresh position was already persisted via pagehide.
|
|
218
|
-
|
|
226
|
+
// pre-refresh position was already persisted via pagehide. Capture is also
|
|
227
|
+
// skipped while the scroll is unsettled — a second navigation in the same
|
|
228
|
+
// frame, before the prior nav's rAF snap (see `scrollSettled`, #782).
|
|
229
|
+
if (previousRoute && scrollSettled) {
|
|
219
230
|
const prevKey = safeKeyOf(previousRoute);
|
|
220
231
|
if (prevKey !== null) {
|
|
221
232
|
putPos(prevKey, readPos());
|
|
222
233
|
}
|
|
223
234
|
}
|
|
235
|
+
// This navigation's scroll effect is now pending: the viewport position no
|
|
236
|
+
// longer belongs to `route` until the rAF below runs and settles it.
|
|
237
|
+
scrollSettled = false;
|
|
224
238
|
requestAnimationFrame(() => {
|
|
225
239
|
if (destroyed) {
|
|
226
240
|
return;
|
|
227
241
|
}
|
|
242
|
+
// Effect running — the position now belongs to `route`, so the next
|
|
243
|
+
// capture is honest again.
|
|
244
|
+
scrollSettled = true;
|
|
228
245
|
if (mode === "top") {
|
|
229
246
|
scrollToHashOrTop(route);
|
|
230
247
|
return;
|
|
@@ -51,9 +51,13 @@ export interface ScrollSpyOptions {
|
|
|
51
51
|
*/
|
|
52
52
|
rootMargin?: string | undefined;
|
|
53
53
|
/**
|
|
54
|
-
* Lazy getter for the scrollable container.
|
|
55
|
-
*
|
|
56
|
-
*
|
|
54
|
+
* Lazy getter for the scrollable container. Consulted at creation and
|
|
55
|
+
* re-consulted on every reconcile (DOM mutation), so a container that
|
|
56
|
+
* MOUNTS or CHANGES after the spy is created is honoured: the
|
|
57
|
+
* `IntersectionObserver` root and `MutationObserver` target — both immutable
|
|
58
|
+
* once constructed — are rebuilt to match (#780). `null` (or a missing
|
|
59
|
+
* getter) falls back to the window viewport (`root: null` on the
|
|
60
|
+
* `IntersectionObserver`).
|
|
57
61
|
*/
|
|
58
62
|
scrollContainer?: (() => HTMLElement | null) | undefined;
|
|
59
63
|
}
|
|
@@ -180,11 +180,24 @@ const createObserverPair = (selector, rootMargin, getContainer, onIntersection,
|
|
|
180
180
|
}
|
|
181
181
|
onIntersection();
|
|
182
182
|
};
|
|
183
|
-
|
|
184
|
-
|
|
183
|
+
// Build (and rebuild) the IntersectionObserver for a given root. A root is
|
|
184
|
+
// immutable once its IO is constructed, so `reconcile` recreates the IO via
|
|
185
|
+
// this same factory when the resolved container changes (#780) — one
|
|
186
|
+
// definition, two call sites.
|
|
187
|
+
const makeIo = (container) => new IntersectionObserver(handleIntersection, {
|
|
188
|
+
root: container,
|
|
185
189
|
rootMargin,
|
|
186
190
|
threshold: 0,
|
|
187
191
|
});
|
|
192
|
+
// Container the IntersectionObserver root + MutationObserver target are
|
|
193
|
+
// built with. Both are immutable once the observer is constructed (W3C), so
|
|
194
|
+
// a `scrollContainer` that resolves to a different element after creation —
|
|
195
|
+
// most importantly one that MOUNTS after the spy starts (Angular wires the
|
|
196
|
+
// spy at bootstrap, before any component renders; a docs route's container
|
|
197
|
+
// mounts on navigation) — is only honoured by rebuilding the pair in
|
|
198
|
+
// `reconcile`. Tracked here so `reconcile` can compare on every run (#780).
|
|
199
|
+
let observerContainer = getContainer();
|
|
200
|
+
let io = makeIo(observerContainer);
|
|
188
201
|
const observeMatches = () => {
|
|
189
202
|
const scope = getContainer() ?? document;
|
|
190
203
|
let candidates;
|
|
@@ -216,6 +229,22 @@ const createObserverPair = (selector, rootMargin, getContainer, onIntersection,
|
|
|
216
229
|
observed.add(element);
|
|
217
230
|
}
|
|
218
231
|
};
|
|
232
|
+
// MutationObserver init — reused when the observer is re-pointed at a new
|
|
233
|
+
// container in `reconcile`. `childList: true, subtree: true` catches
|
|
234
|
+
// structural changes; `attributes: true, attributeFilter: ["id"]` catches
|
|
235
|
+
// anchor id renames (typical for client-rendered docs). The MO targets the
|
|
236
|
+
// scroll container (or document.body for the window viewport).
|
|
237
|
+
const MUTATION_OBSERVE_INIT = {
|
|
238
|
+
childList: true,
|
|
239
|
+
subtree: true,
|
|
240
|
+
attributes: true,
|
|
241
|
+
attributeFilter: ["id"],
|
|
242
|
+
};
|
|
243
|
+
// Null-then-assigned — the same forward-reference idiom as `flush` in the
|
|
244
|
+
// main scope. `reconcile` re-points it on a container change, so it must be
|
|
245
|
+
// in scope above its assignment; it is non-null by the time any async
|
|
246
|
+
// mutation callback (or `reconcile`) runs.
|
|
247
|
+
let mo = null;
|
|
219
248
|
const reconcile = () => {
|
|
220
249
|
// Drop observed elements that left the DOM. Avoids observer holding
|
|
221
250
|
// strong refs to detached nodes. Also drop their accumulated entry so
|
|
@@ -229,15 +258,28 @@ const createObserverPair = (selector, rootMargin, getContainer, onIntersection,
|
|
|
229
258
|
observed.delete(element);
|
|
230
259
|
pending.delete(element);
|
|
231
260
|
}
|
|
261
|
+
// Honour a container that mounted (or changed) after construction (#780).
|
|
262
|
+
// The IntersectionObserver root and MutationObserver target cannot be
|
|
263
|
+
// mutated in place, so rebuild the pair under the new container. Clearing
|
|
264
|
+
// `observed` + `pending` makes the rebuild equivalent to constructing the
|
|
265
|
+
// spy with this container from the start: `observeMatches` below
|
|
266
|
+
// re-populates the tracked set from the new container's scope, and the
|
|
267
|
+
// stale merged snapshot (computed against the old root's geometry) is
|
|
268
|
+
// dropped — one empty debounce window, acceptable for a rare event.
|
|
269
|
+
const nextContainer = getContainer();
|
|
270
|
+
if (nextContainer !== observerContainer) {
|
|
271
|
+
observerContainer = nextContainer;
|
|
272
|
+
io.disconnect();
|
|
273
|
+
io = makeIo(nextContainer);
|
|
274
|
+
observed.clear();
|
|
275
|
+
pending.clear();
|
|
276
|
+
mo?.disconnect();
|
|
277
|
+
mo?.observe(nextContainer ?? document.body, MUTATION_OBSERVE_INIT);
|
|
278
|
+
}
|
|
232
279
|
observeMatches();
|
|
233
280
|
};
|
|
234
281
|
observeMatches();
|
|
235
|
-
|
|
236
|
-
// window viewport). `childList: true, subtree: true` catches structural
|
|
237
|
-
// changes; `attributes: true, attributeFilter: ["id"]` catches anchor
|
|
238
|
-
// id renames (typical for client-rendered docs).
|
|
239
|
-
const mutationTarget = getContainer() ?? document.body;
|
|
240
|
-
const mo = new MutationObserver(() => {
|
|
282
|
+
mo = new MutationObserver(() => {
|
|
241
283
|
if (mutationTimer !== null) {
|
|
242
284
|
clearTimeout(mutationTimer);
|
|
243
285
|
}
|
|
@@ -246,12 +288,7 @@ const createObserverPair = (selector, rootMargin, getContainer, onIntersection,
|
|
|
246
288
|
reconcile();
|
|
247
289
|
}, MUTATION_DEBOUNCE_MS);
|
|
248
290
|
});
|
|
249
|
-
mo.observe(
|
|
250
|
-
childList: true,
|
|
251
|
-
subtree: true,
|
|
252
|
-
attributes: true,
|
|
253
|
-
attributeFilter: ["id"],
|
|
254
|
-
});
|
|
291
|
+
mo.observe(observerContainer ?? document.body, MUTATION_OBSERVE_INIT);
|
|
255
292
|
return {
|
|
256
293
|
pending,
|
|
257
294
|
destroy() {
|
|
@@ -89,6 +89,13 @@ export function createViewTransitions(router) {
|
|
|
89
89
|
currentVT = null;
|
|
90
90
|
}
|
|
91
91
|
else {
|
|
92
|
+
// The VT this resolver belongs to. If the next navigation opens a new VT
|
|
93
|
+
// in the task-queue window before this setTimeout runs, the stale
|
|
94
|
+
// resolver must NOT null the new `currentVT` — otherwise a later
|
|
95
|
+
// cancellation reads `null` and skips nothing, leaking a stale animation
|
|
96
|
+
// (#781). Same identity-guard technique the navigation-plugin uses to
|
|
97
|
+
// heal its seams.
|
|
98
|
+
const scheduledVT = currentVT;
|
|
92
99
|
// CRITICAL: CANNOT use requestAnimationFrame here. When the router
|
|
93
100
|
// takes the async path (leave returned a Promise), subscribe fires
|
|
94
101
|
// AFTER the browser has already transitioned VT into the
|
|
@@ -103,7 +110,9 @@ export function createViewTransitions(router) {
|
|
|
103
110
|
// so the new DOM is committed by the time our callback runs.
|
|
104
111
|
setTimeout(() => {
|
|
105
112
|
resolver();
|
|
106
|
-
currentVT
|
|
113
|
+
if (currentVT === scheduledVT) {
|
|
114
|
+
currentVT = null;
|
|
115
|
+
}
|
|
107
116
|
}, 0);
|
|
108
117
|
}
|
|
109
118
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@real-router/svelte",
|
|
3
|
-
"version": "0.13.
|
|
3
|
+
"version": "0.13.14",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Svelte 5 integration for Real-Router",
|
|
6
6
|
"svelte": "./dist/index.js",
|
|
@@ -48,8 +48,8 @@
|
|
|
48
48
|
"license": "MIT",
|
|
49
49
|
"sideEffects": false,
|
|
50
50
|
"dependencies": {
|
|
51
|
-
"@real-router/core": "^0.62.0",
|
|
52
51
|
"@real-router/route-utils": "^0.2.5",
|
|
52
|
+
"@real-router/core": "^0.62.3",
|
|
53
53
|
"@real-router/sources": "^0.10.1"
|
|
54
54
|
},
|
|
55
55
|
"devDependencies": {
|
|
@@ -60,10 +60,10 @@
|
|
|
60
60
|
"@testing-library/user-event": "14.6.1",
|
|
61
61
|
"eslint-plugin-svelte": "3.19.0",
|
|
62
62
|
"rimraf": "6.1.3",
|
|
63
|
-
"svelte": "5.56.
|
|
63
|
+
"svelte": "5.56.4",
|
|
64
64
|
"svelte-check": "4.6.0",
|
|
65
65
|
"svelte-eslint-parser": "1.8.0",
|
|
66
|
-
"@real-router/browser-plugin": "^0.
|
|
66
|
+
"@real-router/browser-plugin": "^0.18.1"
|
|
67
67
|
},
|
|
68
68
|
"peerDependencies": {
|
|
69
69
|
"svelte": ">=5.7.0"
|