@real-router/vue 0.8.0 → 0.9.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.
@@ -0,0 +1,116 @@
1
+ import { onScopeDispose } from "vue";
2
+
3
+ import { useRouter } from "./useRouter";
4
+
5
+ import type { State } from "@real-router/core";
6
+
7
+ export interface RouteExitContext {
8
+ /** The route being left. */
9
+ route: State;
10
+ /** The route being navigated to. */
11
+ nextRoute: State;
12
+ /**
13
+ * AbortSignal that fires when this navigation is superseded by a later
14
+ * one (rapid clicks). Already filtered: when the handler runs,
15
+ * `signal.aborted` is guaranteed to be `false`. Use
16
+ * `signal.addEventListener("abort", cleanup, { once: true })` for
17
+ * cleanup that must run on cancellation.
18
+ */
19
+ signal: AbortSignal;
20
+ }
21
+
22
+ export interface UseRouteExitOptions {
23
+ /**
24
+ * Skip the handler when `route.name === nextRoute.name`
25
+ * (sort/filter/query-only navigations on the same route). Default:
26
+ * `true`.
27
+ */
28
+ skipSameRoute?: boolean;
29
+ }
30
+
31
+ export type RouteExitHandler = (
32
+ context: RouteExitContext,
33
+ ) => void | Promise<void>;
34
+
35
+ /**
36
+ * Subscribe to the router's leave-window with the universal guards baked
37
+ * in. Wraps `router.subscribeLeave` so consumers don't repeat the same
38
+ * boilerplate every time:
39
+ *
40
+ * - **Reentrant abort pre-check**: if `signal.aborted` is already `true`
41
+ * when the handler would run (rapid navigation superseded a slower
42
+ * one), the handler is skipped entirely.
43
+ * - **Same-route skip**: by default, `route.name === nextRoute.name`
44
+ * short-circuits the handler — query-only navigations skip the work.
45
+ * Opt out with `skipSameRoute: false`.
46
+ *
47
+ * Cleanup is bound to the component's effect scope via `onScopeDispose`.
48
+ *
49
+ * If the handler returns a Promise, the router blocks on it. If the
50
+ * Promise resolves, navigation proceeds. If it rejects, the router emits
51
+ * `TRANSITION_CANCELLED`.
52
+ *
53
+ * **Handler reactivity (Vue):** Vue composables run **once** during
54
+ * `setup()`; `handler` is captured in closure at the call site. To vary
55
+ * behavior over time, read refs/computeds inside the handler body — do
56
+ * not rely on swapping the handler reference.
57
+ *
58
+ * @example Animation
59
+ * ```ts
60
+ * const ref = useTemplateRef<HTMLDivElement>("box");
61
+ *
62
+ * useRouteExit(async ({ signal }) => {
63
+ * const el = ref.value;
64
+ * if (!el) return;
65
+ * el.classList.add("fade-out");
66
+ * const cleanup = () => el.classList.remove("fade-out");
67
+ * signal.addEventListener("abort", cleanup, { once: true });
68
+ * try {
69
+ * el.getBoundingClientRect();
70
+ * await Promise.allSettled(el.getAnimations().map((a) => a.finished));
71
+ * } finally {
72
+ * cleanup();
73
+ * }
74
+ * });
75
+ * ```
76
+ *
77
+ * @example Auto-save form draft
78
+ * ```ts
79
+ * useRouteExit(async ({ signal }) => {
80
+ * if (formState.value.dirty) {
81
+ * await api.saveDraft(formState.value, { signal });
82
+ * }
83
+ * });
84
+ * ```
85
+ *
86
+ * @example Reading rich transition metadata via `nextRoute.transition`
87
+ * ```ts
88
+ * useRouteExit(({ route, nextRoute }) => {
89
+ * if (nextRoute.transition.segments.deactivated.includes("products")) {
90
+ * productCache.clear();
91
+ * }
92
+ * if (nextRoute.transition.redirected) return;
93
+ * });
94
+ * ```
95
+ */
96
+ export function useRouteExit(
97
+ handler: RouteExitHandler,
98
+ options?: UseRouteExitOptions,
99
+ ): void {
100
+ const router = useRouter();
101
+ const skipSameRoute = options?.skipSameRoute ?? true;
102
+
103
+ const off = router.subscribeLeave(({ route, nextRoute, signal }) => {
104
+ if (skipSameRoute && route.name === nextRoute.name) {
105
+ return;
106
+ }
107
+
108
+ if (signal.aborted) {
109
+ return;
110
+ }
111
+
112
+ return handler({ route, nextRoute, signal });
113
+ });
114
+
115
+ onScopeDispose(off);
116
+ }
package/src/index.ts CHANGED
@@ -21,6 +21,10 @@ export { useRouteNode } from "./composables/useRouteNode";
21
21
 
22
22
  export { useRouterTransition } from "./composables/useRouterTransition";
23
23
 
24
+ export { useRouteExit } from "./composables/useRouteExit";
25
+
26
+ export { useRouteEnter } from "./composables/useRouteEnter";
27
+
24
28
  // Plugin
25
29
  export { createRouterPlugin } from "./createRouterPlugin";
26
30
 
@@ -42,6 +46,18 @@ export type {
42
46
  RouteViewNotFoundProps,
43
47
  } from "./components/RouteView";
44
48
 
49
+ export type {
50
+ RouteExitContext,
51
+ RouteExitHandler,
52
+ UseRouteExitOptions,
53
+ } from "./composables/useRouteExit";
54
+
55
+ export type {
56
+ RouteEnterContext,
57
+ RouteEnterHandler,
58
+ UseRouteEnterOptions,
59
+ } from "./composables/useRouteEnter";
60
+
45
61
  export type { Navigator } from "@real-router/core";
46
62
 
47
63
  export type { RouterTransitionSnapshot } from "@real-router/sources";