@solidjs/router 0.15.3 → 0.16.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.
Files changed (66) hide show
  1. package/README.md +178 -141
  2. package/dist/data/action.d.ts +1 -1
  3. package/dist/data/createAsync.js +8 -6
  4. package/dist/data/query.js +11 -5
  5. package/dist/data/response.d.ts +1 -1
  6. package/dist/index.d.ts +1 -1
  7. package/dist/index.js +57 -22
  8. package/dist/routers/HashRouter.d.ts +1 -1
  9. package/dist/routers/Router.js +1 -1
  10. package/dist/routers/components.d.ts +1 -1
  11. package/dist/routers/components.jsx +5 -2
  12. package/dist/routers/createRouter.d.ts +1 -1
  13. package/dist/src/components.d.ts +31 -0
  14. package/dist/src/components.jsx +39 -0
  15. package/dist/src/data/action.d.ts +17 -0
  16. package/dist/src/data/action.js +163 -0
  17. package/dist/src/data/action.spec.d.ts +1 -0
  18. package/dist/src/data/action.spec.js +297 -0
  19. package/dist/src/data/createAsync.d.ts +32 -0
  20. package/dist/src/data/createAsync.js +96 -0
  21. package/dist/src/data/createAsync.spec.d.ts +1 -0
  22. package/dist/src/data/createAsync.spec.js +196 -0
  23. package/dist/src/data/events.d.ts +9 -0
  24. package/dist/src/data/events.js +123 -0
  25. package/dist/src/data/events.spec.d.ts +1 -0
  26. package/dist/src/data/events.spec.js +567 -0
  27. package/dist/src/data/index.d.ts +4 -0
  28. package/dist/src/data/index.js +4 -0
  29. package/dist/src/data/query.d.ts +23 -0
  30. package/dist/src/data/query.js +232 -0
  31. package/dist/src/data/query.spec.d.ts +1 -0
  32. package/dist/src/data/query.spec.js +354 -0
  33. package/dist/src/data/response.d.ts +4 -0
  34. package/dist/src/data/response.js +42 -0
  35. package/dist/src/data/response.spec.d.ts +1 -0
  36. package/dist/src/data/response.spec.js +165 -0
  37. package/dist/src/index.d.ts +7 -0
  38. package/dist/src/index.jsx +6 -0
  39. package/dist/src/lifecycle.d.ts +5 -0
  40. package/dist/src/lifecycle.js +69 -0
  41. package/dist/src/routers/HashRouter.d.ts +9 -0
  42. package/dist/src/routers/HashRouter.js +41 -0
  43. package/dist/src/routers/MemoryRouter.d.ts +24 -0
  44. package/dist/src/routers/MemoryRouter.js +57 -0
  45. package/dist/src/routers/Router.d.ts +9 -0
  46. package/dist/src/routers/Router.js +45 -0
  47. package/dist/src/routers/StaticRouter.d.ts +6 -0
  48. package/dist/src/routers/StaticRouter.js +15 -0
  49. package/dist/src/routers/components.d.ts +27 -0
  50. package/dist/src/routers/components.jsx +118 -0
  51. package/dist/src/routers/createRouter.d.ts +10 -0
  52. package/dist/src/routers/createRouter.js +41 -0
  53. package/dist/src/routers/index.d.ts +11 -0
  54. package/dist/src/routers/index.js +6 -0
  55. package/dist/src/routing.d.ts +175 -0
  56. package/dist/src/routing.js +560 -0
  57. package/dist/src/types.d.ts +200 -0
  58. package/dist/src/types.js +1 -0
  59. package/dist/src/utils.d.ts +13 -0
  60. package/dist/src/utils.js +185 -0
  61. package/dist/test/helpers.d.ts +6 -0
  62. package/dist/test/helpers.js +50 -0
  63. package/dist/types.d.ts +2 -2
  64. package/dist/utils.d.ts +1 -1
  65. package/dist/utils.js +3 -0
  66. package/package.json +3 -2
