preact-hashish-router 0.0.6 → 0.0.7
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.d.ts +5 -0
- package/dist/A.js +16 -0
- package/dist/ErrorRoute.d.ts +4 -0
- package/dist/ErrorRoute.js +12 -0
- package/dist/Route.d.ts +10 -0
- package/dist/Route.js +20 -0
- package/dist/Router.d.ts +7 -0
- package/dist/Router.js +74 -0
- package/dist/RouterErrorBoundary.d.ts +14 -0
- package/dist/RouterErrorBoundary.js +23 -0
- package/dist/context.d.ts +9 -0
- package/dist/context.js +2 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +6 -0
- package/dist/useRouter.d.ts +1 -0
- package/dist/useRouter.js +9 -0
- package/package.json +5 -2
package/dist/A.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { AnchorHTMLAttributes, PropsWithChildren } from "preact/compat";
|
|
2
|
+
export type AProps = PropsWithChildren & AnchorHTMLAttributes;
|
|
3
|
+
export declare const A: import("preact").FunctionalComponent<import("preact/compat").PropsWithoutRef<AProps> & {
|
|
4
|
+
ref?: import("preact").Ref<HTMLAnchorElement>;
|
|
5
|
+
}>;
|
package/dist/A.js
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { jsx as _jsx } from "preact/jsx-runtime";
|
|
2
|
+
import { forwardRef } from "preact/compat";
|
|
3
|
+
import { useCallback, useMemo } from "preact/hooks";
|
|
4
|
+
import { useRouter } from "./useRouter";
|
|
5
|
+
export const A = forwardRef(({ href, className, ...props }) => {
|
|
6
|
+
const router = useRouter();
|
|
7
|
+
const isActive = useMemo(() => {
|
|
8
|
+
return router.path === href;
|
|
9
|
+
}, [router.path, href]);
|
|
10
|
+
const browserRouterClickAnchorHandler = useCallback((e) => {
|
|
11
|
+
e.preventDefault();
|
|
12
|
+
e.stopPropagation();
|
|
13
|
+
router.go(href.toString());
|
|
14
|
+
}, [router.type]);
|
|
15
|
+
return (_jsx("a", { href: router.type === "browser" ? href : `#${href}`, className: className, "data-route-active": isActive, ...props, onClick: router.type === "browser" ? browserRouterClickAnchorHandler : undefined }));
|
|
16
|
+
});
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { jsx as _jsx } from "preact/jsx-runtime";
|
|
2
|
+
import { Suspense } from "preact/compat";
|
|
3
|
+
import { useRouter } from "./useRouter";
|
|
4
|
+
export function ErrorRoute(props) {
|
|
5
|
+
const router = useRouter();
|
|
6
|
+
if (router.itMatch)
|
|
7
|
+
return null;
|
|
8
|
+
if (props.lazy) {
|
|
9
|
+
return _jsx(Suspense, { fallback: _jsx("div", { children: "Loading..." }), children: props.children });
|
|
10
|
+
}
|
|
11
|
+
return props.children;
|
|
12
|
+
}
|
package/dist/Route.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { VNode } from "preact";
|
|
2
|
+
import { PropsWithChildren } from "preact/compat";
|
|
3
|
+
type RouteProps = PropsWithChildren & {
|
|
4
|
+
path: string;
|
|
5
|
+
exact?: boolean;
|
|
6
|
+
lazy?: boolean;
|
|
7
|
+
fallback?: VNode;
|
|
8
|
+
};
|
|
9
|
+
export declare function Route(props: RouteProps): import("preact").ComponentChildren;
|
|
10
|
+
export {};
|
package/dist/Route.js
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { jsx as _jsx } from "preact/jsx-runtime";
|
|
2
|
+
import { Suspense } from "preact/compat";
|
|
3
|
+
import { useRouter } from "./useRouter";
|
|
4
|
+
export function Route(props) {
|
|
5
|
+
const router = useRouter();
|
|
6
|
+
if (props.exact === undefined) {
|
|
7
|
+
props.exact = true;
|
|
8
|
+
}
|
|
9
|
+
if (props.exact && router.path !== props.path) {
|
|
10
|
+
return null;
|
|
11
|
+
}
|
|
12
|
+
else if (!router.path.includes(props.path)) {
|
|
13
|
+
return null;
|
|
14
|
+
}
|
|
15
|
+
router.setItMatch(true);
|
|
16
|
+
if (props.lazy) {
|
|
17
|
+
return _jsx(Suspense, { fallback: props.fallback ?? _jsx("div", { children: "Loading..." }), children: props.children });
|
|
18
|
+
}
|
|
19
|
+
return props.children;
|
|
20
|
+
}
|
package/dist/Router.d.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { PropsWithChildren } from "preact/compat";
|
|
2
|
+
import { RouterContext } from "./context";
|
|
3
|
+
type RouterProps = PropsWithChildren & {
|
|
4
|
+
type: RouterContext["type"];
|
|
5
|
+
};
|
|
6
|
+
export declare const Router: (props: RouterProps) => import("preact").JSX.Element;
|
|
7
|
+
export {};
|
package/dist/Router.js
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { jsx as _jsx } from "preact/jsx-runtime";
|
|
2
|
+
import { useEffect, useMemo, useState } from "preact/hooks";
|
|
3
|
+
import { router_context } from "./context";
|
|
4
|
+
const get_hash_route = () => location.hash.slice(1) || "/";
|
|
5
|
+
export const Router = (props) => {
|
|
6
|
+
const [path, setPath] = useState(get_hash_route());
|
|
7
|
+
const [query, setQuery] = useState("");
|
|
8
|
+
const [itMatch, setItMatch] = useState(false);
|
|
9
|
+
const router_type = useMemo(() => {
|
|
10
|
+
return props.type;
|
|
11
|
+
}, [props.type]);
|
|
12
|
+
const hashEffectHandler = {
|
|
13
|
+
listener: () => {
|
|
14
|
+
const [path, query] = get_hash_route().split("?");
|
|
15
|
+
setQuery(query || "");
|
|
16
|
+
setPath(path);
|
|
17
|
+
setItMatch(false);
|
|
18
|
+
},
|
|
19
|
+
effect: () => {
|
|
20
|
+
if (location.hash === "")
|
|
21
|
+
location.hash = "/";
|
|
22
|
+
window.addEventListener("hashchange", hashEffectHandler.listener);
|
|
23
|
+
},
|
|
24
|
+
cleanUp: () => {
|
|
25
|
+
window.removeEventListener("hashchange", hashEffectHandler.listener);
|
|
26
|
+
},
|
|
27
|
+
};
|
|
28
|
+
const browserEffectHandler = {
|
|
29
|
+
listener: () => {
|
|
30
|
+
setPath(location.pathname);
|
|
31
|
+
setQuery(location.search || "");
|
|
32
|
+
setItMatch(false);
|
|
33
|
+
},
|
|
34
|
+
effect: () => {
|
|
35
|
+
window.addEventListener("popstate", browserEffectHandler.listener);
|
|
36
|
+
},
|
|
37
|
+
cleanUp: () => {
|
|
38
|
+
window.removeEventListener("hashchange", browserEffectHandler.listener);
|
|
39
|
+
},
|
|
40
|
+
};
|
|
41
|
+
useEffect(() => {
|
|
42
|
+
if (router_type !== "hash")
|
|
43
|
+
return;
|
|
44
|
+
const [path, query] = get_hash_route().split("?");
|
|
45
|
+
setQuery(query || "");
|
|
46
|
+
setPath(path);
|
|
47
|
+
hashEffectHandler.effect();
|
|
48
|
+
return () => hashEffectHandler.cleanUp();
|
|
49
|
+
}, []);
|
|
50
|
+
useEffect(() => {
|
|
51
|
+
if (router_type !== "browser")
|
|
52
|
+
return;
|
|
53
|
+
setPath(location.pathname);
|
|
54
|
+
setQuery(location.search || "");
|
|
55
|
+
browserEffectHandler.effect();
|
|
56
|
+
return () => browserEffectHandler.cleanUp();
|
|
57
|
+
}, []);
|
|
58
|
+
const handlerManualRouteChange = (r) => {
|
|
59
|
+
setPath(r);
|
|
60
|
+
setItMatch(false);
|
|
61
|
+
if (router_type === "hash") {
|
|
62
|
+
location.hash = r;
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
if (router_type === "browser") {
|
|
66
|
+
history.pushState(null, "", new URL(r, location.origin));
|
|
67
|
+
return;
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
const ProviderValue = useMemo(() => {
|
|
71
|
+
return { path, go: handlerManualRouteChange, itMatch, setItMatch, type: router_type, query };
|
|
72
|
+
}, [path, handlerManualRouteChange, itMatch, setItMatch, router_type]);
|
|
73
|
+
return _jsx(router_context.Provider, { value: ProviderValue, children: props.children });
|
|
74
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Component, VNode } from "preact";
|
|
2
|
+
import { PropsWithChildren } from "preact/compat";
|
|
3
|
+
export declare class RouterErrorBoundary extends Component<PropsWithChildren & {
|
|
4
|
+
fallback?: VNode;
|
|
5
|
+
}> {
|
|
6
|
+
state: {
|
|
7
|
+
error: any;
|
|
8
|
+
};
|
|
9
|
+
static getDerivedStateFromError(error: any): {
|
|
10
|
+
error: any;
|
|
11
|
+
};
|
|
12
|
+
componentDidCatch(error: any): void;
|
|
13
|
+
render(): import("preact").ComponentChildren;
|
|
14
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { jsxs as _jsxs } from "preact/jsx-runtime";
|
|
2
|
+
import { Component } from "preact";
|
|
3
|
+
export class RouterErrorBoundary extends Component {
|
|
4
|
+
constructor() {
|
|
5
|
+
super(...arguments);
|
|
6
|
+
this.state = { error: null };
|
|
7
|
+
}
|
|
8
|
+
static getDerivedStateFromError(error) {
|
|
9
|
+
return { error: error.message };
|
|
10
|
+
}
|
|
11
|
+
componentDidCatch(error) {
|
|
12
|
+
console.error(error);
|
|
13
|
+
this.setState({ error: error.message });
|
|
14
|
+
}
|
|
15
|
+
render() {
|
|
16
|
+
if (this.state.error) {
|
|
17
|
+
if (this.props.fallback)
|
|
18
|
+
return this.props.fallback;
|
|
19
|
+
return _jsxs("p", { children: ["Oh no! We ran into an error: ", this.state.error] });
|
|
20
|
+
}
|
|
21
|
+
return this.props.children;
|
|
22
|
+
}
|
|
23
|
+
}
|
package/dist/context.js
ADDED
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function useRouter(): import("./context").RouterContext;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { useContext } from "preact/hooks";
|
|
2
|
+
import { router_context } from "./context";
|
|
3
|
+
export function useRouter() {
|
|
4
|
+
const context = useContext(router_context);
|
|
5
|
+
if (!context) {
|
|
6
|
+
throw new Error("useRoute should be used within a Router");
|
|
7
|
+
}
|
|
8
|
+
return context;
|
|
9
|
+
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "preact-hashish-router",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.7",
|
|
4
4
|
"type": "module",
|
|
5
|
+
"module": "dist/index.js",
|
|
5
6
|
"description": "A simple router for preact",
|
|
6
7
|
"scripts": {
|
|
7
8
|
"build": "tsc -p ./tsconfig.json",
|
|
@@ -12,7 +13,7 @@
|
|
|
12
13
|
"push-major": "npm version major && git push"
|
|
13
14
|
},
|
|
14
15
|
"exports": {
|
|
15
|
-
".": "./index.
|
|
16
|
+
".": "./dist/index.js"
|
|
16
17
|
},
|
|
17
18
|
"peerDependencies": {
|
|
18
19
|
"preact": "^10.26.2"
|
|
@@ -29,6 +30,7 @@
|
|
|
29
30
|
"author": "LiasCode",
|
|
30
31
|
"devDependencies": {
|
|
31
32
|
"@preact/preset-vite": "^2.10.1",
|
|
33
|
+
"preact-hashish-router": "^0.0.6",
|
|
32
34
|
"prettier": "^3.5.1",
|
|
33
35
|
"prettier-plugin-organize-imports": "^4.1.0",
|
|
34
36
|
"typescript": "^5.7.3",
|
|
@@ -36,6 +38,7 @@
|
|
|
36
38
|
},
|
|
37
39
|
"files": [
|
|
38
40
|
"src",
|
|
41
|
+
"dist",
|
|
39
42
|
"LICENSE",
|
|
40
43
|
"package.json",
|
|
41
44
|
"README.md"
|