@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
package/README.md CHANGED
@@ -68,6 +68,8 @@ All hooks that subscribe to route state return `Accessor<T>` — call the access
68
68
  | `useRouterTransition()` | `Accessor<RouterTransitionSnapshot>` | On transition start/end |
69
69
  | `useRouteStore()` | `RouteState` (store) | Granular — per-property |
70
70
  | `useRouteNodeStore(name)` | `RouteState` (store) | Granular — per-property, node-scoped |
71
+ | `useRouteExit(handler, options?)` | `void` — wraps `subscribeLeave` with abort + same-route guards | Never (handler captured at hook call) |
72
+ | `useRouteEnter(handler, options?)` | `void` — fires once on nav-driven mount via `useRoute()` + `transition.from` | Never (handler captured at hook call) |
71
73
 
72
74
  ### Store-Based Hooks (Granular Reactivity)
73
75
 
@@ -137,8 +139,39 @@ function GlobalProgress() {
137
139
  </Show>
138
140
  );
139
141
  }
142
+
143
+ // useRouteExit — exit animations, draft autosave, AbortSignal-aware cleanup
144
+ function FadeOut() {
145
+ let ref: HTMLDivElement | undefined;
146
+ useRouteExit(async ({ signal }) => {
147
+ if (!ref) return;
148
+ ref.classList.add("fade-out");
149
+ const cleanup = () => ref!.classList.remove("fade-out");
150
+ signal.addEventListener("abort", cleanup, { once: true });
151
+ ref.getBoundingClientRect(); // style flush
152
+ await Promise.allSettled(ref.getAnimations().map((a) => a.finished));
153
+ cleanup();
154
+ });
155
+ return <div ref={ref}>...</div>;
156
+ }
157
+
158
+ // useRouteEnter — page-enter analytics, focus management, entry animations
159
+ function PageEnterAnalytics() {
160
+ useRouteEnter(({ route, previousRoute }) => {
161
+ analytics.track("page_enter", {
162
+ route: route.name,
163
+ from: previousRoute.name,
164
+ });
165
+ });
166
+ return null;
167
+ }
140
168
  ```
141
169
 
170
+ > **Solid handler-reactivity:** components run once, so `handler` is captured at
171
+ > hook-call time. To vary behavior over time, read signals **inside** the
172
+ > handler body. See [CLAUDE.md](./CLAUDE.md) → "useRouteExit / useRouteEnter
173
+ > Handler Is Captured At Init".
174
+
142
175
  ## Components
143
176
 
144
177
  ### `<Link>`
@@ -332,18 +365,30 @@ Opt-in preservation of scroll position across navigations:
332
365
 
333
366
  Restores scroll on back/forward, scrolls to top (or `#hash`) on push. Three modes: `"restore"` (default), `"top"`, `"manual"`. Custom containers via `scrollContainer: () => HTMLElement | null`. Options are read once on mount — changing the prop at runtime does not reconfigure the utility (Solid `onMount` is non-reactive). See [Scroll Restoration guide](https://github.com/greydragon888/real-router/wiki/Scroll-Restoration) for details.
334
367
 
