@real-router/svelte 0.14.0 → 0.15.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 +4 -0
- package/dist/actions/link.svelte.js +81 -26
- package/package.json +6 -6
package/README.md
CHANGED
|
@@ -163,6 +163,8 @@ All composables must be called during component initialization (not inside `$eff
|
|
|
163
163
|
|
|
164
164
|
Navigation link with automatic active state detection. Uses `$derived` for href and class — only the DOM attributes update when active state changes.
|
|
165
165
|
|
|
166
|
+
> **Rendering many links?** For nav menus, sitemaps or long/paginated lists, [`use:link`](#createlinkaction) is a lighter alternative — it enhances a plain `<a>` instead of instantiating a component per link (roughly **2× cheaper to mount** at ~1000 links).
|
|
167
|
+
|
|
166
168
|
```svelte
|
|
167
169
|
<Link
|
|
168
170
|
routeName="users.profile"
|
|
@@ -330,6 +332,8 @@ Implementation: `$state(false)` + `$effect(() => mounted = true)`. The Svelte co
|
|
|
330
332
|
|
|
331
333
|
Factory function that creates a low-level action for adding navigation to any element. Must be called during component initialization to capture the router context.
|
|
332
334
|
|
|
335
|
+
> **Prefer `use:link` for link-heavy pages.** Because it enhances a plain element instead of instantiating a Svelte component per link, `use:link` skips the component overhead `<Link>` pays on every instance — mounting a large batch of `use:link` anchors costs roughly **half** the script time of the same batch of `<Link>`s (directional, measured on ~1000 links). For nav menus, sitemaps, mega-menus and paginated lists, reach for `use:link` — set `href` yourself via `router.buildPath(name, params)` for real, middle-clickable anchors. Keep `<Link>` for its ergonomics (automatic active state, `hash` support) on ordinary links.
|
|
336
|
+
|
|
333
337
|
```svelte
|
|
334
338
|
<script lang="ts">
|
|
335
339
|
import { createLinkAction } from "@real-router/svelte";
|
|
@@ -1,6 +1,83 @@
|
|
|
1
1
|
import { ROUTER_KEY, getContextOrThrow } from "../context";
|
|
2
2
|
import { EMPTY_OPTIONS, EMPTY_PARAMS, NOOP } from "../constants";
|
|
3
3
|
import { shouldNavigate, applyLinkA11y } from "../dom-utils";
|
|
4
|
+
const delegationByRouter = new WeakMap();
|
|
5
|
+
function findRegisteredNode(nodes, target) {
|
|
6
|
+
let el = target instanceof HTMLElement ? target : null;
|
|
7
|
+
while (el) {
|
|
8
|
+
if (nodes.has(el)) {
|
|
9
|
+
return el;
|
|
10
|
+
}
|
|
11
|
+
el = el.parentElement;
|
|
12
|
+
}
|
|
13
|
+
return undefined;
|
|
14
|
+
}
|
|
15
|
+
function getDelegation(router) {
|
|
16
|
+
const cached = delegationByRouter.get(router);
|
|
17
|
+
if (cached) {
|
|
18
|
+
return cached;
|
|
19
|
+
}
|
|
20
|
+
const nodes = new WeakMap();
|
|
21
|
+
let count = 0;
|
|
22
|
+
function navigate(target) {
|
|
23
|
+
router
|
|
24
|
+
.navigate(target.name, target.params ?? EMPTY_PARAMS, target.options ?? EMPTY_OPTIONS)
|
|
25
|
+
.catch(NOOP);
|
|
26
|
+
}
|
|
27
|
+
function handleClick(evt) {
|
|
28
|
+
if (!shouldNavigate(evt)) {
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
const node = findRegisteredNode(nodes, evt.target);
|
|
32
|
+
if (!node) {
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
// Anchor `target="_blank"` opens a new tab — let the browser handle it.
|
|
36
|
+
if (node instanceof HTMLAnchorElement &&
|
|
37
|
+
node.getAttribute("target") === "_blank") {
|
|
38
|
+
return;
|
|
39
|
+
}
|
|
40
|
+
evt.preventDefault();
|
|
41
|
+
navigate(nodes.get(node));
|
|
42
|
+
}
|
|
43
|
+
function handleKeyDown(evt) {
|
|
44
|
+
if (evt.key !== "Enter") {
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
const node = findRegisteredNode(nodes, evt.target);
|
|
48
|
+
// Buttons activate on Enter natively (WAI-ARIA) — don't double-fire.
|
|
49
|
+
if (!node || node instanceof HTMLButtonElement) {
|
|
50
|
+
return;
|
|
51
|
+
}
|
|
52
|
+
navigate(nodes.get(node));
|
|
53
|
+
}
|
|
54
|
+
const state = {
|
|
55
|
+
register(node, params) {
|
|
56
|
+
nodes.set(node, params);
|
|
57
|
+
if (count === 0) {
|
|
58
|
+
document.addEventListener("click", handleClick);
|
|
59
|
+
document.addEventListener("keydown", handleKeyDown);
|
|
60
|
+
}
|
|
61
|
+
count++;
|
|
62
|
+
},
|
|
63
|
+
update(node, params) {
|
|
64
|
+
nodes.set(node, params);
|
|
65
|
+
},
|
|
66
|
+
unregister(node) {
|
|
67
|
+
// Svelte calls an action's `destroy()` exactly once per node, so every
|
|
68
|
+
// `register` is balanced by one `unregister` — no double-decrement guard
|
|
69
|
+
// needed (mirrors the original per-node `removeEventListener` teardown).
|
|
70
|
+
nodes.delete(node);
|
|
71
|
+
count--;
|
|
72
|
+
if (count === 0) {
|
|
73
|
+
document.removeEventListener("click", handleClick);
|
|
74
|
+
document.removeEventListener("keydown", handleKeyDown);
|
|
75
|
+
}
|
|
76
|
+
},
|
|
77
|
+
};
|
|
78
|
+
delegationByRouter.set(router, state);
|
|
79
|
+
return state;
|
|
80
|
+
}
|
|
4
81
|
/**
|
|
5
82
|
* Factory function that captures router context during component initialization.
|
|
6
83
|
* Must be called during component init (not inside event handlers or effects).
|
|
@@ -21,38 +98,16 @@ import { shouldNavigate, applyLinkA11y } from "../dom-utils";
|
|
|
21
98
|
*/
|
|
22
99
|
export function createLinkAction() {
|
|
23
100
|
const router = getContextOrThrow(ROUTER_KEY, "createLinkAction");
|
|
101
|
+
const delegation = getDelegation(router);
|
|
24
102
|
return function link(node, params) {
|
|
25
|
-
let currentParams = params;
|
|
26
103
|
applyLinkA11y(node);
|
|
27
|
-
|
|
28
|
-
router
|
|
29
|
-
.navigate(currentParams.name, currentParams.params ?? EMPTY_PARAMS, currentParams.options ?? EMPTY_OPTIONS)
|
|
30
|
-
.catch(NOOP);
|
|
31
|
-
}
|
|
32
|
-
function handleClick(evt) {
|
|
33
|
-
if (!shouldNavigate(evt))
|
|
34
|
-
return;
|
|
35
|
-
if (node instanceof HTMLAnchorElement &&
|
|
36
|
-
node.getAttribute("target") === "_blank") {
|
|
37
|
-
return;
|
|
38
|
-
}
|
|
39
|
-
evt.preventDefault();
|
|
40
|
-
navigate();
|
|
41
|
-
}
|
|
42
|
-
function handleKeyDown(evt) {
|
|
43
|
-
if (evt.key === "Enter" && !(node instanceof HTMLButtonElement)) {
|
|
44
|
-
navigate();
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
node.addEventListener("click", handleClick);
|
|
48
|
-
node.addEventListener("keydown", handleKeyDown);
|
|
104
|
+
delegation.register(node, params);
|
|
49
105
|
return {
|
|
50
106
|
update(newParams) {
|
|
51
|
-
|
|
107
|
+
delegation.update(node, newParams);
|
|
52
108
|
},
|
|
53
109
|
destroy() {
|
|
54
|
-
|
|
55
|
-
node.removeEventListener("keydown", handleKeyDown);
|
|
110
|
+
delegation.unregister(node);
|
|
56
111
|
},
|
|
57
112
|
};
|
|
58
113
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@real-router/svelte",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.15.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Svelte 5 integration for Real-Router",
|
|
6
6
|
"svelte": "./dist/index.js",
|
|
@@ -48,22 +48,22 @@
|
|
|
48
48
|
"license": "MIT",
|
|
49
49
|
"sideEffects": false,
|
|
50
50
|
"dependencies": {
|
|
51
|
-
"@real-router/
|
|
52
|
-
"@real-router/
|
|
53
|
-
"@real-router/sources": "^0.10.
|
|
51
|
+
"@real-router/route-utils": "^0.2.6",
|
|
52
|
+
"@real-router/core": "^0.63.2",
|
|
53
|
+
"@real-router/sources": "^0.10.2"
|
|
54
54
|
},
|
|
55
55
|
"devDependencies": {
|
|
56
56
|
"@sveltejs/package": "2.5.8",
|
|
57
57
|
"@sveltejs/vite-plugin-svelte": "6.2.4",
|
|
58
58
|
"@testing-library/jest-dom": "6.9.1",
|
|
59
|
-
"@testing-library/svelte": "5.
|
|
59
|
+
"@testing-library/svelte": "5.4.2",
|
|
60
60
|
"@testing-library/user-event": "14.6.1",
|
|
61
61
|
"eslint-plugin-svelte": "3.19.0",
|
|
62
62
|
"rimraf": "6.1.3",
|
|
63
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.18.
|
|
66
|
+
"@real-router/browser-plugin": "^0.18.2"
|
|
67
67
|
},
|
|
68
68
|
"peerDependencies": {
|
|
69
69
|
"svelte": ">=5.7.0"
|