@@ -0,0 +1,41 @@
1
+ import { createSignal, onCleanup, sharedConfig } from "solid-js";
2
+ import { createRouterComponent } from "./components.jsx";
3
+ function intercept([value, setValue], get, set) {
4
+ return [get ? () => get(value()) : value, set ? (v) => setValue(set(v)) : setValue];
5
+ }
6
+ export function createRouter(config) {
7
+ let ignore = false;
8
+ const wrap = (value) => (typeof value === "string" ? { value } : value);
9
+ const signal = intercept(createSignal(wrap(config.get()), {
10
+ equals: (a, b) => a.value === b.value && a.state === b.state
11
+ }), undefined, next => {
12
+ !ignore && config.set(next);
13
+ if (sharedConfig.registry && !sharedConfig.done)
14
+ sharedConfig.done = true;
15
+ return next;
16
+ });
17
+ config.init &&
18
+ onCleanup(config.init((value = config.get()) => {
19
+ ignore = true;
20
+ signal[1](wrap(value));
21
+ ignore = false;
22
+ }));
23
+ return createRouterComponent({
24
+ signal,
25
+ create: config.create,
26
+ utils: config.utils
27
+ });
28
+ }
29
+ export function bindEvent(target, type, handler) {
30
+ target.addEventListener(type, handler);
31
+ return () => target.removeEventListener(type, handler);
32
+ }
33
+ export function scrollToHash(hash, fallbackTop) {
34
+ const el = hash && document.getElementById(hash);
35
+ if (el) {
36
+ el.scrollIntoView();
37
+ }
38
+ else if (fallbackTop) {
39
+ window.scrollTo(0, 0);
40
+ }
41
+ }
@@ -0,0 +1,11 @@
1
+ export { Route } from "./components.jsx";
2
+ export type { BaseRouterProps, RouteProps } from "./components.jsx";
3
+ export { createRouter } from "./createRouter.js";
4
+ export { Router } from "./Router.js";
5
+ export type { RouterProps } from "./Router.js";
6
+ export { HashRouter } from "./HashRouter.js";
7
+ export type { HashRouterProps } from "./HashRouter.js";
8
+ export { MemoryRouter, createMemoryHistory } from "./MemoryRouter.js";
9
+ export type { MemoryRouterProps, MemoryHistory } from "./MemoryRouter.js";
10
+ export { StaticRouter } from "./StaticRouter.js";
11
+ export type { StaticRouterProps } from "./StaticRouter.js";
@@ -0,0 +1,6 @@
1
+ export { Route } from "./components.jsx";
2
+ export { createRouter } from "./createRouter.js";
3
+ export { Router } from "./Router.js";
4
+ export { HashRouter } from "./HashRouter.js";
5
+ export { MemoryRouter, createMemoryHistory } from "./MemoryRouter.js";
6
+ export { StaticRouter } from "./StaticRouter.js";
@@ -0,0 +1,175 @@
1
+ import { JSX, Accessor } from "solid-js";
2
+ import type { BeforeLeaveEventArgs, Branch, Intent, Location, MatchFilters, NavigateOptions, Navigator, Params, RouteDescription, RouteContext, RouteDefinition, RouteMatch, RouterContext, RouterIntegration, SearchParams, SetSearchParams } from "./types.js";
3
+ /** Consider this API opaque and internal. It is likely to change in the future. */
4
+ export declare const RouterContextObj: import("solid-js").Context<RouterContext | undefined>;
5
+ export declare const RouteContextObj: import("solid-js").Context<RouteContext | undefined>;
6
+ export declare const useRouter: () => RouterContext;
7
+ export declare const useRoute: () => RouteContext;
8
+ export declare const useResolvedPath: (path: () => string) => Accessor<string | undefined>;
9
+ export declare const useHref: <T extends string | undefined>(to: () => T) => () => string | T;
10
+ /**
11
+ * Retrieves method to do navigation. The method accepts a path to navigate to and an optional object with the following options:
12
+ *
13
+ * - resolve (*boolean*, default `true`): resolve the path against the current route
14
+ * - replace (*boolean*, default `false`): replace the history entry
15
+ * - scroll (*boolean*, default `true`): scroll to top after navigation
16
+ * - state (*any*, default `undefined`): pass custom state to `location.state`
17
+ *
18
+ * **Note**: The state is serialized using the structured clone algorithm which does not support all object types.
19
+ *
20
+ * @example
21
+ * ```js
22
+ * const navigate = useNavigate();
23
+ *
24
+ * if (unauthorized) {
25
+ * navigate("/login", { replace: true });
26
+ * }
27
+ * ```
28
+ */
29
+ export declare const useNavigate: () => Navigator;
30
+ /**
31
+ * Retrieves reactive `location` object useful for getting things like `pathname`.
32
+ *
33
+ * @example
34
+ * ```js
35
+ * const location = useLocation();
36
+ *
37
+ * const pathname = createMemo(() => parsePath(location.pathname));
38
+ * ```
39
+ */
40
+ export declare const useLocation: <S = unknown>() => Location<S>;
41
+ /**
42
+ * Retrieves signal that indicates whether the route is currently in a *Transition*.
43
+ * Useful for showing stale/pending state when the route resolution is *Suspended* during concurrent rendering.
44
+ *
45
+ * @example
46
+ * ```js
47
+ * const isRouting = useIsRouting();
48
+ *
49
+ * return (
50
+ * <div classList={{ "grey-out": isRouting() }}>
51
+ * <MyAwesomeContent />
52
+ * </div>
53
+ * );
54
+ * ```
55
+ */
56
+ export declare const useIsRouting: () => () => boolean;
57
+ /**
58
+ * usePreloadRoute returns a function that can be used to preload a route manual.
59
+ * This is what happens automatically with link hovering and similar focus based behavior, but it is available here as an API.
60
+ *
61
+ * @example
62
+ * ```js
63
+ * const preload = usePreloadRoute();
64
+ *
65
+ * preload(`/users/settings`, { preloadData: true });
66
+ * ```
67
+ */
68
+ export declare const usePreloadRoute: () => (url: string | URL, options?: {
69
+ preloadData?: boolean;
70
+ }) => void;
71
+ /**
72
+ * `useMatch` takes an accessor that returns the path and creates a `Memo` that returns match information if the current path matches the provided path.
73
+ * Useful for determining if a given path matches the current route.
74
+ *
75
+ * @example
76
+ * ```js
77
+ * const match = useMatch(() => props.href);
78
+ *
79
+ * return <div classList={{ active: Boolean(match()) }} />;
80
+ * ```
81
+ */
82
+ export declare const useMatch: <S extends string>(path: () => S, matchFilters?: MatchFilters<S>) => Accessor<import("./types.js").PathMatch | undefined>;
83
+ /**
84
+ * `useCurrentMatches` returns all the matches for the current matched route.
85
+ * Useful for getting all the route information.
86
+ *
87
+ * @example
88
+ * ```js
89
+ * const matches = useCurrentMatches();
90
+ *
91
+ * const breadcrumbs = createMemo(() => matches().map(m => m.route.info.breadcrumb))
92
+ * ```
93
+ */
94
+ export declare const useCurrentMatches: () => () => RouteMatch[];
95
+ /**
96
+ * Retrieves a reactive, store-like object containing the current route path parameters as defined in the Route.
97
+ *
98
+ * @example
99
+ * ```js
100
+ * const params = useParams();
101
+ *
102
+ * // fetch user based on the id path parameter
103
+ * const [user] = createResource(() => params.id, fetchUser);
104
+ * ```
105
+ */
106
+ export declare const useParams: <T extends Params>() => T;
107
+ /**
108
+ * Retrieves a tuple containing a reactive object to read the current location's query parameters and a method to update them.
109
+ * The object is a proxy so you must access properties to subscribe to reactive updates.
110
+ * **Note** that values will be strings and property names will retain their casing.
111
+ *
112
+ * The setter method accepts an object whose entries will be merged into the current query string.
113
+ * Values `''`, `undefined` and `null` will remove the key from the resulting query string.
114
+ * Updates will behave just like a navigation and the setter accepts the same optional second parameter as `navigate` and auto-scrolling is disabled by default.
115
+ *
116
+ * @examples
117
+ * ```js
118
+ * const [searchParams, setSearchParams] = useSearchParams();
119
+ *
120
+ * return (
121
+ * <div>
122
+ * <span>Page: {searchParams.page}</span>
123
+ * <button
124
+ * onClick={() =>
125
+ * setSearchParams({ page: (parseInt(searchParams.page) || 0) + 1 })
126
+ * }
127
+ * >
128
+ * Next Page
129
+ * </button>
130
+ * </div>
131
+ * );
132
+ * ```
133
+ */
134
+ export declare const useSearchParams: <T extends SearchParams>() => [Partial<T>, (params: SetSearchParams, options?: Partial<NavigateOptions>) => void];
135
+ /**
136
+ * useBeforeLeave takes a function that will be called prior to leaving a route.
137
+ * The function will be called with:
138
+ *
139
+ * - from (*Location*): current location (before change).
140
+ * - to (*string | number*): path passed to `navigate`.
141
+ * - options (*NavigateOptions*): options passed to navigate.
142
+ * - preventDefault (*function*): call to block the route change.
143
+ * - defaultPrevented (*readonly boolean*): `true` if any previously called leave handlers called `preventDefault`.
144
+ * - retry (*function*, force?: boolean ): call to retry the same navigation, perhaps after confirming with the user. Pass `true` to skip running the leave handlers again (i.e. force navigate without confirming).
145
+ *
146
+ * @example
147
+ * ```js
148
+ * useBeforeLeave((e: BeforeLeaveEventArgs) => {
149
+ * if (form.isDirty && !e.defaultPrevented) {
150
+ * // preventDefault to block immediately and prompt user async
151
+ * e.preventDefault();
152
+ * setTimeout(() => {
153
+ * if (window.confirm("Discard unsaved changes - are you sure?")) {
154
+ * // user wants to proceed anyway so retry with force=true
155
+ * e.retry(true);
156
+ * }
157
+ * }, 100);
158
+ * }
159
+ * });
160
+ * ```
161
+ */
162
+ export declare const useBeforeLeave: (listener: (e: BeforeLeaveEventArgs) => void) => void;
163
+ export declare function createRoutes(routeDef: RouteDefinition, base?: string): RouteDescription[];
164
+ export declare function createBranch(routes: RouteDescription[], index?: number): Branch;
165
+ export declare function createBranches(routeDef: RouteDefinition | RouteDefinition[], base?: string, stack?: RouteDescription[], branches?: Branch[]): Branch[];
166
+ export declare function getRouteMatches(branches: Branch[], location: string): RouteMatch[];
167
+ export declare function getIntent(): Intent | undefined;
168
+ export declare function getInPreloadFn(): boolean;
169
+ export declare function setInPreloadFn(value: boolean): void;
170
+ export declare function createRouterContext(integration: RouterIntegration, branches: () => Branch[], getContext?: () => any, options?: {
171
+ base?: string;
172
+ singleFlight?: boolean;
173
+ transformUrl?: (url: string) => string;
174
+ }): RouterContext;
175
+ export declare function createRouteContext(router: RouterContext, parent: RouteContext, outlet: () => JSX.Element, match: () => RouteMatch): RouteContext;