368
+ ## View Transitions
369
+
370
+ Opt-in animated route transitions via the browser's [View Transitions API](https://developer.mozilla.org/en-US/docs/Web/API/View_Transitions_API):
371
+
372
+ ```tsx
373
+ <RouterProvider router={router} viewTransitions>
374
+ {/* Your app */}
375
+ </RouterProvider>
376
+ ```
377
+
378
+ No-op on unsupported browsers (Firefox as of 2026-04, SSR). Prop is read once on mount (Solid `onMount` is non-reactive) — if you need toggle, unmount/remount the provider. Customization is pure CSS via `::view-transition-*` pseudo-elements and `view-transition-name` for hero morphs. See [View Transitions guide](https://github.com/greydragon888/real-router/wiki/View-Transitions) for patterns.
379
+
335
380
  ## Documentation
336
381
 
337
382
  Full documentation: [Wiki](https://github.com/greydragon888/real-router/wiki)
338
383
 
339
- - [RouterProvider](https://github.com/greydragon888/real-router/wiki/RouterProvider) · [RouteView](https://github.com/greydragon888/real-router/wiki/RouteView) · [RouterErrorBoundary](https://github.com/greydragon888/real-router/wiki/RouterErrorBoundary) · [Link](https://github.com/greydragon888/real-router/wiki/Link) · [Scroll Restoration](https://github.com/greydragon888/real-router/wiki/Scroll-Restoration)
340
- - [useRouter](https://github.com/greydragon888/real-router/wiki/useRouter) · [useRoute](https://github.com/greydragon888/real-router/wiki/useRoute) · [useRouteNode](https://github.com/greydragon888/real-router/wiki/useRouteNode) · [useNavigator](https://github.com/greydragon888/real-router/wiki/useNavigator) · [useRouteUtils](https://github.com/greydragon888/real-router/wiki/useRouteUtils) · [useRouterTransition](https://github.com/greydragon888/real-router/wiki/useRouterTransition)
384
+ - [RouterProvider](https://github.com/greydragon888/real-router/wiki/RouterProvider) · [RouteView](https://github.com/greydragon888/real-router/wiki/RouteView) · [RouterErrorBoundary](https://github.com/greydragon888/real-router/wiki/RouterErrorBoundary) · [Link](https://github.com/greydragon888/real-router/wiki/Link) · [Scroll Restoration](https://github.com/greydragon888/real-router/wiki/Scroll-Restoration) · [View Transitions](https://github.com/greydragon888/real-router/wiki/View-Transitions)
385
+ - [useRouter](https://github.com/greydragon888/real-router/wiki/useRouter) · [useRoute](https://github.com/greydragon888/real-router/wiki/useRoute) · [useRouteNode](https://github.com/greydragon888/real-router/wiki/useRouteNode) · [useNavigator](https://github.com/greydragon888/real-router/wiki/useNavigator) · [useRouteUtils](https://github.com/greydragon888/real-router/wiki/useRouteUtils) · [useRouterTransition](https://github.com/greydragon888/real-router/wiki/useRouterTransition) · [useRouteExit](https://github.com/greydragon888/real-router/wiki/useRouteExit) · [useRouteEnter](https://github.com/greydragon888/real-router/wiki/useRouteEnter)
341
386
 
342
387
  ## Examples
343
388
 
344
- 14 runnable examples — each is a standalone Vite app. Run: `cd examples/solid/basic && pnpm dev`
389
+ 14 runnable examples — each is a standalone Vite app. Run: `cd examples/web/solid/basic && pnpm dev`
345
390
 
346
- [basic](../../examples/solid/basic) · [nested-routes](../../examples/solid/nested-routes) · [auth-guards](../../examples/solid/auth-guards) · [data-loading](../../examples/solid/data-loading) · [lazy-loading](../../examples/solid/lazy-loading) · [async-guards](../../examples/solid/async-guards) · [hash-routing](../../examples/solid/hash-routing) · [persistent-params](../../examples/solid/persistent-params) · [error-handling](../../examples/solid/error-handling) · [dynamic-routes](../../examples/solid/dynamic-routes) · [store-based-state](../../examples/solid/store-based-state) · [use-link-directive](../../examples/solid/use-link-directive) · [signal-primitives](../../examples/solid/signal-primitives) · [combined](../../examples/solid/combined)
391
+ [basic](../../examples/web/solid/basic) · [nested-routes](../../examples/web/solid/nested-routes) · [auth-guards](../../examples/web/solid/auth-guards) · [data-loading](../../examples/web/solid/data-loading) · [lazy-loading](../../examples/web/solid/lazy-loading) · [async-guards](../../examples/web/solid/async-guards) · [hash-routing](../../examples/web/solid/hash-routing) · [persistent-params](../../examples/web/solid/persistent-params) · [error-handling](../../examples/web/solid/error-handling) · [dynamic-routes](../../examples/web/solid/dynamic-routes) · [store-based-state](../../examples/web/solid/store-based-state) · [use-link-directive](../../examples/web/solid/use-link-directive) · [signal-primitives](../../examples/web/solid/signal-primitives) · [combined](../../examples/web/solid/combined)
347
392
 
348
393
  ## Related Packages
349
394
 
@@ -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 };