preact-hashish-router 0.0.15 → 0.0.16
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/dist/A.js +6 -8
- package/dist/ErrorRoute.js +2 -2
- package/dist/Redirect.js +2 -2
- package/dist/Route.d.ts +1 -2
- package/dist/Route.js +2 -2
- package/dist/Router.js +17 -11
- package/dist/RouterErrorBoundary.d.ts +1 -1
- package/dist/context.d.ts +1 -1
- package/dist/useInternalRouter .d.ts +1 -0
- package/dist/useInternalRouter .js +9 -0
- package/dist/useRouter.d.ts +3 -1
- package/dist/useRouter.js +9 -2
- package/package.json +1 -1
package/dist/A.js
CHANGED
|
@@ -1,16 +1,14 @@
|
|
|
1
1
|
import { jsx as _jsx } from "preact/jsx-runtime";
|
|
2
2
|
import { forwardRef } from "preact/compat";
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
3
|
+
import { useMemo } from "preact/hooks";
|
|
4
|
+
import { useInternalRouter } from "./useInternalRouter ";
|
|
5
5
|
export const A = forwardRef(({ href, className, ...props }) => {
|
|
6
|
-
const router =
|
|
7
|
-
const isActive = useMemo(() =>
|
|
8
|
-
|
|
9
|
-
}, [router.path, href]);
|
|
10
|
-
const browserRouterClickAnchorHandler = useCallback((e) => {
|
|
6
|
+
const router = useInternalRouter();
|
|
7
|
+
const isActive = useMemo(() => router.path === href?.split("?")[0], [router.path, href]);
|
|
8
|
+
const browserRouterClickAnchorHandler = (e) => {
|
|
11
9
|
e.preventDefault();
|
|
12
10
|
e.stopPropagation();
|
|
13
11
|
router.go(href.toString());
|
|
14
|
-
}
|
|
12
|
+
};
|
|
15
13
|
return (_jsx("a", { href: router.type === "browser" ? href : `#${href}`, className: className, "data-route-active": isActive, ...props, onClick: router.type === "browser" ? browserRouterClickAnchorHandler : undefined }));
|
|
16
14
|
});
|
package/dist/ErrorRoute.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { jsx as _jsx } from "preact/jsx-runtime";
|
|
2
2
|
import { Suspense } from "preact/compat";
|
|
3
|
-
import {
|
|
3
|
+
import { useInternalRouter } from "./useInternalRouter ";
|
|
4
4
|
export function ErrorRoute(props) {
|
|
5
|
-
const router =
|
|
5
|
+
const router = useInternalRouter();
|
|
6
6
|
if (router.itMatch)
|
|
7
7
|
return null;
|
|
8
8
|
if (props.lazy) {
|
package/dist/Redirect.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import { jsx as _jsx } from "preact/jsx-runtime";
|
|
2
2
|
import { Fragment, useLayoutEffect } from "preact/compat";
|
|
3
|
-
import {
|
|
3
|
+
import { useInternalRouter } from "./useInternalRouter ";
|
|
4
4
|
export const Redirect = (props) => {
|
|
5
|
-
const router =
|
|
5
|
+
const router = useInternalRouter();
|
|
6
6
|
useLayoutEffect(() => {
|
|
7
7
|
router.go(props.to);
|
|
8
8
|
}, []);
|
package/dist/Route.d.ts
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
import { VNode } from "preact";
|
|
2
2
|
import { PropsWithChildren } from "preact/compat";
|
|
3
|
-
type RouteProps = PropsWithChildren & {
|
|
3
|
+
export type RouteProps = PropsWithChildren & {
|
|
4
4
|
path: string;
|
|
5
5
|
exact?: boolean;
|
|
6
6
|
lazy?: boolean;
|
|
7
7
|
fallback?: VNode;
|
|
8
8
|
};
|
|
9
9
|
export declare function Route(props: RouteProps): import("preact").ComponentChildren;
|
|
10
|
-
export {};
|
package/dist/Route.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { jsx as _jsx } from "preact/jsx-runtime";
|
|
2
2
|
import { Suspense, useLayoutEffect, useState } from "preact/compat";
|
|
3
3
|
import { matchRoute } from "./match";
|
|
4
|
-
import {
|
|
4
|
+
import { useInternalRouter } from "./useInternalRouter ";
|
|
5
5
|
export function Route(props) {
|
|
6
|
-
const router =
|
|
6
|
+
const router = useInternalRouter();
|
|
7
7
|
const [render, setRender] = useState(false);
|
|
8
8
|
useLayoutEffect(() => {
|
|
9
9
|
setRender(false);
|
package/dist/Router.js
CHANGED
|
@@ -13,14 +13,14 @@ export const Router = (props) => {
|
|
|
13
13
|
}, [props.type]);
|
|
14
14
|
const hashEffectHandler = {
|
|
15
15
|
listener: () => {
|
|
16
|
-
const [
|
|
16
|
+
const [newPath, query] = get_hash_route().split("?");
|
|
17
17
|
setQuery(query || "");
|
|
18
|
-
setPath(
|
|
19
|
-
|
|
18
|
+
setPath(newPath);
|
|
19
|
+
if (newPath !== path) {
|
|
20
|
+
setItMatch(false);
|
|
21
|
+
}
|
|
20
22
|
},
|
|
21
23
|
effect: () => {
|
|
22
|
-
if (location.hash === "")
|
|
23
|
-
location.hash = "/";
|
|
24
24
|
window.addEventListener("hashchange", hashEffectHandler.listener);
|
|
25
25
|
},
|
|
26
26
|
cleanUp: () => {
|
|
@@ -30,8 +30,10 @@ export const Router = (props) => {
|
|
|
30
30
|
const browserEffectHandler = {
|
|
31
31
|
listener: () => {
|
|
32
32
|
setPath(location.pathname);
|
|
33
|
-
setQuery(location.search || "");
|
|
34
|
-
|
|
33
|
+
setQuery(location.search.split("?")[1] || "");
|
|
34
|
+
if (path !== location.pathname) {
|
|
35
|
+
setItMatch(false);
|
|
36
|
+
}
|
|
35
37
|
},
|
|
36
38
|
effect: () => {
|
|
37
39
|
window.addEventListener("popstate", browserEffectHandler.listener);
|
|
@@ -59,20 +61,24 @@ export const Router = (props) => {
|
|
|
59
61
|
if (router_type !== "browser")
|
|
60
62
|
return;
|
|
61
63
|
setPath(location.pathname);
|
|
62
|
-
setQuery(location.search || "");
|
|
64
|
+
setQuery(location.search.split("?")[1] || "");
|
|
63
65
|
browserEffectHandler.effect();
|
|
64
66
|
return () => browserEffectHandler.cleanUp();
|
|
65
67
|
}, []);
|
|
66
68
|
const handlerManualRouteChange = (newPath) => {
|
|
67
|
-
|
|
69
|
+
const [np, nq] = newPath.split("?");
|
|
70
|
+
if (path === np && query === nq)
|
|
68
71
|
return;
|
|
69
|
-
setPath(newPath);
|
|
70
|
-
setItMatch(false);
|
|
71
72
|
if (router_type === "hash") {
|
|
72
73
|
location.hash = newPath;
|
|
73
74
|
return;
|
|
74
75
|
}
|
|
75
76
|
if (router_type === "browser") {
|
|
77
|
+
setPath(np);
|
|
78
|
+
setQuery(nq || "");
|
|
79
|
+
if (path !== np) {
|
|
80
|
+
setItMatch(false);
|
|
81
|
+
}
|
|
76
82
|
history.pushState(null, "", new URL(newPath, location.origin));
|
|
77
83
|
return;
|
|
78
84
|
}
|
package/dist/context.d.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { Matches } from "./match";
|
|
2
2
|
export type RouterContext = {
|
|
3
|
+
type: "hash" | "browser";
|
|
3
4
|
path: string;
|
|
4
5
|
query: string;
|
|
5
6
|
go: (r: string) => void;
|
|
6
7
|
itMatch: boolean;
|
|
7
8
|
setItMatch: (r: boolean) => void;
|
|
8
|
-
type: "hash" | "browser";
|
|
9
9
|
params: Matches["params"];
|
|
10
10
|
setParams: (p: Matches["params"]) => void;
|
|
11
11
|
rest: Matches["rest"];
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function useInternalRouter(): import("./context").RouterContext;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { useContext } from "preact/hooks";
|
|
2
|
+
import { router_context } from "./context";
|
|
3
|
+
export function useInternalRouter() {
|
|
4
|
+
const context = useContext(router_context);
|
|
5
|
+
if (!context) {
|
|
6
|
+
throw new Error("useInternalRouter should be used within a Router");
|
|
7
|
+
}
|
|
8
|
+
return context;
|
|
9
|
+
}
|
package/dist/useRouter.d.ts
CHANGED
|
@@ -1 +1,3 @@
|
|
|
1
|
-
|
|
1
|
+
import { RouterContext } from "./context";
|
|
2
|
+
export type PublicRouterContext = Pick<RouterContext, "go" | "path" | "params" | "rest" | "query">;
|
|
3
|
+
export declare function useRouter(): PublicRouterContext;
|
package/dist/useRouter.js
CHANGED
|
@@ -3,7 +3,14 @@ import { router_context } from "./context";
|
|
|
3
3
|
export function useRouter() {
|
|
4
4
|
const context = useContext(router_context);
|
|
5
5
|
if (!context) {
|
|
6
|
-
throw new Error("
|
|
6
|
+
throw new Error("useRouter should be used within a Router");
|
|
7
7
|
}
|
|
8
|
-
|
|
8
|
+
const { go, path, params, rest, query } = context;
|
|
9
|
+
return {
|
|
10
|
+
go,
|
|
11
|
+
path,
|
|
12
|
+
params,
|
|
13
|
+
rest,
|
|
14
|
+
query,
|
|
15
|
+
};
|
|
9
16
|
}
|