preact-hashish-router 0.1.9 → 0.1.10

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,6 +1,9 @@
1
1
  import type { PropsWithChildren } from "preact/compat";
2
2
  export type RouterProps = PropsWithChildren<{
3
- type: "hash" | "browser";
3
+ /**
4
+ * @default browser
5
+ */
6
+ type?: "hash" | "browser";
4
7
  /**
5
8
  * Only for `hash` routers.
6
9
  *
@@ -11,5 +14,18 @@ export type RouterProps = PropsWithChildren<{
11
14
  * @deprecated
12
15
  */
13
16
  redirect_path_to_hash?: boolean;
17
+ /**
18
+ * Trigger hook before the next route calcultation begin
19
+ */
20
+ onBeforeRouteChange?: () => Promise<void> | void;
21
+ /**
22
+ * Trigger hook when the next route calcultation finish
23
+ */
24
+ onRouteDidChange?: () => Promise<void> | void;
25
+ /**
26
+ * If true don't trigger hooks on first render
27
+ * @default false
28
+ */
29
+ ignoreInitial?: boolean;
14
30
  }>;
15
- export declare const Router: (props: RouterProps) => import("preact").JSX.Element;
31
+ export declare const Router: ({ type: router_type, ignoreInitial, ...props }: RouterProps) => import("preact").JSX.Element;
package/dist/context.d.ts CHANGED
@@ -6,7 +6,7 @@ export type HashisherContextVal = {
6
6
  searchParams: URLSearchParams;
7
7
  };
8
8
  export type HashisherContextMethods = {
9
- go(newPath: string): void;
9
+ go(newPath: string): Promise<void>;
10
10
  };
11
11
  export declare const HashisherContext: import("preact").Context<HashisherContextVal & HashisherContextMethods>;
12
12
  export declare const useHashisherContext: () => HashisherContextVal & HashisherContextMethods;
@@ -22,5 +22,5 @@ export declare const useRouter: () => {
22
22
  path: string | null;
23
23
  params: Record<string, string> | undefined;
24
24
  searchParams: URLSearchParams;
25
- go: (newPath: string) => void;
25
+ go: (newPath: string) => Promise<void>;
26
26
  };
package/dist/index.js CHANGED
@@ -1 +1 @@
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
+ import{forwardRef as A}from"preact/compat";import{createContext as k}from"preact";import{useContext as c}from"preact/hooks";var s=k({active_path:"",active_route_data:null,params:void 0,searchParams:new URLSearchParams,async go(){}}),p=()=>{let e=c(s);if(!e)throw new Error("useHashisherContext should be inside a HashisherContext provider");return e};function E(){let e=c(s);if(!e)throw new Error("useParams should be inside a HashisherContext provider");return e.params}function D(){let e=c(s);if(!e)throw new Error("useSearchParams should be inside a HashisherContext provider");return e.searchParams}var U=()=>{let e=c(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 B}from"preact/jsx-runtime";var z=A(({href:e,...t},r)=>{let{go:n}=p();if(!e)throw new Error("A: href must be defined");return B("a",{ref:r,href:e,onClick:o=>{o&&o.type==="click"&&(o.ctrlKey||o.metaKey||o.altKey||o.shiftKey||o.button!==0)||(o.preventDefault(),n(e))},...t})});import{Fragment as K,jsx as _}from"preact/jsx-runtime";var W=e=>(F(e.element),_(K,{})),R=_("div",{children:"404 Not Found"}),F=e=>{R=e},l=()=>R;import{addRoute as T,createRouter as O}from"rou3";var f=O(),P=(e,t)=>{T(f,void 0,e,{component:t.element,fallback:t.fallback||null,lazy:!!t.lazy})};import{Fragment as G,jsx as J}from"preact/jsx-runtime";function q(e){return P(e.path,e),J(G,{})}import{useCallback as Y,useLayoutEffect as w,useRef as Z,useState as u}from"preact/hooks";import{findRoute as $}from"rou3";import{parseURL as C}from"ufo";import{Suspense as Q}from"preact/compat";import{jsx as X}from"preact/jsx-runtime";var y=()=>{let{active_route_data:e}=p();return e?e.component===null?l():e.lazy?X(Q,{fallback:e.fallback,children:e.component}):e.component:l()};import{jsx as ee,jsxs as te}from"preact/jsx-runtime";var I=({type:e="browser",ignoreInitial:t=!1,...r})=>{let[n,o]=u("/"),[g,v]=u(void 0),[b,H]=u(new URLSearchParams),[V,M]=u(null),h=Z(0),i=Y(async a=>{console.log({ignoreInitial:t,renderCount:h}),t===!0?h.current!==0&&await r.onBeforeRouteChange?.():await r.onBeforeRouteChange?.();let{params:m,path:d,route_data:S,search_params:L}=await j(a,e);e==="browser"&&window.history.pushState(null,"",d),e==="hash"&&(window.location.hash=d),o(d),v(m),H(L),M(S),t===!0?h.current!==0&&await r.onRouteDidChange?.():await r.onRouteDidChange?.(),h.current+=1},[e,r.onBeforeRouteChange,r.onRouteDidChange]);w(()=>{if(e!=="browser")return;let a=()=>{i(null)};return window.addEventListener("popstate",a),a(),()=>{window.removeEventListener("popstate",a)}},[i,e]),w(()=>{if(e!=="hash")return;let a=()=>{i(null)};return window.addEventListener("hashchange",a),a(),()=>{window.removeEventListener("hashchange",a)}},[i,e]);let N=async a=>{let m=C(a).pathname;i(m)};return te(s.Provider,{value:{active_path:n,searchParams:b,params:g,active_route_data:V,go:N},children:[r.children,ee(y,{})]})},j=async(e,t="browser")=>{let r=C(window.location.href),n=e||"";e===null&&(t==="hash"?n=r.hash:n=r.pathname);let o=$(f,void 0,n);return o?{path:n,search_params:new URLSearchParams(r.search),route_data:{...o.data},params:{...o.params}}:{path:n,search_params:new URLSearchParams(r.search),route_data:null,params:void 0}};import{Component as re}from"preact";import{jsxs as oe}from"preact/jsx-runtime";var x=class extends re{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:oe("p",{children:["Oh no! We ran into an error: ",this.state.error]}):this.props.children}};export{z as A,W as NotFound,q as Route,I as Router,x as RouterErrorBoundary,E as useParams,U as useRouter,D as useSearchParams};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "preact-hashish-router",
3
- "version": "0.1.9",
3
+ "version": "0.1.10",
4
4
  "type": "module",
5
5
  "description": "A simple router for preact",
6
6
  "scripts": {