solid-navigator 0.1.0 → 0.2.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 +13 -2
- package/dist/dev.js +21 -9
- package/dist/dev.jsx +32 -8
- package/dist/index.d.ts +2 -1
- package/dist/index.js +21 -9
- package/dist/index.jsx +32 -8
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
|
|
7
7
|
[](https://pnpm.io/)
|
|
8
8
|
|
|
9
|
-
Solid Navigator is a library that is inspired by vue
|
|
9
|
+
Solid Navigator is a library that is inspired by vue-router and @solidjs/router.
|
|
10
10
|
|
|
11
11
|
|
|
12
12
|
## Quick start
|
|
@@ -22,7 +22,7 @@ pnpm add solid-navigator
|
|
|
22
22
|
Use it:
|
|
23
23
|
|
|
24
24
|
```tsx
|
|
25
|
-
import {
|
|
25
|
+
import { Router, Route, Outlet, A, Navigate, useNavigate, useLocation, useParams, useMatch, useSearchParams } from 'solid-navigator'
|
|
26
26
|
```
|
|
27
27
|
|
|
28
28
|
|
|
@@ -65,6 +65,17 @@ const match = useMatch(() => "/chats/1234");
|
|
|
65
65
|
} | null
|
|
66
66
|
```
|
|
67
67
|
|
|
68
|
+
### `useSearchParams`
|
|
69
|
+
```js
|
|
70
|
+
// path: /chats?id=1234
|
|
71
|
+
const [params, setParams] = useSearchParams();
|
|
72
|
+
params = {
|
|
73
|
+
id: "1234"
|
|
74
|
+
} | null
|
|
75
|
+
|
|
76
|
+
setParams({id: "5678", hello: "Bye"}) // path: /chats?id=5678&hello=Bye
|
|
77
|
+
```
|
|
78
|
+
|
|
68
79
|
## Components
|
|
69
80
|
|
|
70
81
|
### `Router`
|
package/dist/dev.js
CHANGED
|
@@ -58,8 +58,7 @@ function matchSegment(input, filter) {
|
|
|
58
58
|
}
|
|
59
59
|
return false;
|
|
60
60
|
}
|
|
61
|
-
var createLocation = (path) => {
|
|
62
|
-
const [query, setQuery] = createStore({});
|
|
61
|
+
var createLocation = (path, query, setQuery) => {
|
|
63
62
|
const url = createMemo(() => {
|
|
64
63
|
return new URL(path(), "http://owo");
|
|
65
64
|
});
|
|
@@ -109,8 +108,9 @@ function Router(props) {
|
|
|
109
108
|
const [pathname, setPathname] = createSignal(location.pathname);
|
|
110
109
|
const [hashAndSearch, setHashAndSearch] = createSignal(getHashAndSearch());
|
|
111
110
|
const [params, setParams] = createStore({});
|
|
111
|
+
const [query, setQuery] = createStore({});
|
|
112
112
|
const pathnameWithHashAndSearch = createMemo(() => pathname() + hashAndSearch());
|
|
113
|
-
const loc = createLocation(pathnameWithHashAndSearch);
|
|
113
|
+
const loc = createLocation(pathnameWithHashAndSearch, query, setQuery);
|
|
114
114
|
const matched = createMemo(() => {
|
|
115
115
|
if (!routes())
|
|
116
116
|
return;
|
|
@@ -140,8 +140,8 @@ function Router(props) {
|
|
|
140
140
|
setHashAndSearch(getHashAndSearch());
|
|
141
141
|
};
|
|
142
142
|
const onClick = (event) => {
|
|
143
|
-
const target = event.
|
|
144
|
-
if (target
|
|
143
|
+
const target = event.composedPath().find((el) => el instanceof Node && el.nodeName.toUpperCase() === "A");
|
|
144
|
+
if (target?.tagName !== "A")
|
|
145
145
|
return;
|
|
146
146
|
if (!target.hasAttribute("sn-link"))
|
|
147
147
|
return;
|
|
@@ -167,7 +167,9 @@ function Router(props) {
|
|
|
167
167
|
params,
|
|
168
168
|
location: loc,
|
|
169
169
|
setHashAndSearch,
|
|
170
|
-
setPathname
|
|
170
|
+
setPathname,
|
|
171
|
+
setQuery,
|
|
172
|
+
query
|
|
171
173
|
},
|
|
172
174
|
get children() {
|
|
173
175
|
return props.root?.();
|
|
@@ -235,8 +237,6 @@ var flattenedRoute = (route) => {
|
|
|
235
237
|
}
|
|
236
238
|
return routes;
|
|
237
239
|
};
|
|
238
|
-
|
|
239
|
-
// src/navigator.ts
|
|
240
240
|
var createNavigate = (routes, pathname, setPathname, setHashAndSearch) => {
|
|
241
241
|
return (path, options) => {
|
|
242
242
|
let newPath = path;
|
|
@@ -269,6 +269,18 @@ var Navigate = (props) => {
|
|
|
269
269
|
navigate(props.href, { replace: true });
|
|
270
270
|
return null;
|
|
271
271
|
};
|
|
272
|
+
var useSearchParams = () => {
|
|
273
|
+
const context = useRouterContext();
|
|
274
|
+
const navigate = useNavigate();
|
|
275
|
+
const updateQuery = (query) => {
|
|
276
|
+
context.setQuery(reconcile(query));
|
|
277
|
+
const url = new URL(window.location.href);
|
|
278
|
+
const newSearch = new URLSearchParams(context.query);
|
|
279
|
+
url.search = newSearch.toString();
|
|
280
|
+
navigate(url.pathname + url.search + url.hash);
|
|
281
|
+
};
|
|
282
|
+
return [context.location.query, updateQuery];
|
|
283
|
+
};
|
|
272
284
|
var Fragment = () => [];
|
|
273
285
|
var Outlet = (props) => {
|
|
274
286
|
const context = useRouterContext();
|
|
@@ -316,4 +328,4 @@ var useMatch = (path) => {
|
|
|
316
328
|
return createMemo(() => matcher()(context.location.pathname));
|
|
317
329
|
};
|
|
318
330
|
|
|
319
|
-
export { A, Navigate, Outlet, Route, Router, useLocation, useMatch, useNavigate, useParams };
|
|
331
|
+
export { A, Navigate, Outlet, Route, Router, useLocation, useMatch, useNavigate, useParams, useSearchParams };
|
package/dist/dev.jsx
CHANGED
|
@@ -67,9 +67,8 @@ function matchSegment(input, filter) {
|
|
|
67
67
|
|
|
68
68
|
// src/createLocation.ts
|
|
69
69
|
import { createEffect, createMemo, on } from "solid-js";
|
|
70
|
-
import {
|
|
71
|
-
var createLocation = (path) => {
|
|
72
|
-
const [query, setQuery] = createStore({});
|
|
70
|
+
import { reconcile } from "solid-js/store";
|
|
71
|
+
var createLocation = (path, query, setQuery) => {
|
|
73
72
|
const url = createMemo(() => {
|
|
74
73
|
return new URL(path(), "http://owo");
|
|
75
74
|
});
|
|
@@ -119,8 +118,9 @@ function Router(props) {
|
|
|
119
118
|
const [pathname, setPathname] = createSignal(location.pathname);
|
|
120
119
|
const [hashAndSearch, setHashAndSearch] = createSignal(getHashAndSearch());
|
|
121
120
|
const [params, setParams] = createStore2({});
|
|
121
|
+
const [query, setQuery] = createStore2({});
|
|
122
122
|
const pathnameWithHashAndSearch = createMemo2(() => pathname() + hashAndSearch());
|
|
123
|
-
const loc = createLocation(pathnameWithHashAndSearch);
|
|
123
|
+
const loc = createLocation(pathnameWithHashAndSearch, query, setQuery);
|
|
124
124
|
const matched = createMemo2(() => {
|
|
125
125
|
if (!routes())
|
|
126
126
|
return;
|
|
@@ -147,8 +147,8 @@ function Router(props) {
|
|
|
147
147
|
setHashAndSearch(getHashAndSearch());
|
|
148
148
|
};
|
|
149
149
|
const onClick = (event) => {
|
|
150
|
-
const target = event.
|
|
151
|
-
if (target
|
|
150
|
+
const target = event.composedPath().find((el) => el instanceof Node && el.nodeName.toUpperCase() === "A");
|
|
151
|
+
if (target?.tagName !== "A")
|
|
152
152
|
return;
|
|
153
153
|
if (!target.hasAttribute("sn-link"))
|
|
154
154
|
return;
|
|
@@ -167,7 +167,17 @@ function Router(props) {
|
|
|
167
167
|
});
|
|
168
168
|
});
|
|
169
169
|
return <RouterContext.Provider
|
|
170
|
-
value={{
|
|
170
|
+
value={{
|
|
171
|
+
routes,
|
|
172
|
+
matched,
|
|
173
|
+
navigate,
|
|
174
|
+
params,
|
|
175
|
+
location: loc,
|
|
176
|
+
setHashAndSearch,
|
|
177
|
+
setPathname,
|
|
178
|
+
setQuery,
|
|
179
|
+
query
|
|
180
|
+
}}
|
|
171
181
|
>{props.root?.()}</RouterContext.Provider>;
|
|
172
182
|
}
|
|
173
183
|
var useRouterContext = () => {
|
|
@@ -231,6 +241,7 @@ var flattenedRoute = (route) => {
|
|
|
231
241
|
};
|
|
232
242
|
|
|
233
243
|
// src/navigator.ts
|
|
244
|
+
import { reconcile as reconcile3 } from "solid-js/store";
|
|
234
245
|
var createNavigate = (routes, pathname, setPathname, setHashAndSearch) => {
|
|
235
246
|
return (path, options) => {
|
|
236
247
|
let newPath = path;
|
|
@@ -263,6 +274,18 @@ var Navigate = (props) => {
|
|
|
263
274
|
navigate(props.href, { replace: true });
|
|
264
275
|
return null;
|
|
265
276
|
};
|
|
277
|
+
var useSearchParams = () => {
|
|
278
|
+
const context = useRouterContext();
|
|
279
|
+
const navigate = useNavigate();
|
|
280
|
+
const updateQuery = (query) => {
|
|
281
|
+
context.setQuery(reconcile3(query));
|
|
282
|
+
const url = new URL(window.location.href);
|
|
283
|
+
const newSearch = new URLSearchParams(context.query);
|
|
284
|
+
url.search = newSearch.toString();
|
|
285
|
+
navigate(url.pathname + url.search + url.hash);
|
|
286
|
+
};
|
|
287
|
+
return [context.location.query, updateQuery];
|
|
288
|
+
};
|
|
266
289
|
|
|
267
290
|
// src/Outlet.tsx
|
|
268
291
|
import { createMemo as createMemo3 } from "solid-js";
|
|
@@ -322,5 +345,6 @@ export {
|
|
|
322
345
|
useLocation,
|
|
323
346
|
useMatch,
|
|
324
347
|
useNavigate,
|
|
325
|
-
useParams
|
|
348
|
+
useParams,
|
|
349
|
+
useSearchParams
|
|
326
350
|
};
|
package/dist/index.d.ts
CHANGED
|
@@ -18,6 +18,7 @@ declare const useNavigate: () => (path: string, options?: NavigateOptions | unde
|
|
|
18
18
|
declare const Navigate: (props: {
|
|
19
19
|
href: string;
|
|
20
20
|
}) => null;
|
|
21
|
+
declare const useSearchParams: () => readonly [Record<string, string>, (query: Record<string, string>) => void];
|
|
21
22
|
|
|
22
23
|
declare const Outlet: (props: {
|
|
23
24
|
children?: string;
|
|
@@ -49,4 +50,4 @@ type LinkProps = {
|
|
|
49
50
|
declare const A: (props: LinkProps) => JSX.Element;
|
|
50
51
|
declare const useMatch: (path: () => string) => solid_js.Accessor<PathMatch | null>;
|
|
51
52
|
|
|
52
|
-
export { A, Navigate, type NavigateOptions, Outlet, Route, type RouteObject, Router, type RouterProps, useLocation, useMatch, useNavigate, useParams };
|
|
53
|
+
export { A, Navigate, type NavigateOptions, Outlet, Route, type RouteObject, Router, type RouterProps, useLocation, useMatch, useNavigate, useParams, useSearchParams };
|
package/dist/index.js
CHANGED
|
@@ -58,8 +58,7 @@ function matchSegment(input, filter) {
|
|
|
58
58
|
}
|
|
59
59
|
return false;
|
|
60
60
|
}
|
|
61
|
-
var createLocation = (path) => {
|
|
62
|
-
const [query, setQuery] = createStore({});
|
|
61
|
+
var createLocation = (path, query, setQuery) => {
|
|
63
62
|
const url = createMemo(() => {
|
|
64
63
|
return new URL(path(), "http://owo");
|
|
65
64
|
});
|
|
@@ -109,8 +108,9 @@ function Router(props) {
|
|
|
109
108
|
const [pathname, setPathname] = createSignal(location.pathname);
|
|
110
109
|
const [hashAndSearch, setHashAndSearch] = createSignal(getHashAndSearch());
|
|
111
110
|
const [params, setParams] = createStore({});
|
|
111
|
+
const [query, setQuery] = createStore({});
|
|
112
112
|
const pathnameWithHashAndSearch = createMemo(() => pathname() + hashAndSearch());
|
|
113
|
-
const loc = createLocation(pathnameWithHashAndSearch);
|
|
113
|
+
const loc = createLocation(pathnameWithHashAndSearch, query, setQuery);
|
|
114
114
|
const matched = createMemo(() => {
|
|
115
115
|
if (!routes())
|
|
116
116
|
return;
|
|
@@ -140,8 +140,8 @@ function Router(props) {
|
|
|
140
140
|
setHashAndSearch(getHashAndSearch());
|
|
141
141
|
};
|
|
142
142
|
const onClick = (event) => {
|
|
143
|
-
const target = event.
|
|
144
|
-
if (target
|
|
143
|
+
const target = event.composedPath().find((el) => el instanceof Node && el.nodeName.toUpperCase() === "A");
|
|
144
|
+
if (target?.tagName !== "A")
|
|
145
145
|
return;
|
|
146
146
|
if (!target.hasAttribute("sn-link"))
|
|
147
147
|
return;
|
|
@@ -167,7 +167,9 @@ function Router(props) {
|
|
|
167
167
|
params,
|
|
168
168
|
location: loc,
|
|
169
169
|
setHashAndSearch,
|
|
170
|
-
setPathname
|
|
170
|
+
setPathname,
|
|
171
|
+
setQuery,
|
|
172
|
+
query
|
|
171
173
|
},
|
|
172
174
|
get children() {
|
|
173
175
|
return props.root?.();
|
|
@@ -235,8 +237,6 @@ var flattenedRoute = (route) => {
|
|
|
235
237
|
}
|
|
236
238
|
return routes;
|
|
237
239
|
};
|
|
238
|
-
|
|
239
|
-
// src/navigator.ts
|
|
240
240
|
var createNavigate = (routes, pathname, setPathname, setHashAndSearch) => {
|
|
241
241
|
return (path, options) => {
|
|
242
242
|
let newPath = path;
|
|
@@ -268,6 +268,18 @@ var Navigate = (props) => {
|
|
|
268
268
|
navigate(props.href, { replace: true });
|
|
269
269
|
return null;
|
|
270
270
|
};
|
|
271
|
+
var useSearchParams = () => {
|
|
272
|
+
const context = useRouterContext();
|
|
273
|
+
const navigate = useNavigate();
|
|
274
|
+
const updateQuery = (query) => {
|
|
275
|
+
context.setQuery(reconcile(query));
|
|
276
|
+
const url = new URL(window.location.href);
|
|
277
|
+
const newSearch = new URLSearchParams(context.query);
|
|
278
|
+
url.search = newSearch.toString();
|
|
279
|
+
navigate(url.pathname + url.search + url.hash);
|
|
280
|
+
};
|
|
281
|
+
return [context.location.query, updateQuery];
|
|
282
|
+
};
|
|
271
283
|
var Fragment = () => [];
|
|
272
284
|
var Outlet = (props) => {
|
|
273
285
|
const context = useRouterContext();
|
|
@@ -313,4 +325,4 @@ var useMatch = (path) => {
|
|
|
313
325
|
return createMemo(() => matcher()(context.location.pathname));
|
|
314
326
|
};
|
|
315
327
|
|
|
316
|
-
export { A, Navigate, Outlet, Route, Router, useLocation, useMatch, useNavigate, useParams };
|
|
328
|
+
export { A, Navigate, Outlet, Route, Router, useLocation, useMatch, useNavigate, useParams, useSearchParams };
|
package/dist/index.jsx
CHANGED
|
@@ -67,9 +67,8 @@ function matchSegment(input, filter) {
|
|
|
67
67
|
|
|
68
68
|
// src/createLocation.ts
|
|
69
69
|
import { createEffect, createMemo, on } from "solid-js";
|
|
70
|
-
import {
|
|
71
|
-
var createLocation = (path) => {
|
|
72
|
-
const [query, setQuery] = createStore({});
|
|
70
|
+
import { reconcile } from "solid-js/store";
|
|
71
|
+
var createLocation = (path, query, setQuery) => {
|
|
73
72
|
const url = createMemo(() => {
|
|
74
73
|
return new URL(path(), "http://owo");
|
|
75
74
|
});
|
|
@@ -119,8 +118,9 @@ function Router(props) {
|
|
|
119
118
|
const [pathname, setPathname] = createSignal(location.pathname);
|
|
120
119
|
const [hashAndSearch, setHashAndSearch] = createSignal(getHashAndSearch());
|
|
121
120
|
const [params, setParams] = createStore2({});
|
|
121
|
+
const [query, setQuery] = createStore2({});
|
|
122
122
|
const pathnameWithHashAndSearch = createMemo2(() => pathname() + hashAndSearch());
|
|
123
|
-
const loc = createLocation(pathnameWithHashAndSearch);
|
|
123
|
+
const loc = createLocation(pathnameWithHashAndSearch, query, setQuery);
|
|
124
124
|
const matched = createMemo2(() => {
|
|
125
125
|
if (!routes())
|
|
126
126
|
return;
|
|
@@ -147,8 +147,8 @@ function Router(props) {
|
|
|
147
147
|
setHashAndSearch(getHashAndSearch());
|
|
148
148
|
};
|
|
149
149
|
const onClick = (event) => {
|
|
150
|
-
const target = event.
|
|
151
|
-
if (target
|
|
150
|
+
const target = event.composedPath().find((el) => el instanceof Node && el.nodeName.toUpperCase() === "A");
|
|
151
|
+
if (target?.tagName !== "A")
|
|
152
152
|
return;
|
|
153
153
|
if (!target.hasAttribute("sn-link"))
|
|
154
154
|
return;
|
|
@@ -167,7 +167,17 @@ function Router(props) {
|
|
|
167
167
|
});
|
|
168
168
|
});
|
|
169
169
|
return <RouterContext.Provider
|
|
170
|
-
value={{
|
|
170
|
+
value={{
|
|
171
|
+
routes,
|
|
172
|
+
matched,
|
|
173
|
+
navigate,
|
|
174
|
+
params,
|
|
175
|
+
location: loc,
|
|
176
|
+
setHashAndSearch,
|
|
177
|
+
setPathname,
|
|
178
|
+
setQuery,
|
|
179
|
+
query
|
|
180
|
+
}}
|
|
171
181
|
>{props.root?.()}</RouterContext.Provider>;
|
|
172
182
|
}
|
|
173
183
|
var useRouterContext = () => {
|
|
@@ -231,6 +241,7 @@ var flattenedRoute = (route) => {
|
|
|
231
241
|
};
|
|
232
242
|
|
|
233
243
|
// src/navigator.ts
|
|
244
|
+
import { reconcile as reconcile3 } from "solid-js/store";
|
|
234
245
|
var createNavigate = (routes, pathname, setPathname, setHashAndSearch) => {
|
|
235
246
|
return (path, options) => {
|
|
236
247
|
let newPath = path;
|
|
@@ -262,6 +273,18 @@ var Navigate = (props) => {
|
|
|
262
273
|
navigate(props.href, { replace: true });
|
|
263
274
|
return null;
|
|
264
275
|
};
|
|
276
|
+
var useSearchParams = () => {
|
|
277
|
+
const context = useRouterContext();
|
|
278
|
+
const navigate = useNavigate();
|
|
279
|
+
const updateQuery = (query) => {
|
|
280
|
+
context.setQuery(reconcile3(query));
|
|
281
|
+
const url = new URL(window.location.href);
|
|
282
|
+
const newSearch = new URLSearchParams(context.query);
|
|
283
|
+
url.search = newSearch.toString();
|
|
284
|
+
navigate(url.pathname + url.search + url.hash);
|
|
285
|
+
};
|
|
286
|
+
return [context.location.query, updateQuery];
|
|
287
|
+
};
|
|
265
288
|
|
|
266
289
|
// src/Outlet.tsx
|
|
267
290
|
import { createMemo as createMemo3 } from "solid-js";
|
|
@@ -319,5 +342,6 @@ export {
|
|
|
319
342
|
useLocation,
|
|
320
343
|
useMatch,
|
|
321
344
|
useNavigate,
|
|
322
|
-
useParams
|
|
345
|
+
useParams,
|
|
346
|
+
useSearchParams
|
|
323
347
|
};
|