solid-navigator 0.0.1 → 0.2.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 CHANGED
@@ -6,7 +6,7 @@
6
6
 
7
7
  [![pnpm](https://img.shields.io/badge/maintained%20with-pnpm-cc00ff.svg?style=for-the-badge&logo=pnpm)](https://pnpm.io/)
8
8
 
9
- Solid Navigator is a library that is inspired by vue router and solid router.
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 { useNavigate, Outlet, Route, Router, useLocation, useParams, A } from 'solid-navigator'
25
+ import { Router, Route, Outlet, A, Navigate, useNavigate, useLocation, useParams, useMatch, useSearchParams } from 'solid-navigator'
26
26
  ```
27
27
 
28
28
 
@@ -55,6 +55,27 @@ const params = useParams<{id: string}>();
55
55
  }
56
56
  ```
57
57
 
58
+ ### `useMatch`
59
+ ```js
60
+ // path: /chats/1234
61
+ const match = useMatch(() => "/chats/1234");
62
+ {
63
+ path: "/chats/1234"
64
+ params: {}
65
+ } | null
66
+ ```
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
+
58
79
  ## Components
59
80
 
60
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;
@@ -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();
@@ -310,5 +322,10 @@ var A = (props) => {
310
322
  return _el$;
311
323
  })();
312
324
  };
325
+ var useMatch = (path) => {
326
+ const context = useRouterContext();
327
+ const matcher = createMemo(() => createMatcher(path()));
328
+ return createMemo(() => matcher()(context.location.pathname));
329
+ };
313
330
 
314
- export { A, Navigate, Outlet, Route, Router, useLocation, 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 { createStore, reconcile } from "solid-js/store";
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;
@@ -167,7 +167,7 @@ function Router(props) {
167
167
  });
168
168
  });
169
169
  return <RouterContext.Provider
170
- value={{ routes, matched, navigate, params, location: loc, setHashAndSearch, setPathname }}
170
+ value={{ routes, matched, navigate, params, location: loc, setHashAndSearch, setPathname, setQuery, query }}
171
171
  >{props.root?.()}</RouterContext.Provider>;
172
172
  }
173
173
  var useRouterContext = () => {
@@ -231,6 +231,7 @@ var flattenedRoute = (route) => {
231
231
  };
232
232
 
233
233
  // src/navigator.ts
234
+ import { reconcile as reconcile3 } from "solid-js/store";
234
235
  var createNavigate = (routes, pathname, setPathname, setHashAndSearch) => {
235
236
  return (path, options) => {
236
237
  let newPath = path;
@@ -263,6 +264,19 @@ var Navigate = (props) => {
263
264
  navigate(props.href, { replace: true });
264
265
  return null;
265
266
  };
267
+ var useSearchParams = () => {
268
+ const context = useRouterContext();
269
+ const navigate = useNavigate();
270
+ const updateQuery = (query) => {
271
+ context.setQuery(reconcile3(query));
272
+ const url = new URL(window.location.href);
273
+ const newSearch = new URLSearchParams(context.query);
274
+ ;
275
+ url.search = newSearch.toString();
276
+ navigate(url.pathname + url.search + url.hash);
277
+ };
278
+ return [context.location.query, updateQuery];
279
+ };
266
280
 
267
281
  // src/Outlet.tsx
268
282
  import { createMemo as createMemo3 } from "solid-js";
@@ -304,9 +318,15 @@ var Route = (props) => {
304
318
  };
305
319
 
306
320
  // src/Link.tsx
321
+ import { createMemo as createMemo4 } from "solid-js";
307
322
  var A = (props) => {
308
323
  return <a sn-link {...props} />;
309
324
  };
325
+ var useMatch = (path) => {
326
+ const context = useRouterContext();
327
+ const matcher = createMemo4(() => createMatcher(path()));
328
+ return createMemo4(() => matcher()(context.location.pathname));
329
+ };
310
330
  export {
311
331
  A,
312
332
  Navigate,
@@ -314,6 +334,8 @@ export {
314
334
  Route,
315
335
  Router,
316
336
  useLocation,
337
+ useMatch,
317
338
  useNavigate,
318
- useParams
339
+ useParams,
340
+ useSearchParams
319
341
  };
package/dist/index.d.ts CHANGED
@@ -18,12 +18,19 @@ 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;
24
25
  name?: string;
25
26
  }) => solid_js.JSX.Element;
26
27
 
28
+ type Params = Record<string, string>;
29
+ interface PathMatch {
30
+ params: Params;
31
+ path: string;
32
+ }
33
+
27
34
  interface RouterProps {
28
35
  children?: JSX.Element;
29
36
  root?: () => JSX.Element;
@@ -41,5 +48,6 @@ type LinkProps = {
41
48
  replace?: boolean;
42
49
  } & JSX.AnchorHTMLAttributes<HTMLAnchorElement>;
43
50
  declare const A: (props: LinkProps) => JSX.Element;
51
+ declare const useMatch: (path: () => string) => solid_js.Accessor<PathMatch | null>;
44
52
 
45
- export { A, Navigate, type NavigateOptions, Outlet, Route, type RouteObject, Router, type RouterProps, useLocation, 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;
@@ -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();
@@ -307,5 +319,10 @@ var A = (props) => {
307
319
  return _el$;
308
320
  })();
309
321
  };
322
+ var useMatch = (path) => {
323
+ const context = useRouterContext();
324
+ const matcher = createMemo(() => createMatcher(path()));
325
+ return createMemo(() => matcher()(context.location.pathname));
326
+ };
310
327
 
311
- export { A, Navigate, Outlet, Route, Router, useLocation, 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 { createStore, reconcile } from "solid-js/store";
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;
@@ -167,7 +167,7 @@ function Router(props) {
167
167
  });
168
168
  });
