@real-router/preact 0.15.2 → 0.15.4

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 (40) hide show
  1. package/dist/cjs/index.d.ts +2 -2
  2. package/dist/cjs/index.d.ts.map +1 -1
  3. package/dist/cjs/index.js +1 -1
  4. package/dist/cjs/index.js.map +1 -1
  5. package/dist/esm/index.d.mts +2 -2
  6. package/dist/esm/index.d.mts.map +1 -1
  7. package/dist/esm/index.mjs +1 -1
  8. package/dist/esm/index.mjs.map +1 -1
  9. package/package.json +5 -6
  10. package/src/RouterProvider.tsx +0 -152
  11. package/src/components/Await.tsx +0 -99
  12. package/src/components/ClientOnly.tsx +0 -25
  13. package/src/components/HttpStatusCode.tsx +0 -82
  14. package/src/components/HttpStatusProvider.tsx +0 -22
  15. package/src/components/Link.tsx +0 -141
  16. package/src/components/RouteView/RouteView.tsx +0 -57
  17. package/src/components/RouteView/components.tsx +0 -19
  18. package/src/components/RouteView/helpers.tsx +0 -174
  19. package/src/components/RouteView/index.ts +0 -8
  20. package/src/components/RouteView/types.ts +0 -24
  21. package/src/components/RouterErrorBoundary.tsx +0 -84
  22. package/src/components/ServerOnly.tsx +0 -26
  23. package/src/components/Streamed.tsx +0 -24
  24. package/src/constants.ts +0 -9
  25. package/src/context.ts +0 -27
  26. package/src/hooks/useDeferred.tsx +0 -26
  27. package/src/hooks/useIsActiveRoute.tsx +0 -46
  28. package/src/hooks/useNavigator.tsx +0 -8
  29. package/src/hooks/useRoute.tsx +0 -26
  30. package/src/hooks/useRouteEnter.tsx +0 -147
  31. package/src/hooks/useRouteExit.tsx +0 -159
  32. package/src/hooks/useRouteNode.tsx +0 -34
  33. package/src/hooks/useRouteUtils.tsx +0 -12
  34. package/src/hooks/useRouter.tsx +0 -8
  35. package/src/hooks/useRouterTransition.tsx +0 -17
  36. package/src/index.ts +0 -56
  37. package/src/ssr.ts +0 -39
  38. package/src/types.ts +0 -40
  39. package/src/useSyncExternalStore.ts +0 -60
  40. package/src/utils/createHttpStatusSink.ts +0 -27
