@real-router/svelte 0.17.52 → 0.18.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 +1 -1
- package/dist/actions/link.svelte.js +9 -4
- package/dist/components/Link.svelte +2 -1
- package/dist/dom-utils/index.d.ts +1 -1
- package/dist/dom-utils/index.js +1 -1
- package/dist/dom-utils/link-utils.d.ts +47 -0
- package/dist/dom-utils/link-utils.js +54 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -191,7 +191,7 @@ Navigation link with automatic active state detection. Uses `$derived` for href
|
|
|
191
191
|
| `activeStrict` | `boolean` | `false` | Exact match only (no ancestor matching) |
|
|
192
192
|
| `ignoreQueryParams` | `boolean` | `true` | Query params don't affect active state |
|
|
193
193
|
| `hash` | `string` | `undefined` | URL fragment (decoded). Tri-state: undefined preserves, `""` clears, value sets. (#532) |
|
|
194
|
-
| `target` | `string` | `undefined` |
|
|
194
|
+
| `target` | `string` | `undefined` | Anchor `target`. Anything but unset / `""` / `_self` is left to the browser (`_blank`, `_top`, a named frame) |
|
|
195
195
|
| `onclick` | `(evt: MouseEvent) => void` | `undefined` | Custom click handler. Runs **before** the navigation logic — call `evt.preventDefault()` to suppress navigation. |
|
|
196
196
|
|
|
197
197
|
All other props are spread onto the `<a>` element.
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { ROUTER_KEY, getContextOrThrow } from "../context";
|
|
2
2
|
import { EMPTY_OPTIONS, EMPTY_PARAMS, NOOP } from "../constants";
|
|
3
|
-
import { shouldNavigate, applyLinkA11y } from "../dom-utils";
|
|
3
|
+
import { shouldNavigate, anchorTargetsAnotherContext, applyLinkA11y, } from "../dom-utils";
|
|
4
4
|
const delegationByRouter = new WeakMap();
|
|
5
5
|
function findRegisteredNode(nodes, target) {
|
|
6
6
|
let el = target instanceof HTMLElement ? target : null;
|
|
@@ -37,9 +37,7 @@ function getDelegation(router) {
|
|
|
37
37
|
if (!node) {
|
|
38
38
|
return;
|
|
39
39
|
}
|
|
40
|
-
|
|
41
|
-
if (node instanceof HTMLAnchorElement &&
|
|
42
|
-
node.getAttribute("target") === "_blank") {
|
|
40
|
+
if (anchorTargetsAnotherContext(node)) {
|
|
43
41
|
return;
|
|
44
42
|
}
|
|
45
43
|
evt.preventDefault();
|
|
@@ -54,6 +52,13 @@ function getDelegation(router) {
|
|
|
54
52
|
if (!node || node instanceof HTMLButtonElement) {
|
|
55
53
|
return;
|
|
56
54
|
}
|
|
55
|
+
// Enter is an activation like the click above, and the anchor's `target`
|
|
56
|
+
// scopes it the same way: the browser is already loading the URL into the
|
|
57
|
+
// context the markup named, so a second, in-app navigation would move the
|
|
58
|
+
// page out from under it (#1834).
|
|
59
|
+
if (anchorTargetsAnotherContext(node)) {
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
57
62
|
navigate(nodes.get(node));
|
|
58
63
|
}
|
|
59
64
|
const state = {
|
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
import { EMPTY_OPTIONS, EMPTY_PARAMS, NOOP } from "../constants";
|
|
5
5
|
import {
|
|
6
6
|
shouldNavigate,
|
|
7
|
+
targetsAnotherContext,
|
|
7
8
|
buildHref,
|
|
8
9
|
buildActiveClassName,
|
|
9
10
|
navigateWithHash,
|
|
@@ -121,7 +122,7 @@
|
|
|
121
122
|
}
|
|
122
123
|
}
|
|
123
124
|
|
|
124
|
-
if (!shouldNavigate(evt) || target
|
|
125
|
+
if (!shouldNavigate(evt) || targetsAnotherContext(target)) {
|
|
125
126
|
return;
|
|
126
127
|
}
|
|
127
128
|
|
|
@@ -3,7 +3,7 @@ export { createRouteAnnouncer } from "./route-announcer.js";
|
|
|
3
3
|
export { createScrollRestoration } from "./scroll-restore.js";
|
|
4
4
|
export { createScrollSpy } from "./scroll-spy.js";
|
|
5
5
|
export { createViewTransitions } from "./view-transitions.js";
|
|
6
|
-
export { shouldNavigate, buildHref, buildActiveClassName, navigateWithHash, resolveLinkTarget, shallowEqual, applyLinkA11y, } from "./link-utils.js";
|
|
6
|
+
export { shouldNavigate, targetsAnotherContext, anchorTargetsAnotherContext, buildHref, buildActiveClassName, navigateWithHash, resolveLinkTarget, shallowEqual, applyLinkA11y, } from "./link-utils.js";
|
|
7
7
|
export type { RouteAnnouncerOptions } from "./route-announcer.js";
|
|
8
8
|
export type { ScrollRestorationOptions } from "./scroll-restore.js";
|
|
9
9
|
export type { ScrollSpy, ScrollSpyOptions } from "./scroll-spy.js";
|
package/dist/dom-utils/index.js
CHANGED
|
@@ -3,4 +3,4 @@ export { createRouteAnnouncer } from "./route-announcer.js";
|
|
|
3
3
|
export { createScrollRestoration } from "./scroll-restore.js";
|
|
4
4
|
export { createScrollSpy } from "./scroll-spy.js";
|
|
5
5
|
export { createViewTransitions } from "./view-transitions.js";
|
|
6
|
-
export { shouldNavigate, buildHref, buildActiveClassName, navigateWithHash, resolveLinkTarget, shallowEqual, applyLinkA11y, } from "./link-utils.js";
|
|
6
|
+
export { shouldNavigate, targetsAnotherContext, anchorTargetsAnotherContext, buildHref, buildActiveClassName, navigateWithHash, resolveLinkTarget, shallowEqual, applyLinkA11y, } from "./link-utils.js";
|
|
@@ -26,6 +26,53 @@ export interface ResolvedLinkTarget {
|
|
|
26
26
|
*/
|
|
27
27
|
export declare function resolveLinkTarget(to: NavigationTarget | undefined, routeName: string, routeParams: Params | undefined, routeSearch: SearchParams | undefined): ResolvedLinkTarget;
|
|
28
28
|
export declare function shouldNavigate(evt: MouseEvent): boolean;
|
|
29
|
+
/**
|
|
30
|
+
* Does an anchor's `target` send this navigation somewhere the router cannot
|
|
31
|
+
* follow? (#1834)
|
|
32
|
+
*
|
|
33
|
+
* `target` names the browsing context the author wants the URL loaded into.
|
|
34
|
+
* Three values are the router's — absent, empty, `_self` — and every other one
|
|
35
|
+
* goes to the browser, the only thing that can resolve a context name.
|
|
36
|
+
* Intercepting instead is how a `<Link target="_blank">` ends up reloading the
|
|
37
|
+
* same tab rather than opening a new one. React Router's
|
|
38
|
+
* `shouldProcessLinkClick` and TanStack Router's `handleClick` split the same
|
|
39
|
+
* way; neither reproduces browsing contexts inside the router, and neither does
|
|
40
|
+
* this.
|
|
41
|
+
*
|
|
42
|
+
* ⚠ The split is by SPELLING, not by where the value resolves to, and three
|
|
43
|
+
* spellings resolve back to this context anyway: `_parent` and `_top` fall back
|
|
44
|
+
* to `_self` in a document with no ancestor, and `_SELF` matches `_self`
|
|
45
|
+
* ASCII-case-insensitively (MDN, `<a>` § target). All three are handed to the
|
|
46
|
+
* browser, which reaches the right destination by a full page load instead of a
|
|
47
|
+
* transition. Resolving them properly means reproducing frame ancestry and
|
|
48
|
+
* keyword folding here; both reference routers decline, and the cost is a page
|
|
49
|
+
* load rather than a wrong destination.
|
|
50
|
+
*
|
|
51
|
+
* ⚠ Ask this only about an `<a>`. On a `<button v-link>` or a `<div use:link>`
|
|
52
|
+
* the attribute is inert markup the browser will not act on, so deferring there
|
|
53
|
+
* would leave the activation unhandled by anyone. Callers that cannot assume an
|
|
54
|
+
* anchor ask {@link anchorTargetsAnotherContext} instead, which narrows first;
|
|
55
|
+
* `target-predicate-authority-1834` owns which call site is in which set.
|
|
56
|
+
*/
|
|
57
|
+
export declare function targetsAnotherContext(target: string | null | undefined): boolean;
|
|
58
|
+
/**
|
|
59
|
+
* The same question, asked about an ELEMENT — for the `use:link` / `v-link`
|
|
60
|
+
* forms, which attach to whatever element the consumer wrote and so cannot
|
|
61
|
+
* assume an anchor (#1834). The `<Link>` components pass a value instead
|
|
62
|
+
* (they render the anchor), and so does Angular's directive (its selector is
|
|
63
|
+
* `a[realLink]`).
|
|
64
|
+
*
|
|
65
|
+
* ⚠ `tagName`, never `instanceof HTMLAnchorElement`, for the reason
|
|
66
|
+
* `applyLinkA11y` sets out below: the constructor belongs to the realm this
|
|
67
|
+
* module loaded in, so a real anchor from an iframe `contentDocument` or a
|
|
68
|
+
* micro-frontend fails the check. Failing it HERE re-intercepts the very
|
|
69
|
+
* `target="_blank"` click this predicate exists to leave alone.
|
|
70
|
+
*
|
|
71
|
+
* Anything that is not an HTML anchor answers `false`: `target` is
|
|
72
|
+
* anchor-specific markup the browser will not act on, so a `<button use:link>`
|
|
73
|
+
* or a `<div v-link>` carrying one must still navigate in-app.
|
|
74
|
+
*/
|
|
75
|
+
export declare function anchorTargetsAnotherContext(element: Element | null | undefined): boolean;
|
|
29
76
|
/**
|
|
30
77
|
* Builds an href for a `<Link>` element.
|
|
31
78
|
*
|
|
@@ -48,6 +48,60 @@ export function shouldNavigate(evt) {
|
|
|
48
48
|
!evt.ctrlKey &&
|
|
49
49
|
!evt.shiftKey);
|
|
50
50
|
}
|
|
51
|
+
/**
|
|
52
|
+
* Does an anchor's `target` send this navigation somewhere the router cannot
|
|
53
|
+
* follow? (#1834)
|
|
54
|
+
*
|
|
55
|
+
* `target` names the browsing context the author wants the URL loaded into.
|
|
56
|
+
* Three values are the router's — absent, empty, `_self` — and every other one
|
|
57
|
+
* goes to the browser, the only thing that can resolve a context name.
|
|
58
|
+
* Intercepting instead is how a `<Link target="_blank">` ends up reloading the
|
|
59
|
+
* same tab rather than opening a new one. React Router's
|
|
60
|
+
* `shouldProcessLinkClick` and TanStack Router's `handleClick` split the same
|
|
61
|
+
* way; neither reproduces browsing contexts inside the router, and neither does
|
|
62
|
+
* this.
|
|
63
|
+
*
|
|
64
|
+
* ⚠ The split is by SPELLING, not by where the value resolves to, and three
|
|
65
|
+
* spellings resolve back to this context anyway: `_parent` and `_top` fall back
|
|
66
|
+
* to `_self` in a document with no ancestor, and `_SELF` matches `_self`
|
|
67
|
+
* ASCII-case-insensitively (MDN, `<a>` § target). All three are handed to the
|
|
68
|
+
* browser, which reaches the right destination by a full page load instead of a
|
|
69
|
+
* transition. Resolving them properly means reproducing frame ancestry and
|
|
70
|
+
* keyword folding here; both reference routers decline, and the cost is a page
|
|
71
|
+
* load rather than a wrong destination.
|
|
72
|
+
*
|
|
73
|
+
* ⚠ Ask this only about an `<a>`. On a `<button v-link>` or a `<div use:link>`
|
|
74
|
+
* the attribute is inert markup the browser will not act on, so deferring there
|
|
75
|
+
* would leave the activation unhandled by anyone. Callers that cannot assume an
|
|
76
|
+
* anchor ask {@link anchorTargetsAnotherContext} instead, which narrows first;
|
|
77
|
+
* `target-predicate-authority-1834` owns which call site is in which set.
|
|
78
|
+
*/
|
|
79
|
+
export function targetsAnotherContext(target) {
|
|
80
|
+
return Boolean(target) && target !== "_self";
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* The same question, asked about an ELEMENT — for the `use:link` / `v-link`
|
|
84
|
+
* forms, which attach to whatever element the consumer wrote and so cannot
|
|
85
|
+
* assume an anchor (#1834). The `<Link>` components pass a value instead
|
|
86
|
+
* (they render the anchor), and so does Angular's directive (its selector is
|
|
87
|
+
* `a[realLink]`).
|
|
88
|
+
*
|
|
89
|
+
* ⚠ `tagName`, never `instanceof HTMLAnchorElement`, for the reason
|
|
90
|
+
* `applyLinkA11y` sets out below: the constructor belongs to the realm this
|
|
91
|
+
* module loaded in, so a real anchor from an iframe `contentDocument` or a
|
|
92
|
+
* micro-frontend fails the check. Failing it HERE re-intercepts the very
|
|
93
|
+
* `target="_blank"` click this predicate exists to leave alone.
|
|
94
|
+
*
|
|
95
|
+
* Anything that is not an HTML anchor answers `false`: `target` is
|
|
96
|
+
* anchor-specific markup the browser will not act on, so a `<button use:link>`
|
|
97
|
+
* or a `<div v-link>` carrying one must still navigate in-app.
|
|
98
|
+
*/
|
|
99
|
+
export function anchorTargetsAnotherContext(element) {
|
|
100
|
+
if (element?.tagName !== "A") {
|
|
101
|
+
return false;
|
|
102
|
+
}
|
|
103
|
+
return targetsAnotherContext(element.getAttribute("target"));
|
|
104
|
+
}
|
|
51
105
|
/**
|
|
52
106
|
* RFC 3986 fragment encoding: preserve sub-delims (`&`, `=`, `?`, `:`),
|
|
53
107
|
* encode space, `%`, control chars, non-ASCII via encodeURI; defensively
|