169
169
  return <RouterContext.Provider
170
- value={{ routes, matched, navigate, params, location: loc, setHashAndSearch, setPathname }}
170
+ value={{ routes, matched, navigate, params, location: loc, setHashAndSearch, setPathname, setQuery, query }}
171
171
  >{props.root?.()}</RouterContext.Provider>;
172
172
  }
173
173
  var useRouterContext = () => {
@@ -231,6 +231,7 @@ var flattenedRoute = (route) => {
231
231
  };
232
232
 
233
233
  // src/navigator.ts
234
+ import { reconcile as reconcile3 } from "solid-js/store";
234
235
  var createNavigate = (routes, pathname, setPathname, setHashAndSearch) => {
235
236
  return (path, options) => {
236
237
  let newPath = path;
@@ -262,6 +263,19 @@ var Navigate = (props) => {
262
263
  navigate(props.href, { replace: true });
263
264
  return null;
264
265
  };
266
+ var useSearchParams = () => {
267
+ const context = useRouterContext();
268
+ const navigate = useNavigate();
269
+ const updateQuery = (query) => {
270
+ context.setQuery(reconcile3(query));
271
+ const url = new URL(window.location.href);
272
+ const newSearch = new URLSearchParams(context.query);
273
+ ;
274
+ url.search = newSearch.toString();
275
+ navigate(url.pathname + url.search + url.hash);
276
+ };
277
+ return [context.location.query, updateQuery];
278
+ };
265
279
 
266
280
  // src/Outlet.tsx
267
281
  import { createMemo as createMemo3 } from "solid-js";
@@ -301,9 +315,15 @@ var Route = (props) => {
301
315
  };
302
316
 
303
317
  // src/Link.tsx
318
+ import { createMemo as createMemo4 } from "solid-js";
304
319
  var A = (props) => {
305
320
  return <a sn-link {...props} />;
306
321
  };
322
+ var useMatch = (path) => {
323
+ const context = useRouterContext();
324
+ const matcher = createMemo4(() => createMatcher(path()));
325
+ return createMemo4(() => matcher()(context.location.pathname));
326
+ };
307
327
  export {
308
328
  A,
309
329
  Navigate,
@@ -311,6 +331,8 @@ export {
311
331
  Route,
312
332
  Router,
313
333
  useLocation,
334
+ useMatch,
314
335
  useNavigate,
315
- useParams
336
+ useParams,
337
+ useSearchParams
316
338
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "solid-navigator",
3
- "version": "0.0.1",
3
+ "version": "0.2.0",
4
4
  "description": "Solid Navigator is a library that is inspired by vue router and solid router.",
5
5
  "license": "MIT",
6
6
  "author": "SupertigerDev",