@route-weaver/react
Version:
React hooks for @route-weaver/core
40 lines (36 loc) • 23.4 kB
JavaScript
import*as d from"react";import*as st from"react-dom";import*as c from"react";function te(){return te=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e},te.apply(this,arguments)}var L;(function(e){e.Pop="POP",e.Push="PUSH",e.Replace="REPLACE"})(L||(L={}));function m(e,t){if(e===!1||e===null||typeof e=="undefined")throw new Error(t)}function N(e,t){if(!e){typeof console!="undefined"&&console.warn(t);try{throw new Error(t)}catch(r){}}}function T(e){let{pathname:t="/",search:r="",hash:n=""}=e;return r&&r!=="?"&&(t+=r.charAt(0)==="?"?r:"?"+r),n&&n!=="#"&&(t+=n.charAt(0)==="#"?n:"#"+n),t}function U(e){let t={};if(e){let r=e.indexOf("#");r>=0&&(t.hash=e.substr(r),e=e.substr(0,r));let n=e.indexOf("?");n>=0&&(t.search=e.substr(n),e=e.substr(0,n)),e&&(t.pathname=e)}return t}var ye;(function(e){e.data="data",e.deferred="deferred",e.redirect="redirect",e.error="error"})(ye||(ye={}));function ge(e,t){let{route:r,pathname:n,params:a}=e;return{id:r.id,pathname:n,params:a,data:t[r.id],handle:r.handle}}function F(e,t){typeof e=="string"&&(e={path:e,caseSensitive:!1,end:!0});let[r,n]=je(e.path,e.caseSensitive,e.end),a=t.match(r);if(!a)return null;let o=a[0],i=o.replace(/(.)\/+$/,"$1"),l=a.slice(1);return{params:n.reduce((s,f,h)=>{let{paramName:p,isOptional:b}=f;if(p==="*"){let E=l[h]||"";i=o.slice(0,o.length-E.length).replace(/(.)\/+$/,"$1")}let R=l[h];return b&&!R?s[p]=void 0:s[p]=(R||"").replace(/%2F/g,"/"),s},{}),pathname:o,pathnameBase:i,pattern:e}}function je(e,t,r){t===void 0&&(t=!1),r===void 0&&(r=!0),N(e==="*"||!e.endsWith("*")||e.endsWith("/*"),'Route path "'+e+'" will be treated as if it were '+('"'+e.replace(/\*$/,"/*")+'" because the `*` character must ')+"always follow a `/` in the pattern. To get rid of this warning, "+('please change the route path to "'+e.replace(/\*$/,"/*")+'".'));let n=[],a="^"+e.replace(/\/*\*?$/,"").replace(/^\/*/,"/").replace(/[\\.*+^${}|()[\]]/g,"\\$&").replace(/\/:([\w-]+)(\?)?/g,(i,l,u)=>(n.push({paramName:l,isOptional:u!=null}),u?"/?([^\\/]+)?":"/([^\\/]+)"));return e.endsWith("*")?(n.push({paramName:"*"}),a+=e==="*"||e==="/*"?"(.*)$":"(?:\\/(.+)|\\/*)$"):r?a+="\\/*$":e!==""&&e!=="/"&&(a+="(?:(?=\\/|$))"),[new RegExp(a,t?void 0:"i"),n]}function D(e,t){if(t==="/")return e;if(!e.toLowerCase().startsWith(t.toLowerCase()))return null;let r=t.endsWith("/")?t.length-1:t.length,n=e.charAt(r);return n&&n!=="/"?null:e.slice(r)||"/"}function re(e,t){t===void 0&&(t="/");let{pathname:r,search:n="",hash:a=""}=typeof e=="string"?U(e):e;return{pathname:r?r.startsWith("/")?r:ke(r,t):t,search:Be(n),hash:ze(a)}}function ke(e,t){let r=t.replace(/\/+$/,"").split("/");return e.split("/").forEach(a=>{a===".."?r.length>1&&r.pop():a!=="."&&r.push(a)}),r.length>1?r.join("/"):"/"}function ee(e,t,r,n){return"Cannot include a '"+e+"' character in a manually specified "+("`to."+t+"` field ["+JSON.stringify(n)+"]. Please separate it out to the ")+("`to."+r+"` field. Alternatively you may provide the full path as ")+'a string in <Link to="..."> and the router will parse it for you.'}function He(e){return e.filter((t,r)=>r===0||t.route.path&&t.route.path.length>0)}function ne(e,t){let r=He(e);return t?r.map((n,a)=>a===r.length-1?n.pathname:n.pathnameBase):r.map(n=>n.pathnameBase)}function ae(e,t,r,n){n===void 0&&(n=!1);let a;typeof e=="string"?a=U(e):(a=te({},e),m(!a.pathname||!a.pathname.includes("?"),ee("?","pathname","search",a)),m(!a.pathname||!a.pathname.includes("#"),ee("#","pathname","hash",a)),m(!a.search||!a.search.includes("#"),ee("#","search","hash",a)));let o=e===""||a.pathname==="",i=o?"/":a.pathname,l;if(i==null)l=r;else{let h=t.length-1;if(!n&&i.startsWith("..")){let p=i.split("/");for(;p[0]==="..";)p.shift(),h-=1;a.pathname=p.join("/")}l=h>=0?t[h]:"/"}let u=re(a,l),s=i&&i!=="/"&&i.endsWith("/"),f=(o||i===".")&&r.endsWith("/");return!u.pathname.endsWith("/")&&(s||f)&&(u.pathname+="/"),u}var V=e=>e.join("/").replace(/\/\/+/g,"/");var Be=e=>!e||e==="?"?"":e.startsWith("?")?e:"?"+e,ze=e=>!e||e==="#"?"":e.startsWith("#")?e:"#"+e;var Re=["post","put","patch","delete"],Kt=new Set(Re),We=["get",...Re],Yt=new Set(We);var $t=Symbol("deferred");function K(){return K=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e},K.apply(this,arguments)}var M=c.createContext(null);process.env.NODE_ENV!=="production"&&(M.displayName="DataRouter");var A=c.createContext(null);process.env.NODE_ENV!=="production"&&(A.displayName="DataRouterState");var Ze=c.createContext(null);process.env.NODE_ENV!=="production"&&(Ze.displayName="Await");var w=c.createContext(null);process.env.NODE_ENV!=="production"&&(w.displayName="Navigation");var j=c.createContext(null);process.env.NODE_ENV!=="production"&&(j.displayName="Location");var P=c.createContext({outlet:null,matches:[],isDataRoute:!1});process.env.NODE_ENV!=="production"&&(P.displayName="Route");var qe=c.createContext(null);process.env.NODE_ENV!=="production"&&(qe.displayName="RouteError");function oe(e,t){let{relative:r}=t===void 0?{}:t;k()||(process.env.NODE_ENV!=="production"?m(!1,"useHref() may be used only in the context of a <Router> component."):m(!1));let{basename:n,navigator:a}=c.useContext(w),{hash:o,pathname:i,search:l}=C(e,{relative:r}),u=i;return n!=="/"&&(u=i==="/"?n:V([n,i])),a.createHref({pathname:u,search:l,hash:o})}function k(){return c.useContext(j)!=null}function S(){return k()||(process.env.NODE_ENV!=="production"?m(!1,"useLocation() may be used only in the context of a <Router> component."):m(!1)),c.useContext(j).location}var De="You should call navigate() in a React.useEffect(), not when your component is first rendered.";function xe(e){c.useContext(w).static||c.useLayoutEffect(e)}function ie(){let{isDataRoute:e}=c.useContext(P);return e?nt():et()}function et(){k()||(process.env.NODE_ENV!=="production"?m(!1,"useNavigate() may be used only in the context of a <Router> component."):m(!1));let e=c.useContext(M),{basename:t,future:r,navigator:n}=c.useContext(w),{matches:a}=c.useContext(P),{pathname:o}=S(),i=JSON.stringify(ne(a,r.v7_relativeSplatPath)),l=c.useRef(!1);return xe(()=>{l.current=!0}),c.useCallback(function(s,f){if(f===void 0&&(f={}),process.env.NODE_ENV!=="production"&&N(l.current,De),!l.current)return;if(typeof s=="number"){n.go(s);return}let h=ae(s,JSON.parse(i),o,f.relative==="path");e==null&&t!=="/"&&(h.pathname=h.pathname==="/"?t:V([t,h.pathname])),(f.replace?n.replace:n.push)(h,f.state,f)},[t,n,i,o,e])}function C(e,t){let{relative:r}=t===void 0?{}:t,{future:n}=c.useContext(w),{matches:a}=c.useContext(P),{pathname:o}=S(),i=JSON.stringify(ne(a,n.v7_relativeSplatPath));return c.useMemo(()=>ae(e,JSON.parse(i),o,r==="path"),[e,i,o,r])}var Ne=function(e){return e.UseBlocker="useBlocker",e.UseRevalidator="useRevalidator",e.UseNavigateStable="useNavigate",e}(Ne||{}),H=function(e){return e.UseBlocker="useBlocker",e.UseLoaderData="useLoaderData",e.UseActionData="useActionData",e.UseRouteError="useRouteError",e.UseNavigation="useNavigation",e.UseRouteLoaderData="useRouteLoaderData",e.UseMatches="useMatches",e.UseRevalidator="useRevalidator",e.UseNavigateStable="useNavigate",e.UseRouteId="useRouteId",e}(H||{});function le(e){return e+" must be used within a data router. See https://reactrouter.com/v6/routers/picking-a-router."}function tt(e){let t=c.useContext(M);return t||(process.env.NODE_ENV!=="production"?m(!1,le(e)):m(!1)),t}function Pe(e){let t=c.useContext(A);return t||(process.env.NODE_ENV!=="production"?m(!1,le(e)):m(!1)),t}function rt(e){let t=c.useContext(P);return t||(process.env.NODE_ENV!=="production"?m(!1,le(e)):m(!1)),t}function Ce(e){let t=rt(e),r=t.matches[t.matches.length-1];return r.route.id||(process.env.NODE_ENV!=="production"?m(!1,e+' can only be used on routes that contain a unique "id"'):m(!1)),r.route.id}function se(){return Ce(H.UseRouteId)}function ue(){return Pe(H.UseNavigation).navigation}function ce(){let{matches:e,loaderData:t}=Pe(H.UseMatches);return c.useMemo(()=>e.map(r=>ge(r,t)),[e,t])}function nt(){let{router:e}=tt(Ne.UseNavigateStable),t=Ce(H.UseNavigateStable),r=c.useRef(!1);return xe(()=>{r.current=!0}),c.useCallback(function(a,o){o===void 0&&(o={}),process.env.NODE_ENV!=="production"&&N(r.current,De),r.current&&(typeof a=="number"?e.navigate(a):e.navigate(a,K({fromRouteId:t},o)))},[e,t])}var Ee={};function at(e,t){process.env.NODE_ENV!=="production"&&!Ee[t]&&(Ee[t]=!0,console.warn(t))}var O=(e,t,r)=>at(e,"\u26A0\uFE0F React Router Future Flag Warning: "+t+". "+("You can use the `"+e+"` future flag to opt-in early. ")+("For more information, see "+r+"."));function _e(e,t){(e==null?void 0:e.v7_startTransition)===void 0&&O("v7_startTransition","React Router will begin wrapping state updates in `React.startTransition` in v7","https://reactrouter.com/v6/upgrading/future#v7_starttransition"),(e==null?void 0:e.v7_relativeSplatPath)===void 0&&(!t||t.v7_relativeSplatPath===void 0)&&O("v7_relativeSplatPath","Relative route resolution within Splat routes is changing in v7","https://reactrouter.com/v6/upgrading/future#v7_relativesplatpath"),t&&(t.v7_fetcherPersist===void 0&&O("v7_fetcherPersist","The persistence behavior of fetchers is changing in v7","https://reactrouter.com/v6/upgrading/future#v7_fetcherpersist"),t.v7_normalizeFormMethod===void 0&&O("v7_normalizeFormMethod","Casing of `formMethod` fields is being normalized to uppercase in v7","https://reactrouter.com/v6/upgrading/future#v7_normalizeformmethod"),t.v7_partialHydration===void 0&&O("v7_partialHydration","`RouterProvider` hydration behavior is changing in v7","https://reactrouter.com/v6/upgrading/future#v7_partialhydration"),t.v7_skipActionErrorRevalidation===void 0&&O("v7_skipActionErrorRevalidation","The revalidation behavior after 4xx/5xx `action` responses is changing in v7","https://reactrouter.com/v6/upgrading/future#v7_skipactionerrorrevalidation"))}var ot="startTransition",er=c[ot];function de(e){let{basename:t="/",children:r=null,location:n,navigationType:a=L.Pop,navigator:o,static:i=!1,future:l}=e;k()&&(process.env.NODE_ENV!=="production"?m(!1,"You cannot render a <Router> inside another <Router>. You should never have more than one in your app."):m(!1));let u=t.replace(/^\/*/,"/"),s=c.useMemo(()=>({basename:u,navigator:o,static:i,future:K({v7_relativeSplatPath:!1},l)}),[u,l,o,i]);typeof n=="string"&&(n=U(n));let{pathname:f="/",search:h="",hash:p="",state:b=null,key:R="default"}=n,E=c.useMemo(()=>{let x=D(f,u);return x==null?null:{location:{pathname:x,search:h,hash:p,state:b,key:R},navigationType:a}},[u,f,h,p,b,R,a]);return process.env.NODE_ENV!=="production"&&N(E!=null,'<Router basename="'+u+'"> is not able to match the URL '+('"'+f+h+p+'" because it does not start with the ')+"basename, so the <Router> won't render anything."),E==null?null:c.createElement(w.Provider,{value:s},c.createElement(j.Provider,{children:r,value:E}))}var tr=new Promise(()=>{});function _(){return _=Object.assign?Object.assign.bind():function(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)Object.prototype.hasOwnProperty.call(r,n)&&(e[n]=r[n])}return e},_.apply(this,arguments)}function pe(e,t){if(e==null)return{};var r={},n=Object.keys(e),a,o;for(o=0;o<n.length;o++)a=n[o],!(t.indexOf(a)>=0)&&(r[a]=e[a]);return r}var J="get",X="application/x-www-form-urlencoded";function G(e){return e!=null&&typeof e.tagName=="string"}function ut(e){return G(e)&&e.tagName.toLowerCase()==="button"}function ct(e){return G(e)&&e.tagName.toLowerCase()==="form"}function dt(e){return G(e)&&e.tagName.toLowerCase()==="input"}function ft(e){return!!(e.metaKey||e.altKey||e.ctrlKey||e.shiftKey)}function ht(e,t){return e.button===0&&(!t||t==="_self")&&!ft(e)}var Y=null;function pt(){if(Y===null)try{new FormData(document.createElement("form"),0),Y=!1}catch(e){Y=!0}return Y}var mt=new Set(["application/x-www-form-urlencoded","multipart/form-data","text/plain"]);function fe(e){return e!=null&&!mt.has(e)?(process.env.NODE_ENV!=="production"&&N(!1,'"'+e+'" is not a valid `encType` for `<Form>`/`<fetcher.Form>` '+('and will default to "'+X+'"')),null):e}function vt(e,t){let r,n,a,o,i;if(ct(e)){let l=e.getAttribute("action");n=l?D(l,t):null,r=e.getAttribute("method")||J,a=fe(e.getAttribute("enctype"))||X,o=new FormData(e)}else if(ut(e)||dt(e)&&(e.type==="submit"||e.type==="image")){let l=e.form;if(l==null)throw new Error('Cannot submit a <button> or <input type="submit"> without a <form>');let u=e.getAttribute("formaction")||l.getAttribute("action");if(n=u?D(u,t):null,r=e.getAttribute("formmethod")||l.getAttribute("method")||J,a=fe(e.getAttribute("formenctype"))||fe(l.getAttribute("enctype"))||X,o=new FormData(l,e),!pt()){let{name:s,type:f,value:h}=e;if(f==="image"){let p=s?s+".":"";o.append(p+"x","0"),o.append(p+"y","0")}else s&&o.append(s,h)}}else{if(G(e))throw new Error('Cannot submit element that is not <form>, <button>, or <input type="submit|image">');r=J,n=null,a=X,i=e}return o&&a==="text/plain"&&(i=o,o=void 0),{action:n,method:r.toLowerCase(),encType:a,formData:o,body:i}}var yt=["onClick","relative","reloadDocument","replace","state","target","to","preventScrollReset","viewTransition"],gt=["aria-current","caseSensitive","className","end","style","to","viewTransition","children"],Rt=["fetcherKey","navigate","reloadDocument","replace","state","method","action","onSubmit","relative","preventScrollReset","viewTransition"],Et="6";try{window.__reactRouterVersion=Et}catch(e){}var Ue=d.createContext({isTransitioning:!1});process.env.NODE_ENV!=="production"&&(Ue.displayName="ViewTransition");var wt=d.createContext(new Map);process.env.NODE_ENV!=="production"&&(wt.displayName="Fetchers");var bt="startTransition",Le=d[bt],St="flushSync",Fr=st[St],Dt="useId",Or=d[Dt];function xt(e){let{basename:t,children:r,future:n,history:a}=e,[o,i]=d.useState({action:a.action,location:a.location}),{v7_startTransition:l}=n||{},u=d.useCallback(s=>{l&&Le?Le(()=>i(s)):i(s)},[i,l]);return d.useLayoutEffect(()=>a.listen(u),[a,u]),d.useEffect(()=>_e(n),[n]),d.createElement(de,{basename:t,children:r,location:o.location,navigationType:o.action,navigator:a,future:n})}process.env.NODE_ENV!=="production"&&(xt.displayName="unstable_HistoryRouter");var Nt=typeof window!="undefined"&&typeof window.document!="undefined"&&typeof window.document.createElement!="undefined",Pt=/^(?:[a-z][a-z0-9+.-]*:|\/\/)/i,Fe=d.forwardRef(function(t,r){let{onClick:n,relative:a,reloadDocument:o,replace:i,state:l,target:u,to:s,preventScrollReset:f,viewTransition:h}=t,p=pe(t,yt),{basename:b}=d.useContext(w),R,E=!1;if(typeof s=="string"&&Pt.test(s)&&(R=s,Nt))try{let g=new URL(window.location.href),y=s.startsWith("//")?new URL(g.protocol+s):new URL(s),W=D(y.pathname,b);y.origin===g.origin&&W!=null?s=W+y.search+y.hash:E=!0}catch(g){process.env.NODE_ENV!=="production"&&N(!1,'<Link to="'+s+'"> contains an invalid URL which will probably break when clicked - please update to a valid URL path.')}let x=oe(s,{relative:a}),I=Ut(s,{replace:i,state:l,target:u,preventScrollReset:f,relative:a,viewTransition:h});function v(g){n&&n(g),g.defaultPrevented||I(g)}return d.createElement("a",_({},p,{href:R||x,onClick:E||o?n:v,ref:r,target:u}))});process.env.NODE_ENV!=="production"&&(Fe.displayName="Link");var Ct=d.forwardRef(function(t,r){let{"aria-current":n="page",caseSensitive:a=!1,className:o="",end:i=!1,style:l,to:u,viewTransition:s,children:f}=t,h=pe(t,gt),p=C(u,{relative:h.relative}),b=S(),R=d.useContext(A),{navigator:E,basename:x}=d.useContext(w),I=R!=null&&kt(p)&&s===!0,v=E.encodeLocation?E.encodeLocation(p).pathname:p.pathname,g=b.pathname,y=R&&R.navigation&&R.navigation.location?R.navigation.location.pathname:null;a||(g=g.toLowerCase(),y=y?y.toLowerCase():null,v=v.toLowerCase()),y&&x&&(y=D(y,x)||y);let W=v!=="/"&&v.endsWith("/")?v.length-1:v.length,Q=g===v||!i&&g.startsWith(v)&&g.charAt(W)==="/",ve=y!=null&&(y===v||!i&&y.startsWith(v)&&y.charAt(v.length)==="/"),Z={isActive:Q,isPending:ve,isTransitioning:I},Ae=Q?n:void 0,q;typeof o=="function"?q=o(Z):q=[o,Q?"active":null,ve?"pending":null,I?"transitioning":null].filter(Boolean).join(" ");let Ie=typeof l=="function"?l(Z):l;return d.createElement(Fe,_({},h,{"aria-current":Ae,className:q,ref:r,style:Ie,to:u,viewTransition:s}),typeof f=="function"?f(Z):f)});process.env.NODE_ENV!=="production"&&(Ct.displayName="NavLink");var _t=d.forwardRef((e,t)=>{let{fetcherKey:r,navigate:n,reloadDocument:a,replace:o,state:i,method:l=J,action:u,onSubmit:s,relative:f,preventScrollReset:h,viewTransition:p}=e,b=pe(e,Rt),R=At(),E=It(u,{relative:f}),x=l.toLowerCase()==="get"?"get":"post";return d.createElement("form",_({ref:t,method:x,action:E,onSubmit:a?s:v=>{if(s&&s(v),v.defaultPrevented)return;v.preventDefault();let g=v.nativeEvent.submitter,y=(g==null?void 0:g.getAttribute("formmethod"))||l;R(g||v.currentTarget,{fetcherKey:r,method:y,navigate:n,replace:o,state:i,relative:f,preventScrollReset:h,viewTransition:p})}},b))});process.env.NODE_ENV!=="production"&&(_t.displayName="Form");function Lt(e){let{getKey:t,storageKey:r}=e;return Vt({getKey:t,storageKey:r}),null}process.env.NODE_ENV!=="production"&&(Lt.displayName="ScrollRestoration");var B;(function(e){e.UseScrollRestoration="useScrollRestoration",e.UseSubmit="useSubmit",e.UseSubmitFetcher="useSubmitFetcher",e.UseFetcher="useFetcher",e.useViewTransitionState="useViewTransitionState"})(B||(B={}));var he;(function(e){e.UseFetcher="useFetcher",e.UseFetchers="useFetchers",e.UseScrollRestoration="useScrollRestoration"})(he||(he={}));function Oe(e){return e+" must be used within a data router. See https://reactrouter.com/v6/routers/picking-a-router."}function me(e){let t=d.useContext(M);return t||(process.env.NODE_ENV!=="production"?m(!1,Oe(e)):m(!1)),t}function Tt(e){let t=d.useContext(A);return t||(process.env.NODE_ENV!=="production"?m(!1,Oe(e)):m(!1)),t}function Ut(e,t){let{target:r,replace:n,state:a,preventScrollReset:o,relative:i,viewTransition:l}=t===void 0?{}:t,u=ie(),s=S(),f=C(e,{relative:i});return d.useCallback(h=>{if(ht(h,r)){h.preventDefault();let p=n!==void 0?n:T(s)===T(f);u(e,{replace:p,state:a,preventScrollReset:o,relative:i,viewTransition:l})}},[s,u,f,n,a,r,e,o,i,l])}function Ft(){if(typeof document=="undefined")throw new Error("You are calling submit during the server render. Try calling submit within a `useEffect` or callback instead.")}var Ot=0,Mt=()=>"__"+String(++Ot)+"__";function At(){let{router:e}=me(B.UseSubmit),{basename:t}=d.useContext(w),r=se();return d.useCallback(function(n,a){a===void 0&&(a={}),Ft();let{action:o,method:i,encType:l,formData:u,body:s}=vt(n,t);if(a.navigate===!1){let f=a.fetcherKey||Mt();e.fetch(f,r,a.action||o,{preventScrollReset:a.preventScrollReset,formData:u,body:s,formMethod:a.method||i,formEncType:a.encType||l,flushSync:a.flushSync})}else e.navigate(a.action||o,{preventScrollReset:a.preventScrollReset,formData:u,body:s,formMethod:a.method||i,formEncType:a.encType||l,replace:a.replace,state:a.state,fromRouteId:r,flushSync:a.flushSync,viewTransition:a.viewTransition})},[e,t,r])}function It(e,t){let{relative:r}=t===void 0?{}:t,{basename:n}=d.useContext(w),a=d.useContext(P);a||(process.env.NODE_ENV!=="production"?m(!1,"useFormAction must be used inside a RouteContext"):m(!1));let[o]=a.matches.slice(-1),i=_({},C(e||".",{relative:r})),l=S();if(e==null){i.search=l.search;let u=new URLSearchParams(i.search),s=u.getAll("index");if(s.some(h=>h==="")){u.delete("index"),s.filter(p=>p).forEach(p=>u.append("index",p));let h=u.toString();i.search=h?"?"+h:""}}return(!e||e===".")&&o.route.index&&(i.search=i.search?i.search.replace(/^\?/,"?index&"):"?index"),n!=="/"&&(i.pathname=i.pathname==="/"?n:V([n,i.pathname])),T(i)}var Te="react-router-scroll-positions",$={};function Vt(e){let{getKey:t,storageKey:r}=e===void 0?{}:e,{router:n}=me(B.UseScrollRestoration),{restoreScrollPosition:a,preventScrollReset:o}=Tt(he.UseScrollRestoration),{basename:i}=d.useContext(w),l=S(),u=ce(),s=ue();d.useEffect(()=>(window.history.scrollRestoration="manual",()=>{window.history.scrollRestoration="auto"}),[]),jt(d.useCallback(()=>{if(s.state==="idle"){let f=(t?t(l,u):null)||l.key;$[f]=window.scrollY}try{sessionStorage.setItem(r||Te,JSON.stringify($))}catch(f){process.env.NODE_ENV!=="production"&&N(!1,"Failed to save scroll positions in sessionStorage, <ScrollRestoration /> will not work properly ("+f+").")}window.history.scrollRestoration="auto"},[r,t,s.state,l,u])),typeof document!="undefined"&&(d.useLayoutEffect(()=>{try{let f=sessionStorage.getItem(r||Te);f&&($=JSON.parse(f))}catch(f){}},[r]),d.useLayoutEffect(()=>{let f=t&&i!=="/"?(p,b)=>t(_({},p,{pathname:D(p.pathname,i)||p.pathname}),b):t,h=n==null?void 0:n.enableScrollRestoration($,()=>window.scrollY,f);return()=>h&&h()},[n,i,t]),d.useLayoutEffect(()=>{if(a!==!1){if(typeof a=="number"){window.scrollTo(0,a);return}if(l.hash){let f=document.getElementById(decodeURIComponent(l.hash.slice(1)));if(f){f.scrollIntoView();return}}o!==!0&&window.scrollTo(0,0)}},[l,a,o]))}function jt(e,t){let{capture:r}=t||{};d.useEffect(()=>{let n=r!=null?{capture:r}:void 0;return window.addEventListener("pagehide",e,n),()=>{window.removeEventListener("pagehide",e,n)}},[e,r])}function kt(e,t){t===void 0&&(t={});let r=d.useContext(Ue);r==null&&(process.env.NODE_ENV!=="production"?m(!1,"`useViewTransitionState` must be used within `react-router-dom`'s `RouterProvider`. Did you accidentally import `RouterProvider` from `react-router`?"):m(!1));let{basename:n}=me(B.useViewTransitionState),a=C(e,{relative:t.relative});if(!r.isTransitioning)return!1;let o=D(r.currentLocation.pathname,n)||r.currentLocation.pathname,i=D(r.nextLocation.pathname,n)||r.nextLocation.pathname;return F(a.pathname,i)!=null||F(a.pathname,o)!=null}import{createContext as Ht,useContext as Bt}from"react";import{jsx as zt}from"react/jsx-runtime";var Me=Ht(void 0),kr=({navInstance:e,children:t})=>zt(Me.Provider,{value:{navInstance:e},children:t}),z=()=>{let e=Bt(Me);if(e===void 0)throw new Error("useRouteWeaver must be used within a RouteWeaverProvider");return e.navInstance};var Kr=()=>z().navigation,Yr=()=>{let e=z(),{pathname:t}=S();return e.getActiveRoute(t)},$r=()=>{let e=z(),{pathname:t}=S();return e.getBreadcrumbs(t)},Jr=()=>z().buildPath;export{kr as RouteWeaverProvider,Yr as useActiveRoute,$r as useBreadcrumbs,Jr as useBuildPath,Kr as useNavigation,z as useRouteWeaver};
/*! Bundled license information:
@remix-run/router/dist/router.js:
(**
* @remix-run/router v1.23.0
*
* Copyright (c) Remix Software Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE.md file in the root directory of this source tree.
*
* @license MIT
*)
react-router/dist/index.js:
(**
* React Router v6.30.1
*
* Copyright (c) Remix Software Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE.md file in the root directory of this source tree.
*
* @license MIT
*)
react-router-dom/dist/index.js:
(**
* React Router DOM v6.30.1
*
* Copyright (c) Remix Software Inc.
*
* This source code is licensed under the MIT license found in the
* LICENSE.md file in the root directory of this source tree.
*
* @license MIT
*)
*/