@solidjs/router 0.7.1 → 0.8.1
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 +11 -0
- package/dist/components.jsx +2 -2
- package/dist/index.js +51 -1
- package/dist/integration.d.ts +9 -0
- package/dist/integration.js +46 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -18,6 +18,7 @@ It supports all of Solid's SSR methods and has Solid's transitions baked in, so
|
|
|
18
18
|
- [Data Functions](#data-functions)
|
|
19
19
|
- [Nested Routes](#nested-routes)
|
|
20
20
|
- [Hash Mode Router](#hash-mode-router)
|
|
21
|
+
- [Memory Mode Router](#memory-mode-router)
|
|
21
22
|
- [Config Based Routing](#config-based-routing)
|
|
22
23
|
- [Router Primitives](#router-primitives)
|
|
23
24
|
- [useParams](#useparams)
|
|
@@ -419,6 +420,16 @@ import { Router, hashIntegration } from '@solidjs/router'
|
|
|
419
420
|
<Router source={hashIntegration()}><App /></Router>
|
|
420
421
|
```
|
|
421
422
|
|
|
423
|
+
## Memory Mode Router
|
|
424
|
+
|
|
425
|
+
You can also use memory mode router for testing purpose.
|
|
426
|
+
|
|
427
|
+
```jsx
|
|
428
|
+
import { Router, memoryIntegration } from '@solidjs/router'
|
|
429
|
+
|
|
430
|
+
<Router source={memoryIntegration()}><App /></Router>
|
|
431
|
+
```
|
|
432
|
+
|
|
422
433
|
## Config Based Routing
|
|
423
434
|
|
|
424
435
|
You don't have to use JSX to set up your routes; you can pass an object directly with `useRoutes`:
|
package/dist/components.jsx
CHANGED
|
@@ -61,8 +61,8 @@ export const Routes = (props) => {
|
|
|
61
61
|
root = next[0];
|
|
62
62
|
return next;
|
|
63
63
|
}));
|
|
64
|
-
return (<Show when={routeStates() && root}>
|
|
65
|
-
{
|
|
64
|
+
return (<Show when={routeStates() && root} keyed>
|
|
65
|
+
{route => <RouteContextObj.Provider value={route}>{route.outlet()}</RouteContextObj.Provider>}
|
|
66
66
|
</Show>);
|
|
67
67
|
};
|
|
68
68
|
export const useRoutes = (routes, base) => {
|
package/dist/index.js
CHANGED
|
@@ -24,6 +24,49 @@ function scrollToHash(hash, fallbackTop) {
|
|
|
24
24
|
window.scrollTo(0, 0);
|
|
25
25
|
}
|
|
26
26
|
}
|
|
27
|
+
function createMemoryHistory() {
|
|
28
|
+
const entries = ["/"];
|
|
29
|
+
let index = 0;
|
|
30
|
+
const listeners = [];
|
|
31
|
+
const go = n => {
|
|
32
|
+
// https://github.com/remix-run/react-router/blob/682810ca929d0e3c64a76f8d6e465196b7a2ac58/packages/router/history.ts#L245
|
|
33
|
+
index = Math.max(0, Math.min(index + n, entries.length - 1));
|
|
34
|
+
const value = entries[index];
|
|
35
|
+
listeners.forEach(listener => listener(value));
|
|
36
|
+
};
|
|
37
|
+
return {
|
|
38
|
+
get: () => entries[index],
|
|
39
|
+
set: ({
|
|
40
|
+
value,
|
|
41
|
+
scroll,
|
|
42
|
+
replace
|
|
43
|
+
}) => {
|
|
44
|
+
if (replace) {
|
|
45
|
+
entries[index] = value;
|
|
46
|
+
} else {
|
|
47
|
+
entries.splice(index + 1, entries.length - index, value);
|
|
48
|
+
index++;
|
|
49
|
+
}
|
|
50
|
+
if (scroll) {
|
|
51
|
+
scrollToHash(value.split("#")[1] || "", true);
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
back: () => {
|
|
55
|
+
go(-1);
|
|
56
|
+
},
|
|
57
|
+
forward: () => {
|
|
58
|
+
go(1);
|
|
59
|
+
},
|
|
60
|
+
go,
|
|
61
|
+
listen: listener => {
|
|
62
|
+
listeners.push(listener);
|
|
63
|
+
return () => {
|
|
64
|
+
const index = listeners.indexOf(listener);
|
|
65
|
+
listeners.splice(index, 1);
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
};
|
|
69
|
+
}
|
|
27
70
|
function createIntegration(get, set, init, utils) {
|
|
28
71
|
let ignore = false;
|
|
29
72
|
const wrap = value => typeof value === "string" ? {
|
|
@@ -115,6 +158,12 @@ function hashIntegration() {
|
|
|
115
158
|
}
|
|
116
159
|
});
|
|
117
160
|
}
|
|
161
|
+
function memoryIntegration() {
|
|
162
|
+
const memoryHistory = createMemoryHistory();
|
|
163
|
+
return createIntegration(memoryHistory.get, memoryHistory.set, memoryHistory.listen, {
|
|
164
|
+
go: memoryHistory.go
|
|
165
|
+
});
|
|
166
|
+
}
|
|
118
167
|
|
|
119
168
|
function createBeforeLeave() {
|
|
120
169
|
let listeners = new Set();
|
|
@@ -787,6 +836,7 @@ const Routes = props => {
|
|
|
787
836
|
get when() {
|
|
788
837
|
return routeStates() && root;
|
|
789
838
|
},
|
|
839
|
+
keyed: true,
|
|
790
840
|
children: route => createComponent$1(RouteContextObj.Provider, {
|
|
791
841
|
value: route,
|
|
792
842
|
get children() {
|
|
@@ -883,4 +933,4 @@ function Navigate(props) {
|
|
|
883
933
|
return null;
|
|
884
934
|
}
|
|
885
935
|
|
|
886
|
-
export { A, A as Link, A as NavLink, Navigate, Outlet, Route, Router, Routes, mergeSearchString as _mergeSearchString, createBeforeLeave, createIntegration, hashIntegration, normalizeIntegration, pathIntegration, staticIntegration, useBeforeLeave, useHref, useIsRouting, useLocation, useMatch, useNavigate, useParams, useResolvedPath, useRouteData, useRoutes, useSearchParams };
|
|
936
|
+
export { A, A as Link, A as NavLink, Navigate, Outlet, Route, Router, Routes, mergeSearchString as _mergeSearchString, createBeforeLeave, createIntegration, createMemoryHistory, hashIntegration, memoryIntegration, normalizeIntegration, pathIntegration, staticIntegration, useBeforeLeave, useHref, useIsRouting, useLocation, useMatch, useNavigate, useParams, useResolvedPath, useRouteData, useRoutes, useSearchParams };
|
package/dist/integration.d.ts
CHANGED
|
@@ -1,6 +1,15 @@
|
|
|
1
1
|
import type { LocationChange, LocationChangeSignal, RouterIntegration, RouterUtils } from "./types";
|
|
2
|
+
export declare function createMemoryHistory(): {
|
|
3
|
+
get: () => string;
|
|
4
|
+
set: ({ value, scroll, replace }: LocationChange) => void;
|
|
5
|
+
back: () => void;
|
|
6
|
+
forward: () => void;
|
|
7
|
+
go: (n: number) => void;
|
|
8
|
+
listen: (listener: (value: string) => void) => () => void;
|
|
9
|
+
};
|
|
2
10
|
export declare function createIntegration(get: () => string | LocationChange, set: (next: LocationChange) => void, init?: (notify: (value?: string | LocationChange) => void) => () => void, utils?: Partial<RouterUtils>): RouterIntegration;
|
|
3
11
|
export declare function normalizeIntegration(integration: RouterIntegration | LocationChangeSignal | undefined): RouterIntegration;
|
|
4
12
|
export declare function staticIntegration(obj: LocationChange): RouterIntegration;
|
|
5
13
|
export declare function pathIntegration(): RouterIntegration;
|
|
6
14
|
export declare function hashIntegration(): RouterIntegration;
|
|
15
|
+
export declare function memoryIntegration(): RouterIntegration;
|
package/dist/integration.js
CHANGED
|
@@ -24,6 +24,46 @@ function scrollToHash(hash, fallbackTop) {
|
|
|
24
24
|
window.scrollTo(0, 0);
|
|
25
25
|
}
|
|
26
26
|
}
|
|
27
|
+
export function createMemoryHistory() {
|
|
28
|
+
const entries = ["/"];
|
|
29
|
+
let index = 0;
|
|
30
|
+
const listeners = [];
|
|
31
|
+
const go = (n) => {
|
|
32
|
+
// https://github.com/remix-run/react-router/blob/682810ca929d0e3c64a76f8d6e465196b7a2ac58/packages/router/history.ts#L245
|
|
33
|
+
index = Math.max(0, Math.min(index + n, entries.length - 1));
|
|
34
|
+
const value = entries[index];
|
|
35
|
+
listeners.forEach(listener => listener(value));
|
|
36
|
+
};
|
|
37
|
+
return {
|
|
38
|
+
get: () => entries[index],
|
|
39
|
+
set: ({ value, scroll, replace }) => {
|
|
40
|
+
if (replace) {
|
|
41
|
+
entries[index] = value;
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
entries.splice(index + 1, entries.length - index, value);
|
|
45
|
+
index++;
|
|
46
|
+
}
|
|
47
|
+
if (scroll) {
|
|
48
|
+
scrollToHash(value.split("#")[1] || "", true);
|
|
49
|
+
}
|
|
50
|
+
},
|
|
51
|
+
back: () => {
|
|
52
|
+
go(-1);
|
|
53
|
+
},
|
|
54
|
+
forward: () => {
|
|
55
|
+
go(1);
|
|
56
|
+
},
|
|
57
|
+
go,
|
|
58
|
+
listen: (listener) => {
|
|
59
|
+
listeners.push(listener);
|
|
60
|
+
return () => {
|
|
61
|
+
const index = listeners.indexOf(listener);
|
|
62
|
+
listeners.splice(index, 1);
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
}
|
|
27
67
|
export function createIntegration(get, set, init, utils) {
|
|
28
68
|
let ignore = false;
|
|
29
69
|
const wrap = (value) => (typeof value === "string" ? { value } : value);
|
|
@@ -103,3 +143,9 @@ export function hashIntegration() {
|
|
|
103
143
|
}
|
|
104
144
|
});
|
|
105
145
|
}
|
|
146
|
+
export function memoryIntegration() {
|
|
147
|
+
const memoryHistory = createMemoryHistory();
|
|
148
|
+
return createIntegration(memoryHistory.get, memoryHistory.set, memoryHistory.listen, {
|
|
149
|
+
go: memoryHistory.go
|
|
150
|
+
});
|
|
151
|
+
}
|