@real-router/preact 0.2.8 → 0.2.10

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 @@
1
+ {"version":3,"file":"index.d.ts","names":[],"sources":["../../src/components/RouteView/types.ts","../../src/components/RouteView/components.tsx","../../src/components/RouteView/RouteView.tsx","../../src/types.ts","../../src/components/Link.tsx","../../src/components/RouterErrorBoundary.tsx","../../src/hooks/useRouter.tsx","../../src/hooks/useNavigator.tsx","../../src/hooks/useRouteUtils.tsx","../../src/hooks/useRoute.tsx","../../src/hooks/useRouteNode.tsx","../../src/hooks/useRouterTransition.tsx","../../src/RouterProvider.tsx","../../src/context.ts"],"mappings":";;;;;;;UAEiB,cAAA;EAAA,SACN,QAAA;EAAA,SACA,QAAA,EAAU,iBAAA;AAAA;AAAA,UAGJ,UAAA;EAAA,SACN,OAAA;EAAA,SACA,KAAA;EAAA,SACA,QAAA,GAAW,iBAAA;EAAA,SACX,QAAA,EAAU,iBAAA;AAAA;AAAA,UAGJ,aAAA;EAAA,SACN,QAAA,EAAU,iBAAA;AAAA;;;iBCbL,KAAA,CAAM,MAAA,EAAQ,UAAA;AAAA,kBAAd,KAAA;EAAA,IAAK,WAAA;AAAA;AAAA,iBAML,QAAA,CAAS,MAAA,EAAQ,aAAA;AAAA,kBAAjB,QAAA;EAAA,IAAQ,WAAA;AAAA;;;iBCDf,aAAA,CAAA;EACP,QAAA;EACA;AAAA,GACC,QAAA,CAAS,cAAA,IAAkB,KAAA;AAAA,kBAHrB,aAAA;EAAA,IAAa,WAAA;AAAA;AAAA,cAyBT,SAAA,SAAS,aAAA;;;;;;UCxBL,UAAA,WAAqB,MAAA,GAAS,MAAA;EAC7C,KAAA,EAAO,KAAA,CAAM,CAAA;EACb,aAAA,GAAgB,KAAA;AAAA;AAAA,KAGN,cAAA;EACV,SAAA,EAAW,WAAA;AAAA,IACT,UAAA;AAAA,UAEa,SAAA,WAAoB,MAAA,GAAS,MAAA,UAAgB,IAAA,CAC5D,GAAA,CAAI,cAAA,CAAe,iBAAA;EAGnB,SAAA;EACA,WAAA,GAAc,CAAA;EACd,YAAA,GAAe,iBAAA;EACf,SAAA;EACA,eAAA;EACA,YAAA;EACA,iBAAA;EACA,MAAA;AAAA;;;cCGW,IAAA,EAAM,iBAAA,CAAkB,SAAA;;;UCvBpB,wBAAA;EAAA,SACN,QAAA,EAAU,iBAAA;EAAA,SACV,QAAA,GACP,KAAA,EAAO,WAAA,EACP,UAAA,iBACG,iBAAA;EAAA,SACI,OAAA,IACP,KAAA,EAAO,WAAA,EACP,OAAA,EAAS,KAAA,SACT,SAAA,EAAW,KAAA;AAAA;AAAA,iBAIC,mBAAA,CAAA;EACd,QAAA;EACA,QAAA;EACA;AAAA,GACC,wBAAA,GAA2B,KAAA;;;cCnBjB,SAAA,QAAgB,MAAA;;;cCAhB,YAAA,QAAmB,WAAA;;;cCCnB,aAAA,QAAoB,UAAA;;;cCDpB,QAAA,QAAe,cAAA;;;iBCGZ,YAAA,CAAa,QAAA,WAAmB,cAAA;;;iBCDhC,mBAAA,CAAA,GAAuB,0BAAA;;;UCGtB,kBAAA;EACf,MAAA,EAAQ,MAAA;EACR,QAAA,EAAU,iBAAA;EACV,kBAAA;AAAA;AAAA,cAGW,cAAA,EAAgB,iBAAA,CAAkB,kBAAA;;;cCZlC,YAAA,EAAY,QAAA,CAAA,OAAA,CAAA,cAAA;AAAA,cAEZ,aAAA,EAAa,QAAA,CAAA,OAAA,CAAA,MAAA;AAAA,cAEb,gBAAA,EAAgB,QAAA,CAAA,OAAA,CAAA,WAAA"}
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.mts","names":[],"sources":["../../src/components/RouteView/types.ts","../../src/components/RouteView/components.tsx","../../src/components/RouteView/RouteView.tsx","../../src/types.ts","../../src/components/Link.tsx","../../src/components/RouterErrorBoundary.tsx","../../src/hooks/useRouter.tsx","../../src/hooks/useNavigator.tsx","../../src/hooks/useRouteUtils.tsx","../../src/hooks/useRoute.tsx","../../src/hooks/useRouteNode.tsx","../../src/hooks/useRouterTransition.tsx","../../src/RouterProvider.tsx","../../src/context.ts"],"mappings":";;;;;;;UAEiB,cAAA;EAAA,SACN,QAAA;EAAA,SACA,QAAA,EAAU,iBAAA;AAAA;AAAA,UAGJ,UAAA;EAAA,SACN,OAAA;EAAA,SACA,KAAA;EAAA,SACA,QAAA,GAAW,iBAAA;EAAA,SACX,QAAA,EAAU,iBAAA;AAAA;AAAA,UAGJ,aAAA;EAAA,SACN,QAAA,EAAU,iBAAA;AAAA;;;iBCbL,KAAA,CAAM,MAAA,EAAQ,UAAA;AAAA,kBAAd,KAAA;EAAA,IAAK,WAAA;AAAA;AAAA,iBAML,QAAA,CAAS,MAAA,EAAQ,aAAA;AAAA,kBAAjB,QAAA;EAAA,IAAQ,WAAA;AAAA;;;iBCDf,aAAA,CAAA;EACP,QAAA;EACA;AAAA,GACC,QAAA,CAAS,cAAA,IAAkB,KAAA;AAAA,kBAHrB,aAAA;EAAA,IAAa,WAAA;AAAA;AAAA,cAyBT,SAAA,SAAS,aAAA;;;;;;UCxBL,UAAA,WAAqB,MAAA,GAAS,MAAA;EAC7C,KAAA,EAAO,KAAA,CAAM,CAAA;EACb,aAAA,GAAgB,KAAA;AAAA;AAAA,KAGN,cAAA;EACV,SAAA,EAAW,WAAA;AAAA,IACT,UAAA;AAAA,UAEa,SAAA,WAAoB,MAAA,GAAS,MAAA,UAAgB,IAAA,CAC5D,GAAA,CAAI,cAAA,CAAe,iBAAA;EAGnB,SAAA;EACA,WAAA,GAAc,CAAA;EACd,YAAA,GAAe,iBAAA;EACf,SAAA;EACA,eAAA;EACA,YAAA;EACA,iBAAA;EACA,MAAA;AAAA;;;cCGW,IAAA,EAAM,iBAAA,CAAkB,SAAA;;;UCvBpB,wBAAA;EAAA,SACN,QAAA,EAAU,iBAAA;EAAA,SACV,QAAA,GACP,KAAA,EAAO,WAAA,EACP,UAAA,iBACG,iBAAA;EAAA,SACI,OAAA,IACP,KAAA,EAAO,WAAA,EACP,OAAA,EAAS,KAAA,SACT,SAAA,EAAW,KAAA;AAAA;AAAA,iBAIC,mBAAA,CAAA;EACd,QAAA;EACA,QAAA;EACA;AAAA,GACC,wBAAA,GAA2B,KAAA;;;cCnBjB,SAAA,QAAgB,MAAA;;;cCAhB,YAAA,QAAmB,WAAA;;;cCCnB,aAAA,QAAoB,UAAA;;;cCDpB,QAAA,QAAe,cAAA;;;iBCGZ,YAAA,CAAa,QAAA,WAAmB,cAAA;;;iBCDhC,mBAAA,CAAA,GAAuB,0BAAA;;;UCGtB,kBAAA;EACf,MAAA,EAAQ,MAAA;EACR,QAAA,EAAU,iBAAA;EACV,kBAAA;AAAA;AAAA,cAGW,cAAA,EAAgB,iBAAA,CAAkB,kBAAA;;;cCZlC,YAAA,EAAY,QAAA,CAAA,OAAA,CAAA,cAAA;AAAA,cAEZ,aAAA,EAAa,QAAA,CAAA,OAAA,CAAA,MAAA;AAAA,cAEb,gBAAA,EAAgB,QAAA,CAAA,OAAA,CAAA,WAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@real-router/preact",
3
- "version": "0.2.8",
3
+ "version": "0.2.10",
4
4
  "type": "commonjs",
