@real-router/solid 0.6.0 → 0.8.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 (38) hide show
  1. package/README.md +49 -4
  2. package/dist/cjs/index.d.ts +178 -2
  3. package/dist/cjs/index.js +407 -22
  4. package/dist/esm/index.d.mts +178 -2
  5. package/dist/esm/index.mjs +406 -23
  6. package/dist/types/RouterProvider.d.ts +1 -0
  7. package/dist/types/RouterProvider.d.ts.map +1 -1
  8. package/dist/types/components/RouteView/RouteView.d.ts +3 -2
  9. package/dist/types/components/RouteView/RouteView.d.ts.map +1 -1
  10. package/dist/types/components/RouteView/components.d.ts +12 -2
  11. package/dist/types/components/RouteView/components.d.ts.map +1 -1
  12. package/dist/types/components/RouteView/helpers.d.ts.map +1 -1
  13. package/dist/types/components/RouteView/index.d.ts +1 -1
  14. package/dist/types/components/RouteView/index.d.ts.map +1 -1
  15. package/dist/types/components/RouteView/types.d.ts +6 -0
  16. package/dist/types/components/RouteView/types.d.ts.map +1 -1
  17. package/dist/types/dom-utils/direction-tracker.d.ts +27 -0
  18. package/dist/types/dom-utils/direction-tracker.d.ts.map +1 -0
  19. package/dist/types/dom-utils/index.d.ts +4 -0
  20. package/dist/types/dom-utils/index.d.ts.map +1 -1
  21. package/dist/types/dom-utils/view-transitions.d.ts +6 -0
  22. package/dist/types/dom-utils/view-transitions.d.ts.map +1 -0
  23. package/dist/types/hooks/useRouteEnter.d.ts +76 -0
  24. package/dist/types/hooks/useRouteEnter.d.ts.map +1 -0
  25. package/dist/types/hooks/useRouteExit.d.ts +90 -0
  26. package/dist/types/hooks/useRouteExit.d.ts.map +1 -0
  27. package/dist/types/index.d.ts +5 -1
  28. package/dist/types/index.d.ts.map +1 -1
  29. package/package.json +3 -3
  30. package/src/RouterProvider.tsx +18 -1
  31. package/src/components/RouteView/RouteView.tsx +7 -2
  32. package/src/components/RouteView/components.tsx +25 -2
  33. package/src/components/RouteView/helpers.tsx +67 -21
  34. package/src/components/RouteView/index.ts +1 -0
  35. package/src/components/RouteView/types.ts +7 -0
  36. package/src/hooks/useRouteEnter.tsx +129 -0
  37. package/src/hooks/useRouteExit.tsx +123 -0
  38. package/src/index.tsx +17 -0
@@ -17,6 +17,12 @@ interface MatchProps {
17
17
  readonly fallback?: JSX.Element;
18
18
  readonly children: JSX.Element;
19
19
  }
20
+ interface SelfProps {
21
+ /** Fallback content while children are suspended. */
22
+ readonly fallback?: JSX.Element;
23
+ /** Content to render when the active route name equals the parent RouteView's nodeName. */
24
+ readonly children: JSX.Element;
25
+ }
20
26
  interface NotFoundProps {
21
27
  readonly children: JSX.Element;
22
28
  }
@@ -25,6 +31,10 @@ declare function Match(props: MatchProps): JSX.Element;
25
31
  declare namespace Match {
26
32
  var displayName: string;
27
33
  }
34
+ declare function Self(props: SelfProps): JSX.Element;
35
+ declare namespace Self {
36
+ var displayName: string;
37
+ }
28
38
  declare function NotFound(props: NotFoundProps): JSX.Element;
