named-react-router
Version:
A lightweight extension to React Router for named routes
550 lines (547 loc) • 19.1 kB
JavaScript
import { Routes as Ae, Route as Se, NavLink as Oe, generatePath as Pe, createBrowserRouter as $e, useNavigate as Me, useLocation as Ye, useMatches as We } from "react-router-dom";
import Ue, { useEffect as qe, Fragment as Ee, useMemo as Ve } from "react";
var P = { exports: {} }, C = {};
/**
* @license React
* react-jsx-runtime.production.js
*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
var ve;
function Fe() {
if (ve) return C;
ve = 1;
var n = Symbol.for("react.transitional.element"), l = Symbol.for("react.fragment");
function c(u, a, d) {
var f = null;
if (d !== void 0 && (f = "" + d), a.key !== void 0 && (f = "" + a.key), "key" in a) {
d = {};
for (var m in a)
m !== "key" && (d[m] = a[m]);
} else d = a;
return a = d.ref, {
$$typeof: n,
type: u,
key: f,
ref: a !== void 0 ? a : null,
props: d
};
}
return C.Fragment = l, C.jsx = c, C.jsxs = c, C;
}
var k = {};
/**
* @license React
* react-jsx-runtime.development.js
*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
var ge;
function Je() {
return ge || (ge = 1, process.env.NODE_ENV !== "production" && function() {
function n(e) {
if (e == null) return null;
if (typeof e == "function")
return e.$$typeof === je ? null : e.displayName || e.name || null;
if (typeof e == "string") return e;
switch (e) {
case Y:
return "Fragment";
case Te:
return "Portal";
case I:
return "Profiler";
case Q:
return "StrictMode";
case U:
return "Suspense";
case q:
return "SuspenseList";
}
if (typeof e == "object")
switch (typeof e.tag == "number" && console.error(
"Received an unexpected object in getComponentNameFromType(). This is likely a bug in React. Please file an issue."
), e.$$typeof) {
case D:
return (e.displayName || "Context") + ".Provider";
case K:
return (e._context.displayName || "Context") + ".Consumer";
case W:
var r = e.render;
return e = e.displayName, e || (e = r.displayName || r.name || "", e = e !== "" ? "ForwardRef(" + e + ")" : "ForwardRef"), e;
case V:
return r = e.displayName || null, r !== null ? r : n(e.type) || "Memo";
case F:
r = e._payload, e = e._init;
try {
return n(e(r));
} catch {
}
}
return null;
}
function l(e) {
return "" + e;
}
function c(e) {
try {
l(e);
var r = !1;
} catch {
r = !0;
}
if (r) {
r = console;
var t = r.error, s = typeof Symbol == "function" && Symbol.toStringTag && e[Symbol.toStringTag] || e.constructor.name || "Object";
return t.call(
r,
"The provided key is an unsupported type %s. This value must be coerced to a string before using it here.",
s
), l(e);
}
}
function u() {
}
function a() {
if (x === 0) {
te = console.log, oe = console.info, ne = console.warn, ae = console.error, ue = console.group, se = console.groupCollapsed, ie = console.groupEnd;
var e = {
configurable: !0,
enumerable: !0,
value: u,
writable: !0
};
Object.defineProperties(console, {
info: e,
log: e,
warn: e,
error: e,
group: e,
groupCollapsed: e,
groupEnd: e
});
}
x++;
}
function d() {
if (x--, x === 0) {
var e = { configurable: !0, enumerable: !0, writable: !0 };
Object.defineProperties(console, {
log: _({}, e, { value: te }),
info: _({}, e, { value: oe }),
warn: _({}, e, { value: ne }),
error: _({}, e, { value: ae }),
group: _({}, e, { value: ue }),
groupCollapsed: _({}, e, { value: se }),
groupEnd: _({}, e, { value: ie })
});
}
0 > x && console.error(
"disabledDepth fell below zero. This is a bug in React. Please file an issue."
);
}
function f(e) {
if (z === void 0)
try {
throw Error();
} catch (t) {
var r = t.stack.trim().match(/\n( *(at )?)/);
z = r && r[1] || "", le = -1 < t.stack.indexOf(`
at`) ? " (<anonymous>)" : -1 < t.stack.indexOf("@") ? "@unknown:0:0" : "";
}
return `
` + z + e + le;
}
function m(e, r) {
if (!e || B) return "";
var t = L.get(e);
if (t !== void 0) return t;
B = !0, t = Error.prepareStackTrace, Error.prepareStackTrace = void 0;
var s = null;
s = y.H, y.H = null, a();
try {
var E = {
DetermineComponentFrameRoot: function() {
try {
if (r) {
var R = function() {
throw Error();
};
if (Object.defineProperty(R.prototype, "props", {
set: function() {
throw Error();
}
}), typeof Reflect == "object" && Reflect.construct) {
try {
Reflect.construct(R, []);
} catch (h) {
var O = h;
}
Reflect.construct(e, [], R);
} else {
try {
R.call();
} catch (h) {
O = h;
}
e.call(R.prototype);
}
} else {
try {
throw Error();
} catch (h) {
O = h;
}
(R = e()) && typeof R.catch == "function" && R.catch(function() {
});
}
} catch (h) {
if (h && O && typeof h.stack == "string")
return [h.stack, O.stack];
}
return [null, null];
}
};
E.DetermineComponentFrameRoot.displayName = "DetermineComponentFrameRoot";
var i = Object.getOwnPropertyDescriptor(
E.DetermineComponentFrameRoot,
"name"
);
i && i.configurable && Object.defineProperty(
E.DetermineComponentFrameRoot,
"name",
{ value: "DetermineComponentFrameRoot" }
);
var o = E.DetermineComponentFrameRoot(), p = o[0], N = o[1];
if (p && N) {
var v = p.split(`
`), w = N.split(`
`);
for (o = i = 0; i < v.length && !v[i].includes(
"DetermineComponentFrameRoot"
); )
i++;
for (; o < w.length && !w[o].includes(
"DetermineComponentFrameRoot"
); )
o++;
if (i === v.length || o === w.length)
for (i = v.length - 1, o = w.length - 1; 1 <= i && 0 <= o && v[i] !== w[o]; )
o--;
for (; 1 <= i && 0 <= o; i--, o--)
if (v[i] !== w[o]) {
if (i !== 1 || o !== 1)
do
if (i--, o--, 0 > o || v[i] !== w[o]) {
var j = `
` + v[i].replace(
" at new ",
" at "
);
return e.displayName && j.includes("<anonymous>") && (j = j.replace("<anonymous>", e.displayName)), typeof e == "function" && L.set(e, j), j;
}
while (1 <= i && 0 <= o);
break;
}
}
} finally {
B = !1, y.H = s, d(), Error.prepareStackTrace = t;
}
return v = (v = e ? e.displayName || e.name : "") ? f(v) : "", typeof e == "function" && L.set(e, v), v;
}
function g(e) {
if (e == null) return "";
if (typeof e == "function") {
var r = e.prototype;
return m(
e,
!(!r || !r.isReactComponent)
);
}
if (typeof e == "string") return f(e);
switch (e) {
case U:
return f("Suspense");
case q:
return f("SuspenseList");
}
if (typeof e == "object")
switch (e.$$typeof) {
case W:
return e = m(e.render, !1), e;
case V:
return g(e.type);
case F:
r = e._payload, e = e._init;
try {
return g(e(r));
} catch {
}
}
return "";
}
function b() {
var e = y.A;
return e === null ? null : e.getOwner();
}
function T(e) {
if (re.call(e, "key")) {
var r = Object.getOwnPropertyDescriptor(e, "key").get;
if (r && r.isReactWarning) return !1;
}
return e.key !== void 0;
}
function Re(e, r) {
function t() {
ce || (ce = !0, console.error(
"%s: `key` is not a prop. Trying to access it will result in `undefined` being returned. If you need to access the same value within the child component, you should pass it as a different prop. (https://react.dev/link/special-props)",
r
));
}
t.isReactWarning = !0, Object.defineProperty(e, "key", {
get: t,
configurable: !0
});
}
function ye() {
var e = n(this.type);
return fe[e] || (fe[e] = !0, console.error(
"Accessing element.ref was removed in React 19. ref is now a regular prop. It will be removed from the JSX Element type in a future release."
)), e = this.props.ref, e !== void 0 ? e : null;
}
function _e(e, r, t, s, E, i) {
return t = i.ref, e = {
$$typeof: M,
type: e,
key: r,
props: i,
_owner: E
}, (t !== void 0 ? t : null) !== null ? Object.defineProperty(e, "ref", {
enumerable: !1,
get: ye
}) : Object.defineProperty(e, "ref", { enumerable: !1, value: null }), e._store = {}, Object.defineProperty(e._store, "validated", {
configurable: !1,
enumerable: !1,
writable: !0,
value: 0
}), Object.defineProperty(e, "_debugInfo", {
configurable: !1,
enumerable: !1,
writable: !0,
value: null
}), Object.freeze && (Object.freeze(e.props), Object.freeze(e)), e;
}
function H(e, r, t, s, E, i) {
if (typeof e == "string" || typeof e == "function" || e === Y || e === I || e === Q || e === U || e === q || e === xe || typeof e == "object" && e !== null && (e.$$typeof === F || e.$$typeof === V || e.$$typeof === D || e.$$typeof === K || e.$$typeof === W || e.$$typeof === Ce || e.getModuleId !== void 0)) {
var o = r.children;
if (o !== void 0)
if (s)
if (J(o)) {
for (s = 0; s < o.length; s++)
X(o[s], e);
Object.freeze && Object.freeze(o);
} else
console.error(
"React.jsx: Static children should always be an array. You are likely explicitly calling React.jsxs or React.jsxDEV. Use the Babel transform instead."
);
else X(o, e);
} else
o = "", (e === void 0 || typeof e == "object" && e !== null && Object.keys(e).length === 0) && (o += " You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports."), e === null ? s = "null" : J(e) ? s = "array" : e !== void 0 && e.$$typeof === M ? (s = "<" + (n(e.type) || "Unknown") + " />", o = " Did you accidentally export a JSX literal instead of a component?") : s = typeof e, console.error(
"React.jsx: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: %s.%s",
s,
o
);
if (re.call(r, "key")) {
o = n(e);
var p = Object.keys(r).filter(function(v) {
return v !== "key";
});
s = 0 < p.length ? "{key: someKey, " + p.join(": ..., ") + ": ...}" : "{key: someKey}", de[o + s] || (p = 0 < p.length ? "{" + p.join(": ..., ") + ": ...}" : "{}", console.error(
`A props object containing a "key" prop is being spread into JSX:
let props = %s;
<%s {...props} />
React keys must be passed directly to JSX without using spread:
let props = %s;
<%s key={someKey} {...props} />`,
s,
o,
p,
o
), de[o + s] = !0);
}
if (o = null, t !== void 0 && (c(t), o = "" + t), T(r) && (c(r.key), o = "" + r.key), "key" in r) {
t = {};
for (var N in r)
N !== "key" && (t[N] = r[N]);
} else t = r;
return o && Re(
t,
typeof e == "function" ? e.displayName || e.name || "Unknown" : e
), _e(e, o, i, E, b(), t);
}
function X(e, r) {
if (typeof e == "object" && e && e.$$typeof !== ke) {
if (J(e))
for (var t = 0; t < e.length; t++) {
var s = e[t];
$(s) && Z(s, r);
}
else if ($(e))
e._store && (e._store.validated = 1);
else if (e === null || typeof e != "object" ? t = null : (t = ee && e[ee] || e["@@iterator"], t = typeof t == "function" ? t : null), typeof t == "function" && t !== e.entries && (t = t.call(e), t !== e))
for (; !(e = t.next()).done; )
$(e.value) && Z(e.value, r);
}
}
function $(e) {
return typeof e == "object" && e !== null && e.$$typeof === M;
}
function Z(e, r) {
if (e._store && !e._store.validated && e.key == null && (e._store.validated = 1, r = we(r), !me[r])) {
me[r] = !0;
var t = "";
e && e._owner != null && e._owner !== b() && (t = null, typeof e._owner.tag == "number" ? t = n(e._owner.type) : typeof e._owner.name == "string" && (t = e._owner.name), t = " It was passed a child from " + t + ".");
var s = y.getCurrentStack;
y.getCurrentStack = function() {
var E = g(e.type);
return s && (E += s() || ""), E;
}, console.error(
'Each child in a list should have a unique "key" prop.%s%s See https://react.dev/link/warning-keys for more information.',
r,
t
), y.getCurrentStack = s;
}
}
function we(e) {
var r = "", t = b();
return t && (t = n(t.type)) && (r = `
Check the render method of \`` + t + "`."), r || (e = n(e)) && (r = `
Check the top-level render call using <` + e + ">."), r;
}
var Ne = Ue, M = Symbol.for("react.transitional.element"), Te = Symbol.for("react.portal"), Y = Symbol.for("react.fragment"), Q = Symbol.for("react.strict_mode"), I = Symbol.for("react.profiler"), K = Symbol.for("react.consumer"), D = Symbol.for("react.context"), W = Symbol.for("react.forward_ref"), U = Symbol.for("react.suspense"), q = Symbol.for("react.suspense_list"), V = Symbol.for("react.memo"), F = Symbol.for("react.lazy"), xe = Symbol.for("react.offscreen"), ee = Symbol.iterator, je = Symbol.for("react.client.reference"), y = Ne.__CLIENT_INTERNALS_DO_NOT_USE_OR_WARN_USERS_THEY_CANNOT_UPGRADE, re = Object.prototype.hasOwnProperty, _ = Object.assign, Ce = Symbol.for("react.client.reference"), J = Array.isArray, x = 0, te, oe, ne, ae, ue, se, ie;
u.__reactDisabledLog = !0;
var z, le, B = !1, L = new (typeof WeakMap == "function" ? WeakMap : Map)(), ke = Symbol.for("react.client.reference"), ce, fe = {}, de = {}, me = {};
k.Fragment = Y, k.jsx = function(e, r, t, s, E) {
return H(e, r, t, !1, s, E);
}, k.jsxs = function(e, r, t, s, E) {
return H(e, r, t, !0, s, E);
};
}()), k;
}
var be;
function ze() {
return be || (be = 1, process.env.NODE_ENV === "production" ? P.exports = Fe() : P.exports = Je()), P.exports;
}
var S = ze();
function pe(n) {
return /* @__PURE__ */ S.jsx(S.Fragment, {});
}
function He({ children: n }) {
const l = [];
function c(a) {
if (a.type !== pe && a.type !== Ee)
throw new Error(
`Type ${a.type} is not of type NamedRoute or React.Fragment.`
);
const { props: d } = a, { path: f, name: m, children: g, ...b } = d, T = {
name: m,
path: f,
...b,
children: []
};
return Array.isArray(g) ? T.children = g.map(c) : g && (T.children = [c(g)]), T;
}
qe(() => {
n.forEach((a) => {
l.push(c(a));
}), G(l);
}, []);
function u(a) {
if (a.type !== pe && a.type !== Ee)
throw new Error(
`Type ${a.type} is not of type NamedRoute or React.Fragment.`
);
const { key: d, props: f } = a, { children: m, ...g } = f;
let b = null;
return Array.isArray(m) ? b = m.map(u) : m && (b = u(m)), /* @__PURE__ */ S.jsx(Se, { ...g, children: b }, d);
}
return /* @__PURE__ */ S.jsx(Ae, { children: n.map(u) });
}
function Xe({ to: n, ...l }) {
const c = Ve(() => he(n), [n]);
return /* @__PURE__ */ S.jsx(Oe, { to: c, ...l });
}
function Be(n) {
return !!n.name;
}
const A = /* @__PURE__ */ new Map();
function G(n, l = "", c = "") {
n.forEach((u, a) => {
var m, g;
const d = c ? `${c}-${a}` : `${a}`;
let f;
if ((m = u.path) != null && m.startsWith("/") ? f = u.path : f = l + u.path, u.name) {
u.__absolutePath = f;
const b = A.get(u.name);
process.env.NODE_ENV === "development" && b && console.warn(
`Development mode warning: Duplicate route name: ${u.name} found. Use unique names. Possible caused by HMR. if so, you can ignore this warning`
), A.set(u.name, u), A.set(d, u);
}
(g = u.path) != null && g.endsWith("/") || (f += "/"), u.children && G(u.children, f, d);
});
}
function Ze(n, l) {
return G(n), $e(n, l);
}
function Qe() {
const n = Me();
function l(c, u) {
const a = he(c);
n(a, u);
}
return l;
}
function Ie() {
const n = Ye();
try {
const l = We(), c = l[l.length - 1], u = A.get(c.id);
return { ...n, name: u == null ? void 0 : u.name };
} catch {
process.env.NODE_ENV !== "production" && console.warn(
"useNamedLocation is only supported with createNamedBrowserRouter"
);
}
return n;
}
function he(n) {
if (Be(n)) {
const { name: l, params: c, query: u } = n, a = A.get(l);
if (a != null && a.__absolutePath) {
let d = Pe(a.__absolutePath, c);
if (u) {
const f = new URLSearchParams(u).toString();
d += `?${f}`;
}
return d;
} else
throw new Error(`Route name: ${l} not found.`);
} else
return n;
}
export {
Xe as NamedNavLink,
pe as NamedRoute,
He as NamedRoutes,
he as __namedToFilledPath,
G as collectRouteNames,
Ze as createNamedBrowserRouter,
A as namedRoutesMap,
Ie as useNamedLocation,
Qe as useNamedNavigate
};