solid-navigator 0.1.0 → 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
 
@@ -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;
@@ -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 { 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";
@@ -322,5 +336,6 @@ export {
322
336
  useLocation,
323
337
  useMatch,
324
338
  useNavigate,
325
- useParams
339
+ useParams,
340
+ useSearchParams
326
341
  };
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;
@@ -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 { 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";
@@ -319,5 +333,6 @@ export {
319
333
  useLocation,
320
334
  useMatch,
321
335
  useNavigate,
322
- useParams
336
+ useParams,
337
+ useSearchParams
323
338
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "solid-navigator",
3
- "version": "0.1.0",
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",