preact-hashish-router 0.1.9 → 0.1.11
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 +18 -2
- package/dist/context.d.ts +2 -2
- package/dist/index.js +1 -1
- package/package.json +1 -1
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
|
-
|
|
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
|
|
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};
|