5
5
  "description": "Preact integration for Real-Router",
6
6
  "main": "./dist/cjs/index.js",
@@ -8,7 +8,6 @@
8
8
  "types": "./dist/esm/index.d.mts",
9
9
  "exports": {
10
10
  ".": {
11
- "development": "./src/index.ts",
12
11
  "types": {
13
12
  "import": "./dist/esm/index.d.mts",
14
13
  "require": "./dist/cjs/index.d.ts"
@@ -18,7 +17,8 @@
18
17
  }
19
18
  },
20
19
  "files": [
21
- "dist"
20
+ "dist",
21
+ "src"
22
22
  ],
23
23
  "homepage": "https://github.com/greydragon888/real-router",
24
24
  "repository": {
@@ -47,9 +47,9 @@
47
47
  "license": "MIT",
48
48
  "sideEffects": false,
49
49
  "dependencies": {
50
- "@real-router/core": "^0.45.1",
51
- "@real-router/route-utils": "^0.1.12",
52
- "@real-router/sources": "^0.4.2"
50
+ "@real-router/core": "^0.46.0",
51
+ "@real-router/route-utils": "^0.1.14",
52
+ "@real-router/sources": "^0.4.4"
53
53
  },
54
54
  "devDependencies": {
55
55
  "@testing-library/dom": "10.4.1",
@@ -57,8 +57,8 @@
57
57
  "@testing-library/preact": "3.2.4",
58
58
  "@testing-library/user-event": "14.6.1",
59
59
  "preact": "10.25.4",
60
- "@real-router/browser-plugin": "^0.11.5",
61
- "dom-utils": "^0.2.7"
60
+ "@real-router/browser-plugin": "^0.11.7",
61
+ "dom-utils": "^0.2.8"
62
62
  },
63
63
  "peerDependencies": {
64
64
  "preact": ">=10.0.0"
@@ -66,10 +66,11 @@
66
66
  "scripts": {
67
67
  "build": "tsdown --config-loader unrun",
68
68
  "test": "vitest",
69
+ "test:properties": "vitest run --config vitest.config.properties.mts",
69
70
  "test:stress": "vitest run --config vitest.config.stress.mts",
70
71
  "type-check": "tsc --noEmit",
71
72
  "lint": "eslint --cache --ext .ts,.tsx src/ tests/ --fix --max-warnings 0",
72
- "lint:package": "bash ../../scripts/publint-filter.sh",
73
+ "lint:package": "publint",
73
74
  "lint:types": "attw --pack .",
74
75
  "build:dist-only": "tsdown --config-loader unrun"
75
76
  }
@@ -0,0 +1,59 @@
1
+ import { getNavigator } from "@real-router/core";
2
+ import { createRouteSource } from "@real-router/sources";
3
+ import { createRouteAnnouncer } from "dom-utils";
4
+ import { useEffect, useMemo } from "preact/hooks";
5
+
6
+ import { NavigatorContext, RouteContext, RouterContext } from "./context";
7
+ import { useSyncExternalStore } from "./useSyncExternalStore";
8
+
9
+ import type { Router } from "@real-router/core";
10
+ import type { FunctionComponent, ComponentChildren } from "preact";
11
+
12
+ export interface RouteProviderProps {
13
+ router: Router;
14
+ children: ComponentChildren;
15
+ announceNavigation?: boolean;
16
+ }
17
+
18
+ export const RouterProvider: FunctionComponent<RouteProviderProps> = ({
19
+ router,
20
+ children,
21
+ announceNavigation,
22
+ }) => {
23
+ useEffect(() => {
24
+ if (!announceNavigation) {
25
+ return;
26
+ }
27
+
28
+ const announcer = createRouteAnnouncer(router);
29
+
30
+ return () => {
31
+ announcer.destroy();
32
+ };
33
+ }, [announceNavigation, router]);
34
+ const navigator = useMemo(() => getNavigator(router), [router]);
35
+
36
+ // useSyncExternalStore manages the router subscription lifecycle:
37
+ // subscribe connects to router on first listener, unsubscribes on last.
38
+ const store = useMemo(() => createRouteSource(router), [router]);
39
+ const { route, previousRoute } = useSyncExternalStore(
40
+ store.subscribe,
41
+ store.getSnapshot,
42
+ store.getSnapshot, // SSR: router returns same state on server and client
43
+ );
44
+
45
+ const routeContextValue = useMemo(
46
+ () => ({ navigator, route, previousRoute }),
47
+ [navigator, route, previousRoute],
48
+ );
49
+
50
+ return (
51
+ <RouterContext.Provider value={router}>
52
+ <NavigatorContext.Provider value={navigator}>
53
+ <RouteContext.Provider value={routeContextValue}>
54
+ {children}
55
+ </RouteContext.Provider>
56
+ </NavigatorContext.Provider>
57
+ </RouterContext.Provider>
58
+ );
59
+ };
@@ -0,0 +1,102 @@
1
+ import { shouldNavigate, buildHref, buildActiveClassName } from "dom-utils";
2
+ import { memo } from "preact/compat";
3
+ import { useCallback, useMemo } from "preact/hooks";
4
+
5
+ import { EMPTY_PARAMS, EMPTY_OPTIONS } from "../constants";
6
+ import { useIsActiveRoute } from "../hooks/useIsActiveRoute";
7
+ import { useRouter } from "../hooks/useRouter";
8
+ import { useStableValue } from "../hooks/useStableValue";
9
+
10
+ import type { LinkProps } from "../types";
11
+ import type { FunctionComponent, JSX } from "preact";
12
+
13
+ function areLinkPropsEqual(
14
+ prev: Readonly<LinkProps>,
15
+ next: Readonly<LinkProps>,
16
+ ): boolean {
17
+ return (
18
+ prev.routeName === next.routeName &&
19
+ prev.className === next.className &&
20
+ prev.activeClassName === next.activeClassName &&
21
+ prev.activeStrict === next.activeStrict &&
22
+ prev.ignoreQueryParams === next.ignoreQueryParams &&
23
+ prev.onClick === next.onClick &&
24
+ prev.target === next.target &&
25
+ prev.style === next.style &&
26
+ prev.children === next.children &&
27
+ JSON.stringify(prev.routeParams) === JSON.stringify(next.routeParams) &&
28
+ JSON.stringify(prev.routeOptions) === JSON.stringify(next.routeOptions)
29
+ );
30
+ }
31
+
32
+ export const Link: FunctionComponent<LinkProps> = memo(
33
+ ({
34
+ routeName,
35
+ routeParams = EMPTY_PARAMS,
36
+ routeOptions = EMPTY_OPTIONS,
37
+ className,
38
+ activeClassName = "active",
39
+ activeStrict = false,
40
+ ignoreQueryParams = true,
41
+ onClick,
42
+ target,
43
+ children,
44
+ ...props
45
+ }) => {
46
+ const router = useRouter();
47
+
48
+ const stableParams = useStableValue(routeParams);
49
+ const stableOptions = useStableValue(routeOptions);
50
+
51
+ const isActive = useIsActiveRoute(
52
+ routeName,
53
+ stableParams,
54
+ activeStrict,
55
+ ignoreQueryParams,
56
+ );
57
+
58
+ const href = useMemo(
59
+ () => buildHref(router, routeName, stableParams),
60
+ [router, routeName, stableParams],
61
+ );
62
+
63
+ const handleClick = useCallback(
64
+ (evt: JSX.TargetedMouseEvent<HTMLAnchorElement>) => {
65
+ if (onClick) {
66
+ onClick(evt);
67
+
68
+ if (evt.defaultPrevented) {
69
+ return;
70
+ }
71
+ }
72
+
73
+ if (!shouldNavigate(evt) || target === "_blank") {
74
+ return;
75
+ }
76
+
77
+ evt.preventDefault();
78
+ router.navigate(routeName, stableParams, stableOptions).catch(() => {});
79
+ },
80
+ [onClick, target, router, routeName, stableParams, stableOptions],
81
+ );
82
+
83
+ const finalClassName = useMemo(
84
+ () => buildActiveClassName(isActive, activeClassName, className),
85
+ [isActive, activeClassName, className],
86
+ );
87
+
88
+ return (
89
+ <a
90
+ {...props}
91
+ href={href}
92
+ className={finalClassName}
93
+ onClick={handleClick}
94
+ >
95
+ {children}
96
+ </a>
97
+ );
98
+ },
99
+ areLinkPropsEqual,
100
+ );
101
+
102
+ Link.displayName = "Link";
@@ -0,0 +1,39 @@
1
+ import { Match, NotFound } from "./components";
2
+ import { buildRenderList, collectElements } from "./helpers";
3
+ import { useRouteNode } from "../../hooks/useRouteNode";
4
+
5
+ import type { RouteViewProps } from "./types";
6
+ import type { VNode } from "preact";
7
+
8
+ function RouteViewRoot({
9
+ nodeName,
10
+ children,
11
+ }: Readonly<RouteViewProps>): VNode | null {
12
+ const { route } = useRouteNode(nodeName);
13
+
14
+ if (!route) {
15
+ return null;
16
+ }
17
+
18
+ const elements: VNode[] = [];
19
+
20
+ collectElements(children, elements);
21
+
22
+ const { rendered } = buildRenderList(elements, route.name, nodeName);
23
+
24
+ if (rendered.length > 0) {
25
+ return <>{rendered}</>;
26
+ }
27
+
28
+ return null;
29
+ }
30
+
31
+ RouteViewRoot.displayName = "RouteView";
32
+
33
+ export const RouteView = Object.assign(RouteViewRoot, { Match, NotFound });
34
+
35
+ export type {
36
+ RouteViewProps,
37
+ MatchProps as RouteViewMatchProps,
38
+ NotFoundProps as RouteViewNotFoundProps,
39
+ } from "./types";
@@ -0,0 +1,13 @@
1
+ import type { MatchProps, NotFoundProps } from "./types";
2
+
3
+ export function Match(_props: MatchProps): null {
4
+ return null;
5
+ }
6
+
7
+ Match.displayName = "RouteView.Match";
8
+
9
+ export function NotFound(_props: NotFoundProps): null {
10
+ return null;
11
+ }
12
+
13
+ NotFound.displayName = "RouteView.NotFound";
@@ -0,0 +1,88 @@
1
+ import { UNKNOWN_ROUTE } from "@real-router/core";
2
+ import { startsWithSegment } from "@real-router/route-utils";
3
+ import { Fragment, isValidElement, toChildArray } from "preact";
4
+ import { Suspense } from "preact/compat";
5
+
6
+ import { Match, NotFound } from "./components";
7
+
8
+ import type { MatchProps, NotFoundProps } from "./types";
9
+ import type { VNode, ComponentChildren } from "preact";
10
+
11
+ function isSegmentMatch(
12
+ routeName: string,
13
+ fullSegmentName: string,
14
+ exact: boolean,
15
+ ): boolean {
16
+ if (exact) {
17
+ return routeName === fullSegmentName;
18
+ }
19
+
20
+ return startsWithSegment(routeName, fullSegmentName);
21
+ }
22
+
23
+ export function collectElements(
24
+ children: ComponentChildren,
25
+ result: VNode[],
26
+ ): void {
27
+ for (const child of toChildArray(children)) {
28
+ if (!isValidElement(child)) {
29
+ continue;
30
+ }
31
+
32
+ if (child.type === Match || child.type === NotFound) {
33
+ result.push(child);
34
+ } else {
35
+ collectElements(
36
+ (child.props as { readonly children: ComponentChildren }).children,
37
+ result,
38
+ );
39
+ }
40
+ }
41
+ }
42
+
43
+ export function buildRenderList(
44
+ elements: VNode[],
45
+ routeName: string,
46
+ nodeName: string,
47
+ ): { rendered: VNode[]; activeMatchFound: boolean } {
48
+ let notFoundChildren: ComponentChildren = null;
49
+ let activeMatchFound = false;
50
+ const rendered: VNode[] = [];
51
+
52
+ for (const child of elements) {
53
+ if (child.type === NotFound) {
54
+ notFoundChildren = (child.props as NotFoundProps).children;
55
+ continue;
56
+ }
57
+
58
+ const { segment, exact = false, fallback } = child.props as MatchProps;
59
+ const fullSegmentName = nodeName ? `${nodeName}.${segment}` : segment;
60
+ const isActive =
61
+ !activeMatchFound && isSegmentMatch(routeName, fullSegmentName, exact);
62
+
63
+ if (isActive) {
64
+ activeMatchFound = true;
65
+ const matchChildren = (child.props as MatchProps).children;
66
+ const content =
67
+ fallback === undefined ? (
68
+ matchChildren
69
+ ) : (
70
+ <Suspense fallback={fallback}>{matchChildren}</Suspense>
71
+ );
72
+
73
+ rendered.push(<Fragment key={fullSegmentName}>{content}</Fragment>);
74
+ }
75
+ }
76
+
77
+ if (
78
+ !activeMatchFound &&
79
+ routeName === UNKNOWN_ROUTE &&
80
+ notFoundChildren !== null
81
+ ) {
82
+ rendered.push(
83
+ <Fragment key="__route-view-not-found__">{notFoundChildren}</Fragment>,
84
+ );
85
+ }
86
+
87
+ return { rendered, activeMatchFound };
88
+ }
@@ -0,0 +1,7 @@
1
+ export { RouteView } from "./RouteView";
2
+
3
+ export type {
4
+ RouteViewProps,
5
+ RouteViewMatchProps,
6
+ RouteViewNotFoundProps,
7
+ } from "./RouteView";
@@ -0,0 +1,17 @@
1
+ import type { ComponentChildren } from "preact";
2
+
3
+ export interface RouteViewProps {
4
+ readonly nodeName: string;
5
+ readonly children: ComponentChildren;
6
+ }
7
+
8
+ export interface MatchProps {
9
+ readonly segment: string;
10
+ readonly exact?: boolean;
11
+ readonly fallback?: ComponentChildren;
12
+ readonly children: ComponentChildren;
13
+ }
14
+
15
+ export interface NotFoundProps {
16
+ readonly children: ComponentChildren;
17
+ }
@@ -0,0 +1,59 @@
1
+ import { Fragment } from "preact";
2
+ import { useCallback, useEffect, useRef, useState } from "preact/hooks";
3
+
4
+ import { useRouterError } from "../hooks/useRouterError";
5
+
6
+ import type { RouterError, State } from "@real-router/core";
7
+ import type { ComponentChildren, VNode } from "preact";
8
+
9
+ export interface RouterErrorBoundaryProps {
10
+ readonly children: ComponentChildren;
11
+ readonly fallback: (
12
+ error: RouterError,
13
+ resetError: () => void,
14
+ ) => ComponentChildren;
15
+ readonly onError?: (
16
+ error: RouterError,
17
+ toRoute: State | null,
18
+ fromRoute: State | null,
19
+ ) => void;
20
+ }
21
+
22
+ export function RouterErrorBoundary({
23
+ children,
24
+ fallback,
25
+ onError,
26
+ }: RouterErrorBoundaryProps): VNode {
27
+ const snapshot = useRouterError();
28
+ const [dismissedVersion, setDismissedVersion] = useState(-1);
29
+
30
+ const onErrorRef = useRef(onError);
31
+
32
+ // eslint-disable-next-line @eslint-react/refs -- "latest ref" pattern: sync callback to ref to avoid effect re-runs
33
+ onErrorRef.current = onError;
34
+
35
+ useEffect(() => {
36
+ if (snapshot.error) {
37
+ onErrorRef.current?.(
38
+ snapshot.error,
39
+ snapshot.toRoute,
40
+ snapshot.fromRoute,
41
+ );
42
+ }
43
+ // eslint-disable-next-line @eslint-react/exhaustive-deps -- onError tracked via ref, snapshot fields accessed inside callback
44
+ }, [snapshot.version]);
45
+
46
+ const visibleError =
47
+ snapshot.version > dismissedVersion ? snapshot.error : null;
48
+
49
+ const resetError = useCallback(() => {
50
+ setDismissedVersion(snapshot.version);
51
+ }, [snapshot.version]);
52
+
53
+ return (
54
+ <Fragment>
55
+ {children}
56
+ {visibleError ? fallback(visibleError, resetError) : null}
57
+ </Fragment>
58
+ );
59
+ }
@@ -0,0 +1,9 @@
1
+ /**
2
+ * Stable empty object for default params
3
+ */
4
+ export const EMPTY_PARAMS = Object.freeze({});
5
+
6
+ /**
7
+ * Stable empty options object
8
+ */
9
+ export const EMPTY_OPTIONS = Object.freeze({});
package/src/context.ts ADDED
@@ -0,0 +1,10 @@
1
+ import { createContext } from "preact";
2
+
3
+ import type { RouteContext as RouteContextType } from "./types";
4
+ import type { Router, Navigator } from "@real-router/core";
5
+
6
+ export const RouteContext = createContext<RouteContextType | null>(null);
7
+
8
+ export const RouterContext = createContext<Router | null>(null);
9
+
10
+ export const NavigatorContext = createContext<Navigator | null>(null);
@@ -0,0 +1,34 @@
1
+ import { createActiveRouteSource } from "@real-router/sources";
2
+ import { useMemo } from "preact/hooks";
3
+
4
+ import { useSyncExternalStore } from "../useSyncExternalStore";
5
+ import { useRouter } from "./useRouter";
6
+ import { useStableValue } from "./useStableValue";
7
+
8
+ import type { Params } from "@real-router/core";
9
+
10
+ export function useIsActiveRoute(
11
+ routeName: string,
12
+ params?: Params,
13
+ strict = false,
14
+ ignoreQueryParams = true,
15
+ ): boolean {
16
+ const router = useRouter();
17
+
18
+ const stableParams = useStableValue(params);
19
+
20
+ const store = useMemo(
21
+ () =>
22
+ createActiveRouteSource(router, routeName, stableParams, {
23
+ strict,
24
+ ignoreQueryParams,
25
+ }),
26
+ [router, routeName, stableParams, strict, ignoreQueryParams],
27
+ );
28
+
29
+ return useSyncExternalStore(
30
+ store.subscribe,
31
+ store.getSnapshot,
32
+ store.getSnapshot,
33
+ );
34
+ }
@@ -0,0 +1,15 @@
1
+ import { useContext } from "preact/hooks";
2
+
3
+ import { NavigatorContext } from "../context";
4
+
5
+ import type { Navigator } from "@real-router/core";
6
+
7
+ export const useNavigator = (): Navigator => {
8
+ const navigator = useContext(NavigatorContext);
9
+
10
+ if (!navigator) {
11
+ throw new Error("useNavigator must be used within a RouterProvider");
12
+ }
13
+
14
+ return navigator;
15
+ };
@@ -0,0 +1,15 @@
1
+ import { useContext } from "preact/hooks";
2
+
3
+ import { RouteContext } from "../context";
4
+
5
+ import type { RouteContext as RouteContextType } from "../types";
6
+
7
+ export const useRoute = (): RouteContextType => {
8
+ const routeContext = useContext(RouteContext);
9
+
10
+ if (!routeContext) {
11
+ throw new Error("useRoute must be used within a RouteProvider");
12
+ }
13
+
14
+ return routeContext;
15
+ };
@@ -0,0 +1,30 @@
1
+ import { getNavigator } from "@real-router/core";
2
+ import { createRouteNodeSource } from "@real-router/sources";
3
+ import { useMemo } from "preact/hooks";
4
+
5
+ import { useSyncExternalStore } from "../useSyncExternalStore";
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
+
13
+ const store = useMemo(
14
+ () => createRouteNodeSource(router, nodeName),
15
+ [router, nodeName],
16
+ );
17
+
18
+ const { route, previousRoute } = useSyncExternalStore(
19
+ store.subscribe,
20
+ store.getSnapshot,
21
+ store.getSnapshot,
22
+ );
23
+
24
+ const navigator = useMemo(() => getNavigator(router), [router]);
25
+
26
+ return useMemo(
27
+ (): RouteContext => ({ navigator, route, previousRoute }),
28
+ [navigator, route, previousRoute],
29
+ );
30
+ }
@@ -0,0 +1,12 @@
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
+ };
@@ -0,0 +1,15 @@
1
+ import { useContext } from "preact/hooks";
2
+
3
+ import { RouterContext } from "../context";
4
+
5
+ import type { Router } from "@real-router/core";
6
+
7
+ export const useRouter = (): Router => {
8
+ const router = useContext(RouterContext);
9
+
10
+ if (!router) {
11
+ throw new Error("useRouter must be used within a RouterProvider");
12
+ }
13
+
14
+ return router;
15
+ };
@@ -0,0 +1,31 @@
1
+ import { createErrorSource } from "@real-router/sources";
2
+ import { useMemo } from "preact/hooks";
3
+
4
+ import { useSyncExternalStore } from "../useSyncExternalStore";
5
+ import { useRouter } from "./useRouter";
6
+
7
+ import type { Router } from "@real-router/core";
8
+ import type { RouterErrorSnapshot, RouterSource } from "@real-router/sources";
9
+
10
+ const cache = new WeakMap<Router, RouterSource<RouterErrorSnapshot>>();
11
+
12
+ export function useRouterError(): RouterErrorSnapshot {
13
+ const router = useRouter();
14
+
15
+ const store = useMemo(() => {
16
+ let source = cache.get(router);
17
+
18
+ if (!source) {
19
+ source = createErrorSource(router);
20
+ cache.set(router, source);
21
+ }
22
+
23
+ return source;
24
+ }, [router]);
25
+
26
+ return useSyncExternalStore(
27
+ store.subscribe,
28
+ store.getSnapshot,
29
+ store.getSnapshot,
30
+ );
31
+ }
@@ -0,0 +1,19 @@
1
+ import { createTransitionSource } from "@real-router/sources";
2
+ import { useMemo } from "preact/hooks";
3
+
4
+ import { useSyncExternalStore } from "../useSyncExternalStore";
5
+ import { useRouter } from "./useRouter";
6
+
7
+ import type { RouterTransitionSnapshot } from "@real-router/sources";
8
+
9
+ export function useRouterTransition(): RouterTransitionSnapshot {
10
+ const router = useRouter();
11
+
12
+ const store = useMemo(() => createTransitionSource(router), [router]);
13
+
14
+ return useSyncExternalStore(
15
+ store.subscribe,
16
+ store.getSnapshot,
17
+ store.getSnapshot,
18
+ );
19
+ }
@@ -0,0 +1,8 @@
1
+ import { useMemo } from "preact/hooks";
2
+
3
+ export function useStableValue<T>(value: T): T {
4
+ const serialized = JSON.stringify(value);
5
+
6
+ // eslint-disable-next-line @eslint-react/exhaustive-deps
7
+ return useMemo(() => value, [serialized]);
8
+ }
package/src/index.ts ADDED
@@ -0,0 +1,39 @@
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
+ // Context
22
+ export { RouterProvider } from "./RouterProvider";
23
+
24
+ export { RouterContext, NavigatorContext, RouteContext } from "./context";
25
+
26
+ // Types
27
+ export type { LinkProps } from "./types";
28
+
29
+ export type { RouterErrorBoundaryProps } from "./components/RouterErrorBoundary";
30
+
31
+ export type {
32
+ RouteViewProps,
33
+ RouteViewMatchProps,
34
+ RouteViewNotFoundProps,
35
+ } from "./components/RouteView";
36
+
37
+ export type { Navigator } from "@real-router/core";
38
+
39
+ export type { RouterTransitionSnapshot } from "@real-router/sources";
package/src/types.ts ADDED
@@ -0,0 +1,30 @@
1
+ import type {
2
+ NavigationOptions,
3
+ Params,
4
+ Navigator,
5
+ State,
6
+ } from "@real-router/core";
7
+ import type { JSX } 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 = {
15
+ navigator: Navigator;
16
+ } & RouteState;
17
+
18
+ export interface LinkProps<P extends Params = Params> extends Omit<
19
+ JSX.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
+ target?: string;
30
+ }
@@ -0,0 +1,32 @@
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
+ export function useSyncExternalStore<T>(
16
+ subscribe: (onStoreChange: () => void) => () => void,
17
+ getSnapshot: () => T,
18
+ _getServerSnapshot?: () => T,
19
+ ): T {
20
+ const [value, setValue] = useState(getSnapshot);
21
+
22
+ useEffect(() => {
23
+ // Synchronize before subscribing to handle race condition
24
+ setValue(getSnapshot());
25
+
26
+ return subscribe(() => {
27
+ setValue(getSnapshot());
28
+ });
29
+ }, [subscribe, getSnapshot]);
30
+
31
+ return value;
32
+ }