preact-hashish-router 0.1.7 → 0.1.9

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
@@ -1,13 +1,16 @@
1
1
  # Preact Hashish Router
2
2
 
3
+ A simple router for preact with support for hash and browser routing.
4
+
3
5
  ## Features
4
6
 
5
7
  - `browser` and `hash` routing types.
6
8
  - Support for lazy-loaded routes (`lazy` loading).
7
- - Error handling integration with `ErrorRoute`.
9
+ - Error handling.
8
10
  - Fully typed.
9
11
  - Ultra lightweight.
10
12
  - Minimal external dependencies.
13
+ - Super fast route matching using [unjs/rou3](https://github.com/h3js/rou3)
11
14
 
12
15
  ## Installation
13
16
 
package/dist/A.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { ComponentProps } from "preact/compat";
1
+ import { type ComponentProps } from "preact/compat";
2
2
  export type AProps = Omit<ComponentProps<"a">, "href"> & {
3
3
  href: string;
4
4
  };
@@ -1,4 +1,4 @@
1
- import { VNode } from "preact";
1
+ import type { VNode } from "preact";
2
2
  export declare const NotFound: (props: {
3
3
  element: VNode<any>;
4
4
  }) => import("preact").JSX.Element;
package/dist/Route.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { VNode } from "preact";
1
+ import type { VNode } from "preact";
2
2
  export type RouteProps = {
3
3
  /** The route path matcher
4
4
  * @example "/"
package/dist/Router.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { PropsWithChildren } from "preact/compat";
1
+ import type { PropsWithChildren } from "preact/compat";
2
2
  export type RouterProps = PropsWithChildren<{
3
3
  type: "hash" | "browser";
4
4
  /**
@@ -1,5 +1,5 @@
1
- import { Component, VNode } from "preact";
2
- import { PropsWithChildren } from "preact/compat";
1
+ import { Component, type VNode } from "preact";
2
+ import type { PropsWithChildren } from "preact/compat";
3
3
  export declare class RouterErrorBoundary extends Component<PropsWithChildren & {
4
4
  fallback?: VNode<any>;
5
5
  }> {
package/dist/context.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { RouteMatched } from "./router/matcher";
1
+ import type { RouteMatched } from "./router/matcher";
2
2
  export type HashisherContextVal = {
3
3
  active_path: string | null;
4
4
  params: RouteMatched["params"];
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- import{forwardRef as E}from"preact/compat";import{createContext as S}from"preact";import{useContext as h}from"preact/hooks";var n=S({active_path:"",active_route_data:null,params:void 0,searchParams:new URLSearchParams,go(){}}),p=()=>{let e=h(n);if(!e)throw new Error("useHashisherContext should be inside a HashisherContext provider");return e};function M(){let e=h(n);if(!e)throw new Error("useParams should be inside a HashisherContext provider");return e.params}function N(){let e=h(n);if(!e)throw new Error("useSearchParams should be inside a HashisherContext provider");return e.searchParams}var L=()=>{let e=h(n);if(!e)throw new Error("useRouter should be inside a HashisherContext provider");return{path:e.active_path,params:e.params,searchParams:e.searchParams,go:e.go}};import{jsx as U}from"preact/jsx-runtime";var k=E(({href:e,...r},s)=>{let{go:u}=p();if(!e)throw new Error("A: href must be defined");return U("a",{ref:s,href:e,onClick:i=>{i.preventDefault(),u(e)},...r})});import{Fragment as W,jsx as y}from"preact/jsx-runtime";var A=e=>(z(e.element),y(W,{})),w=y("div",{children:"404 Not Found"}),z=e=>{w=e},l=()=>w;import{addRoute as F,createRouter as T}from"rou3";var f=T(),R=(e,r)=>{F(f,void 0,e,{component:r.element,fallback:r.fallback||null,lazy:!!r.lazy})};import{Fragment as O,jsx as B}from"preact/jsx-runtime";function D(e){return R(e.path,e),B(O,{})}import{useCallback as I,useLayoutEffect as C,useState as c}from"preact/hooks";import{findRoute as J}from"rou3";import{parseURL as g}from"ufo";import{Suspense as q}from"preact/compat";import{jsx as G}from"preact/jsx-runtime";var v=()=>{let{active_route_data:e}=p();return e?e.component===null?l():e.lazy?G(q,{fallback:e.fallback,children:e.component}):e.component:l()};import{jsx as Q,jsxs as X}from"preact/jsx-runtime";var K=e=>{let[r,s]=c("/"),[u,i]=c(void 0),[b,_]=c(new URLSearchParams),[H,P]=c(null),m=I(t=>{let a=g(window.location.href),o=t||"";t===null&&(e.type==="hash"?o=a.hash:o=a.pathname);let d=J(f,void 0,o);if(!d){s(o),_(new URLSearchParams(a.search)),P(null),i(void 0),e.type==="browser"&&window.history.pushState(null,"",o),e.type==="hash"&&(window.location.hash=o);return}s(o),_(new URLSearchParams(a.search)),i({...d.params}),P({...d.data}),e.type==="browser"&&window.history.pushState(null,"",o),e.type==="hash"&&(window.location.hash=o)},[]);C(()=>{if(e.type!=="browser")return;let t=()=>{m(null)};return window.addEventListener("popstate",t),t(),()=>{window.removeEventListener("popstate",t)}},[]),C(()=>{if(e.type!=="hash")return;let t=()=>{m(null)};return window.addEventListener("hashchange",t),t(),()=>{window.removeEventListener("hashchange",t)}},[]);let V=t=>{let a=g(t).pathname;m(a)};return X(n.Provider,{value:{active_path:r,searchParams:b,params:u,active_route_data:H,go:V},children:[e.children,Q(v,{})]})};import{Component as Y}from"preact";import{jsxs as Z}from"preact/jsx-runtime";var x=class extends Y{state={error:null};static getDerivedStateFromError(r){return{error:r.message}}componentDidCatch(r){this.setState({error:r.message})}render(){return this.state.error?this.props.fallback?this.props.fallback:Z("p",{children:["Oh no! We ran into an error: ",this.state.error]}):this.props.children}};export{k as A,A as NotFound,D as Route,K as Router,x as RouterErrorBoundary,M as useParams,L as useRouter,N as useSearchParams};
1
+ import{forwardRef as k}from"preact/compat";import{createContext as S}from"preact";import{useContext as p}from"preact/hooks";var s=S({active_path:"",active_route_data:null,params:void 0,searchParams:new URLSearchParams,go(){}}),c=()=>{let e=p(s);if(!e)throw new Error("useHashisherContext should be inside a HashisherContext provider");return e};function M(){let e=p(s);if(!e)throw new Error("useParams should be inside a HashisherContext provider");return e.params}function N(){let e=p(s);if(!e)throw new Error("useSearchParams should be inside a HashisherContext provider");return e.searchParams}var L=()=>{let e=p(s);if(!e)throw new Error("useRouter should be inside a HashisherContext provider");return{path:e.active_path,params:e.params,searchParams:e.searchParams,go:e.go}};import{jsx as U}from"preact/jsx-runtime";var E=k(({href:e,...r},h)=>{let{go:m}=c();if(!e)throw new Error("A: href must be defined");return U("a",{ref:h,href:e,onClick:o=>{o&&o.type==="click"&&(o.ctrlKey||o.metaKey||o.altKey||o.shiftKey||o.button!==0)||(o.preventDefault(),m(e))},...r})});import{Fragment as W,jsx as w}from"preact/jsx-runtime";var A=e=>(z(e.element),w(W,{})),P=w("div",{children:"404 Not Found"}),z=e=>{P=e},l=()=>P;import{addRoute as F,createRouter as K}from"rou3";var f=K(),R=(e,r)=>{F(f,void 0,e,{component:r.element,fallback:r.fallback||null,lazy:!!r.lazy})};import{Fragment as D,jsx as O}from"preact/jsx-runtime";function T(e){return R(e.path,e),O(D,{})}import{useCallback as G,useLayoutEffect as g,useState as u}from"preact/hooks";import{findRoute as I}from"rou3";import{parseURL as v}from"ufo";import{Suspense as B}from"preact/compat";import{jsx as q}from"preact/jsx-runtime";var C=()=>{let{active_route_data:e}=c();return e?e.component===null?l():e.lazy?q(B,{fallback:e.fallback,children:e.component}):e.component:l()};import{jsx as Q,jsxs as X}from"preact/jsx-runtime";var J=e=>{let[r,h]=u("/"),[m,o]=u(void 0),[b,x]=u(new URLSearchParams),[H,_]=u(null),i=G(t=>{let n=v(window.location.href),a=t||"";t===null&&(e.type==="hash"?a=n.hash:a=n.pathname);let d=I(f,void 0,a);if(!d){h(a),x(new URLSearchParams(n.search)),_(null),o(void 0),e.type==="browser"&&window.history.pushState(null,"",a),e.type==="hash"&&(window.location.hash=a);return}h(a),x(new URLSearchParams(n.search)),o({...d.params}),_({...d.data}),e.type==="browser"&&window.history.pushState(null,"",a),e.type==="hash"&&(window.location.hash=a)},[e.type]);g(()=>{if(e.type!=="browser")return;let t=()=>{i(null)};return window.addEventListener("popstate",t),t(),()=>{window.removeEventListener("popstate",t)}},[i,e.type]),g(()=>{if(e.type!=="hash")return;let t=()=>{i(null)};return window.addEventListener("hashchange",t),t(),()=>{window.removeEventListener("hashchange",t)}},[i,e.type]);let V=t=>{let n=v(t).pathname;i(n)};return X(s.Provider,{value:{active_path:r,searchParams:b,params:m,active_route_data:H,go:V},children:[e.children,Q(C,{})]})};import{Component as Y}from"preact";import{jsxs as Z}from"preact/jsx-runtime";var y=class extends Y{state={error:null};static getDerivedStateFromError(r){return{error:r.message}}componentDidCatch(r){this.setState({error:r.message})}render(){return this.state.error?this.props.fallback?this.props.fallback:Z("p",{children:["Oh no! We ran into an error: ",this.state.error]}):this.props.children}};export{E as A,A as NotFound,T as Route,J as Router,y as RouterErrorBoundary,M as useParams,L as useRouter,N as useSearchParams};
@@ -1,6 +1,6 @@
1
- import { VNode } from "preact";
2
- import { MatchedRoute } from "rou3";
3
- import { RouteProps } from "../Route";
1
+ import type { VNode } from "preact";
2
+ import { type MatchedRoute } from "rou3";
3
+ import type { RouteProps } from "../Route";
4
4
  export type MatcherPayload = {
5
5
  component: VNode<any>;
6
6
  lazy: boolean;
package/package.json CHANGED
@@ -1,26 +1,25 @@
1
1
  {
2
2
  "name": "preact-hashish-router",
3
- "version": "0.1.7",
3
+ "version": "0.1.9",
4
4
  "type": "module",
5
5
  "description": "A simple router for preact",
6
6
  "scripts": {
7
7
  "types": "tsc -p ./tsconfig.json",
8
+ "build": "rm -rf ./dist && bun run types && bun run bundle",
8
9
  "bundle": "esbuild ./src/index.ts --bundle --minify --platform=browser --packages=external --outfile=./dist/index.js --format=esm",
9
10
  "bundle:watch": "esbuild --watch ./src/index.ts --bundle --minify --platform=browser --packages=external --outfile=./dist/index.js --format=esm",
10
11
  "format": "prettier --write ./src --ignore-unknown --cache",
11
- "app:dev": "vite",
12
- "build": "rm -rf ./dist && bun run types && bun run bundle",
13
- "prepublishOnly": "bun run types && bun run bundle",
14
- "patch": "bun pm version patch && git push",
15
- "minor": "bun pm version minor && git push",
16
- "major": "bun pm version major && git push",
17
- "release": "changelogen --release --push && bun publish"
12
+ "lint": "biome lint . --fix --unsafe",
13
+ "code:style": "bun run format && bun run lint",
14
+ "prepublishOnly": "bun run build",
15
+ "release": "bun run build && changelogen --release --publish && git push --follow-tags"
18
16
  },
19
17
  "workspaces": [
20
18
  "./examples/*"
21
19
  ],
22
20
  "exports": {
23
- ".": "./dist/index.js"
21
+ ".": "./dist/index.js",
22
+ "./package.json": "./package.json"
24
23
  },
25
24
  "keywords": [
26
25
  "preact",
@@ -28,7 +27,8 @@
28
27
  "simple",
29
28
  "routing",
30
29
  "browser",
31
- "hash"
30
+ "hash",
31
+ "radix tree"
32
32
  ],
33
33
  "author": {
34
34
  "name": "LiasCode",
@@ -45,18 +45,19 @@
45
45
  "README.md"
46
46
  ],
47
47
  "dependencies": {
48
- "rou3": "^0.7.3",
49
- "ufo": "^1.6.1"
48
+ "rou3": "0.7.3",
49
+ "ufo": "1.6.1"
50
50
  },
51
51
  "peerDependencies": {
52
- "preact": "^10.27.0"
52
+ "preact": "10.27.2"
53
53
  },
54
54
  "devDependencies": {
55
- "@types/node": "^22.17.2",
56
- "changelogen": "^0.6.2",
57
- "esbuild": "^0.25.9",
58
- "prettier": "^3.6.2",
59
- "prettier-plugin-organize-imports": "^4.2.0",
60
- "typescript": "^5.9.2"
55
+ "@biomejs/biome": "1.8.3",
56
+ "@types/node": "22.18.0",
57
+ "changelogen": "0.6.2",
58
+ "esbuild": "0.25.9",
59
+ "prettier": "3.6.2",
60
+ "prettier-plugin-organize-imports": "4.2.0",
61
+ "typescript": "5.9.2"
61
62
  }
62
63
  }