@@ -1,159 +0,0 @@
1
- import { useEffect, useLayoutEffect, useRef } from "preact/hooks";
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. `signal.addEventListener(
43
- * "abort", ...)` does not fire retroactively, so without this guard
44
- * downstream cleanup would never trigger.
45
- * - **Same-route skip**: by default, `route.name === nextRoute.name`
46
- * short-circuits the handler — query-only navigations (sort, filter,
47
- * pagination) skip the work. Opt out with `skipSameRoute: false`.
48
- * - **Stable handler reference**: the handler can change identity on
49
- * every render without causing resubscription — internal ref keeps
50
- * the latest handler accessible to the long-lived subscription.
51
- *
52
- * Returns nothing — the subscription's lifecycle is bound to the
53
- * component's mount.
54
- *
55
- * If the handler returns a Promise, the router blocks on it. If the
56
- * Promise resolves, navigation proceeds. If it rejects, the router emits
57
- * `TRANSITION_CANCELLED` (existing core behavior, no change here).
58
- *
59
- * @example Animation
60
- * ```tsx
61
- * const ref = useRef<HTMLDivElement>(null);
62
- *
63
- * useRouteExit(async ({ signal }) => {
64
- * const el = ref.current;
65
- * if (!el) return;
66
- * el.classList.add("fade-out");
67
- * const cleanup = () => el.classList.remove("fade-out");
68
- * signal.addEventListener("abort", cleanup, { once: true });
69
- * try {
70
- * el.getBoundingClientRect(); // style flush
71
- * await Promise.allSettled(el.getAnimations().map((a) => a.finished));
72
- * } finally {
73
- * cleanup();
74
- * }
75
- * });
76
- * ```
77
- *
78
- * @example Auto-save form draft
79
- * ```tsx
80
- * useRouteExit(async ({ signal }) => {
81
- * if (formState.dirty) await api.saveDraft(formState, { signal });
82
- * });
83
- * ```
84
- *
85
- * @example Cancel inflight requests
86
- * ```tsx
87
- * useRouteExit(() => {
88
- * inflightController.abort();
89
- * });
90
- * ```
91
- *
92
- * @example Library-coordinated exit (motion / framer-motion)
93
- * ```tsx
94
- * const exitResolverRef = useRef<(() => void) | null>(null);
95
- *
96
- * useRouteExit(({ signal }) => {
97
- * return new Promise<void>((resolve) => {
98
- * exitResolverRef.current = resolve;
99
- * signal.addEventListener("abort", () => resolve(), { once: true });
100
- * });
101
- * });
102
- *
103
- * const onExitComplete = () => exitResolverRef.current?.();
104
- * // pass onExitComplete to <AnimatePresence>
105
- * ```
106
- *
107
- * @example Reading rich transition metadata via `nextRoute.transition`
108
- * ```tsx
109
- * useRouteExit(({ route, nextRoute }) => {
110
- * // nextRoute.transition: TransitionMeta — preview of the upcoming nav
111
- * if (nextRoute.transition.segments.deactivated.includes("products")) {
112
- * // leaving the products subtree entirely — flush product-related caches
113
- * productCache.clear();
114
- * }
115
- * if (nextRoute.transition.redirected) {
116
- * // skip animation when navigation arrived via redirect
117
- * return;
118
- * }
119
- * });
120
- * ```
121
- */
122
- export function useRouteExit(
123
- handler: RouteExitHandler,
124
- options?: UseRouteExitOptions,
125
- ): void {
126
- const router = useRouter();
127
- const handlerRef = useRef(handler);
128
- const skipSameRoute = options?.skipSameRoute ?? true;
129
-
130
- // Keep the latest handler accessible to the subscription without
131
- // resubscribing on every render — the subscription registers the
132
- // wrapper once and dispatches to whatever ref points to.
133
- // useLayoutEffect (synchronous, post-render, pre-paint) updates the
134
- // ref before the browser can dispatch any router events that could
135
- // observe a stale closure.
136
- useLayoutEffect(() => {
137
- handlerRef.current = handler;
138
- });
139
-
140
- useEffect(() => {
141
- return router.subscribeLeave(({ route, nextRoute, signal }) => {
142
- if (skipSameRoute && route.name === nextRoute.name) {
143
- return;
144
- }
145
-
146
- // Reentrant abort: signal is already aborted when listener fires
147
- // (e.g. a newer navigate superseded this one before subscribeLeave
148
- // even ran). addEventListener("abort", ...) does not fire
149
- // retroactively, so we skip the handler entirely — any cleanup the
150
- // handler would have wired via abort listener must not run because
151
- // the corresponding `add` work never happened.
152
- if (signal.aborted) {
153
- return;
154
- }
155
-
156
- return handlerRef.current({ route, nextRoute, signal });
157
- });
158
- }, [router, skipSameRoute]);
159
- }
@@ -1,34 +0,0 @@
1
- import { createRouteNodeSource } from "@real-router/sources";
2
- import { useMemo } from "preact/hooks";
3
-
4
- import { useSyncExternalStore } from "../useSyncExternalStore";
5
- import { useNavigator } from "./useNavigator";
6
- import { useRouter } from "./useRouter";
7
-
8
- import type { RouteContext } from "../types";
9
-
10
- export function useRouteNode(nodeName: string): RouteContext {
11
- const router = useRouter();
12
- const navigator = useNavigator();
13
-
14
- // `createRouteNodeSource` is the cached factory from `@real-router/sources`
15
- // keyed on (router, nodeName) — identical args return identical refs across
16
- // renders. No `useMemo` needed.
17
- const store = createRouteNodeSource(router, nodeName);
18
-
19
- const { route, previousRoute } = useSyncExternalStore(
20
- store.subscribe,
21
- store.getSnapshot,
22
- store.getSnapshot,
23
- );
24
-
25
- // Public stable-ref contract (locked by `useRouteNode.test.tsx` "should
26
- // return stable reference when nothing changes"): consecutive renders with
27
- // identical (navigator, route, previousRoute) return the same RouteContext
28
- // ref. Drop this `useMemo` and downstream consumers re-render on every
29
- // parent re-render.
30
- return useMemo(
31
- (): RouteContext => ({ navigator, route, previousRoute }),
32
- [navigator, route, previousRoute],
33
- );
34
- }
@@ -1,12 +0,0 @@
1
- import { getPluginApi } from "@real-router/core/api";
2
- import { getRouteUtils } from "@real-router/route-utils";
3
-
4
- import { useRouter } from "./useRouter";
5
-
6
- import type { RouteUtils } from "@real-router/route-utils";
7
-
8
- export const useRouteUtils = (): RouteUtils => {
9
- const router = useRouter();
10
-
11
- return getRouteUtils(getPluginApi(router).getTree());
12
- };
@@ -1,8 +0,0 @@
1
- import { createUseContextOrThrow, RouterContext } from "../context";
2
-
3
- import type { Router } from "@real-router/core";
4
-
5
- export const useRouter: () => Router = createUseContextOrThrow(
6
- RouterContext,
7
- "useRouter",
8
- );
@@ -1,17 +0,0 @@
1
- import { getTransitionSource } from "@real-router/sources";
2
-
3
- import { useSyncExternalStore } from "../useSyncExternalStore";
4
- import { useRouter } from "./useRouter";
5
-
6
- import type { RouterTransitionSnapshot } from "@real-router/sources";
7
-
8
- export function useRouterTransition(): RouterTransitionSnapshot {
9
- const router = useRouter();
10
- const store = getTransitionSource(router);
11
-
12
- return useSyncExternalStore(
13
- store.subscribe,
14
- store.getSnapshot,
15
- store.getSnapshot,
16
- );
17
- }
package/src/index.ts DELETED
@@ -1,56 +0,0 @@
1
- // Components
2
- export { RouteView } from "./components/RouteView";
3
-
4
- export { Link } from "./components/Link";
5
-
6
- export { RouterErrorBoundary } from "./components/RouterErrorBoundary";
7
-
8
- // Hooks
9
- export { useRouter } from "./hooks/useRouter";
10
-
11
- export { useNavigator } from "./hooks/useNavigator";
12
-
13
- export { useRouteUtils } from "./hooks/useRouteUtils";
14
-
15
- export { useRoute } from "./hooks/useRoute";
16
-
17
- export { useRouteNode } from "./hooks/useRouteNode";
18
-
19
- export { useRouterTransition } from "./hooks/useRouterTransition";
20
-
21
- export { useRouteExit } from "./hooks/useRouteExit";
22
-
23
- export { useRouteEnter } from "./hooks/useRouteEnter";
24
-
25
- // Context
26
- export { RouterProvider } from "./RouterProvider";
27
-
28
- export { RouterContext, NavigatorContext, RouteContext } from "./context";
29
-
30
- // Types
31
- export type { LinkProps } from "./types";
32
-
33
- export type { RouterErrorBoundaryProps } from "./components/RouterErrorBoundary";
34
-
35
- export type {
36
- RouteViewProps,
37
- RouteViewMatchProps,
38
- RouteViewSelfProps,
39
- RouteViewNotFoundProps,
40
- } from "./components/RouteView";
41
-
42
- export type {
43
- RouteExitContext,
44
- RouteExitHandler,
45
- UseRouteExitOptions,
46
- } from "./hooks/useRouteExit";
47
-
48
- export type {
49
- RouteEnterContext,
50
- RouteEnterHandler,
51
- UseRouteEnterOptions,
52
- } from "./hooks/useRouteEnter";
53
-
54
- export type { Navigator } from "@real-router/core";
55
-
56
- export type { RouterTransitionSnapshot } from "@real-router/sources";
package/src/ssr.ts DELETED
@@ -1,39 +0,0 @@
1
- // SSR-feature entry — Preact 10+
2
- //
3
- // Server-side and SSR-aware components/hooks. Mirror of `@real-router/react/ssr`
4
- // — same exports, same API surface. Pair with `@real-router/ssr-data-plugin`'s
5
- // `defer()` helper and `injectDeferredScripts` server-side wire-format.
6
-
7
- // Components
8
- export { ClientOnly } from "./components/ClientOnly";
9
-
10
- export { ServerOnly } from "./components/ServerOnly";
11
-
12
- export { Await } from "./components/Await";
13
-
14
- export { Streamed } from "./components/Streamed";
15
-
16
- export { HttpStatusCode } from "./components/HttpStatusCode";
17
-
18
- export { HttpStatusProvider } from "./components/HttpStatusProvider";
19
-
20
- // Hooks
21
- export { useDeferred } from "./hooks/useDeferred";
22
-
23
- // Utilities
24
- export { createHttpStatusSink } from "./utils/createHttpStatusSink";
25
-
26
- // Types
27
- export type { ClientOnlyProps } from "./components/ClientOnly";
28
-
29
- export type { ServerOnlyProps } from "./components/ServerOnly";
30
-
31
- export type { AwaitProps } from "./components/Await";
32
-
33
- export type { StreamedProps } from "./components/Streamed";
34
-
35
- export type { HttpStatusCodeProps } from "./components/HttpStatusCode";
36
-
37
- export type { HttpStatusProviderProps } from "./components/HttpStatusProvider";
38
-
39
- export type { HttpStatusSink } from "./utils/createHttpStatusSink";
package/src/types.ts DELETED
@@ -1,40 +0,0 @@
1
- import type {
2
- NavigationOptions,
3
- Params,
4
- Navigator,
5
- State,
6
- } from "@real-router/core";
7
- import type { HTMLAttributes } from "preact";
8
-
9
- export interface RouteState<P extends Params = Params> {
10
- route: State<P> | undefined;
11
- previousRoute?: State | undefined;
12
- }
13
-
14
- export type RouteContext<P extends Params = Params> = {
15
- navigator: Navigator;
16
- } & RouteState<P>;
17
-
18
- export interface LinkProps<P extends Params = Params> extends Omit<
19
- HTMLAttributes<HTMLAnchorElement>,
20
- "className"
21
- > {
22
- routeName: string;
23
- routeParams?: P;
24
- routeOptions?: NavigationOptions;
25
- className?: string;
26
- activeClassName?: string;
27
- activeStrict?: boolean;
28
- ignoreQueryParams?: boolean;
29
- /**
30
- * URL fragment (decoded form, no leading "#") (#532).
31
- * - omitted/`undefined` → preserve current fragment on same-route navigation
32
- * - `""` → clear fragment
33
- * - non-empty → set fragment
34
- *
35
- * Requires a URL plugin (browser-plugin or navigation-plugin) for full
36
- * round-trip; hash-plugin ignores the prop with a one-time dev warning.
37
- */
38
- hash?: string;
39
- target?: string;
40
- }
@@ -1,60 +0,0 @@
1
- import { useEffect, useState } from "preact/hooks";
2
-
3
- /**
4
- * Polyfill for React's useSyncExternalStore.
5
- *
6
- * Preact does not provide a native useSyncExternalStore.
7
- * This implementation uses useState + useEffect to subscribe
8
- * to external stores.
9
- *
10
- * Race condition handling: the value may change between
11
- * `useState(getSnapshot)` (render) and `useEffect` (commit).
12
- * We synchronize by calling `setValue(getSnapshot())` before
13
- * subscribing in the effect.
14
- *
15
- * The updater uses `Object.is` to bail out when the snapshot
16
- * is referentially stable, preventing redundant re-renders.
17
- *
18
- * SSR semantics: `_getServerSnapshot` is intentionally ignored.
19
- * Preact's `preact-render-to-string` runs `useState(getSnapshot)`
20
- * on the server and does not commit effects, so the initial
21
- * render already uses `getSnapshot()`. Real-Router's `createRouteSource`
22
- * (and friends) return the same value on server and client given the
23
- * same `router` instance, so passing `getSnapshot` itself as the third
24
- * argument at every call site is the symmetric SSR contract; a separate
25
- * `getServerSnapshot` would diverge during hydration. Consumers that
26
- * truly need a different server value should branch in `getSnapshot`.
27
- *
28
- * Stable-reference contract: `subscribe` and `getSnapshot` are deps of
29
- * the subscription effect. If a consumer passes inline closures, every
30
- * render triggers `unsubscribe → subscribe` plus a fresh `sync()` pass
31
- * — a silent O(N) reconnect that the Preact polyfill cannot bail out
32
- * of (React's native impl uses an internal sub-store keyed by identity;
33
- * we cannot replicate that without losing the latest-snapshot guarantee).
34
- * All Real-Router hooks pass router-keyed cached factories from
35
- * `@real-router/sources`, which produce stable refs per `(router, args…)`
36
- * — keep that pattern for every external use.
37
- */
38
- export function useSyncExternalStore<T>(
39
- subscribe: (onStoreChange: () => void) => () => void,
40
- getSnapshot: () => T,
41
- _getServerSnapshot?: () => T,
42
- ): T {
43
- const [value, setValue] = useState(getSnapshot);
44
-
45
- useEffect(() => {
46
- const sync = (): void => {
47
- setValue((prev) => {
48
- const next = getSnapshot();
49
-
50
- return Object.is(prev, next) ? prev : next;
51
- });
52
- };
53
-
54
- sync();
55
-
56
- return subscribe(sync);
57
- }, [subscribe, getSnapshot]);
58
-
59
- return value;
60
- }
@@ -1,27 +0,0 @@
1
- /**
2
- * Render-scoped HTTP status sink. Created per request on the server, passed to
3
- * `<HttpStatusProvider sink={...}>`, and read after `renderToString` (or the
4
- * Preact streaming helper) to apply the value to the HTTP response.
5
- *
6
- * Last write wins: if the rendered tree mounts more than one
7
- * `<HttpStatusCode />`, the value reflects the last component that ran during
8
- * the render pass.
9
- *
10
- * No-op on the client — `<HttpStatusCode />` reads the optional context and
11
- * skips the write when no provider is mounted, so the same component tree can
12
- * be hydrated without changing behaviour.
13
- *
14
- * Constraints:
15
- * - **Per-request only.** Don't share a sink across requests; the rendered
16
- * tree mutates `code` in place. Module-level singletons leak status
17
- * between concurrent requests.
18
- * - **Don't `Object.freeze` the sink.** The component writes to `.code`;
19
- * freezing makes the assignment throw under ESM strict mode.
20
- */
21
- export interface HttpStatusSink {
22
- code: number | undefined;
23
- }
24
-
25
- export function createHttpStatusSink(): HttpStatusSink {
26
- return { code: undefined };
27
- }