preact-hashish-router 0.1.6 → 0.1.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/Router.d.ts CHANGED
@@ -1,5 +1,15 @@
1
1
  import { PropsWithChildren } from "preact/compat";
2
2
  export type RouterProps = PropsWithChildren<{
3
- type: "browser";
3
+ type: "hash" | "browser";
4
+ /**
5
+ * Only for `hash` routers.
6
+ *
7
+ * Decide if the initial pathname will be rewrite as the initial hash.
8
+ *
9
+ * `Caution`: This will replace the initial url hash
10
+ * @default false
11
+ * @deprecated
12
+ */
13
+ redirect_path_to_hash?: boolean;
4
14
  }>;
5
15
  export declare const Router: (props: RouterProps) => import("preact").JSX.Element;
package/dist/index.js CHANGED
@@ -1 +1 @@
1
- import{forwardRef as k}from"preact/compat";import{createContext as V}from"preact";import{useContext as h}from"preact/hooks";var o=V({active_path:"",active_route_data:null,params:void 0,searchParams:new URLSearchParams,go(){}}),p=()=>{let e=h(o);if(!e)throw new Error("useHashisherContext should be inside a HashisherContext provider");return e};function S(){let e=h(o);if(!e)throw new Error("useParams should be inside a HashisherContext provider");return e.params}function M(){let e=h(o);if(!e)throw new Error("useSearchParams should be inside a HashisherContext provider");return e.searchParams}var N=()=>{let e=h(o);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 E}from"preact/jsx-runtime";var L=k(({href:e,...t},s)=>{let{go:m}=p();if(!e)throw new Error("A: href must be defined");return E("a",{ref:s,href:e,onClick:i=>{i.preventDefault(),m(e)},...t})});import{Fragment as z,jsx as w}from"preact/jsx-runtime";var U=e=>(A(e.element),w(z,{})),R=w("div",{children:"404 Not Found"}),A=e=>{R=e},d=()=>R;import{addRoute as W,createRouter as F}from"rou3";var l=F(),y=(e,t)=>{W(l,void 0,e,{component:t.element,fallback:t.fallback||null,lazy:!!t.lazy})};import{Fragment as D,jsx as O}from"preact/jsx-runtime";function T(e){return y(e.path,e),O(D,{})}import{useCallback as G,useLayoutEffect as I,useState as u}from"preact/hooks";import{findRoute as J}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}=p();return e?e.component===null?d():e.lazy?q(B,{fallback:e.fallback,children:e.component}):e.component:d()};import{jsx as Q,jsxs as X}from"preact/jsx-runtime";var K=e=>{let[t,s]=u(()=>typeof window<"u"?window.location.pathname:null),[m,i]=u(void 0),[g,x]=u(new URLSearchParams),[b,P]=u(null),_=G(r=>{let a=v(window.location.href),n=r===null?a.pathname:r,c=J(l,void 0,n);if(!c){s(n),x(new URLSearchParams(a.search)),P(null),i(void 0),e.type==="browser"&&window.history.pushState(null,"",n);return}s(n),x(new URLSearchParams(a.search)),i({...c.params}),P({...c.data}),e.type==="browser"&&window.history.pushState(null,"",n)},[]);I(()=>{if(e.type!=="browser")return;let r=()=>{_(null)};return window.addEventListener("popstate",r),r(),()=>{window.removeEventListener("popstate",r)}},[]);let H=r=>{let a=v(r).pathname;_(a)};return X(o.Provider,{value:{active_path:t,searchParams:g,params:m,active_route_data:b,go:H},children:[e.children,Q(C,{})]})};import{Component as Y}from"preact";import{jsxs as Z}from"preact/jsx-runtime";var f=class extends Y{state={error:null};static getDerivedStateFromError(t){return{error:t.message}}componentDidCatch(t){this.setState({error:t.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{L as A,U as NotFound,T as Route,K as Router,f as RouterErrorBoundary,S as useParams,N as useRouter,M as useSearchParams};
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};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "preact-hashish-router",
3
- "version": "0.1.6",
3
+ "version": "0.1.7",
4
4
  "type": "module",
5
5
  "description": "A simple router for preact",
6
6
  "scripts": {
@@ -52,7 +52,7 @@
52
52
  "preact": "^10.27.0"
53
53
  },
54
54
  "devDependencies": {
55
- "@types/node": "^22.17.1",
55
+ "@types/node": "^22.17.2",
56
56
  "changelogen": "^0.6.2",
57
57
  "esbuild": "^0.25.9",
58
58
  "prettier": "^3.6.2",