preact-hashish-router 0.0.16 → 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,13 +1,15 @@
1
1
  import { jsx as _jsx } from "preact/jsx-runtime";
2
2
  import { forwardRef } from "preact/compat";
3
3
  import { useMemo } from "preact/hooks";
4
- import { useInternalRouter } from "./useInternalRouter ";
4
+ import { useInternalRouter } from "./useInternalRouter";
5
5
  export const A = forwardRef(({ href, className, ...props }) => {
6
6
  const router = useInternalRouter();
7
7
  const isActive = useMemo(() => router.path === href?.split("?")[0], [router.path, href]);
8
8
  const browserRouterClickAnchorHandler = (e) => {
9
9
  e.preventDefault();
10
10
  e.stopPropagation();
11
+ if (!href)
12
+ return;
11
13
  router.go(href.toString());
12
14
  };
13
15
  return (_jsx("a", { href: router.type === "browser" ? href : `#${href}`, className: className, "data-route-active": isActive, ...props, onClick: router.type === "browser" ? browserRouterClickAnchorHandler : undefined }));
@@ -1,6 +1,6 @@
1
1
  import { jsx as _jsx } from "preact/jsx-runtime";
2
2
  import { Suspense } from "preact/compat";
3
- import { useInternalRouter } from "./useInternalRouter ";
3
+ import { useInternalRouter } from "./useInternalRouter";
4
4
  export function ErrorRoute(props) {
5
5
  const router = useInternalRouter();
6
6
  if (router.itMatch)
package/dist/Redirect.js CHANGED
@@ -1,6 +1,6 @@
1
1
  import { jsx as _jsx } from "preact/jsx-runtime";
2
2
  import { Fragment, useLayoutEffect } from "preact/compat";
3
- import { useInternalRouter } from "./useInternalRouter ";
3
+ import { useInternalRouter } from "./useInternalRouter";
4
4
  export const Redirect = (props) => {
5
5
  const router = useInternalRouter();
6
6
  useLayoutEffect(() => {
package/dist/Route.js CHANGED
@@ -1,7 +1,7 @@
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 { useInternalRouter } from "./useInternalRouter ";
4
+ import { useInternalRouter } from "./useInternalRouter";
5
5
  export function Route(props) {
6
6
  const router = useInternalRouter();
7
7
  const [render, setRender] = useState(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("");
@@ -4,7 +4,7 @@ export declare class RouterErrorBoundary extends Component<PropsWithChildren & {
4
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
@@ -11,4 +11,4 @@ export type RouterContext = {
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
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "preact-hashish-router",
3
- "version": "0.0.16",
3
+ "version": "0.0.17",
4
4
  "type": "module",
5
5
  "module": "dist/index.js",
6
6
  "description": "A simple router for preact",