@real-router/preact 0.25.0 → 0.25.1

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.
@@ -1,2 +1,2 @@
1
- import{useEffect as e,useLayoutEffect as t,useMemo as n,useRef as r,useState as i}from"preact/hooks";import{UNKNOWN_ROUTE as a,events as o,getNavigator as s}from"@real-router/core";import{getRouteUtils as c,startsWithSegment as l}from"@real-router/route-utils";import{Fragment as u,isValidElement as d,toChildArray as f}from"preact";import{Suspense as p,memo as m}from"preact/compat";import{Fragment as h,jsx as g,jsxs as _}from"preact/jsx-runtime";import{createActiveSource as v,createDismissableError as y,createRouteEnterGate as b,createRouteNodeSource as x,createRouteSource as ee,getTransitionSource as S,guardLeaveListener as C,primeErrorSource as te}from"@real-router/sources";import{a as w,i as T,n as E,r as D,t as O}from"./useRoute-DJHXwUfc.mjs";import{getPluginApi as k}from"@real-router/core/api";import{putField as A}from"@real-router/core/utils";function j(e){return null}j.displayName=`RouteView.Match`;function M(e){return null}M.displayName=`RouteView.Self`;function N(e){return null}N.displayName=`RouteView.NotFound`;const ne=new Set([j,M,N]);function re(e,t,n){return t===``?!1:n?e===t:l(e,t)}function P(e,t){for(let n of f(e))d(n)&&(ne.has(n.type)?t.push(n):P(n.props.children,t))}function F(e,t,n){return g(u,{children:n===void 0?e:g(p,{fallback:n,children:e})},t)}function ie(e){return e.type===N||e.type===M}function ae(e,t){e.type===N?t.notFoundFound||=(t.notFoundChildren=e.props.children,!0):t.selfFound||=(t.selfChildren=e.props.children,t.selfFallback=e.props.fallback,!0)}function oe(e,t,n,r){let{segment:i,exact:a=!1,fallback:o,children:s}=e.props,c=n?`${n}.${i}`:i;return r||!re(t,c,a)?null:F(s,c,o)}function se(e,t,n,r){r.selfFound&&t===n?e.push(F(r.selfChildren,`__route-view-self__`,r.selfFallback)):t===a&&r.notFoundChildren!==null&&e.push(g(u,{children:r.notFoundChildren},`__route-view-not-found__`))}function ce(e,t,n){let r={selfChildren:null,selfFallback:void 0,selfFound:!1,notFoundChildren:null,notFoundFound:!1},i=!1,a=[];for(let o of e){if(ie(o)){ae(o,r);continue}let e=oe(o,t,n,i);e!==null&&(i=!0,a.push(e))}return i||se(a,t,n,r),{rendered:a,activeMatchFound:i}}function I(t,n,r){let[a,o]=i(n);return e(()=>{let e=()=>{o(e=>{let t=n();return Object.is(e,t)?e:t})};return e(),t(e)},[t,n]),a}const L=w(E,`useNavigator`),R=w(T,`useRouter`);function le(e){let t=R(),r=L(),i=x(t,e),{route:a,previousRoute:o}=I(i.subscribe,i.getSnapshot,i.getSnapshot);return n(()=>({navigator:r,route:a,previousRoute:o}),[r,a,o])}function z({nodeName:e,children:t}){let{route:r}=le(e),i=n(()=>{let e=[];return P(t,e),e},[t]),a=r?.name,o=n(()=>a===void 0?[]:ce(i,a,e).rendered,[i,a,e]);return o.length>0?g(h,{children:o}):null}z.displayName=`RouteView`;const ue=Object.assign(z,{Match:j,Self:M,NotFound:N}),de=Object.freeze({}),fe=Object.freeze({}),pe=Object.freeze({destroy:()=>{}});function me(e){if(typeof document>`u`)return pe;let t=!1;document.documentElement.dataset.navDirection=`forward`;let n,r,i=!1,a=t=>{let n=e.buildUrl;if(n!==void 0)try{let e=t.context?.url?.hash,r=n(t.name,t.params,t.search,e?{hash:e}:void 0);return r===void 0?void 0:new URL(r,globalThis.location.href).href}catch{return}},s=()=>{let t=e.getState();return t!==void 0&&a(t)===globalThis.location.href},c=()=>{s()?n===void 0&&(t=!1):n===void 0?t=!0:r=globalThis.location.href};globalThis.addEventListener(`popstate`,c);let l=k(e),u=()=>{n=void 0,t=r!==void 0,i=t,r=void 0},d=[l.addEventListener(o.TRANSITION_START,e=>{n=e,t&&!i&&s()&&(t=!1)}),l.addEventListener(o.TRANSITION_SUCCESS,u),l.addEventListener(o.TRANSITION_ERROR,e=>{if(n===void 0||e?.context===n.context){u();return}let t=e===void 0?void 0:a(e);(t===void 0||t===r)&&(r=void 0)}),l.addEventListener(o.TRANSITION_CANCEL,u),e.subscribeLeave(()=>{document.documentElement.dataset.navDirection=t?`back`:`forward`,t=!1})];return{destroy:()=>{for(let e of d)e();globalThis.removeEventListener(`popstate`,c),delete document.documentElement.dataset.navDirection}}}const B=`data-real-router-announcer`,he=Object.freeze({destroy:()=>{}}),V=`data-rr-announcer-refs`,H=`data-rr-announcer-generation`;function U(e){return Number(e.getAttribute(V))}function W(e,t){e.setAttribute(V,String(t))}function ge(e,t){if(typeof document>`u`)return he;let n=t?.prefix??`Navigated to `,r=t?.getAnnouncementText,i=!1,a=!1,o=``,s=null,c,{element:l,generation:u}=ve();W(l,U(l)+1);let d=(e,t)=>{o=e,clearTimeout(c),l.textContent=e,c=setTimeout(()=>{l.textContent=``,o=``},7e3),xe(t)},f=setTimeout(()=>{if(i=!0,s!==null&&!a){let e=s;s=null,d(e,document.querySelector(`h1`))}},100),p=e.getState()===void 0,m=e.subscribe(({route:e,previousRoute:t})=>{t===void 0&&p?p=!1:requestAnimationFrame(()=>{requestAnimationFrame(()=>{if(a)return;let t=document.querySelector(`h1`),c=be(e,n,r,t);c&&c!==o&&(i?d(c,t):s=c)})})});return{destroy(){if(a||(a=!0,m(),clearTimeout(c),clearTimeout(f),u!==G()))return;let e=U(l)-1;W(l,e),e===0&&ye(l)}}}function G(){return Number(document.documentElement.getAttribute(H))}function _e(){let e=document.documentElement,t=G()+1;return e.setAttribute(H,String(t)),t}function ve(){let e=document.querySelector(`[${B}]`);if(e)return{element:e,generation:Number(e.getAttribute(H))};let t=document.createElement(`div`);return t.setAttribute(`style`,`position:absolute;width:1px;height:1px;padding:0;margin:-1px;overflow:hidden;clip:rect(0,0,0,0);clip-path:inset(50%);white-space:nowrap;border:0`),t.setAttribute(`aria-live`,`assertive`),t.setAttribute(`aria-atomic`,`true`),t.setAttribute(B,``),W(t,0),t.setAttribute(H,String(_e())),(document.body??document.documentElement).prepend(t),{element:t,generation:Number(t.getAttribute(H))}}function ye(e){e.remove()}function be(e,t,n,r){if(n)try{let t=n(e);if(t)return t}catch(e){console.error(`[real-router] getAnnouncementText threw; falling back to default resolution.`,e)}let i=(r?.textContent??``).trim(),a=e.name.startsWith(`@@`)?``:e.name;return`${t}${i||document.title||a||globalThis.location.pathname}`}function xe(e){e&&(e.hasAttribute(`tabindex`)||e.setAttribute(`tabindex`,`-1`),e.focus({preventScroll:!0}))}const K=Object.create,q=Object.freeze({destroy:()=>{}});function Se(e,t){if(globalThis.window===void 0)return q;let n=t?.mode??`restore`;if(n===`native`)return q;let r=t?.anchorScrolling??!0,i=t?.scrollContainer,a=t?.behavior??`auto`,o=t?.storageKey??`real-router:scroll`,s,c=()=>{if(s!==void 0)return s;try{let e=sessionStorage.getItem(o),t=e?JSON.parse(e):void 0;s=Object.assign(K(null),t)}catch{s=K(null)}return s},l=(e,t)=>{try{let n=c();if(n[e]===t)return;n[e]=t,sessionStorage.setItem(o,JSON.stringify(n))}catch{}},u=history.scrollRestoration;try{history.scrollRestoration=`manual`}catch{}let d=()=>{let e=i?.();return e?e.scrollTop:globalThis.scrollY},f=e=>{let t=i?.();t?t.scrollTo({top:e,left:0,behavior:a}):globalThis.scrollTo({top:e,left:0,behavior:a})},p=0,m=(e,t)=>{let n=c()[J(e)]??0,r=0,o=e=>e.scrollHeight-e.clientHeight>=n-1,s=()=>{if(g||t!==p)return;let e=i?.();if(e){if(e.scrollTo({top:n,left:0,behavior:a}),o(e))return}else if(globalThis.scrollTo({top:n,left:0,behavior:a}),!i&&o(document.scrollingElement??document.documentElement))return;r>=10||(r+=1,requestAnimationFrame(s))};s()},h=e=>{let t=e.context?.url?.hash;if(t!==void 0){if(r&&t.length>0){let e=document.getElementById(t);if(e){e.scrollIntoView({behavior:a});return}}f(0);return}let n=globalThis.location.hash;if(r&&n.length>1){let e;try{e=decodeURIComponent(n.slice(1))}catch{e=n.slice(1)}let t=document.getElementById(e);if(t){t.scrollIntoView({behavior:a});return}}f(0)},g=!1,_=!0,v=!1,y=e=>{v=e.state===null||e.state===void 0},b=e.subscribe(({route:e,previousRoute:t})=>{let r=e.context,i=r.navigation,a=r.memory?.direction,o=r.browser?.source===`popstate`,s=o||i?.direction===`back`||i?.navigationType===`traverse`||a===`back`||a===`forward`,c=e.transition,u=(c?.reload??!1)||i?.navigationType===`reload`,f=(c?.replace??!1)||i?.navigationType===`replace`,y=_;t&&y&&l(J(t),d()),_=!1,(!f||s||u||t?.name!==e.name)&&(p+=1);let b=p;requestAnimationFrame(()=>{if(g)return;if(_=!0,n===`top`){h(e);return}if(u){m(e,b);return}let r=y&&t!==void 0&&J(t)===J(e);s&&!(o&&(v||r))?m(e,b):f||h(e)})}),x=()=>{let t=e.getState();t&&l(J(t),d())};return globalThis.addEventListener(`pagehide`,x),globalThis.addEventListener(`popstate`,y),{destroy:()=>{g=!0,b(),globalThis.removeEventListener(`pagehide`,x),globalThis.removeEventListener(`popstate`,y);try{history.scrollRestoration=u}catch{}}}}function J(e){return e.path}const Y=Object.freeze({destroy:()=>{}}),X=e=>e.context?.url,Ce=e=>{let t=null,n=1/0,r=null,i=-1/0;for(let a of e){if(!a.isIntersecting)continue;let e=a.rootBounds?.top??0,o=a.boundingClientRect.top-e;o>=0?o<n&&(t=a,n=o):o>i&&(r=a,i=o)}return t??r},we=(e,t)=>{let n=null,r=e=>{let n=e.context;n&&n.url===void 0&&(console.warn(`[real-router] scroll-spy: state.context.url is not claimed. Spy requires browser-plugin or navigation-plugin. Disabling.`),t())},i=e.getState();if(i)r(i);else{let t=!1;n=e.subscribe(({route:e})=>{t||(t=!0,r(e),n?.(),n=null)})}return{destroy(){n?.(),n=null}}},Te=e=>{let t=!1,n=null,r=null,i=null,a=()=>{n!==null&&(clearTimeout(n),n=null),i&&(r??globalThis).removeEventListener(`scrollend`,i),i=null,r=null,t=!1};return{get active(){return t},start(){a(),t=!0;let o=()=>{a()};i=o,r=e(),(r??globalThis).addEventListener(`scrollend`,o,{once:!0}),n=setTimeout(o,500)},destroy(){a()}}},Ee=(e,t)=>{let n=null,r=null;return{schedule(){n===null&&(n=requestAnimationFrame(()=>{n=null,r!==null&&clearTimeout(r),r=setTimeout(()=>{r=null,e()},t)}))},destroy(){n!==null&&(cancelAnimationFrame(n),n=null),r!==null&&(clearTimeout(r),r=null)}}},De=(e,t,n,r,i,a)=>{let o=new Set,s=new Map,c=!1,l=null,u=e=>{if(!a()){for(let t of e)s.set(t.target,t);r()}},d=e=>new IntersectionObserver(u,{root:e,rootMargin:t,threshold:0}),f=n(),p=d(f),m=()=>{let t=n()??document,r;try{r=t.querySelectorAll(e)}catch{i();return}let a=new Set;for(let e of r){let t=e.id;t&&!c&&(a.has(t)&&(c=!0,console.warn(`[real-router] scroll-spy: duplicate id "${t}" observed. Selection picks the topmost visible match deterministically.`)),a.add(t)),o.has(e)||(p.observe(e),o.add(e))}},h={childList:!0,subtree:!0,attributes:!0,attributeFilter:[`id`]},g=null,_=()=>{for(let e of o)e.isConnected||(p.unobserve(e),o.delete(e),s.delete(e));let e=n();e!==f&&(f=e,p.disconnect(),p=d(e),o.clear(),s.clear(),g?.disconnect(),g?.observe(e??document.body,h)),m()};return m(),g=new MutationObserver(()=>{l!==null&&clearTimeout(l),l=setTimeout(()=>{l=null,_()},250)}),g.observe(f??document.body,h),{pending:s,isContainerDetached:()=>f!==null&&!f.isConnected,reconcile:_,destroy(){p.disconnect(),g.disconnect(),l!==null&&(clearTimeout(l),l=null),o.clear(),s.clear()}}};function Oe(e,t){if(typeof document>`u`||typeof IntersectionObserver>`u`)return Y;let{selector:n}=t;if(!n)return Y;let r=t.rootMargin??`-20% 0px -60% 0px`,i=t.scrollContainer,a=()=>i?.()??null,o=!1,s=!1,c=!1,l=()=>s||o,u=null,d=S(e),f=we(e,()=>{s=!0}),p=Te(a),m=Ee(()=>{u?.()},150),h=De(n,r,a,()=>{m.schedule()},()=>{s||(s=!0,console.warn(`[real-router] scroll-spy: invalid selector "${n}". Disabling.`))},l);u=()=>{if(o||s){h.pending.clear();return}if(d.getSnapshot().isTransitioning||p.active)return;let t=Ce(h.pending.values());if(h.pending.clear(),!t)return;let n=t.target.id;if(!n)return;let r=e.getState();if(!r||n===X(r)?.hash)return;let i={hash:n,replace:!0,force:!0,hashChange:!0};c=!0,e.navigate(r.name,r.params,r.search,i).catch(()=>{}).finally(()=>{c=!1})};let g=e.subscribe(({route:e})=>{c||(h.isContainerDetached()&&h.reconcile(),X(e)?.hashChanged&&p.start())});return{destroy(){o=!0,g(),h.destroy(),m.destroy(),p.destroy(),f.destroy()}}}const ke=Object.freeze({destroy:()=>{}});function Ae(e){if(typeof document>`u`||typeof document.startViewTransition!=`function`)return ke;let t=null,n=null,r=!1,i=()=>{t?.(),t=null},a=e.subscribeLeave(({signal:e})=>{if(!e.aborted)return r=!1,i(),new Promise(a=>{let o=new Promise(e=>{t=e});e.addEventListener(`abort`,()=>{r||(i(),n?.skipTransition?.(),a())},{once:!0});try{n=document.startViewTransition(()=>(a(),o))}catch{i(),a()}})}),o=e.subscribe(()=>{let e=t;if(r=!0,t=null,e===null)n=null;else{let t=n;setTimeout(()=>{e(),n===t&&(n=null)},0)}});return{destroy:()=>{a(),o(),n?.skipTransition?.(),n=null,i()}}}const Z=Object.keys,Q=Object.hasOwn,je=Symbol.for(`real-router.router`);function Me(e){try{return e?.[je]===!0}catch{return!1}}function Ne(e,t){try{return k(e)}catch{Me(e)&&console.error(`[real-router] Route "${t}" rendered its LITERAL path: this IS a router, but not one this copy of @real-router/core built, so \`forwardTo\` was not resolved. It is either wrapped in a Proxy (Vue \`reactive()\` / Pinia — store it with \`markRaw\`), or your dependency tree holds two copies of @real-router/core — dedupe it to one.`);return}}function Pe(e,t,n,r){return e===void 0?{name:t,params:n,search:r}:((t!==``||n!==void 0||r!==void 0)&&console.warn("[real-router] <Link> received both `to` and channel props (routeName / routeParams / routeSearch). `to` wins; the channel props are ignored. Use one form or the other."),{name:e.name,params:e.params,search:e.search})}function Fe(e){return e.button===0&&!e.metaKey&&!e.altKey&&!e.ctrlKey&&!e.shiftKey}function Ie(e){return!!e&&e!==`_self`}function Le(e){return encodeURI(e).replaceAll(`#`,`%23`)}const Re=/^\/[\t\n\r]*[/\\]/;function ze(e,t,n,r,i){try{let a;i!==void 0&&(a=i.startsWith(`#`)?i.slice(1):i);let o=e.buildUrl;if(o){let e=o(t,n,r,a===void 0?void 0:{hash:a});if(typeof e==`string`&&e.length>0)return e}let s,c=Ne(e,t);try{let e=c?.forwardState(t,n,r);s=e&&c?.buildPathResolved(e.name,e.params,e.search)}catch{s=void 0}let l=s??e.buildPath(t,n,r);if(typeof l!=`string`||l.length===0){console.error(`[real-router] Route "${t}" yielded an empty path. The element will render without an href attribute.`);return}let u=Re.test(l)?`/.${l}`:l;return a?`${u}#${Le(a)}`:u}catch{console.error(`[real-router] Route "${t}" is not defined. The element will render without an href attribute.`);return}}function Be(e,t,n,r,i){try{let a=k(e),o=a.forwardState(t,n,r);return a.buildPathResolved(o.name,o.params,o.search)===i}catch{return!1}}function Ve(e){let t=e;A(t,`force`,!0),A(t,`hashChange`,!0)}function He(e){return Q(e,`reload`)&&!!e.reload||Q(e,`force`)&&!!e.force}function Ue(e,t,n,r,i,o){let s={...o};i!==void 0&&A(s,`hash`,i);let c=e.getState(),l=r;if(c!==void 0){let o=r??c.search,u=e.isActiveRoute(t,n,o,!0,!1),d=c.context?.url?.hash??``;if(d!==(i??d)){if(u)Ve(s),l=o;else if(!He(s)&&c.name!==a&&Be(e,t,n,r,c.path))return Ve(s),e.navigate(c.name,c.params,c.search,s)}}return e.navigate(t,n,l,s)}const We=/\s/,Ge=/\S+/g;function Ke(e){return We.test(e)?e.match(Ge)??[]:[e]}function qe(e,t,n){if(e&&t){let e=Ke(t);if(e.length===0)return n??void 0;if(!n)return e.join(` `);let r=Ke(n),i=new Set(r);for(let t of e)i.has(t)||(i.add(t),r.push(t));return r.join(` `)}return n??void 0}function $(e,t){if(Object.is(e,t))return!0;if(!e||!t)return!1;let n=Z(e),r=Z(t);if(n.length!==r.length)return!1;let i=e,a=t;for(let e of n)if(!r.includes(e)||!Object.is(i[e],a[e]))return!1;return!0}function Je(e,t,r,i=!1,a=!0,o){let s=R(),c=n(()=>v(s,e,t,r,i,a,o),[s,e,t,r,i,a,o]);return I(c.subscribe,c.getSnapshot,c.getSnapshot)}function Ye(e,t){return e.routeName===t.routeName&&e.className===t.className&&e.activeClassName===t.activeClassName&&e.activeStrict===t.activeStrict&&e.ignoreQueryParams===t.ignoreQueryParams&&e.onClick===t.onClick&&e.target===t.target&&e.style===t.style&&e.children===t.children&&e.hash===t.hash&&$(e.routeParams,t.routeParams)&&$(e.routeSearch,t.routeSearch)&&$(e.to,t.to)&&$(e.routeOptions,t.routeOptions)}const Xe=m(({routeName:e,routeParams:t,routeSearch:n,to:r,routeOptions:i=fe,className:a,activeClassName:o=`active`,activeStrict:s=!1,ignoreQueryParams:c=!0,hash:l,onClick:u,target:d,children:f,...p})=>{let m=R(),{name:h,params:_,search:v}=Pe(r,e??``,t,n),y=Je(h,_,v,s,c,l),b=_??de,x=ze(m,h,b,v,l),ee=e=>{if(u){try{u(e)}catch(e){console.error(`[real-router] A <Link> onClick handler threw; navigation is unaffected.`,e)}if(e.defaultPrevented)return}Fe(e)&&!Ie(d)&&(e.preventDefault(),Ue(m,h,b,v,l,i).catch(()=>{}))},S=qe(y,o,a);return g(`a`,{...p,target:d,href:x,className:S,onClick:ee,children:f})},Ye);Xe.displayName=`Link`;function Ze({children:n,fallback:i,onError:a}){let o=R(),s=y(o),c=I(s.subscribe,s.getSnapshot,s.getSnapshot),l=r(a);return t(()=>{l.current=a}),e(()=>{c.error&&l.current?.(c.error,c.toRoute,c.fromRoute)},[c.version]),_(u,{children:[n,c.error?i(c.error,c.resetError):null]})}const Qe=()=>{let e=R();return c(k(e).getTree())};function $e(){let e=R(),t=S(e);return I(t.subscribe,t.getSnapshot,t.getSnapshot)}function et(n,i){let a=R(),o=r(n),s=i?.skipSameRoute??!0;t(()=>{o.current=n}),e(()=>a.subscribeLeave(C(e=>o.current(e),{skipSameRoute:s})),[a,s])}function tt(n,a){let{route:o,previousRoute:s}=O(),c=r(n),[l]=i(()=>b()),u=a?.skipSameRoute??!0;t(()=>{c.current=n}),e(()=>{let e=l(o,s,u);e&&c.current(e)},[l,o,s,u])}const nt=({router:t,children:r,announceNavigation:i,scrollRestoration:a,scrollSpy:o,viewTransitions:c})=>{let l=i!==void 0&&i!==!1,u=typeof i==`object`?i:void 0,d=u?.prefix;e(()=>{if(!l)return;let e=ge(t,u);return()=>{e.destroy()}},[t,l,d]);let f=a?.mode,p=a?.anchorScrolling,m=a?.behavior,h=a?.storageKey,_=a!==void 0;e(()=>{if(!_)return;let e=Se(t,{mode:f,anchorScrolling:p,behavior:m,storageKey:h,scrollContainer:a.scrollContainer});return()=>{e.destroy()}},[t,_,f,p,m,h]);let v=o?.selector,y=o?.rootMargin,b=o!==void 0&&v!==void 0&&v!==``;e(()=>{if(!b)return;let e=Oe(t,{selector:v,rootMargin:y,scrollContainer:o.scrollContainer});return()=>{e.destroy()}},[t,b,v,y]),e(()=>{if(!c)return;let e=Ae(t);return()=>{e.destroy()}},[t,c]);let x=s(t),S=n(()=>ee(t),[t]);e(()=>{te(t)},[t]);let{route:C,previousRoute:w}=I(S.subscribe,S.getSnapshot,S.getSnapshot),O=n(()=>({navigator:x,route:C,previousRoute:w}),[x,C,w]);return g(T.Provider,{value:t,children:g(E.Provider,{value:x,children:g(D.Provider,{value:O,children:r})})})};export{Xe as Link,E as NavigatorContext,D as RouteContext,ue as RouteView,T as RouterContext,Ze as RouterErrorBoundary,nt as RouterProvider,me as createDirectionTracker,L as useNavigator,O as useRoute,tt as useRouteEnter,et as useRouteExit,le as useRouteNode,Qe as useRouteUtils,R as useRouter,$e as useRouterTransition};
1
+ import{useEffect as e,useLayoutEffect as t,useMemo as n,useRef as r,useState as i}from"preact/hooks";import{createActiveSource as a,createDismissableError as o,createRouteEnterGate as s,createRouteNodeSource as c,createRouteSource as l,getTransitionSource as u,guardLeaveListener as d,primeErrorSource as f}from"@real-router/sources";import{a as p,i as m,n as h,r as g,t as _}from"./useRoute-DJHXwUfc.mjs";import{UNKNOWN_ROUTE as v,events as y,getNavigator as b}from"@real-router/core";import{getRouteUtils as x,startsWithSegment as S}from"@real-router/route-utils";import{Fragment as C,isValidElement as w,toChildArray as T}from"preact";import{Suspense as E,memo as D}from"preact/compat";import{Fragment as O,jsx as k,jsxs as A}from"preact/jsx-runtime";import{getPluginApi as j}from"@real-router/core/api";import{putField as M}from"@real-router/core/utils";function N(t,n,r){let[a,o]=i(n);return e(()=>{let e=()=>{o(e=>{let t=n();return Object.is(e,t)?e:t})};return e(),t(e)},[t,n]),a}const P=p(h,`useNavigator`),F=p(m,`useRouter`);function I(e){let t=F(),r=P(),i=c(t,e),{route:a,previousRoute:o}=N(i.subscribe,i.getSnapshot,i.getSnapshot);return n(()=>({navigator:r,route:a,previousRoute:o}),[r,a,o])}function L(e){return null}L.displayName=`RouteView.Match`;function R(e){return null}R.displayName=`RouteView.Self`;function z(e){return null}z.displayName=`RouteView.NotFound`;const ee=new Set([L,R,z]);function te(e,t,n){return t===``?!1:n?e===t:S(e,t)}function ne(e,t){for(let n of T(e))w(n)&&(ee.has(n.type)?t.push(n):ne(n.props.children,t))}function re(e,t,n){return k(C,{children:n===void 0?e:k(E,{fallback:n,children:e})},t)}function ie(e){return e.type===z||e.type===R}function ae(e,t){e.type===z?t.notFoundFound||=(t.notFoundChildren=e.props.children,!0):t.selfFound||=(t.selfChildren=e.props.children,t.selfFallback=e.props.fallback,!0)}function oe(e,t,n,r){let{segment:i,exact:a=!1,fallback:o,children:s}=e.props,c=n?`${n}.${i}`:i;return r||!te(t,c,a)?null:re(s,c,o)}function se(e,t,n,r){r.selfFound&&t===n?e.push(re(r.selfChildren,`__route-view-self__`,r.selfFallback)):t===v&&r.notFoundChildren!==null&&e.push(k(C,{children:r.notFoundChildren},`__route-view-not-found__`))}function ce(e,t,n){let r={selfChildren:null,selfFallback:void 0,selfFound:!1,notFoundChildren:null,notFoundFound:!1},i=!1,a=[];for(let o of e){if(ie(o)){ae(o,r);continue}let e=oe(o,t,n,i);e!==null&&(i=!0,a.push(e))}return i||se(a,t,n,r),{rendered:a,activeMatchFound:i}}function le({nodeName:e,children:t}){let{route:r}=I(e),i=n(()=>{let e=[];return ne(t,e),e},[t]),a=r?.name,o=n(()=>a===void 0?[]:ce(i,a,e).rendered,[i,a,e]);return o.length>0?k(O,{children:o}):null}le.displayName=`RouteView`;const ue=Object.assign(le,{Match:L,Self:R,NotFound:z}),de=Object.freeze({}),fe=Object.freeze({}),pe=Object.freeze({destroy:()=>{}});function me(e){if(typeof document>`u`)return pe;let t=!1;document.documentElement.dataset.navDirection=`forward`;let n,r,i=!1,a=t=>{let n=e.buildUrl;if(n!==void 0)try{let e=t.context?.url?.hash,r=n(t.name,t.params,t.search,e?{hash:e}:void 0);return r===void 0?void 0:new URL(r,globalThis.location.href).href}catch{return}},o=()=>{let t=e.getState();return t!==void 0&&a(t)===globalThis.location.href},s=()=>{o()?n===void 0&&(t=!1):n===void 0?t=!0:r=globalThis.location.href};globalThis.addEventListener(`popstate`,s);let c=j(e),l=()=>{n=void 0,t=r!==void 0,i=t,r=void 0},u=[c.addEventListener(y.TRANSITION_START,e=>{n=e,t&&!i&&o()&&(t=!1)}),c.addEventListener(y.TRANSITION_SUCCESS,l),c.addEventListener(y.TRANSITION_ERROR,e=>{if(n===void 0||e?.context===n.context){l();return}let t=e===void 0?void 0:a(e);(t===void 0||t===r)&&(r=void 0)}),c.addEventListener(y.TRANSITION_CANCEL,l),e.subscribeLeave(()=>{document.documentElement.dataset.navDirection=t?`back`:`forward`,t=!1})];return{destroy:()=>{for(let e of u)e();globalThis.removeEventListener(`popstate`,s),delete document.documentElement.dataset.navDirection}}}const B=`data-real-router-announcer`,he=Object.freeze({destroy:()=>{}}),V=`data-rr-announcer-refs`,H=`data-rr-announcer-generation`;function U(e){return Number(e.getAttribute(V))}function W(e,t){e.setAttribute(V,String(t))}function ge(e,t){if(typeof document>`u`)return he;let n=t?.prefix??`Navigated to `,r=t?.getAnnouncementText,i=!1,a=!1,o=``,s=null,c,{element:l,generation:u}=ve();W(l,U(l)+1);let d=(e,t)=>{o=e,clearTimeout(c),l.textContent=e,c=setTimeout(()=>{l.textContent=``,o=``},7e3),xe(t)},f=setTimeout(()=>{if(i=!0,s!==null&&!a){let e=s;s=null,d(e,document.querySelector(`h1`))}},100),p=e.getState()===void 0,m=e.subscribe(({route:e,previousRoute:t})=>{t===void 0&&p?p=!1:requestAnimationFrame(()=>{requestAnimationFrame(()=>{if(a)return;let t=document.querySelector(`h1`),c=be(e,n,r,t);c&&c!==o&&(i?d(c,t):s=c)})})});return{destroy(){if(a||(a=!0,m(),clearTimeout(c),clearTimeout(f),u!==G()))return;let e=U(l)-1;W(l,e),e===0&&ye(l)}}}function G(){return Number(document.documentElement.getAttribute(H))}function _e(){let e=document.documentElement,t=G()+1;return e.setAttribute(H,String(t)),t}function ve(){let e=document.querySelector(`[${B}]`);if(e)return{element:e,generation:Number(e.getAttribute(H))};let t=document.createElement(`div`);return t.setAttribute(`style`,`position:absolute;width:1px;height:1px;padding:0;margin:-1px;overflow:hidden;clip:rect(0,0,0,0);clip-path:inset(50%);white-space:nowrap;border:0`),t.setAttribute(`aria-live`,`assertive`),t.setAttribute(`aria-atomic`,`true`),t.setAttribute(B,``),W(t,0),t.setAttribute(H,String(_e())),(document.body??document.documentElement).prepend(t),{element:t,generation:Number(t.getAttribute(H))}}function ye(e){e.remove()}function be(e,t,n,r){if(n)try{let t=n(e);if(t)return t}catch(e){console.error(`[real-router] getAnnouncementText threw; falling back to default resolution.`,e)}let i=(r?.textContent??``).trim(),a=e.name.startsWith(`@@`)?``:e.name;return`${t}${i||document.title||a||globalThis.location.pathname}`}function xe(e){e&&(e.hasAttribute(`tabindex`)||e.setAttribute(`tabindex`,`-1`),e.focus({preventScroll:!0}))}const K=Object.create,q=Object.freeze({destroy:()=>{}});function Se(e,t){if(globalThis.window===void 0)return q;let n=t?.mode??`restore`;if(n===`native`)return q;let r=t?.anchorScrolling??!0,i=t?.scrollContainer,a=t?.behavior??`auto`,o=t?.storageKey??`real-router:scroll`,s,c=()=>{if(s!==void 0)return s;try{let e=sessionStorage.getItem(o),t=e?JSON.parse(e):void 0;s=Object.assign(K(null),t)}catch{s=K(null)}return s},l=(e,t)=>{try{let n=c();if(n[e]===t)return;n[e]=t,sessionStorage.setItem(o,JSON.stringify(n))}catch{}},u=history.scrollRestoration;try{history.scrollRestoration=`manual`}catch{}let d=()=>{let e=i?.();return e?e.scrollTop:globalThis.scrollY},f=e=>{let t=i?.();t?t.scrollTo({top:e,left:0,behavior:a}):globalThis.scrollTo({top:e,left:0,behavior:a})},p=0,m=(e,t)=>{let n=c()[J(e)]??0,r=0,o=e=>e.scrollHeight-e.clientHeight>=n-1,s=()=>{if(g||t!==p)return;let e=i?.();if(e){if(e.scrollTo({top:n,left:0,behavior:a}),o(e))return}else if(globalThis.scrollTo({top:n,left:0,behavior:a}),!i&&o(document.scrollingElement??document.documentElement))return;r>=10||(r+=1,requestAnimationFrame(s))};s()},h=e=>{let t=e.context?.url?.hash;if(t!==void 0){if(r&&t.length>0){let e=document.getElementById(t);if(e){e.scrollIntoView({behavior:a});return}}f(0);return}let n=globalThis.location.hash;if(r&&n.length>1){let e;try{e=decodeURIComponent(n.slice(1))}catch{e=n.slice(1)}let t=document.getElementById(e);if(t){t.scrollIntoView({behavior:a});return}}f(0)},g=!1,_=!0,v=!1,y=e=>{v=e.state===null||e.state===void 0},b=e.subscribe(({route:e,previousRoute:t})=>{let r=e.context,i=r.navigation,a=r.memory?.direction,o=r.browser?.source===`popstate`,s=o||i?.direction===`back`||i?.navigationType===`traverse`||a===`back`||a===`forward`,c=e.transition,u=(c?.reload??!1)||i?.navigationType===`reload`,f=(c?.replace??!1)||i?.navigationType===`replace`,y=_;t&&y&&l(J(t),d()),_=!1,(!f||s||u||t?.name!==e.name)&&(p+=1);let b=p;requestAnimationFrame(()=>{if(g)return;if(_=!0,n===`top`){h(e);return}if(u){m(e,b);return}let r=y&&t!==void 0&&J(t)===J(e);s&&!(o&&(v||r))?m(e,b):f||h(e)})}),x=()=>{let t=e.getState();t&&l(J(t),d())};return globalThis.addEventListener(`pagehide`,x),globalThis.addEventListener(`popstate`,y),{destroy:()=>{g=!0,b(),globalThis.removeEventListener(`pagehide`,x),globalThis.removeEventListener(`popstate`,y);try{history.scrollRestoration=u}catch{}}}}function J(e){return e.path}const Y=Object.freeze({destroy:()=>{}}),X=e=>e.context?.url,Ce=e=>{let t=null,n=1/0,r=null,i=-1/0;for(let a of e){if(!a.isIntersecting)continue;let e=a.rootBounds?.top??0,o=a.boundingClientRect.top-e;o>=0?o<n&&(t=a,n=o):o>i&&(r=a,i=o)}return t??r},we=(e,t)=>{let n=null,r=e=>{let n=e.context;n&&n.url===void 0&&(console.warn(`[real-router] scroll-spy: state.context.url is not claimed. Spy requires browser-plugin or navigation-plugin. Disabling.`),t())},i=e.getState();if(i)r(i);else{let t=!1;n=e.subscribe(({route:e})=>{t||(t=!0,r(e),n?.(),n=null)})}return{destroy(){n?.(),n=null}}},Te=e=>{let t=!1,n=null,r=null,i=null,a=()=>{n!==null&&(clearTimeout(n),n=null),i&&(r??globalThis).removeEventListener(`scrollend`,i),i=null,r=null,t=!1};return{get active(){return t},start(){a(),t=!0;let o=()=>{a()};i=o,r=e(),(r??globalThis).addEventListener(`scrollend`,o,{once:!0}),n=setTimeout(o,500)},destroy(){a()}}},Ee=(e,t)=>{let n=null,r=null;return{schedule(){n===null&&(n=requestAnimationFrame(()=>{n=null,r!==null&&clearTimeout(r),r=setTimeout(()=>{r=null,e()},t)}))},destroy(){n!==null&&(cancelAnimationFrame(n),n=null),r!==null&&(clearTimeout(r),r=null)}}},De=(e,t,n,r,i,a)=>{let o=new Set,s=new Map,c=!1,l=null,u=e=>{if(!a()){for(let t of e)s.set(t.target,t);r()}},d=e=>new IntersectionObserver(u,{root:e,rootMargin:t,threshold:0}),f=n(),p=d(f),m=()=>{let t=n()??document,r;try{r=t.querySelectorAll(e)}catch{i();return}let a=new Set;for(let e of r){let t=e.id;t&&!c&&(a.has(t)&&(c=!0,console.warn(`[real-router] scroll-spy: duplicate id "${t}" observed. Selection picks the topmost visible match deterministically.`)),a.add(t)),o.has(e)||(p.observe(e),o.add(e))}},h={childList:!0,subtree:!0,attributes:!0,attributeFilter:[`id`]},g=null,_=()=>{for(let e of o)e.isConnected||(p.unobserve(e),o.delete(e),s.delete(e));let e=n();e!==f&&(f=e,p.disconnect(),p=d(e),o.clear(),s.clear(),g?.disconnect(),g?.observe(e??document.body,h)),m()};return m(),g=new MutationObserver(()=>{l!==null&&clearTimeout(l),l=setTimeout(()=>{l=null,_()},250)}),g.observe(f??document.body,h),{pending:s,isContainerDetached:()=>f!==null&&!f.isConnected,reconcile:_,destroy(){p.disconnect(),g.disconnect(),l!==null&&(clearTimeout(l),l=null),o.clear(),s.clear()}}};function Oe(e,t){if(typeof document>`u`||typeof IntersectionObserver>`u`)return Y;let{selector:n}=t;if(!n)return Y;let r=t.rootMargin??`-20% 0px -60% 0px`,i=t.scrollContainer,a=()=>i?.()??null,o=!1,s=!1,c=!1,l=()=>s||o,d=null,f=u(e),p=we(e,()=>{s=!0}),m=Te(a),h=Ee(()=>{d?.()},150),g=De(n,r,a,()=>{h.schedule()},()=>{s||(s=!0,console.warn(`[real-router] scroll-spy: invalid selector "${n}". Disabling.`))},l);d=()=>{if(o||s){g.pending.clear();return}if(f.getSnapshot().isTransitioning||m.active)return;let t=Ce(g.pending.values());if(g.pending.clear(),!t)return;let n=t.target.id;if(!n)return;let r=e.getState();if(!r||n===X(r)?.hash)return;let i={hash:n,replace:!0,force:!0,hashChange:!0};c=!0,e.navigate(r.name,r.params,r.search,i).catch(()=>{}).finally(()=>{c=!1})};let _=e.subscribe(({route:e})=>{c||(g.isContainerDetached()&&g.reconcile(),X(e)?.hashChanged&&m.start())});return{destroy(){o=!0,_(),g.destroy(),h.destroy(),m.destroy(),p.destroy()}}}const ke=Object.freeze({destroy:()=>{}});function Ae(e){if(typeof document>`u`||typeof document.startViewTransition!=`function`)return ke;let t=null,n=null,r=!1,i=()=>{t?.(),t=null},a=e.subscribeLeave(({signal:e})=>{if(!e.aborted)return r=!1,i(),new Promise(a=>{let o=new Promise(e=>{t=e});e.addEventListener(`abort`,()=>{r||(i(),n?.skipTransition?.(),a())},{once:!0});try{n=document.startViewTransition(()=>(a(),o))}catch{i(),a()}})}),o=e.subscribe(()=>{let e=t;if(r=!0,t=null,e===null)n=null;else{let t=n;setTimeout(()=>{e(),n===t&&(n=null)},0)}});return{destroy:()=>{a(),o(),n?.skipTransition?.(),n=null,i()}}}const Z=Object.keys,je=Object.hasOwn,Me=Symbol.for(`real-router.router`);function Ne(e){try{return e?.[Me]===!0}catch{return!1}}function Pe(e,t){try{return j(e)}catch{Ne(e)&&console.error(`[real-router] Route "${t}" rendered its LITERAL path: this IS a router, but not one this copy of @real-router/core built, so \`forwardTo\` was not resolved. It is either wrapped in a Proxy (Vue \`reactive()\` / Pinia — store it with \`markRaw\`), or your dependency tree holds two copies of @real-router/core — dedupe it to one.`);return}}function Fe(e,t,n,r){return e===void 0?{name:t,params:n,search:r}:((t!==``||n!==void 0||r!==void 0)&&console.warn("[real-router] <Link> received both `to` and channel props (routeName / routeParams / routeSearch). `to` wins; the channel props are ignored. Use one form or the other."),{name:e.name,params:e.params,search:e.search})}function Ie(e){return e.button===0&&!e.metaKey&&!e.altKey&&!e.ctrlKey&&!e.shiftKey}function Le(e){return!!e&&e!==`_self`}function Re(e){return encodeURI(e).replaceAll(`#`,`%23`)}const ze=/^\/[\t\n\r]*[/\\]/;function Be(e,t,n,r,i){try{let a;i!==void 0&&(a=i.startsWith(`#`)?i.slice(1):i);let o=e.buildUrl;if(o){let e=o(t,n,r,a===void 0?void 0:{hash:a});if(typeof e==`string`&&e.length>0)return e}let s,c=Pe(e,t);try{let e=c?.forwardState(t,n,r);s=e&&c?.buildPathResolved(e.name,e.params,e.search)}catch{s=void 0}let l=s??e.buildPath(t,n,r);if(typeof l!=`string`||l.length===0){console.error(`[real-router] Route "${t}" yielded an empty path. The element will render without an href attribute.`);return}let u=ze.test(l)?`/.${l}`:l;return a?`${u}#${Re(a)}`:u}catch{console.error(`[real-router] Route "${t}" is not defined. The element will render without an href attribute.`);return}}function Ve(e,t,n,r,i){try{let a=j(e),o=a.forwardState(t,n,r);return a.buildPathResolved(o.name,o.params,o.search)===i}catch{return!1}}function Q(e){let t=e;M(t,`force`,!0),M(t,`hashChange`,!0)}function He(e){return je(e,`reload`)&&!!e.reload||je(e,`force`)&&!!e.force}function Ue(e,t,n,r,i,a){let o={...a};i!==void 0&&M(o,`hash`,i);let s=e.getState(),c=r;if(s!==void 0){let a=r??s.search,l=e.isActiveRoute(t,n,a,!0,!1),u=s.context?.url?.hash??``;if(u!==(i??u)){if(l)Q(o),c=a;else if(!He(o)&&s.name!==v&&Ve(e,t,n,r,s.path))return Q(o),e.navigate(s.name,s.params,s.search,o)}}return e.navigate(t,n,c,o)}const We=/\s/,Ge=/\S+/g;function Ke(e){return We.test(e)?e.match(Ge)??[]:[e]}function qe(e,t,n){if(e&&t){let e=Ke(t);if(e.length===0)return n??void 0;if(!n)return e.join(` `);let r=Ke(n),i=new Set(r);for(let t of e)i.has(t)||(i.add(t),r.push(t));return r.join(` `)}return n??void 0}function $(e,t){if(Object.is(e,t))return!0;if(!e||!t)return!1;let n=Z(e),r=Z(t);if(n.length!==r.length)return!1;let i=e,a=t;for(let e of n)if(!r.includes(e)||!Object.is(i[e],a[e]))return!1;return!0}function Je(e,t,r,i=!1,o=!0,s){let c=F(),l=n(()=>a(c,e,t,r,i,o,s),[c,e,t,r,i,o,s]);return N(l.subscribe,l.getSnapshot,l.getSnapshot)}function Ye(e,t){return e.routeName===t.routeName&&e.className===t.className&&e.activeClassName===t.activeClassName&&e.activeStrict===t.activeStrict&&e.ignoreQueryParams===t.ignoreQueryParams&&e.onClick===t.onClick&&e.target===t.target&&e.style===t.style&&e.children===t.children&&e.hash===t.hash&&$(e.routeParams,t.routeParams)&&$(e.routeSearch,t.routeSearch)&&$(e.to,t.to)&&$(e.routeOptions,t.routeOptions)}const Xe=D(({routeName:e,routeParams:t,routeSearch:n,to:r,routeOptions:i=fe,className:a,activeClassName:o=`active`,activeStrict:s=!1,ignoreQueryParams:c=!0,hash:l,onClick:u,target:d,children:f,...p})=>{let m=F(),{name:h,params:g,search:_}=Fe(r,e??``,t,n),v=Je(h,g,_,s,c,l),y=g??de,b=Be(m,h,y,_,l),x=e=>{if(u){try{u(e)}catch(e){console.error(`[real-router] A <Link> onClick handler threw; navigation is unaffected.`,e)}if(e.defaultPrevented)return}Ie(e)&&!Le(d)&&(e.preventDefault(),Ue(m,h,y,_,l,i).catch(()=>{}))},S=qe(v,o,a);return k(`a`,{...p,target:d,href:b,className:S,onClick:x,children:f})},Ye);Xe.displayName=`Link`;function Ze({children:n,fallback:i,onError:a}){let s=F(),c=o(s),l=N(c.subscribe,c.getSnapshot,c.getSnapshot),u=r(a);return t(()=>{u.current=a}),e(()=>{l.error&&u.current?.(l.error,l.toRoute,l.fromRoute)},[l.version]),A(C,{children:[n,l.error?i(l.error,l.resetError):null]})}const Qe=()=>{let e=F();return x(j(e).getTree())};function $e(){let e=F(),t=u(e);return N(t.subscribe,t.getSnapshot,t.getSnapshot)}function et(n,i){let a=F(),o=r(n),s=i?.skipSameRoute??!0;t(()=>{o.current=n}),e(()=>a.subscribeLeave(d(e=>o.current(e),{skipSameRoute:s})),[a,s])}function tt(n,a){let{route:o,previousRoute:c}=_(),l=r(n),[u]=i(()=>s()),d=a?.skipSameRoute??!0;t(()=>{l.current=n}),e(()=>{let e=u(o,c,d);e&&l.current(e)},[u,o,c,d])}const nt=({router:t,children:r,announceNavigation:i,scrollRestoration:a,scrollSpy:o,viewTransitions:s})=>{let c=i!==void 0&&i!==!1,u=typeof i==`object`?i:void 0,d=u?.prefix;e(()=>{if(!c)return;let e=ge(t,u);return()=>{e.destroy()}},[t,c,d]);let p=a?.mode,_=a?.anchorScrolling,v=a?.behavior,y=a?.storageKey,x=a!==void 0;e(()=>{if(!x)return;let e=Se(t,{mode:p,anchorScrolling:_,behavior:v,storageKey:y,scrollContainer:a.scrollContainer});return()=>{e.destroy()}},[t,x,p,_,v,y]);let S=o?.selector,C=o?.rootMargin,w=o!==void 0&&S!==void 0&&S!==``;e(()=>{if(!w)return;let e=Oe(t,{selector:S,rootMargin:C,scrollContainer:o.scrollContainer});return()=>{e.destroy()}},[t,w,S,C]),e(()=>{if(!s)return;let e=Ae(t);return()=>{e.destroy()}},[t,s]);let T=b(t),E=n(()=>l(t),[t]);e(()=>{f(t)},[t]);let{route:D,previousRoute:O}=N(E.subscribe,E.getSnapshot,E.getSnapshot),A=n(()=>({navigator:T,route:D,previousRoute:O}),[T,D,O]);return k(m.Provider,{value:t,children:k(h.Provider,{value:T,children:k(g.Provider,{value:A,children:r})})})};export{Xe as Link,h as NavigatorContext,g as RouteContext,ue as RouteView,m as RouterContext,Ze as RouterErrorBoundary,nt as RouterProvider,me as createDirectionTracker,P as useNavigator,_ as useRoute,tt as useRouteEnter,et as useRouteExit,I as useRouteNode,Qe as useRouteUtils,F as useRouter,$e as useRouterTransition};
2
2
  //# sourceMappingURL=index.mjs.map