preact-hashish-router 0.1.0 → 0.1.1

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,6 @@
1
+ import { VNode } from "preact";
2
+ export declare const NotFound: (props: {
3
+ element: VNode<any>;
4
+ }) => import("preact").JSX.Element;
5
+ export declare const set_not_found_element: (el: VNode<any>) => void;
6
+ export declare const get_not_found_element: () => VNode<any>;
@@ -0,0 +1,10 @@
1
+ import { Fragment as _Fragment, jsx as _jsx } from "preact/jsx-runtime";
2
+ export const NotFound = (props) => {
3
+ set_not_found_element(props.element);
4
+ return _jsx(_Fragment, {});
5
+ };
6
+ let not_found_element = _jsx("div", { children: "404 Not Found" });
7
+ export const set_not_found_element = (el) => {
8
+ not_found_element = el;
9
+ };
10
+ export const get_not_found_element = () => not_found_element;
@@ -1,3 +1 @@
1
- import { VNode } from "preact";
2
- export declare const RenderMatchedRoute: () => VNode<any>;
3
- export declare const set_not_found_element: (el: VNode<any>) => void;
1
+ export declare const RenderMatchedRoute: () => import("preact").VNode<any>;
@@ -1,19 +1,16 @@
1
1
  import { jsx as _jsx } from "preact/jsx-runtime";
2
2
  import { Suspense } from "preact/compat";
3
3
  import { useHashisherContext } from "./context";
4
+ import { get_not_found_element } from "./NotFound";
4
5
  export const RenderMatchedRoute = () => {
5
6
  const { active_route_data } = useHashisherContext();
6
7
  if (!active_route_data)
7
- return not_found_element;
8
+ return get_not_found_element();
8
9
  if (active_route_data.component === null) {
9
- return not_found_element;
10
+ return get_not_found_element();
10
11
  }
11
12
  if (active_route_data.lazy) {
12
13
  return _jsx(Suspense, { fallback: active_route_data.fallback, children: active_route_data.component });
13
14
  }
14
15
  return active_route_data.component;
15
16
  };
16
- let not_found_element = _jsx("div", { children: "404 Not Found" });
17
- export const set_not_found_element = (el) => {
18
- not_found_element = el;
19
- };
package/dist/Router.d.ts CHANGED
@@ -1,9 +1,5 @@
1
- import { VNode } from "preact";
2
1
  import { PropsWithChildren } from "preact/compat";
3
2
  export type RouterProps = PropsWithChildren<{
4
3
  type: "browser";
5
4
  }>;
6
5
  export declare const Router: (props: RouterProps) => import("preact").JSX.Element;
7
- export declare const NotFound: (props: {
8
- element: VNode<any>;
9
- }) => import("preact").JSX.Element;
package/dist/Router.js CHANGED
@@ -1,9 +1,9 @@
1
- import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "preact/jsx-runtime";
1
+ import { jsx as _jsx, jsxs as _jsxs } from "preact/jsx-runtime";
2
2
  import { useCallback, useLayoutEffect, useState } from "preact/hooks";
3
3
  import { findRoute } from "rou3";
4
4
  import { parseURL } from "ufo";
5
5
  import { HashisherContext } from "./context";
6
- import { RenderMatchedRoute, set_not_found_element } from "./RenderMatchedRoute";
6
+ import { RenderMatchedRoute } from "./RenderMatchedRoute";
7
7
  import { Matcher } from "./router/matcher";
8
8
  export const Router = (props) => {
9
9
  const [active_path, set_active_path] = useState(() => {
@@ -60,7 +60,3 @@ export const Router = (props) => {
60
60
  go: go_imperative,
61
61
  }, children: [props.children, _jsx(RenderMatchedRoute, {})] }));
62
62
  };
