UNPKG

@route-weaver/react

Version:
40 lines (36 loc) 23.3 kB
import{createContext as Vt,useContext as kt}from"react";import*as d from"react";import*as it from"react-dom";import*as c from"react";function ee(){return ee=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},ee.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 x(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 F(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 ve;(function(e){e.data="data",e.deferred="deferred",e.redirect="redirect",e.error="error"})(ve||(ve={}));function ye(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 O(e,t){typeof e=="string"&&(e={path:e,caseSensitive:!1,end:!0});let[r,n]=Ie(e.path,e.caseSensitive,e.end),a=t.match(r);if(!a)return null;let o=a[0],i=o.replace(/(.)\/+$/,"$1"),s=a.slice(1);return{params:n.reduce((u,f,h)=>{let{paramName:p,isOptional:w}=f;if(p==="*"){let E=s[h]||"";i=o.slice(0,o.length-E.length).replace(/(.)\/+$/,"$1")}let R=s[h];return w&&!R?u[p]=void 0:u[p]=(R||"").replace(/%2F/g,"/"),u},{}),pathname:o,pathnameBase:i,pattern:e}}function Ie(e,t,r){t===void 0&&(t=!1),r===void 0&&(r=!0),x(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,s,l)=>(n.push({paramName:s,isOptional:l!=null}),l?"/?([^\\/]+)?":"/([^\\/]+)"));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 te(e,t){t===void 0&&(t="/");let{pathname:r,search:n="",hash:a=""}=typeof e=="string"?F(e):e;return{pathname:r?r.startsWith("/")?r:je(r,t):t,search:ke(n),hash:He(a)}}function je(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 q(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 Ve(e){return e.filter((t,r)=>r===0||t.route.path&&t.route.path.length>0)}function re(e,t){let r=Ve(e);return t?r.map((n,a)=>a===r.length-1?n.pathname:n.pathnameBase):r.map(n=>n.pathnameBase)}function ne(e,t,r,n){n===void 0&&(n=!1);let a;typeof e=="string"?a=F(e):(a=ee({},e),m(!a.pathname||!a.pathname.includes("?"),q("?","pathname","search",a)),m(!a.pathname||!a.pathname.includes("#"),q("#","pathname","hash",a)),m(!a.search||!a.search.includes("#"),q("#","search","hash",a)));let o=e===""||a.pathname==="",i=o?"/":a.pathname,s;if(i==null)s=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("/")}s=h>=0?t[h]:"/"}let l=te(a,s),u=i&&i!=="/"&&i.endsWith("/"),f=(o||i===".")&&r.endsWith("/");return!l.pathname.endsWith("/")&&(u||f)&&(l.pathname+="/"),l}var j=e=>e.join("/").replace(/\/\/+/g,"/");var ke=e=>!e||e==="?"?"":e.startsWith("?")?e:"?"+e,He=e=>!e||e==="#"?"":e.startsWith("#")?e:"#"+e;var ge=["post","put","patch","delete"],Wt=new Set(ge),Be=["get",...ge],Kt=new Set(Be);var Yt=Symbol("deferred");function W(){return W=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},W.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 Ge=c.createContext(null);process.env.NODE_ENV!=="production"&&(Ge.displayName="Await");var b=c.createContext(null);process.env.NODE_ENV!=="production"&&(b.displayName="Navigation");var V=c.createContext(null);process.env.NODE_ENV!=="production"&&(V.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 ae(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(b),{hash:o,pathname:i,search:s}=C(e,{relative:r}),l=i;return n!=="/"&&(l=i==="/"?n:j([n,i])),a.createHref({pathname:l,search:s,hash:o})}function k(){return c.useContext(V)!=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(V).location}var Se="You should call navigate() in a React.useEffect(), not when your component is first rendered.";function De(e){c.useContext(b).static||c.useLayoutEffect(e)}function oe(){let{isDataRoute:e}=c.useContext(P);return e?tt():Ze()}function Ze(){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(b),{matches:a}=c.useContext(P),{pathname:o}=S(),i=JSON.stringify(re(a,r.v7_relativeSplatPath)),s=c.useRef(!1);return De(()=>{s.current=!0}),c.useCallback(function(u,f){if(f===void 0&&(f={}),process.env.NODE_ENV!=="production"&&x(s.current,Se),!s.current)return;if(typeof u=="number"){n.go(u);return}let h=ne(u,JSON.parse(i),o,f.relative==="path");e==null&&t!=="/"&&(h.pathname=h.pathname==="/"?t:j([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(b),{matches:a}=c.useContext(P),{pathname:o}=S(),i=JSON.stringify(re(a,n.v7_relativeSplatPath));return c.useMemo(()=>ne(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 ie(e){return e+" must be used within a data router. See https://reactrouter.com/v6/routers/picking-a-router."}function qe(e){let t=c.useContext(M);return t||(process.env.NODE_ENV!=="production"?m(!1,ie(e)):m(!1)),t}function xe(e){let t=c.useContext(A);return t||(process.env.NODE_ENV!=="production"?m(!1,ie(e)):m(!1)),t}function et(e){let t=c.useContext(P);return t||(process.env.NODE_ENV!=="production"?m(!1,ie(e)):m(!1)),t}function Pe(e){let t=et(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 le(){return Pe(H.UseRouteId)}function se(){return xe(H.UseNavigation).navigation}function ue(){let{matches:e,loaderData:t}=xe(H.UseMatches);return c.useMemo(()=>e.map(r=>ye(r,t)),[e,t])}function tt(){let{router:e}=qe(Ne.UseNavigateStable),t=Pe(H.UseNavigateStable),r=c.useRef(!1);return De(()=>{r.current=!0}),c.useCallback(function(a,o){o===void 0&&(o={}),process.env.NODE_ENV!=="production"&&x(r.current,Se),r.current&&(typeof a=="number"?e.navigate(a):e.navigate(a,W({fromRouteId:t},o)))},[e,t])}var Re={};function rt(e,t){process.env.NODE_ENV!=="production"&&!Re[t]&&(Re[t]=!0,console.warn(t))}var U=(e,t,r)=>rt(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 Ce(e,t){(e==null?void 0:e.v7_startTransition)===void 0&&U("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)&&U("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&&U("v7_fetcherPersist","The persistence behavior of fetchers is changing in v7","https://reactrouter.com/v6/upgrading/future#v7_fetcherpersist"),t.v7_normalizeFormMethod===void 0&&U("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&&U("v7_partialHydration","`RouterProvider` hydration behavior is changing in v7","https://reactrouter.com/v6/upgrading/future#v7_partialhydration"),t.v7_skipActionErrorRevalidation===void 0&&U("v7_skipActionErrorRevalidation","The revalidation behavior after 4xx/5xx `action` responses is changing in v7","https://reactrouter.com/v6/upgrading/future#v7_skipactionerrorrevalidation"))}var nt="startTransition",qt=c[nt];function ce(e){let{basename:t="/",children:r=null,location:n,navigationType:a=L.Pop,navigator:o,static:i=!1,future:s}=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 l=t.replace(/^\/*/,"/"),u=c.useMemo(()=>({basename:l,navigator:o,static:i,future:W({v7_relativeSplatPath:!1},s)}),[l,s,o,i]);typeof n=="string"&&(n=F(n));let{pathname:f="/",search:h="",hash:p="",state:w=null,key:R="default"}=n,E=c.useMemo(()=>{let N=D(f,l);return N==null?null:{location:{pathname:N,search:h,hash:p,state:w,key:R},navigationType:a}},[l,f,h,p,w,R,a]);return process.env.NODE_ENV!=="production"&&x(E!=null,'<Router basename="'+l+'"> 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(b.Provider,{value:u},c.createElement(V.Provider,{children:r,value:E}))}var er=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 he(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 $="get",J="application/x-www-form-urlencoded";function X(e){return e!=null&&typeof e.tagName=="string"}function lt(e){return X(e)&&e.tagName.toLowerCase()==="button"}function st(e){return X(e)&&e.tagName.toLowerCase()==="form"}function ut(e){return X(e)&&e.tagName.toLowerCase()==="input"}function ct(e){return!!(e.metaKey||e.altKey||e.ctrlKey||e.shiftKey)}function dt(e,t){return e.button===0&&(!t||t==="_self")&&!ct(e)}var K=null;function ft(){if(K===null)try{new FormData(document.createElement("form"),0),K=!1}catch(e){K=!0}return K}var ht=new Set(["application/x-www-form-urlencoded","multipart/form-data","text/plain"]);function de(e){return e!=null&&!ht.has(e)?(process.env.NODE_ENV!=="production"&&x(!1,'"'+e+'" is not a valid `encType` for `<Form>`/`<fetcher.Form>` '+('and will default to "'+J+'"')),null):e}function pt(e,t){let r,n,a,o,i;if(st(e)){let s=e.getAttribute("action");n=s?D(s,t):null,r=e.getAttribute("method")||$,a=de(e.getAttribute("enctype"))||J,o=new FormData(e)}else if(lt(e)||ut(e)&&(e.type==="submit"||e.type==="image")){let s=e.form;if(s==null)throw new Error('Cannot submit a <button> or <input type="submit"> without a <form>');let l=e.getAttribute("formaction")||s.getAttribute("action");if(n=l?D(l,t):null,r=e.getAttribute("formmethod")||s.getAttribute("method")||$,a=de(e.getAttribute("formenctype"))||de(s.getAttribute("enctype"))||J,o=new FormData(s,e),!ft()){let{name:u,type:f,value:h}=e;if(f==="image"){let p=u?u+".":"";o.append(p+"x","0"),o.append(p+"y","0")}else u&&o.append(u,h)}}else{if(X(e))throw new Error('Cannot submit element that is not <form>, <button>, or <input type="submit|image">');r=$,n=null,a=J,i=e}return o&&a==="text/plain"&&(i=o,o=void 0),{action:n,method:r.toLowerCase(),encType:a,formData:o,body:i}}var mt=["onClick","relative","reloadDocument","replace","state","target","to","preventScrollReset","viewTransition"],vt=["aria-current","caseSensitive","className","end","style","to","viewTransition","children"],yt=["fetcherKey","navigate","reloadDocument","replace","state","method","action","onSubmit","relative","preventScrollReset","viewTransition"],gt="6";try{window.__reactRouterVersion=gt}catch(e){}var Te=d.createContext({isTransitioning:!1});process.env.NODE_ENV!=="production"&&(Te.displayName="ViewTransition");var Rt=d.createContext(new Map);process.env.NODE_ENV!=="production"&&(Rt.displayName="Fetchers");var Et="startTransition",_e=d[Et],bt="flushSync",Fr=it[bt],wt="useId",Or=d[wt];function St(e){let{basename:t,children:r,future:n,history:a}=e,[o,i]=d.useState({action:a.action,location:a.location}),{v7_startTransition:s}=n||{},l=d.useCallback(u=>{s&&_e?_e(()=>i(u)):i(u)},[i,s]);return d.useLayoutEffect(()=>a.listen(l),[a,l]),d.useEffect(()=>Ce(n),[n]),d.createElement(ce,{basename:t,children:r,location:o.location,navigationType:o.action,navigator:a,future:n})}process.env.NODE_ENV!=="production"&&(St.displayName="unstable_HistoryRouter");var Dt=typeof window!="undefined"&&typeof window.document!="undefined"&&typeof window.document.createElement!="undefined",Nt=/^(?:[a-z][a-z0-9+.-]*:|\/\/)/i,Fe=d.forwardRef(function(t,r){let{onClick:n,relative:a,reloadDocument:o,replace:i,state:s,target:l,to:u,preventScrollReset:f,viewTransition:h}=t,p=he(t,mt),{basename:w}=d.useContext(b),R,E=!1;if(typeof u=="string"&&Nt.test(u)&&(R=u,Dt))try{let g=new URL(window.location.href),y=u.startsWith("//")?new URL(g.protocol+u):new URL(u),z=D(y.pathname,w);y.origin===g.origin&&z!=null?u=z+y.search+y.hash:E=!0}catch(g){process.env.NODE_ENV!=="production"&&x(!1,'<Link to="'+u+'"> contains an invalid URL which will probably break when clicked - please update to a valid URL path.')}let N=ae(u,{relative:a}),I=Lt(u,{replace:i,state:s,target:l,preventScrollReset:f,relative:a,viewTransition:h});function v(g){n&&n(g),g.defaultPrevented||I(g)}return d.createElement("a",_({},p,{href:R||N,onClick:E||o?n:v,ref:r,target:l}))});process.env.NODE_ENV!=="production"&&(Fe.displayName="Link");var xt=d.forwardRef(function(t,r){let{"aria-current":n="page",caseSensitive:a=!1,className:o="",end:i=!1,style:s,to:l,viewTransition:u,children:f}=t,h=he(t,vt),p=C(l,{relative:h.relative}),w=S(),R=d.useContext(A),{navigator:E,basename:N}=d.useContext(b),I=R!=null&&jt(p)&&u===!0,v=E.encodeLocation?E.encodeLocation(p).pathname:p.pathname,g=w.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&&N&&(y=D(y,N)||y);let z=v!=="/"&&v.endsWith("/")?v.length-1:v.length,G=g===v||!i&&g.startsWith(v)&&g.charAt(z)==="/",me=y!=null&&(y===v||!i&&y.startsWith(v)&&y.charAt(v.length)==="/"),Q={isActive:G,isPending:me,isTransitioning:I},Ue=G?n:void 0,Z;typeof o=="function"?Z=o(Q):Z=[o,G?"active":null,me?"pending":null,I?"transitioning":null].filter(Boolean).join(" ");let Me=typeof s=="function"?s(Q):s;return d.createElement(Fe,_({},h,{"aria-current":Ue,className:Z,ref:r,style:Me,to:l,viewTransition:u}),typeof f=="function"?f(Q):f)});process.env.NODE_ENV!=="production"&&(xt.displayName="NavLink");var Pt=d.forwardRef((e,t)=>{let{fetcherKey:r,navigate:n,reloadDocument:a,replace:o,state:i,method:s=$,action:l,onSubmit:u,relative:f,preventScrollReset:h,viewTransition:p}=e,w=he(e,yt),R=Ut(),E=Mt(l,{relative:f}),N=s.toLowerCase()==="get"?"get":"post";return d.createElement("form",_({ref:t,method:N,action:E,onSubmit:a?u:v=>{if(u&&u(v),v.defaultPrevented)return;v.preventDefault();let g=v.nativeEvent.submitter,y=(g==null?void 0:g.getAttribute("formmethod"))||s;R(g||v.currentTarget,{fetcherKey:r,method:y,navigate:n,replace:o,state:i,relative:f,preventScrollReset:h,viewTransition:p})}},w))});process.env.NODE_ENV!=="production"&&(Pt.displayName="Form");function Ct(e){let{getKey:t,storageKey:r}=e;return At({getKey:t,storageKey:r}),null}process.env.NODE_ENV!=="production"&&(Ct.displayName="ScrollRestoration");var B;(function(e){e.UseScrollRestoration="useScrollRestoration",e.UseSubmit="useSubmit",e.UseSubmitFetcher="useSubmitFetcher",e.UseFetcher="useFetcher",e.useViewTransitionState="useViewTransitionState"})(B||(B={}));var fe;(function(e){e.UseFetcher="useFetcher",e.UseFetchers="useFetchers",e.UseScrollRestoration="useScrollRestoration"})(fe||(fe={}));function Oe(e){return e+" must be used within a data router. See https://reactrouter.com/v6/routers/picking-a-router."}function pe(e){let t=d.useContext(M);return t||(process.env.NODE_ENV!=="production"?m(!1,Oe(e)):m(!1)),t}function _t(e){let t=d.useContext(A);return t||(process.env.NODE_ENV!=="production"?m(!1,Oe(e)):m(!1)),t}function Lt(e,t){let{target:r,replace:n,state:a,preventScrollReset:o,relative:i,viewTransition:s}=t===void 0?{}:t,l=oe(),u=S(),f=C(e,{relative:i});return d.useCallback(h=>{if(dt(h,r)){h.preventDefault();let p=n!==void 0?n:T(u)===T(f);l(e,{replace:p,state:a,preventScrollReset:o,relative:i,viewTransition:s})}},[u,l,f,n,a,r,e,o,i,s])}function Tt(){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 Ft=0,Ot=()=>"__"+String(++Ft)+"__";function Ut(){let{router:e}=pe(B.UseSubmit),{basename:t}=d.useContext(b),r=le();return d.useCallback(function(n,a){a===void 0&&(a={}),Tt();let{action:o,method:i,encType:s,formData:l,body:u}=pt(n,t);if(a.navigate===!1){let f=a.fetcherKey||Ot();e.fetch(f,r,a.action||o,{preventScrollReset:a.preventScrollReset,formData:l,body:u,formMethod:a.method||i,formEncType:a.encType||s,flushSync:a.flushSync})}else e.navigate(a.action||o,{preventScrollReset:a.preventScrollReset,formData:l,body:u,formMethod:a.method||i,formEncType:a.encType||s,replace:a.replace,state:a.state,fromRouteId:r,flushSync:a.flushSync,viewTransition:a.viewTransition})},[e,t,r])}function Mt(e,t){let{relative:r}=t===void 0?{}:t,{basename:n}=d.useContext(b),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})),s=S();if(e==null){i.search=s.search;let l=new URLSearchParams(i.search),u=l.getAll("index");if(u.some(h=>h==="")){l.delete("index"),u.filter(p=>p).forEach(p=>l.append("index",p));let h=l.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:j([n,i.pathname])),T(i)}var Le="react-router-scroll-positions",Y={};function At(e){let{getKey:t,storageKey:r}=e===void 0?{}:e,{router:n}=pe(B.UseScrollRestoration),{restoreScrollPosition:a,preventScrollReset:o}=_t(fe.UseScrollRestoration),{basename:i}=d.useContext(b),s=S(),l=ue(),u=se();d.useEffect(()=>(window.history.scrollRestoration="manual",()=>{window.history.scrollRestoration="auto"}),[]),It(d.useCallback(()=>{if(u.state==="idle"){let f=(t?t(s,l):null)||s.key;Y[f]=window.scrollY}try{sessionStorage.setItem(r||Le,JSON.stringify(Y))}catch(f){process.env.NODE_ENV!=="production"&&x(!1,"Failed to save scroll positions in sessionStorage, <ScrollRestoration /> will not work properly ("+f+").")}window.history.scrollRestoration="auto"},[r,t,u.state,s,l])),typeof document!="undefined"&&(d.useLayoutEffect(()=>{try{let f=sessionStorage.getItem(r||Le);f&&(Y=JSON.parse(f))}catch(f){}},[r]),d.useLayoutEffect(()=>{let f=t&&i!=="/"?(p,w)=>t(_({},p,{pathname:D(p.pathname,i)||p.pathname}),w):t,h=n==null?void 0:n.enableScrollRestoration(Y,()=>window.scrollY,f);return()=>h&&h()},[n,i,t]),d.useLayoutEffect(()=>{if(a!==!1){if(typeof a=="number"){window.scrollTo(0,a);return}if(s.hash){let f=document.getElementById(decodeURIComponent(s.hash.slice(1)));if(f){f.scrollIntoView();return}}o!==!0&&window.scrollTo(0,0)}},[s,a,o]))}function It(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 jt(e,t){t===void 0&&(t={});let r=d.useContext(Te);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}=pe(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 O(a.pathname,i)!=null||O(a.pathname,o)!=null}import{jsx as Bt}from"react/jsx-runtime";var Ht=e=>{let t=Vt(null),r=()=>{let l=kt(t);if(!l)throw new Error("useRouteWeaver must be used within a RouteWeaverProvider");return l};return{RouteWeaverProvider:({children:l})=>Bt(t.Provider,{value:e,children:l}),useNavigation:()=>r().navigation,useActiveRoute:()=>{let l=r(),{pathname:u}=S();return l.getActiveRoute(u)},useBreadcrumbs:()=>{let l=r(),{pathname:u}=S();return l.getBreadcrumbs(u)},useBuildPath:()=>r().buildPath}};export{Ht as createRouteWeaverReactApi}; /*! 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 *) */