29
39
  declare namespace NotFound {
30
40
  var displayName: string;
@@ -36,6 +46,7 @@ declare namespace RouteViewRoot {
36
46
  }
37
47
  declare const RouteView: typeof RouteViewRoot & {
38
48
  Match: typeof Match;
49
+ Self: typeof Self;
39
50
  NotFound: typeof NotFound;
40
51
  };
41
52
 
@@ -89,6 +100,170 @@ declare function useRouteNodeStore(nodeName: string): RouteState;
89
100
 
90
101
  declare function useRouterTransition(): Accessor<RouterTransitionSnapshot>;
91
102
 
103
+ interface RouteExitContext {
104
+ /** The route being left. */
105
+ route: State;
106
+ /** The route being navigated to. */
107
+ nextRoute: State;
108
+ /**
109
+ * AbortSignal that fires when this navigation is superseded by a later
110
+ * one (rapid clicks). Already filtered: when the handler runs,
111
+ * `signal.aborted` is guaranteed to be `false`. Use
112
+ * `signal.addEventListener("abort", cleanup, { once: true })` for
113
+ * cleanup that must run on cancellation.
114
+ */
115
+ signal: AbortSignal;
116
+ }
117
+ interface UseRouteExitOptions {
118
+ /**
119
+ * Skip the handler when `route.name === nextRoute.name`
120
+ * (sort/filter/query-only navigations on the same route). Default:
121
+ * `true`.
122
+ */
123
+ skipSameRoute?: boolean;
124
+ }
125
+ type RouteExitHandler = (context: RouteExitContext) => void | Promise<void>;
126
+ /**
127
+ * Subscribe to the router's leave-window with the universal guards baked
128
+ * in. Wraps `router.subscribeLeave` so consumers don't repeat the same
129
+ * boilerplate every time:
130
+ *
131
+ * - **Reentrant abort pre-check**: if `signal.aborted` is already `true`
132
+ * when the handler would run (rapid navigation superseded a slower
133
+ * one), the handler is skipped entirely. `signal.addEventListener(
134
+ * "abort", ...)` does not fire retroactively, so without this guard
135
+ * downstream cleanup would never trigger.
136
+ * - **Same-route skip**: by default, `route.name === nextRoute.name`
137
+ * short-circuits the handler — query-only navigations (sort, filter,
138
+ * pagination) skip the work. Opt out with `skipSameRoute: false`.
139
+ *
140
+ * Returns nothing — the subscription's lifecycle is bound to the
141
+ * component via `onCleanup`.
142
+ *
143
+ * If the handler returns a Promise, the router blocks on it. If the
144
+ * Promise resolves, navigation proceeds. If it rejects, the router emits
145
+ * `TRANSITION_CANCELLED`.
146
+ *
147
+ * **Handler reactivity (Solid)**: Solid components run **once** at mount;
148
+ * `handler` is captured in closure at the call site. If you need
149
+ * different behavior across renders, derive it from a signal inside the
150
+ * handler body — do not rely on swapping the handler reference.
151
+ *
152
+ * @example Animation
153
+ * ```tsx
154
+ * let ref: HTMLDivElement | undefined;
155
+ *
156
+ * useRouteExit(async ({ signal }) => {
157
+ * const el = ref;
158
+ * if (!el) return;
159
+ * el.classList.add("fade-out");
160
+ * const cleanup = () => el.classList.remove("fade-out");
161
+ * signal.addEventListener("abort", cleanup, { once: true });
162
+ * try {
163
+ * el.getBoundingClientRect(); // style flush
164
+ * await Promise.allSettled(el.getAnimations().map((a) => a.finished));
165
+ * } finally {
166
+ * cleanup();
167
+ * }
168
+ * });
169
+ * ```
170
+ *
171
+ * @example Auto-save form draft
172
+ * ```tsx
173
+ * useRouteExit(async ({ signal }) => {
174
+ * if (formState.dirty) await api.saveDraft(formState, { signal });
175
+ * });
176
+ * ```
177
+ *
178
+ * @example Reading rich transition metadata via `nextRoute.transition`
179
+ * ```tsx
180
+ * useRouteExit(({ route, nextRoute }) => {
181
+ * if (nextRoute.transition.segments.deactivated.includes("products")) {
182
+ * productCache.clear();
183
+ * }
184
+ * if (nextRoute.transition.redirected) {
185
+ * return;
186
+ * }
187
+ * });
188
+ * ```
189
+ */
190
+ declare function useRouteExit(handler: RouteExitHandler, options?: UseRouteExitOptions): void;
191
+
192
+ interface RouteEnterContext {
193
+ /** The route that was just activated. */
194
+ route: State;
195
+ /** The route that was active immediately before this navigation. */
196
+ previousRoute: State;
197
+ }
198
+ type RouteEnterHandler = (context: RouteEnterContext) => void;
199
+ interface UseRouteEnterOptions {
200
+ /**
201
+ * Skip the handler when `route.name === previousRoute.name`
202
+ * (sort/filter/query-only navigations on the same route). Default:
203
+ * `true`. Symmetric with `useRouteExit`'s same-name option.
204
+ */
205
+ skipSameRoute?: boolean;
206
+ }
207
+ /**
208
+ * Fire `handler` once when the component mounts as a result of a
209
+ * navigation. Mirror of `useRouteExit` for the entry side.
210
+ *
211
+ * What this hook covers that an ad-hoc `createEffect` + `useRoute()`
212
+ * doesn't:
213
+ *
214
+ * - **Skip-initial**: handler is skipped when there is no
215
+ * `transition.from` (i.e. first-load mount). Most consumers want to
216
+ * fire side effects only on real navigations, not on hydration.
217
+ * - **Same-route skip** (default): handler is skipped when
218
+ * `route.transition.from === route.name`. Sort/filter/query-only
219
+ * navigations re-trigger the effect (because the `route` reference
220
+ * changes), but they are not "entries" in the animation / analytics
221
+ * sense — the component instance has stayed mounted throughout.
222
+ * Opt out with `skipSameRoute: false`.
223
+ * - **Mount-time `route` / `previousRoute` snapshot**: the handler
224
+ * receives the values that were live at the moment of effect
225
+ * activation, not the latest ones (which may have moved on if the
226
+ * user navigated again before the effect drained).
227
+ *
228
+ * **Handler reactivity (Solid)**: Solid components run **once** at mount;
229
+ * `handler` is captured in closure when the hook is called. If you need
230
+ * different behavior across renders, derive it from a signal inside the
231
+ * handler body — do not rely on swapping the handler reference.
232
+ *
233
+ * @example Direction-aware entry animation
234
+ * ```tsx
235
+ * useRouteEnter(({ route }) => {
236
+ * const direction = route.context.browser?.direction;
237
+ * ref?.classList.add(
238
+ * direction === "back" ? "slide-from-left" : "slide-from-right",
239
+ * );
240
+ * });
241
+ * ```
242
+ *
243
+ * @example Analytics page-enter event (skip-initial built-in)
244
+ * ```tsx
245
+ * useRouteEnter(({ route, previousRoute }) => {
246
+ * analytics.track("page_enter", {
247
+ * route: route.name,
248
+ * from: previousRoute.name,
249
+ * });
250
+ * });
251
+ * ```
252
+ *
253
+ * @example Reading rich transition metadata via `route.transition`
254
+ * ```tsx
255
+ * useRouteEnter(({ route }) => {
256
+ * if (route.transition.redirected) {
257
+ * showToast(`Redirected from ${route.transition.from}`);
258
+ * }
259
+ * if (route.transition.segments.activated.includes("products")) {
260
+ * // products subtree just became active
261
+ * }
262
+ * });
263
+ * ```
264
+ */
265
+ declare function useRouteEnter(handler: RouteEnterHandler, options?: UseRouteEnterOptions): void;
266
+
92
267
  type ScrollRestorationMode = "restore" | "top" | "manual";
93
268
  interface ScrollRestorationOptions {
94
269
  mode?: ScrollRestorationMode | undefined;
@@ -100,6 +275,7 @@ interface RouteProviderProps {
100
275
  router: Router;
101
276
  announceNavigation?: boolean;
102
277
  scrollRestoration?: ScrollRestorationOptions;
278
+ viewTransitions?: boolean;
103
279
  }
104
280
  declare function RouterProvider(props: ParentProps<RouteProviderProps>): JSX.Element;
105
281
 
@@ -115,5 +291,5 @@ declare function createSignalFromSource<T>(source: RouterSource<T>): Accessor<T>
115
291
 
116
292
  declare function createStoreFromSource<T extends object>(source: RouterSource<T>): T;
117
293
 
118
- export { Link, RouteContext, RouteView, RouterContext, RouterErrorBoundary, RouterProvider, createSignalFromSource, createStoreFromSource, link, useNavigator, useRoute, useRouteNode, useRouteNodeStore, useRouteStore, useRouteUtils, useRouter, useRouterTransition };
119
- export type { LinkDirectiveOptions, LinkProps, RouteState, MatchProps as RouteViewMatchProps, NotFoundProps as RouteViewNotFoundProps, RouteViewProps, RouterErrorBoundaryProps };
294
+ export { Link, RouteContext, RouteView, RouterContext, RouterErrorBoundary, RouterProvider, createSignalFromSource, createStoreFromSource, link, useNavigator, useRoute, useRouteEnter, useRouteExit, useRouteNode, useRouteNodeStore, useRouteStore, useRouteUtils, useRouter, useRouterTransition };
295
+ export type { LinkDirectiveOptions, LinkProps, RouteEnterContext, RouteEnterHandler, RouteExitContext, RouteExitHandler, RouteState, MatchProps as RouteViewMatchProps, NotFoundProps as RouteViewNotFoundProps, RouteViewProps, SelfProps as RouteViewSelfProps, RouterErrorBoundaryProps, UseRouteEnterOptions, UseRouteExitOptions };