preact-hashish-router 0.0.15 → 0.0.17

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/README.md CHANGED
@@ -30,7 +30,7 @@ import ProductPage from "./routes/Product";
30
30
 
31
31
  export default function App() {
32
32
  return (
33
- <Router type="hash">
33
+ <Router type="hash"> {/* <-- or browser */}
34
34
  <RouterErrorBoundary>
35
35
  <Route path="/">
36
36
  <HomePage />
package/dist/A.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { AnchorHTMLAttributes, PropsWithChildren } from "preact/compat";
2
2
  export type AProps = PropsWithChildren & AnchorHTMLAttributes;
3
3
  export declare const A: import("preact").FunctionalComponent<import("preact/compat").PropsWithoutRef<AProps> & {
4
- ref?: import("preact").Ref<HTMLAnchorElement>;
4
+ ref?: import("preact").Ref<HTMLAnchorElement> | undefined;
5
5
  }>;
package/dist/A.js CHANGED
@@ -1,16 +1,16 @@
1
1
  import { jsx as _jsx } from "preact/jsx-runtime";
2
2
  import { forwardRef } from "preact/compat";
3
- import { useCallback, useMemo } from "preact/hooks";
4
- import { useRouter } from "./useRouter";
3
+ import { useMemo } from "preact/hooks";
4
+ import { useInternalRouter } from "./useInternalRouter";
5
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) => {
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();
11
+ if (!href)
12
+ return;
13
13
  router.go(href.toString());
14
- }, [router.type]);
14
+ };
15
15
  return (_jsx("a", { href: router.type === "browser" ? href : `#${href}`, className: className, "data-route-active": isActive, ...props, onClick: router.type === "browser" ? browserRouterClickAnchorHandler : undefined }));
16
16
  });
@@ -1,8 +1,8 @@
1
1
  import { jsx as _jsx } from "preact/jsx-runtime";
2
2
  import { Suspense } from "preact/compat";
3
- import { useRouter } from "./useRouter";
3
+ import { useInternalRouter } from "./useInternalRouter";
4
4
  export function ErrorRoute(props) {
5
- const router = useRouter();
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 { useRouter } from "./useRouter";
3
+ import { useInternalRouter } from "./useInternalRouter";
4
4
  export const Redirect = (props) => {
5
- const router = useRouter();
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 { useRouter } from "./useRouter";
4
+ import { useInternalRouter } from "./useInternalRouter";
5
5
  export function Route(props) {
6
- const router = useRouter();
6
+ const router = useInternalRouter();
7
7
  const [render, setRender] = useState(false);
8
8
  useLayoutEffect(() => {
9
9
  setRender(false);
@@ -17,8 +17,8 @@ export function Route(props) {
17
17
  if (props.exact === false && matches === undefined) {
18
18
  return;
19
19
  }
20
- router.setParams(matches.params);
21
- router.setRest(matches.rest);
20
+ router.setParams(matches?.params || {});
21
+ router.setRest(matches?.rest);
22
22
  router.setItMatch(true);
23
23
  setRender(true);
24
24
  }, [router.path]);
package/dist/Router.js CHANGED
@@ -3,7 +3,7 @@ import { useLayoutEffect, useMemo, useState } from "preact/hooks";
3
3
  import { router_context } from "./context";
4
4
  const get_hash_route = () => location.hash.slice(1) || "/";
5
5
  export const Router = (props) => {
6
- const [path, setPath] = useState();
6
+ const [path, setPath] = useState("");
7
7
  const [query, setQuery] = useState("");
8
8
  const [params, setParams] = useState({});
9
9
  const [rest, setRest] = useState("");
@@ -13,14 +13,14 @@ export const Router = (props) => {
13
13
  }, [props.type]);
14
14
  const hashEffectHandler = {
15
15
  listener: () => {
16
- const [path, query] = get_hash_route().split("?");
16
+ const [newPath, query] = get_hash_route().split("?");
17
17
  setQuery(query || "");
18
- setPath(path);
19
- setItMatch(false);
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
- setItMatch(false);
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
- if (path === newPath)
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
  }
@@ -1,10 +1,10 @@
1
1
  import { Component, VNode } from "preact";
2
2
  import { PropsWithChildren } from "preact/compat";
3
3
  export declare class RouterErrorBoundary extends Component<PropsWithChildren & {
4
- fallback?: VNode;
4
+ fallback?: VNode<any>;
5
5
  }> {
6
6
  state: {
7
- error: any;
7
+ error: null;
8
8
  };
9
9
  static getDerivedStateFromError(error: any): {
10
10
  error: any;
package/dist/context.d.ts CHANGED
@@ -1,14 +1,14 @@
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"];
12
12
  setRest: (r: Matches["rest"]) => void;
13
13
  };
14
- export declare const router_context: import("preact").Context<RouterContext>;
14
+ export declare const router_context: import("preact").Context<RouterContext | null>;
package/dist/match.js CHANGED
@@ -6,7 +6,6 @@
6
6
  * @returns Un objeto Matches si la URL coincide con el patrón; de lo contrario, undefined.
7
7
  */
8
8
  export const matchRoute = (url, route, matches = { params: {} }) => {
9
- // Separa la URL y el patrón en segmentos, eliminando elementos vacíos para facilitar la comparación.
10
9
  const urlSegments = url.split("/").filter(Boolean);
11
10
  const routeSegments = route.split("/").filter(Boolean);
12
11
  // Se itera hasta el máximo número de segmentos entre la URL y el patrón para cubrir ambos casos.
@@ -39,6 +38,5 @@ export const matchRoute = (url, route, matches = { params: {} }) => {
39
38
  if (isRest)
40
39
  break;
41
40
  }
42
- // Retorna el objeto con los parámetros y/o resto capturados.
43
41
  return matches;
44
42
  };
@@ -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
+ }
@@ -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
+ }
@@ -1 +1,3 @@
1
- export declare function useRouter(): import("./context").RouterContext;
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("useRoute should be used within a Router");
6
+ throw new Error("useRouter should be used within a Router");
7
7
  }
8
- return context;
8
+ const { go, path, params, rest, query } = context;
9
+ return {
10
+ go,
11
+ path,
12
+ params,
13
+ rest,
14
+ query,
15
+ };
9
16
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "preact-hashish-router",
3
- "version": "0.0.15",
3
+ "version": "0.0.17",
4
4
  "type": "module",
5
5
  "module": "dist/index.js",
6
6
  "description": "A simple router for preact",