63
- export const NotFound = (props) => {
64
- set_not_found_element(props.element);
65
- return _jsx(_Fragment, {});
66
- };
package/dist/index.d.ts CHANGED
@@ -1,5 +1,6 @@
1
- export * from "./A";
2
- export * from "./context";
3
- export * from "./Route";
4
- export * from "./Router";
5
- export * from "./RouterErrorBoundary";
1
+ export { A } from "./A";
2
+ export { useParams, useRouter, useSearchParams } from "./context";
3
+ export { NotFound } from "./NotFound";
4
+ export { Route } from "./Route";
5
+ export { Router } from "./Router";
6
+ export { RouterErrorBoundary } from "./RouterErrorBoundary";
package/dist/index.js CHANGED
@@ -1,5 +1,6 @@
1
- export * from "./A";
2
- export * from "./context";
3
- export * from "./Route";
4
- export * from "./Router";
5
- export * from "./RouterErrorBoundary";
1
+ export { A } from "./A";
2
+ export { useParams, useRouter, useSearchParams } from "./context";
3
+ export { NotFound } from "./NotFound";
4
+ export { Route } from "./Route";
5
+ export { Router } from "./Router";
6
+ export { RouterErrorBoundary } from "./RouterErrorBoundary";
@@ -0,0 +1,6 @@
1
+ "use strict";
2
+ import { createRouter as a, addRoute as t } from "rou3";
3
+ export const Matcher = a(),
4
+ add_route_to_matcher = (e, o) => {
5
+ t(Matcher, void 0, e, { component: o.element, fallback: o.fallback || null, lazy: !!o.lazy });
6
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "preact-hashish-router",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "type": "module",
5
5
  "module": "dist/index.js",
6
6
  "description": "A simple router for preact",
@@ -1,4 +0,0 @@
1
- import { PropsWithChildren } from "preact/compat";
2
- export declare function ErrorRoute(props: PropsWithChildren<{
3
- lazy?: boolean;
4
- }>): import("preact").ComponentChildren;
@@ -1,12 +0,0 @@
1
- import { jsx as _jsx } from "preact/jsx-runtime";
2
- import { Suspense } from "preact/compat";
3
- import { useInternalRouter } from "./useInternalRouter";
4
- export function ErrorRoute(props) {
5
- const router = useInternalRouter();
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
- }
@@ -1,4 +0,0 @@
1
- export type RedirectProps = {
2
- to: string;
3
- };
4
- export declare const Redirect: (props: RedirectProps) => import("preact").JSX.Element;
package/dist/Redirect.js DELETED
@@ -1,10 +0,0 @@
1
- import { jsx as _jsx } from "preact/jsx-runtime";
2
- import { Fragment, useLayoutEffect } from "preact/compat";
3
- import { useInternalRouter } from "./useInternalRouter";
4
- export const Redirect = (props) => {
5
- const router = useInternalRouter();
6
- useLayoutEffect(() => {
7
- router.go(props.to);
8
- }, []);
9
- return _jsx(Fragment, {});
10
- };
package/dist/match.d.ts DELETED
@@ -1,12 +0,0 @@
1
- export type Matches = {
2
- params: Record<string, string>;
3
- rest?: string;
4
- };
5
- /**
6
- * Compara una URL contra un patrón definido y extrae parámetros.
7
- * @param url - La URL a analizar (ejemplo: "/user/12345").
8
- * @param route - El patrón de URL (ejemplo: "/user/:id").
9
- * @param matches - Objeto opcional para acumular coincidencias, inicializado por defecto.
10
- * @returns Un objeto Matches si la URL coincide con el patrón; de lo contrario, undefined.
11
- */
12
- export declare const matchRoute: (url: string, route: string, matches?: Matches) => Matches | undefined;
package/dist/match.js DELETED
@@ -1,42 +0,0 @@
1
- /**
2
- * Compara una URL contra un patrón definido y extrae parámetros.
3
- * @param url - La URL a analizar (ejemplo: "/user/12345").
4
- * @param route - El patrón de URL (ejemplo: "/user/:id").
5
- * @param matches - Objeto opcional para acumular coincidencias, inicializado por defecto.
6
- * @returns Un objeto Matches si la URL coincide con el patrón; de lo contrario, undefined.
7
- */
8
- export const matchRoute = (url, route, matches = { params: {} }) => {
9
- const urlSegments = url.split("/").filter(Boolean);
10
- const routeSegments = route.split("/").filter(Boolean);
11
- // Se itera hasta el máximo número de segmentos entre la URL y el patrón para cubrir ambos casos.
12
- for (let i = 0; i < Math.max(urlSegments.length, routeSegments.length); i++) {
13
- // Extrae y desestructura el segmento actual del patrón:
14
- // - isParam: indica si el segmento es un parámetro (comienza con ":").
15
- // - paramName: nombre del parámetro o valor literal.
16
- // - modifier: puede ser '+', '*' o '?' para modificar el comportamiento de captura.
17
- const [, isParam, paramName, modifier] = (routeSegments[i] || "").match(/^(:)?(.*?)([+*?]?)$/) || [];
18
- // Si el segmento del patrón no es un parámetro y coincide exactamente con el de la URL, continúa.
19
- if (!isParam && paramName === urlSegments[i])
20
- continue;
21
- // Si no es parámetro y el modificador es '*' se captura el resto de la URL.
22
- if (!isParam && modifier === "*") {
23
- matches.rest = `/${urlSegments.slice(i).map(decodeURIComponent).join("/")}`;
24
- break;
25
- }
26
- // Valida que el segmento sea un parámetro y que, si es obligatorio, exista en la URL.
27
- if (!isParam || (!urlSegments[i] && modifier !== "?" && modifier !== "*"))
28
- return;
29
- // Determina si el modificador indica la captura del resto de la URL ('+' o '*').
30
- const isRest = modifier === "+" || modifier === "*";
31
- // Asigna el valor del parámetro:
32
- // - Si es una captura 'rest', se unen todos los segmentos restantes.
33
- // - De lo contrario, se decodifica el segmento individual.
34
- matches.params[paramName] = isRest
35
- ? urlSegments.slice(i).map(decodeURIComponent).join("/")
36
- : decodeURIComponent(urlSegments[i]);
37
- // Si se capturaron múltiples segmentos (caso de 'rest'), se interrumpe la iteración.
38
- if (isRest)
39
- break;
40
- }
41
- return matches;
42
- };
@@ -1 +0,0 @@
1
- export declare function useInternalRouter(): import("./context").RouterContext;
@@ -1,9 +0,0 @@
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,3 +0,0 @@
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 DELETED
@@ -1,16 +0,0 @@
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("useRouter should be used within a Router");
7
- }
8
- const { go, path, params, rest, query } = context;
9
- return {
10
- go,
11
- path,
12
- params,
13
- rest,
14
- query,
15
- };
16
- }