UNPKG

stone-kit

Version:
1,583 lines (1,581 loc) • 47.3 kB
import * as i from "react"; var ae = "popstate"; function Se(e = {}) { function t(n, a) { let { pathname: o, search: l, hash: s } = n.location; return q( "", { pathname: o, search: l, hash: s }, // state defaults to `null` because `window.history.state` does a.state && a.state.usr || null, a.state && a.state.key || "default" ); } function r(n, a) { return typeof a == "string" ? a : M(a); } return $e( t, r, null, e ); } function x(e, t) { if (e === !1 || e === null || typeof e > "u") throw new Error(t); } function P(e, t) { if (!e) { typeof console < "u" && console.warn(t); try { throw new Error(t); } catch { } } } function ke() { return Math.random().toString(36).substring(2, 10); } function oe(e, t) { return { usr: e.state, key: e.key, idx: t }; } function q(e, t, r = null, n) { return { pathname: typeof e == "string" ? e : e.pathname, search: "", hash: "", ...typeof t == "string" ? T(t) : t, state: r, // TODO: This could be cleaned up. push/replace should probably just take // full Locations now and avoid the need to run through this flow at all // But that's a pretty big refactor to the current test suite so going to // keep as is for the time being and just let any incoming keys take precedence key: t && t.key || n || ke() }; } function M({ pathname: e = "/", search: t = "", hash: r = "" }) { return t && t !== "?" && (e += t.charAt(0) === "?" ? t : "?" + t), r && r !== "#" && (e += r.charAt(0) === "#" ? r : "#" + r), e; } function T(e) { let t = {}; if (e) { let r = e.indexOf("#"); r >= 0 && (t.hash = e.substring(r), e = e.substring(0, r)); let n = e.indexOf("?"); n >= 0 && (t.search = e.substring(n), e = e.substring(0, n)), e && (t.pathname = e); } return t; } function $e(e, t, r, n = {}) { let { window: a = document.defaultView, v5Compat: o = !1 } = n, l = a.history, s = "POP", u = null, c = f(); c == null && (c = 0, l.replaceState({ ...l.state, idx: c }, "")); function f() { return (l.state || { idx: null }).idx; } function d() { s = "POP"; let h = f(), m = h == null ? null : h - c; c = h, u && u({ action: s, location: w.location, delta: m }); } function y(h, m) { s = "PUSH"; let p = q(w.location, h, m); c = f() + 1; let R = oe(p, c), E = w.createHref(p); try { l.pushState(R, "", E); } catch (C) { if (C instanceof DOMException && C.name === "DataCloneError") throw C; a.location.assign(E); } o && u && u({ action: s, location: w.location, delta: 1 }); } function g(h, m) { s = "REPLACE"; let p = q(w.location, h, m); c = f(); let R = oe(p, c), E = w.createHref(p); l.replaceState(R, "", E), o && u && u({ action: s, location: w.location, delta: 0 }); } function v(h) { return Fe(h); } let w = { get action() { return s; }, get location() { return e(a, l); }, listen(h) { if (u) throw new Error("A history only accepts one active listener"); return a.addEventListener(ae, d), u = h, () => { a.removeEventListener(ae, d), u = null; }; }, createHref(h) { return t(a, h); }, createURL: v, encodeLocation(h) { let m = v(h); return { pathname: m.pathname, search: m.search, hash: m.hash }; }, push: y, replace: g, go(h) { return l.go(h); } }; return w; } function Fe(e, t = !1) { let r = "http://localhost"; typeof window < "u" && (r = window.location.origin !== "null" ? window.location.origin : window.location.href), x(r, "No window.location.(origin|href) available to create URL"); let n = typeof e == "string" ? e : M(e); return n = n.replace(/ $/, "%20"), !t && n.startsWith("//") && (n = r + n), new URL(n, r); } function ce(e, t, r = "/") { return Ie(e, t, r, !1); } function Ie(e, t, r, n) { let a = typeof t == "string" ? T(t) : t, o = k(a.pathname || "/", r); if (o == null) return null; let l = fe(e); Te(l); let s = null; for (let u = 0; s == null && u < l.length; ++u) { let c = ze(o); s = He( l[u], c, n ); } return s; } function fe(e, t = [], r = [], n = "", a = !1) { let o = (l, s, u = a, c) => { let f = { relativePath: c === void 0 ? l.path || "" : c, caseSensitive: l.caseSensitive === !0, childrenIndex: s, route: l }; if (f.relativePath.startsWith("/")) { if (!f.relativePath.startsWith(n) && u) return; x( f.relativePath.startsWith(n), `Absolute route path "${f.relativePath}" nested under path "${n}" is not valid. An absolute child route path must start with the combined path of all its parent routes.` ), f.relativePath = f.relativePath.slice(n.length); } let d = S([n, f.relativePath]), y = r.concat(f); l.children && l.children.length > 0 && (x( // Our types know better, but runtime JS may not! // @ts-expect-error l.index !== !0, `Index routes must not have child routes. Please remove all child routes from route path "${d}".` ), fe( l.children, t, y, d, u )), !(l.path == null && !l.index) && t.push({ path: d, score: Ue(d, l.index), routesMeta: y }); }; return e.forEach((l, s) => { if (l.path === "" || !l.path?.includes("?")) o(l, s); else for (let u of de(l.path)) o(l, s, !0, u); }), t; } function de(e) { let t = e.split("/"); if (t.length === 0) return []; let [r, ...n] = t, a = r.endsWith("?"), o = r.replace(/\?$/, ""); if (n.length === 0) return a ? [o, ""] : [o]; let l = de(n.join("/")), s = []; return s.push( ...l.map( (u) => u === "" ? o : [o, u].join("/") ) ), a && s.push(...l), s.map( (u) => e.startsWith("/") && u === "" ? "/" : u ); } function Te(e) { e.sort( (t, r) => t.score !== r.score ? r.score - t.score : We( t.routesMeta.map((n) => n.childrenIndex), r.routesMeta.map((n) => n.childrenIndex) ) ); } var Ne = /^:[\w-]+$/, Be = 3, De = 2, Me = 1, Oe = 10, Ae = -2, le = (e) => e === "*"; function Ue(e, t) { let r = e.split("/"), n = r.length; return r.some(le) && (n += Ae), t && (n += De), r.filter((a) => !le(a)).reduce( (a, o) => a + (Ne.test(o) ? Be : o === "" ? Me : Oe), n ); } function We(e, t) { return e.length === t.length && e.slice(0, -1).every((n, a) => n === t[a]) ? ( // If two routes are siblings, we should try to match the earlier sibling // first. This allows people to have fine-grained control over the matching // behavior by simply putting routes with identical paths in the order they // want them tried. e[e.length - 1] - t[t.length - 1] ) : ( // Otherwise, it doesn't really make sense to rank non-siblings by index, // so they sort equally. 0 ); } function He(e, t, r = !1) { let { routesMeta: n } = e, a = {}, o = "/", l = []; for (let s = 0; s < n.length; ++s) { let u = n[s], c = s === n.length - 1, f = o === "/" ? t : t.slice(o.length) || "/", d = j( { path: u.relativePath, caseSensitive: u.caseSensitive, end: c }, f ), y = u.route; if (!d && c && r && !n[n.length - 1].route.index && (d = j( { path: u.relativePath, caseSensitive: u.caseSensitive, end: !1 }, f )), !d) return null; Object.assign(a, d.params), l.push({ // TODO: Can this as be avoided? params: a, pathname: S([o, d.pathname]), pathnameBase: Ye( S([o, d.pathnameBase]) ), route: y }), d.pathnameBase !== "/" && (o = S([o, d.pathnameBase])); } return l; } function j(e, t) { typeof e == "string" && (e = { path: e, caseSensitive: !1, end: !0 }); let [r, n] = _e( e.path, e.caseSensitive, e.end ), a = t.match(r); if (!a) return null; let o = a[0], l = o.replace(/(.)\/+$/, "$1"), s = a.slice(1); return { params: n.reduce( (c, { paramName: f, isOptional: d }, y) => { if (f === "*") { let v = s[y] || ""; l = o.slice(0, o.length - v.length).replace(/(.)\/+$/, "$1"); } const g = s[y]; return d && !g ? c[f] = void 0 : c[f] = (g || "").replace(/%2F/g, "/"), c; }, {} ), pathname: o, pathnameBase: l, pattern: e }; } function _e(e, t = !1, r = !0) { P( 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, (l, s, u) => (n.push({ paramName: s, isOptional: u != null }), u ? "/?([^\\/]+)?" : "/([^\\/]+)") ).replace(/\/([\w-]+)\?(\/|$)/g, "(/$1)?$2"); return e.endsWith("*") ? (n.push({ paramName: "*" }), a += e === "*" || e === "/*" ? "(.*)$" : "(?:\\/(.+)|\\/*)$") : r ? a += "\\/*$" : e !== "" && e !== "/" && (a += "(?:(?=\\/|$))"), [new RegExp(a, t ? void 0 : "i"), n]; } function ze(e) { try { return e.split("/").map((t) => decodeURIComponent(t).replace(/\//g, "%2F")).join("/"); } catch (t) { return P( !1, `The URL path "${e}" could not be decoded because it is a malformed URL segment. This is probably due to a bad percent encoding (${t}).` ), e; } } function k(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) || "/"; } var Ve = /^(?:[a-z][a-z0-9+.-]*:|\/\/)/i, je = (e) => Ve.test(e); function Ke(e, t = "/") { let { pathname: r, search: n = "", hash: a = "" } = typeof e == "string" ? T(e) : e, o; if (r) if (je(r)) o = r; else { if (r.includes("//")) { let l = r; r = r.replace(/\/\/+/g, "/"), P( !1, `Pathnames cannot have embedded double slashes - normalizing ${l} -> ${r}` ); } r.startsWith("/") ? o = ie(r.substring(1), "/") : o = ie(r, t); } else o = t; return { pathname: o, search: Ge(n), hash: qe(a) }; } function ie(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 Y(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 Je(e) { return e.filter( (t, r) => r === 0 || t.route.path && t.route.path.length > 0 ); } function he(e) { let t = Je(e); return t.map( (r, n) => n === t.length - 1 ? r.pathname : r.pathnameBase ); } function pe(e, t, r, n = !1) { let a; typeof e == "string" ? a = T(e) : (a = { ...e }, x( !a.pathname || !a.pathname.includes("?"), Y("?", "pathname", "search", a) ), x( !a.pathname || !a.pathname.includes("#"), Y("#", "pathname", "hash", a) ), x( !a.search || !a.search.includes("#"), Y("#", "search", "hash", a) )); let o = e === "" || a.pathname === "", l = o ? "/" : a.pathname, s; if (l == null) s = r; else { let d = t.length - 1; if (!n && l.startsWith("..")) { let y = l.split("/"); for (; y[0] === ".."; ) y.shift(), d -= 1; a.pathname = y.join("/"); } s = d >= 0 ? t[d] : "/"; } let u = Ke(a, s), c = l && l !== "/" && l.endsWith("/"), f = (o || l === ".") && r.endsWith("/"); return !u.pathname.endsWith("/") && (c || f) && (u.pathname += "/"), u; } var S = (e) => e.join("/").replace(/\/\/+/g, "/"), Ye = (e) => e.replace(/\/+$/, "").replace(/^\/*/, "/"), Ge = (e) => !e || e === "?" ? "" : e.startsWith("?") ? e : "?" + e, qe = (e) => !e || e === "#" ? "" : e.startsWith("#") ? e : "#" + e; function Xe(e) { return e != null && typeof e.status == "number" && typeof e.statusText == "string" && typeof e.internal == "boolean" && "data" in e; } Object.getOwnPropertyNames(Object.prototype).sort().join("\0"); var me = [ "POST", "PUT", "PATCH", "DELETE" ]; new Set( me ); var Qe = [ "GET", ...me ]; new Set(Qe); var N = i.createContext(null); N.displayName = "DataRouter"; var K = i.createContext(null); K.displayName = "DataRouterState"; i.createContext(!1); var ye = i.createContext({ isTransitioning: !1 }); ye.displayName = "ViewTransition"; var Ze = i.createContext( /* @__PURE__ */ new Map() ); Ze.displayName = "Fetchers"; var et = i.createContext(null); et.displayName = "Await"; var L = i.createContext( null ); L.displayName = "Navigation"; var O = i.createContext( null ); O.displayName = "Location"; var $ = i.createContext({ outlet: null, matches: [], isDataRoute: !1 }); $.displayName = "Route"; var Q = i.createContext(null); Q.displayName = "RouteError"; function tt(e, { relative: t } = {}) { x( A(), // TODO: This error is probably because they somehow have 2 versions of the // router loaded. We can help them understand how to avoid that. "useHref() may be used only in the context of a <Router> component." ); let { basename: r, navigator: n } = i.useContext(L), { hash: a, pathname: o, search: l } = U(e, { relative: t }), s = o; return r !== "/" && (s = o === "/" ? r : S([r, o])), n.createHref({ pathname: s, search: l, hash: a }); } function A() { return i.useContext(O) != null; } function F() { return x( A(), // TODO: This error is probably because they somehow have 2 versions of the // router loaded. We can help them understand how to avoid that. "useLocation() may be used only in the context of a <Router> component." ), i.useContext(O).location; } var ge = "You should call navigate() in a React.useEffect(), not when your component is first rendered."; function ve(e) { i.useContext(L).static || i.useLayoutEffect(e); } function rt() { let { isDataRoute: e } = i.useContext($); return e ? mt() : nt(); } function nt() { x( A(), // TODO: This error is probably because they somehow have 2 versions of the // router loaded. We can help them understand how to avoid that. "useNavigate() may be used only in the context of a <Router> component." ); let e = i.useContext(N), { basename: t, navigator: r } = i.useContext(L), { matches: n } = i.useContext($), { pathname: a } = F(), o = JSON.stringify(he(n)), l = i.useRef(!1); return ve(() => { l.current = !0; }), i.useCallback( (u, c = {}) => { if (P(l.current, ge), !l.current) return; if (typeof u == "number") { r.go(u); return; } let f = pe( u, JSON.parse(o), a, c.relative === "path" ); e == null && t !== "/" && (f.pathname = f.pathname === "/" ? t : S([t, f.pathname])), (c.replace ? r.replace : r.push)( f, c.state, c ); }, [ t, r, o, a, e ] ); } i.createContext(null); function U(e, { relative: t } = {}) { let { matches: r } = i.useContext($), { pathname: n } = F(), a = JSON.stringify(he(r)); return i.useMemo( () => pe( e, JSON.parse(a), n, t === "path" ), [e, a, n, t] ); } function at(e, t) { return we(e, t); } function we(e, t, r, n, a) { x( A(), // TODO: This error is probably because they somehow have 2 versions of the // router loaded. We can help them understand how to avoid that. "useRoutes() may be used only in the context of a <Router> component." ); let { navigator: o } = i.useContext(L), { matches: l } = i.useContext($), s = l[l.length - 1], u = s ? s.params : {}, c = s ? s.pathname : "/", f = s ? s.pathnameBase : "/", d = s && s.route; { let p = d && d.path || ""; Re( c, !d || p.endsWith("*") || p.endsWith("*?"), `You rendered descendant <Routes> (or called \`useRoutes()\`) at "${c}" (under <Route path="${p}">) but the parent route path has no trailing "*". This means if you navigate deeper, the parent won't match anymore and therefore the child routes will never render. Please change the parent <Route path="${p}"> to <Route path="${p === "/" ? "*" : `${p}/*`}">.` ); } let y = F(), g; if (t) { let p = typeof t == "string" ? T(t) : t; x( f === "/" || p.pathname?.startsWith(f), `When overriding the location using \`<Routes location>\` or \`useRoutes(routes, location)\`, the location pathname must begin with the portion of the URL pathname that was matched by all parent routes. The current pathname base is "${f}" but pathname "${p.pathname}" was given in the \`location\` prop.` ), g = p; } else g = y; let v = g.pathname || "/", w = v; if (f !== "/") { let p = f.replace(/^\//, "").split("/"); w = "/" + v.replace(/^\//, "").split("/").slice(p.length).join("/"); } let h = ce(e, { pathname: w }); P( d || h != null, `No routes matched location "${g.pathname}${g.search}${g.hash}" ` ), P( h == null || h[h.length - 1].route.element !== void 0 || h[h.length - 1].route.Component !== void 0 || h[h.length - 1].route.lazy !== void 0, `Matched leaf route at location "${g.pathname}${g.search}${g.hash}" does not have an element or Component. This means it will render an <Outlet /> with a null value by default resulting in an "empty" page.` ); let m = st( h && h.map( (p) => Object.assign({}, p, { params: Object.assign({}, u, p.params), pathname: S([ f, // Re-encode pathnames that were decoded inside matchRoutes. // Pre-encode `?` and `#` ahead of `encodeLocation` because it uses // `new URL()` internally and we need to prevent it from treating // them as separators o.encodeLocation ? o.encodeLocation( p.pathname.replace(/\?/g, "%3F").replace(/#/g, "%23") ).pathname : p.pathname ]), pathnameBase: p.pathnameBase === "/" ? f : S([ f, // Re-encode pathnames that were decoded inside matchRoutes // Pre-encode `?` and `#` ahead of `encodeLocation` because it uses // `new URL()` internally and we need to prevent it from treating // them as separators o.encodeLocation ? o.encodeLocation( p.pathnameBase.replace(/\?/g, "%3F").replace(/#/g, "%23") ).pathname : p.pathnameBase ]) }) ), l, r, n, a ); return t && m ? /* @__PURE__ */ i.createElement( O.Provider, { value: { location: { pathname: "/", search: "", hash: "", state: null, key: "default", ...g }, navigationType: "POP" /* Pop */ } }, m ) : m; } function ot() { let e = pt(), t = Xe(e) ? `${e.status} ${e.statusText}` : e instanceof Error ? e.message : JSON.stringify(e), r = e instanceof Error ? e.stack : null, n = "rgba(200,200,200, 0.5)", a = { padding: "0.5rem", backgroundColor: n }, o = { padding: "2px 4px", backgroundColor: n }, l = null; return console.error( "Error handled by React Router default ErrorBoundary:", e ), l = /* @__PURE__ */ i.createElement(i.Fragment, null, /* @__PURE__ */ i.createElement("p", null, "💿 Hey developer 👋"), /* @__PURE__ */ i.createElement("p", null, "You can provide a way better UX than this when your app throws errors by providing your own ", /* @__PURE__ */ i.createElement("code", { style: o }, "ErrorBoundary"), " or", " ", /* @__PURE__ */ i.createElement("code", { style: o }, "errorElement"), " prop on your route.")), /* @__PURE__ */ i.createElement(i.Fragment, null, /* @__PURE__ */ i.createElement("h2", null, "Unexpected Application Error!"), /* @__PURE__ */ i.createElement("h3", { style: { fontStyle: "italic" } }, t), r ? /* @__PURE__ */ i.createElement("pre", { style: a }, r) : null, l); } var lt = /* @__PURE__ */ i.createElement(ot, null), it = class extends i.Component { constructor(e) { super(e), this.state = { location: e.location, revalidation: e.revalidation, error: e.error }; } static getDerivedStateFromError(e) { return { error: e }; } static getDerivedStateFromProps(e, t) { return t.location !== e.location || t.revalidation !== "idle" && e.revalidation === "idle" ? { error: e.error, location: e.location, revalidation: e.revalidation } : { error: e.error !== void 0 ? e.error : t.error, location: t.location, revalidation: e.revalidation || t.revalidation }; } componentDidCatch(e, t) { this.props.onError ? this.props.onError(e, t) : console.error( "React Router caught the following error during render", e ); } render() { return this.state.error !== void 0 ? /* @__PURE__ */ i.createElement($.Provider, { value: this.props.routeContext }, /* @__PURE__ */ i.createElement( Q.Provider, { value: this.state.error, children: this.props.component } )) : this.props.children; } }; function ut({ routeContext: e, match: t, children: r }) { let n = i.useContext(N); return n && n.static && n.staticContext && (t.route.errorElement || t.route.ErrorBoundary) && (n.staticContext._deepestRenderedBoundaryId = t.route.id), /* @__PURE__ */ i.createElement($.Provider, { value: e }, r); } function st(e, t = [], r = null, n = null, a = null) { if (e == null) { if (!r) return null; if (r.errors) e = r.matches; else if (t.length === 0 && !r.initialized && r.matches.length > 0) e = r.matches; else return null; } let o = e, l = r?.errors; if (l != null) { let f = o.findIndex( (d) => d.route.id && l?.[d.route.id] !== void 0 ); x( f >= 0, `Could not find a matching route for errors on route IDs: ${Object.keys( l ).join(",")}` ), o = o.slice( 0, Math.min(o.length, f + 1) ); } let s = !1, u = -1; if (r) for (let f = 0; f < o.length; f++) { let d = o[f]; if ((d.route.HydrateFallback || d.route.hydrateFallbackElement) && (u = f), d.route.id) { let { loaderData: y, errors: g } = r, v = d.route.loader && !y.hasOwnProperty(d.route.id) && (!g || g[d.route.id] === void 0); if (d.route.lazy || v) { s = !0, u >= 0 ? o = o.slice(0, u + 1) : o = [o[0]]; break; } } } let c = r && n ? (f, d) => { n(f, { location: r.location, params: r.matches?.[0]?.params ?? {}, errorInfo: d }); } : void 0; return o.reduceRight( (f, d, y) => { let g, v = !1, w = null, h = null; r && (g = l && d.route.id ? l[d.route.id] : void 0, w = d.route.errorElement || lt, s && (u < 0 && y === 0 ? (Re( "route-fallback", !1, "No `HydrateFallback` element provided to render during initial hydration" ), v = !0, h = null) : u === y && (v = !0, h = d.route.hydrateFallbackElement || null))); let m = t.concat(o.slice(0, y + 1)), p = () => { let R; return g ? R = w : v ? R = h : d.route.Component ? R = /* @__PURE__ */ i.createElement(d.route.Component, null) : d.route.element ? R = d.route.element : R = f, /* @__PURE__ */ i.createElement( ut, { match: d, routeContext: { outlet: f, matches: m, isDataRoute: r != null }, children: R } ); }; return r && (d.route.ErrorBoundary || d.route.errorElement || y === 0) ? /* @__PURE__ */ i.createElement( it, { location: r.location, revalidation: r.revalidation, component: w, error: g, children: p(), routeContext: { outlet: null, matches: m, isDataRoute: !0 }, onError: c } ) : p(); }, null ); } function Z(e) { return `${e} must be used within a data router. See https://reactrouter.com/en/main/routers/picking-a-router.`; } function ct(e) { let t = i.useContext(N); return x(t, Z(e)), t; } function ft(e) { let t = i.useContext(K); return x(t, Z(e)), t; } function dt(e) { let t = i.useContext($); return x(t, Z(e)), t; } function ee(e) { let t = dt(e), r = t.matches[t.matches.length - 1]; return x( r.route.id, `${e} can only be used on routes that contain a unique "id"` ), r.route.id; } function ht() { return ee( "useRouteId" /* UseRouteId */ ); } function pt() { let e = i.useContext(Q), t = ft( "useRouteError" /* UseRouteError */ ), r = ee( "useRouteError" /* UseRouteError */ ); return e !== void 0 ? e : t.errors?.[r]; } function mt() { let { router: e } = ct( "useNavigate" /* UseNavigateStable */ ), t = ee( "useNavigate" /* UseNavigateStable */ ), r = i.useRef(!1); return ve(() => { r.current = !0; }), i.useCallback( async (a, o = {}) => { P(r.current, ge), r.current && (typeof a == "number" ? e.navigate(a) : await e.navigate(a, { fromRouteId: t, ...o })); }, [e, t] ); } var ue = {}; function Re(e, t, r) { !t && !ue[e] && (ue[e] = !0, P(!1, r)); } i.memo(yt); function yt({ routes: e, future: t, state: r, unstable_onError: n }) { return we(e, void 0, r, n, t); } function gt(e) { x( !1, "A <Route> is only ever to be used as the child of <Routes> element, never rendered directly. Please wrap your <Route> in a <Routes>." ); } function vt({ basename: e = "/", children: t = null, location: r, navigationType: n = "POP", navigator: a, static: o = !1 }) { x( !A(), "You cannot render a <Router> inside another <Router>. You should never have more than one in your app." ); let l = e.replace(/^\/*/, "/"), s = i.useMemo( () => ({ basename: l, navigator: a, static: o, future: {} }), [l, a, o] ); typeof r == "string" && (r = T(r)); let { pathname: u = "/", search: c = "", hash: f = "", state: d = null, key: y = "default" } = r, g = i.useMemo(() => { let v = k(u, l); return v == null ? null : { location: { pathname: v, search: c, hash: f, state: d, key: y }, navigationType: n }; }, [l, u, c, f, d, y, n]); return P( g != null, `<Router basename="${l}"> is not able to match the URL "${u}${c}${f}" because it does not start with the basename, so the <Router> won't render anything.` ), g == null ? null : /* @__PURE__ */ i.createElement(L.Provider, { value: s }, /* @__PURE__ */ i.createElement(O.Provider, { children: t, value: g })); } function qt({ children: e, location: t }) { return at(X(e), t); } function X(e, t = []) { let r = []; return i.Children.forEach(e, (n, a) => { if (!i.isValidElement(n)) return; let o = [...t, a]; if (n.type === i.Fragment) { r.push.apply( r, X(n.props.children, o) ); return; } x( n.type === gt, `[${typeof n.type == "string" ? n.type : n.type.name}] is not a <Route> component. All component children of <Routes> must be a <Route> or <React.Fragment>` ), x( !n.props.index || !n.props.children, "An index route cannot have child routes." ); let l = { id: n.props.id || o.join("-"), caseSensitive: n.props.caseSensitive, element: n.props.element, Component: n.props.Component, index: n.props.index, path: n.props.path, middleware: n.props.middleware, loader: n.props.loader, action: n.props.action, hydrateFallbackElement: n.props.hydrateFallbackElement, HydrateFallback: n.props.HydrateFallback, errorElement: n.props.errorElement, ErrorBoundary: n.props.ErrorBoundary, hasErrorBoundary: n.props.hasErrorBoundary === !0 || n.props.ErrorBoundary != null || n.props.errorElement != null, shouldRevalidate: n.props.shouldRevalidate, handle: n.props.handle, lazy: n.props.lazy }; n.props.children && (l.children = X( n.props.children, o )), r.push(l); }), r; } var z = "get", V = "application/x-www-form-urlencoded"; function J(e) { return e != null && typeof e.tagName == "string"; } function wt(e) { return J(e) && e.tagName.toLowerCase() === "button"; } function Rt(e) { return J(e) && e.tagName.toLowerCase() === "form"; } function xt(e) { return J(e) && e.tagName.toLowerCase() === "input"; } function Et(e) { return !!(e.metaKey || e.altKey || e.ctrlKey || e.shiftKey); } function Ct(e, t) { return e.button === 0 && // Ignore everything but left clicks (!t || t === "_self") && // Let browser handle "target=_blank" etc. !Et(e); } var _ = null; function bt() { if (_ === null) try { new FormData( document.createElement("form"), // @ts-expect-error if FormData supports the submitter parameter, this will throw 0 ), _ = !1; } catch { _ = !0; } return _; } var Pt = /* @__PURE__ */ new Set([ "application/x-www-form-urlencoded", "multipart/form-data", "text/plain" ]); function G(e) { return e != null && !Pt.has(e) ? (P( !1, `"${e}" is not a valid \`encType\` for \`<Form>\`/\`<fetcher.Form>\` and will default to "${V}"` ), null) : e; } function Lt(e, t) { let r, n, a, o, l; if (Rt(e)) { let s = e.getAttribute("action"); n = s ? k(s, t) : null, r = e.getAttribute("method") || z, a = G(e.getAttribute("enctype")) || V, o = new FormData(e); } else if (wt(e) || xt(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 u = e.getAttribute("formaction") || s.getAttribute("action"); if (n = u ? k(u, t) : null, r = e.getAttribute("formmethod") || s.getAttribute("method") || z, a = G(e.getAttribute("formenctype")) || G(s.getAttribute("enctype")) || V, o = new FormData(s, e), !bt()) { let { name: c, type: f, value: d } = e; if (f === "image") { let y = c ? `${c}.` : ""; o.append(`${y}x`, "0"), o.append(`${y}y`, "0"); } else c && o.append(c, d); } } else { if (J(e)) throw new Error( 'Cannot submit element that is not <form>, <button>, or <input type="submit|image">' ); r = z, n = null, a = V, l = e; } return o && a === "text/plain" && (l = o, o = void 0), { action: n, method: r.toLowerCase(), encType: a, formData: o, body: l }; } Object.getOwnPropertyNames(Object.prototype).sort().join("\0"); function te(e, t) { if (e === !1 || e === null || typeof e > "u") throw new Error(t); } function St(e, t, r) { let n = typeof e == "string" ? new URL( e, // This can be called during the SSR flow via PrefetchPageLinksImpl so // don't assume window is available typeof window > "u" ? "server://singlefetch/" : window.location.origin ) : e; return n.pathname === "/" ? n.pathname = `_root.${r}` : t && k(n.pathname, t) === "/" ? n.pathname = `${t.replace(/\/$/, "")}/_root.${r}` : n.pathname = `${n.pathname.replace(/\/$/, "")}.${r}`, n; } async function kt(e, t) { if (e.id in t) return t[e.id]; try { let r = await import( /* @vite-ignore */ /* webpackIgnore: true */ e.module ); return t[e.id] = r, r; } catch (r) { return console.error( `Error loading route module \`${e.module}\`, reloading page...` ), console.error(r), window.__reactRouterContext && window.__reactRouterContext.isSpaMode, window.location.reload(), new Promise(() => { }); } } function $t(e) { return e == null ? !1 : e.href == null ? e.rel === "preload" && typeof e.imageSrcSet == "string" && typeof e.imageSizes == "string" : typeof e.rel == "string" && typeof e.href == "string"; } async function Ft(e, t, r) { let n = await Promise.all( e.map(async (a) => { let o = t.routes[a.route.id]; if (o) { let l = await kt(o, r); return l.links ? l.links() : []; } return []; }) ); return Bt( n.flat(1).filter($t).filter((a) => a.rel === "stylesheet" || a.rel === "preload").map( (a) => a.rel === "stylesheet" ? { ...a, rel: "prefetch", as: "style" } : { ...a, rel: "prefetch" } ) ); } function se(e, t, r, n, a, o) { let l = (u, c) => r[c] ? u.route.id !== r[c].route.id : !0, s = (u, c) => ( // param change, /users/123 -> /users/456 r[c].pathname !== u.pathname || // splat param changed, which is not present in match.path // e.g. /files/images/avatar.jpg -> files/finances.xls r[c].route.path?.endsWith("*") && r[c].params["*"] !== u.params["*"] ); return o === "assets" ? t.filter( (u, c) => l(u, c) || s(u, c) ) : o === "data" ? t.filter((u, c) => { let f = n.routes[u.route.id]; if (!f || !f.hasLoader) return !1; if (l(u, c) || s(u, c)) return !0; if (u.route.shouldRevalidate) { let d = u.route.shouldRevalidate({ currentUrl: new URL( a.pathname + a.search + a.hash, window.origin ), currentParams: r[0]?.params || {}, nextUrl: new URL(e, window.origin), nextParams: u.params, defaultShouldRevalidate: !0 }); if (typeof d == "boolean") return d; } return !0; }) : []; } function It(e, t, { includeHydrateFallback: r } = {}) { return Tt( e.map((n) => { let a = t.routes[n.route.id]; if (!a) return []; let o = [a.module]; return a.clientActionModule && (o = o.concat(a.clientActionModule)), a.clientLoaderModule && (o = o.concat(a.clientLoaderModule)), r && a.hydrateFallbackModule && (o = o.concat(a.hydrateFallbackModule)), a.imports && (o = o.concat(a.imports)), o; }).flat(1) ); } function Tt(e) { return [...new Set(e)]; } function Nt(e) { let t = {}, r = Object.keys(e).sort(); for (let n of r) t[n] = e[n]; return t; } function Bt(e, t) { let r = /* @__PURE__ */ new Set(); return new Set(t), e.reduce((n, a) => { let o = JSON.stringify(Nt(a)); return r.has(o) || (r.add(o), n.push({ key: o, link: a })), n; }, []); } function xe() { let e = i.useContext(N); return te( e, "You must render this element inside a <DataRouterContext.Provider> element" ), e; } function Dt() { let e = i.useContext(K); return te( e, "You must render this element inside a <DataRouterStateContext.Provider> element" ), e; } var re = i.createContext(void 0); re.displayName = "FrameworkContext"; function Ee() { let e = i.useContext(re); return te( e, "You must render this element inside a <HydratedRouter> element" ), e; } function Mt(e, t) { let r = i.useContext(re), [n, a] = i.useState(!1), [o, l] = i.useState(!1), { onFocus: s, onBlur: u, onMouseEnter: c, onMouseLeave: f, onTouchStart: d } = t, y = i.useRef(null); i.useEffect(() => { if (e === "render" && l(!0), e === "viewport") { let w = (m) => { m.forEach((p) => { l(p.isIntersecting); }); }, h = new IntersectionObserver(w, { threshold: 0.5 }); return y.current && h.observe(y.current), () => { h.disconnect(); }; } }, [e]), i.useEffect(() => { if (n) { let w = setTimeout(() => { l(!0); }, 100); return () => { clearTimeout(w); }; } }, [n]); let g = () => { a(!0); }, v = () => { a(!1), l(!1); }; return r ? e !== "intent" ? [o, y, {}] : [ o, y, { onFocus: D(s, g), onBlur: D(u, v), onMouseEnter: D(c, g), onMouseLeave: D(f, v), onTouchStart: D(d, g) } ] : [!1, y, {}]; } function D(e, t) { return (r) => { e && e(r), r.defaultPrevented || t(r); }; } function Ot({ page: e, ...t }) { let { router: r } = xe(), n = i.useMemo( () => ce(r.routes, e, r.basename), [r.routes, e, r.basename] ); return n ? /* @__PURE__ */ i.createElement(Ut, { page: e, matches: n, ...t }) : null; } function At(e) { let { manifest: t, routeModules: r } = Ee(), [n, a] = i.useState([]); return i.useEffect(() => { let o = !1; return Ft(e, t, r).then( (l) => { o || a(l); } ), () => { o = !0; }; }, [e, t, r]), n; } function Ut({ page: e, matches: t, ...r }) { let n = F(), { manifest: a, routeModules: o } = Ee(), { basename: l } = xe(), { loaderData: s, matches: u } = Dt(), c = i.useMemo( () => se( e, t, u, a, n, "data" ), [e, t, u, a, n] ), f = i.useMemo( () => se( e, t, u, a, n, "assets" ), [e, t, u, a, n] ), d = i.useMemo(() => { if (e === n.pathname + n.search + n.hash) return []; let v = /* @__PURE__ */ new Set(), w = !1; if (t.forEach((m) => { let p = a.routes[m.route.id]; !p || !p.hasLoader || (!c.some((R) => R.route.id === m.route.id) && m.route.id in s && o[m.route.id]?.shouldRevalidate || p.hasClientLoader ? w = !0 : v.add(m.route.id)); }), v.size === 0) return []; let h = St(e, l, "data"); return w && v.size > 0 && h.searchParams.set( "_routes", t.filter((m) => v.has(m.route.id)).map((m) => m.route.id).join(",") ), [h.pathname + h.search]; }, [ l, s, n, a, c, t, e, o ]), y = i.useMemo( () => It(f, a), [f, a] ), g = At(f); return /* @__PURE__ */ i.createElement(i.Fragment, null, d.map((v) => /* @__PURE__ */ i.createElement("link", { key: v, rel: "prefetch", as: "fetch", href: v, ...r })), y.map((v) => /* @__PURE__ */ i.createElement("link", { key: v, rel: "modulepreload", href: v, ...r })), g.map(({ key: v, link: w }) => ( // these don't spread `linkProps` because they are full link descriptors // already with their own props /* @__PURE__ */ i.createElement("link", { key: v, nonce: r.nonce, ...w }) ))); } function Wt(...e) { return (t) => { e.forEach((r) => { typeof r == "function" ? r(t) : r != null && (r.current = t); }); }; } var Ce = typeof window < "u" && typeof window.document < "u" && typeof window.document.createElement < "u"; try { Ce && (window.__reactRouterVersion = // @ts-expect-error "7.9.6"); } catch { } function Xt({ basename: e, children: t, window: r }) { let n = i.useRef(); n.current == null && (n.current = Se({ window: r, v5Compat: !0 })); let a = n.current, [o, l] = i.useState({ action: a.action, location: a.location }), s = i.useCallback( (u) => { i.startTransition(() => l(u)); }, [l] ); return i.useLayoutEffect(() => a.listen(s), [a, s]), /* @__PURE__ */ i.createElement( vt, { basename: e, children: t, location: o.location, navigationType: o.action, navigator: a } ); } var be = /^(?:[a-z][a-z0-9+.-]*:|\/\/)/i, Pe = i.forwardRef( function({ onClick: t, discover: r = "render", prefetch: n = "none", relative: a, reloadDocument: o, replace: l, state: s, target: u, to: c, preventScrollReset: f, viewTransition: d, ...y }, g) { let { basename: v } = i.useContext(L), w = typeof c == "string" && be.test(c), h, m = !1; if (typeof c == "string" && w && (h = c, Ce)) try { let b = new URL(window.location.href), I = c.startsWith("//") ? new URL(b.protocol + c) : new URL(c), ne = k(I.pathname, v); I.origin === b.origin && ne != null ? c = ne + I.search + I.hash : m = !0; } catch { P( !1, `<Link to="${c}"> contains an invalid URL which will probably break when clicked - please update to a valid URL path.` ); } let p = tt(c, { relative: a }), [R, E, C] = Mt( n, y ), W = Vt(c, { replace: l, state: s, target: u, preventScrollReset: f, relative: a, viewTransition: d }); function B(b) { t && t(b), b.defaultPrevented || W(b); } let H = ( // eslint-disable-next-line jsx-a11y/anchor-has-content /* @__PURE__ */ i.createElement( "a", { ...y, ...C, href: h || p, onClick: m || o ? t : B, ref: Wt(g, E), target: u, "data-discover": !w && r === "render" ? "true" : void 0 } ) ); return R && !w ? /* @__PURE__ */ i.createElement(i.Fragment, null, H, /* @__PURE__ */ i.createElement(Ot, { page: p })) : H; } ); Pe.displayName = "Link"; var Ht = i.forwardRef( function({ "aria-current": t = "page", caseSensitive: r = !1, className: n = "", end: a = !1, style: o, to: l, viewTransition: s, children: u, ...c }, f) { let d = U(l, { relative: c.relative }), y = F(), g = i.useContext(K), { navigator: v, basename: w } = i.useContext(L), h = g != null && // Conditional usage is OK here because the usage of a data router is static // eslint-disable-next-line react-hooks/rules-of-hooks Gt(d) && s === !0, m = v.encodeLocation ? v.encodeLocation(d).pathname : d.pathname, p = y.pathname, R = g && g.navigation && g.navigation.location ? g.navigation.location.pathname : null; r || (p = p.toLowerCase(), R = R ? R.toLowerCase() : null, m = m.toLowerCase()), R && w && (R = k(R, w) || R); const E = m !== "/" && m.endsWith("/") ? m.length - 1 : m.length; let C = p === m || !a && p.startsWith(m) && p.charAt(E) === "/", W = R != null && (R === m || !a && R.startsWith(m) && R.charAt(m.length) === "/"), B = { isActive: C, isPending: W, isTransitioning: h }, H = C ? t : void 0, b; typeof n == "function" ? b = n(B) : b = [ n, C ? "active" : null, W ? "pending" : null, h ? "transitioning" : null ].filter(Boolean).join(" "); let I = typeof o == "function" ? o(B) : o; return /* @__PURE__ */ i.createElement( Pe, { ...c, "aria-current": H, className: b, ref: f, style: I, to: l, viewTransition: s }, typeof u == "function" ? u(B) : u ); } ); Ht.displayName = "NavLink"; var _t = i.forwardRef( ({ discover: e = "render", fetcherKey: t, navigate: r, reloadDocument: n, replace: a, state: o, method: l = z, action: s, onSubmit: u, relative: c, preventScrollReset: f, viewTransition: d, ...y }, g) => { let v = Jt(), w = Yt(s, { relative: c }), h = l.toLowerCase() === "get" ? "get" : "post", m = typeof s == "string" && be.test(s), p = (R) => { if (u && u(R), R.defaultPrevented) return; R.preventDefault(); let E = R.nativeEvent.submitter, C = E?.getAttribute("formmethod") || l; v(E || R.currentTarget, { fetcherKey: t, method: C, navigate: r, replace: a, state: o, relative: c, preventScrollReset: f, viewTransition: d }); }; return /* @__PURE__ */ i.createElement( "form", { ref: g, method: h, action: w, onSubmit: n ? u : p, ...y, "data-discover": !m && e === "render" ? "true" : void 0 } ); } ); _t.displayName = "Form"; function zt(e) { return `${e} must be used within a data router. See https://reactrouter.com/en/main/routers/picking-a-router.`; } function Le(e) { let t = i.useContext(N); return x(t, zt(e)), t; } function Vt(e, { target: t, replace: r, state: n, preventScrollReset: a, relative: o, viewTransition: l } = {}) { let s = rt(), u = F(), c = U(e, { relative: o }); return i.useCallback( (f) => { if (Ct(f, t)) { f.preventDefault(); let d = r !== void 0 ? r : M(u) === M(c); s(e, { replace: d, state: n, preventScrollReset: a, relative: o, viewTransition: l }); } }, [ u, s, c, r, n, t, e, a, o, l ] ); } var jt = 0, Kt = () => `__${String(++jt)}__`; function Jt() { let { router: e } = Le( "useSubmit" /* UseSubmit */ ), { basename: t } = i.useContext(L), r = ht(); return i.useCallback( async (n, a = {}) => { let { action: o, method: l, encType: s, formData: u, body: c } = Lt( n, t ); if (a.navigate === !1) { let f = a.fetcherKey || Kt(); await e.fetch(f, r, a.action || o, { preventScrollReset: a.preventScrollReset, formData: u, body: c, formMethod: a.method || l, formEncType: a.encType || s, flushSync: a.flushSync }); } else await e.navigate(a.action || o, { preventScrollReset: a.preventScrollReset, formData: u, body: c, formMethod: a.method || l, formEncType: a.encType || s, replace: a.replace, state: a.state, fromRouteId: r, flushSync: a.flushSync, viewTransition: a.viewTransition }); }, [e, t, r] ); } function Yt(e, { relative: t } = {}) { let { basename: r } = i.useContext(L), n = i.useContext($); x(n, "useFormAction must be used inside a RouteContext"); let [a] = n.matches.slice(-1), o = { ...U(e || ".", { relative: t }) }, l = F(); if (e == null) { o.search = l.search; let s = new URLSearchParams(o.search), u = s.getAll("index"); if (u.some((f) => f === "")) { s.delete("index"), u.filter((d) => d).forEach((d) => s.append("index", d)); let f = s.toString(); o.search = f ? `?${f}` : ""; } } return (!e || e === ".") && a.route.index && (o.search = o.search ? o.search.replace(/^\?/, "?index&") : "?index"), r !== "/" && (o.pathname = o.pathname === "/" ? r : S([r, o.pathname])), M(o); } function Gt(e, { relative: t } = {}) { let r = i.useContext(ye); x( r != null, "`useViewTransitionState` must be used within `react-router-dom`'s `RouterProvider`. Did you accidentally import `RouterProvider` from `react-router`?" ); let { basename: n } = Le( "useViewTransitionState" /* useViewTransitionState */ ), a = U(e, { relative: t }); if (!r.isTransitioning) return !1; let o = k(r.currentLocation.pathname, n) || r.currentLocation.pathname, l = k(r.nextLocation.pathname, n) || r.nextLocation.pathname; return j(a.pathname, l) != null || j(a.pathname, o) != null; } export { Xt as B, Pe as L, qt as R, gt as a };