@real-router/solid 0.7.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.
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,12 +365,24 @@ 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
 
@@ -100,6 +100,170 @@ declare function useRouteNodeStore(nodeName: string): RouteState;
100
100
 
101
101
  declare function useRouterTransition(): Accessor<RouterTransitionSnapshot>;
102
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
+
103
267
  type ScrollRestorationMode = "restore" | "top" | "manual";
104
268
  interface ScrollRestorationOptions {
105
269
  mode?: ScrollRestorationMode | undefined;
@@ -111,6 +275,7 @@ interface RouteProviderProps {
111
275
  router: Router;
112
276
  announceNavigation?: boolean;
113
277
  scrollRestoration?: ScrollRestorationOptions;
278
+ viewTransitions?: boolean;
114
279
  }
115
280
  declare function RouterProvider(props: ParentProps<RouteProviderProps>): JSX.Element;
116
281
 
@@ -126,5 +291,5 @@ declare function createSignalFromSource<T>(source: RouterSource<T>): Accessor<T>
126
291
 
127
292
  declare function createStoreFromSource<T extends object>(source: RouterSource<T>): T;
128
293
 
129
- export { Link, RouteContext, RouteView, RouterContext, RouterErrorBoundary, RouterProvider, createSignalFromSource, createStoreFromSource, link, useNavigator, useRoute, useRouteNode, useRouteNodeStore, useRouteStore, useRouteUtils, useRouter, useRouterTransition };
130
- export type { LinkDirectiveOptions, LinkProps, RouteState, MatchProps as RouteViewMatchProps, NotFoundProps as RouteViewNotFoundProps, RouteViewProps, SelfProps as RouteViewSelfProps, 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 };
package/dist/cjs/index.js CHANGED
@@ -319,7 +319,7 @@ function removeAnnouncer() {
319
319
  document.querySelector(`[${ANNOUNCER_ATTR}]`)?.remove();
320
320
  }
321
321
  function resolveText(route, prefix, getCustomText, h1) {
322
- const h1Text = h1?.textContent.trim() ?? "";
322
+ const h1Text = (h1?.textContent ?? "").trim();
323
323
  const routeName = route.name.startsWith(INTERNAL_ROUTE_PREFIX) ? "" : route.name;
324
324
  const rawText = h1Text || document.title || routeName || globalThis.location.pathname;
325
325
  return `${prefix}${rawText}`;
@@ -337,14 +337,14 @@ function manageFocus(h1) {
337
337
  }
338
338
 
339
339
  const STORAGE_KEY = "real-router:scroll";
340
- const NOOP_INSTANCE = Object.freeze({
340
+ const NOOP_INSTANCE$1 = Object.freeze({
341
341
  destroy: () => {
342
342
  /* no-op */
343
343
  }
344
344
  });
345
345
  function createScrollRestoration(router, options) {
346
346
  if (typeof globalThis.window === "undefined") {
347
- return NOOP_INSTANCE;
347
+ return NOOP_INSTANCE$1;
348
348
  }
349
349
  const mode = options?.mode ?? "restore";
350
350
 
@@ -352,7 +352,7 @@ function createScrollRestoration(router, options) {
352
352
  // don't subscribe, don't register pagehide — leave the browser's native
353
353
  // auto-restore intact for the app to override if it wants to.
354
354
  if (mode === "manual") {
355
- return NOOP_INSTANCE;
355
+ return NOOP_INSTANCE$1;
356
356
  }
357
357
  const anchorEnabled = options?.anchorScrolling ?? true;
358
358
  const getContainer = options?.scrollContainer;
@@ -493,6 +493,128 @@ function canonicalReplacer(_key, val) {
493
493
  return val;
494
494
  }
495
495
 
496
+ const NOOP_INSTANCE = Object.freeze({
497
+ destroy: () => {
498
+ /* no-op */
499
+ }
500
+ });
501
+ function createViewTransitions(router) {
502
+ if (typeof document === "undefined" || typeof document.startViewTransition !== "function") {
503
+ return NOOP_INSTANCE;
504
+ }
505
+ let closeVT = null;
506
+ let currentVT = null;
507
+ // Tracks whether TRANSITION_SUCCESS fired for the current leave. Used to
508
+ // distinguish "benign cleanup abort" (router's async path aborts its own
509
+ // controller in a finally block after successful navigation) from "real
510
+ // cancellation" (concurrent navigate, guard rejection, dispose).
511
+ let successFired = false;
512
+ const resolveAndClear = () => {
513
+ closeVT?.();
514
+ closeVT = null;
515
+ };
516
+ const offLeave = router.subscribeLeave(({
517
+ signal
518
+ }) => {
519
+ // Reentrant abort: signal already aborted when we're called. Open no VT
520
+ // — router will fall through to TRANSITION_CANCELLED via isCurrentNav()
521
+ // after leave resolves. addEventListener("abort", ...) does not re-fire
522
+ // for past events, so skipping startViewTransition is the safe path.
523
+ if (signal.aborted) {
524
+ return;
525
+ }
526
+ successFired = false;
527
+ resolveAndClear();
528
+
529
+ // Return a Promise so the router awaits until the browser invokes
530
+ // updateCallback. This ensures old DOM snapshot is captured BEFORE the
531
+ // router commits the new state — giving correct exit→state→entry
532
+ // ordering (vs fire-and-forget, where URL changes before VT captures).
533
+ return new Promise(resolveLeave => {
534
+ // Capture the resolver synchronously BEFORE startViewTransition() is
535
+ // called. The browser invokes updateCallback in a later task, but
536
+ // router.subscribe (TRANSITION_SUCCESS) can fire before that. If we
537
+ // captured `resolve` inside the callback, subscribe would see closeVT
538
+ // still null and skip resolving — the deferred would hang for 4s
539
+ // until the VT API aborts with TimeoutError.
540
+ const deferred = new Promise(resolve => {
541
+ closeVT = resolve;
542
+ });
543
+ signal.addEventListener("abort", () => {
544
+ if (successFired) {
545
+ // Router's async path (#finishAsyncNavigation) aborts its own
546
+ // controller in a finally block AFTER completeTransition (and
547
+ // thus AFTER subscribe fired). This is cleanup, not
548
+ // cancellation — VT is progressing normally, do nothing.
549
+ return;
550
+ }
551
+
552
+ // Real cancellation (concurrent navigate, dispose). Resolve the
553
+ // deferred so updateCallback can complete, skip the VT so no
554
+ // stale animation leaks, and unblock the router if the abort
555
+ // fires before updateCallback was invoked.
556
+ resolveAndClear();
557
+ currentVT?.skipTransition?.();
558
+ resolveLeave();
559
+ }, {
560
+ once: true
561
+ });
562
+ try {
563
+ currentVT = document.startViewTransition(() => {
564
+ // Resolving here unblocks the router at the moment the browser
565
+ // enters updateCallback — by spec, old DOM snapshot is captured
566
+ // before this callback runs. Router now proceeds through
567
+ // activation guards and setState; the VT animation waits on
568
+ // `deferred`, which is resolved from router.subscribe after a
569
+ // task-queue tick (see NOTE on setTimeout below).
570
+ resolveLeave();
571
+ return deferred;
572
+ });
573
+ } catch {
574
+ // Defensive: spec says startViewTransition doesn't throw under
575
+ // normal conditions, but Chromium has had edge cases (detached
576
+ // document, extension interference). Clean up and unblock router.
577
+ resolveAndClear();
578
+ resolveLeave();
579
+ }
580
+ });
581
+ });
582
+ const offSuccess = router.subscribe(() => {
583
+ const resolver = closeVT;
584
+ successFired = true;
585
+ closeVT = null;
586
+ if (resolver === null) {
587
+ currentVT = null;
588
+ } else {
589
+ // CRITICAL: CANNOT use requestAnimationFrame here. When the router
590
+ // takes the async path (leave returned a Promise), subscribe fires
591
+ // AFTER the browser has already transitioned VT into the
592
+ // "update-callback-called" phase. In that phase Chromium sets
593
+ // rendering suppression to true, which ALSO blocks rAF callbacks.
594
+ // rAF would never fire → deferred never resolves → browser aborts
595
+ // vt.ready with TimeoutError after 4s (observed in Chromium).
596
+ //
597
+ // setTimeout runs on the task queue independent of the rendering
598
+ // pipeline, so it fires regardless of suppression. React's scheduler
599
+ // uses MessageChannel tasks, which are queued before our setTimeout,
600
+ // so the new DOM is committed by the time our callback runs.
601
+ setTimeout(() => {
602
+ resolver();
603
+ currentVT = null;
604
+ }, 0);
605
+ }
606
+ });
607
+ return {
608
+ destroy: () => {
609
+ offLeave();
610
+ offSuccess();
611
+ currentVT?.skipTransition?.();
612
+ currentVT = null;
613
+ resolveAndClear();
614
+ }
615
+ };
616
+ }
617
+
496
618
  function shouldNavigate(evt) {
497
619
  return evt.button === 0 && !evt.metaKey && !evt.altKey && !evt.ctrlKey && !evt.shiftKey;
498
620
  }
@@ -710,6 +832,206 @@ function useRouterTransition() {
710
832
  return createSignalFromSource(source);
711
833
  }
712
834
 
835
+ /**
836
+ * Subscribe to the router's leave-window with the universal guards baked
837
+ * in. Wraps `router.subscribeLeave` so consumers don't repeat the same
838
+ * boilerplate every time:
839
+ *
840
+ * - **Reentrant abort pre-check**: if `signal.aborted` is already `true`
841
+ * when the handler would run (rapid navigation superseded a slower
842
+ * one), the handler is skipped entirely. `signal.addEventListener(
843
+ * "abort", ...)` does not fire retroactively, so without this guard
844
+ * downstream cleanup would never trigger.
845
+ * - **Same-route skip**: by default, `route.name === nextRoute.name`
846
+ * short-circuits the handler — query-only navigations (sort, filter,
847
+ * pagination) skip the work. Opt out with `skipSameRoute: false`.
848
+ *
849
+ * Returns nothing — the subscription's lifecycle is bound to the
850
+ * component via `onCleanup`.
851
+ *
852
+ * If the handler returns a Promise, the router blocks on it. If the
853
+ * Promise resolves, navigation proceeds. If it rejects, the router emits
854
+ * `TRANSITION_CANCELLED`.
855
+ *
856
+ * **Handler reactivity (Solid)**: Solid components run **once** at mount;
857
+ * `handler` is captured in closure at the call site. If you need
858
+ * different behavior across renders, derive it from a signal inside the
859
+ * handler body — do not rely on swapping the handler reference.
860
+ *
861
+ * @example Animation
862
+ * ```tsx
863
+ * let ref: HTMLDivElement | undefined;
864
+ *
865
+ * useRouteExit(async ({ signal }) => {
866
+ * const el = ref;
867
+ * if (!el) return;
868
+ * el.classList.add("fade-out");
869
+ * const cleanup = () => el.classList.remove("fade-out");
870
+ * signal.addEventListener("abort", cleanup, { once: true });
871
+ * try {
872
+ * el.getBoundingClientRect(); // style flush
873
+ * await Promise.allSettled(el.getAnimations().map((a) => a.finished));
874
+ * } finally {
875
+ * cleanup();
876
+ * }
877
+ * });
878
+ * ```
879
+ *
880
+ * @example Auto-save form draft
881
+ * ```tsx
882
+ * useRouteExit(async ({ signal }) => {
883
+ * if (formState.dirty) await api.saveDraft(formState, { signal });
884
+ * });
885
+ * ```
886
+ *
887
+ * @example Reading rich transition metadata via `nextRoute.transition`
888
+ * ```tsx
889
+ * useRouteExit(({ route, nextRoute }) => {
890
+ * if (nextRoute.transition.segments.deactivated.includes("products")) {
891
+ * productCache.clear();
892
+ * }
893
+ * if (nextRoute.transition.redirected) {
894
+ * return;
895
+ * }
896
+ * });
897
+ * ```
898
+ */
899
+ function useRouteExit(handler, options) {
900
+ const router = useRouter();
901
+ const skipSameRoute = options?.skipSameRoute ?? true;
902
+ const off = router.subscribeLeave(({
903
+ route,
904
+ nextRoute,
905
+ signal
906
+ }) => {
907
+ if (skipSameRoute && route.name === nextRoute.name) {
908
+ return;
909
+ }
910
+
911
+ // Reentrant abort: signal is already aborted when listener fires
912
+ // (e.g. a newer navigate superseded this one before subscribeLeave
913
+ // even ran). addEventListener("abort", ...) does not fire
914
+ // retroactively, so we skip the handler entirely.
915
+ if (signal.aborted) {
916
+ return;
917
+ }
918
+ return handler({
919
+ route,
920
+ nextRoute,
921
+ signal
922
+ });
923
+ });
924
+ solidJs.onCleanup(off);
925
+ }
926
+
927
+ /**
928
+ * Fire `handler` once when the component mounts as a result of a
929
+ * navigation. Mirror of `useRouteExit` for the entry side.
930
+ *
931
+ * What this hook covers that an ad-hoc `createEffect` + `useRoute()`
932
+ * doesn't:
933
+ *
934
+ * - **Skip-initial**: handler is skipped when there is no
935
+ * `transition.from` (i.e. first-load mount). Most consumers want to
936
+ * fire side effects only on real navigations, not on hydration.
937
+ * - **Same-route skip** (default): handler is skipped when
938
+ * `route.transition.from === route.name`. Sort/filter/query-only
939
+ * navigations re-trigger the effect (because the `route` reference
940
+ * changes), but they are not "entries" in the animation / analytics
941
+ * sense — the component instance has stayed mounted throughout.
942
+ * Opt out with `skipSameRoute: false`.
943
+ * - **Mount-time `route` / `previousRoute` snapshot**: the handler
944
+ * receives the values that were live at the moment of effect
945
+ * activation, not the latest ones (which may have moved on if the
946
+ * user navigated again before the effect drained).
947
+ *
948
+ * **Handler reactivity (Solid)**: Solid components run **once** at mount;
949
+ * `handler` is captured in closure when the hook is called. If you need
950
+ * different behavior across renders, derive it from a signal inside the
951
+ * handler body — do not rely on swapping the handler reference.
952
+ *
953
+ * @example Direction-aware entry animation
954
+ * ```tsx
955
+ * useRouteEnter(({ route }) => {
956
+ * const direction = route.context.browser?.direction;
957
+ * ref?.classList.add(
958
+ * direction === "back" ? "slide-from-left" : "slide-from-right",
959
+ * );
960
+ * });
961
+ * ```
962
+ *
963
+ * @example Analytics page-enter event (skip-initial built-in)
964
+ * ```tsx
965
+ * useRouteEnter(({ route, previousRoute }) => {
966
+ * analytics.track("page_enter", {
967
+ * route: route.name,
968
+ * from: previousRoute.name,
969
+ * });
970
+ * });
971
+ * ```
972
+ *
973
+ * @example Reading rich transition metadata via `route.transition`
974
+ * ```tsx
975
+ * useRouteEnter(({ route }) => {
976
+ * if (route.transition.redirected) {
977
+ * showToast(`Redirected from ${route.transition.from}`);
978
+ * }
979
+ * if (route.transition.segments.activated.includes("products")) {
980
+ * // products subtree just became active
981
+ * }
982
+ * });
983
+ * ```
984
+ */
985
+ function useRouteEnter(handler, options) {
986
+ const routeState = useRoute();
987
+ const skipSameRoute = options?.skipSameRoute ?? true;
988
+ let lastHandledRoute = null;
989
+ solidJs.createEffect(() => {
990
+ const {
991
+ route,
992
+ previousRoute
993
+ } = routeState();
994
+
995
+ // Early-exit guards, top-down:
996
+ //
997
+ // - **Defensive**: `route` may be undefined during SSR or
998
+ // pre-start hydration. Not testable from vitest (tests start
999
+ // the router before render), so v8-ignored.
1000
+ // - **Skip-initial**: `state.transition.from` is undefined only
1001
+ // for the very first state committed by `router.start()`.
1002
+ // - **Skip-same-route**: query-only navigations have
1003
+ // `transition.from === route.name`. Opt-out via
1004
+ // `skipSameRoute: false`.
1005
+ // - **Defensive dedupe + missing `previousRoute`**: same `route`
1006
+ // ref between effect activations is unexpected on Solid (effects
1007
+ // run once per dependency change); `!previousRoute` is unreachable
1008
+ // once `transition.from` is set (the two are populated together by
1009
+ // core). Both kept for parity with React; v8-ignored.
1010
+ /* v8 ignore start */
1011
+ if (!route) {
1012
+ return;
1013
+ }
1014
+ /* v8 ignore stop */
1015
+ if (!route.transition.from) {
1016
+ return;
1017
+ }
1018
+ if (skipSameRoute && route.transition.from === route.name) {
1019
+ return;
1020
+ }
1021
+ /* v8 ignore start */
1022
+ if (lastHandledRoute === route || !previousRoute) {
1023
+ return;
1024
+ }
1025
+ /* v8 ignore stop */
1026
+
1027
+ lastHandledRoute = route;
1028
+ handler({
1029
+ route,
1030
+ previousRoute
1031
+ });
1032
+ });
1033
+ }
1034
+
713
1035
  function isRouteActive(linkRouteName, currentRouteName) {
714
1036
  return currentRouteName === linkRouteName || currentRouteName.startsWith(`${linkRouteName}.`);
715
1037
  }
@@ -732,6 +1054,15 @@ function RouterProvider(props) {
732
1054
  sr.destroy();
733
1055
  });
734
1056
  });
1057
+ solidJs.onMount(() => {
1058
+ if (!props.viewTransitions) {
1059
+ return;
1060
+ }
1061
+ const vt = createViewTransitions(props.router);
1062
+ solidJs.onCleanup(() => {
1063
+ vt.destroy();
1064
+ });
1065
+ });
735
1066
  const navigator = core.getNavigator(props.router);
736
1067
  const routeSource = sources.createRouteSource(props.router);
737
1068
  const routeSignal = createSignalFromSource(routeSource);
@@ -766,6 +1097,8 @@ exports.createStoreFromSource = createStoreFromSource;
766
1097
  exports.link = link;
767
1098
  exports.useNavigator = useNavigator;
768
1099
  exports.useRoute = useRoute;
1100
+ exports.useRouteEnter = useRouteEnter;
1101
+ exports.useRouteExit = useRouteExit;
769
1102
  exports.useRouteNode = useRouteNode;
770
1103
  exports.useRouteNodeStore = useRouteNodeStore;
771
1104
  exports.useRouteStore = useRouteStore;