UNPKG

bear-react-router-hash

Version:

Hash Route + History Route Additional based for React Route v6

761 lines (757 loc) 24.7 kB
import C from "react"; import { useInRouterContext as dr, useLocation as je, matchRoutes as pr } from "react-router-dom"; import { parsePath as vr } from "history"; function U(i, u) { if (!i) throw new Error(u); } const xe = (i) => i.join("/").replace(/\/\/+/g, "/"); function ee(i) { let u = []; return C.Children.forEach(i, (c) => { if (!C.isValidElement(c)) return; if (c.type === C.Fragment) { u.push.apply( u, ee(c.props.children) ); return; } U( c.type === hr, `[${typeof c.type == "string" ? c.type : c.type.name}] is not a <HashRoute> component. All component children of <HashRoutes> must be a <HashRoute> or <React.Fragment>` ); let f = { caseSensitive: c.props.caseSensitive, element: c.props.element, index: c.props.index, path: c.props.path }; c.props.children && (f.children = ee(c.props.children)), u.push(f); }), u; } function hr(i) { U( !1, "A <HashRoute> is only ever to be used as the child of <HashRoutes> element, never rendered directly. Please wrap your <HashRoute> in a <HashRoutes>." ); } var B = {}, mr = { get exports() { return B; }, set exports(i) { B = i; } }, I = {}; /** * @license React * react-jsx-runtime.production.min.js * * Copyright (c) Facebook, Inc. and its affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ var we; function Rr() { if (we) return I; we = 1; var i = C, u = Symbol.for("react.element"), c = Symbol.for("react.fragment"), f = Object.prototype.hasOwnProperty, P = i.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED.ReactCurrentOwner, y = { key: !0, ref: !0, __self: !0, __source: !0 }; function w(_, v, x) { var m, b = {}, h = null, H = null; x !== void 0 && (h = "" + x), v.key !== void 0 && (h = "" + v.key), v.ref !== void 0 && (H = v.ref); for (m in v) f.call(v, m) && !y.hasOwnProperty(m) && (b[m] = v[m]); if (_ && _.defaultProps) for (m in v = _.defaultProps, v) b[m] === void 0 && (b[m] = v[m]); return { $$typeof: u, type: _, key: h, ref: H, props: b, _owner: P.current }; } return I.Fragment = c, I.jsx = w, I.jsxs = w, I; } var $ = {}; /** * @license React * react-jsx-runtime.development.js * * Copyright (c) Facebook, Inc. and its affiliates. * * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ var Se; function gr() { return Se || (Se = 1, process.env.NODE_ENV !== "production" && function() { var i = C, u = Symbol.for("react.element"), c = Symbol.for("react.portal"), f = Symbol.for("react.fragment"), P = Symbol.for("react.strict_mode"), y = Symbol.for("react.profiler"), w = Symbol.for("react.provider"), _ = Symbol.for("react.context"), v = Symbol.for("react.forward_ref"), x = Symbol.for("react.suspense"), m = Symbol.for("react.suspense_list"), b = Symbol.for("react.memo"), h = Symbol.for("react.lazy"), H = Symbol.for("react.offscreen"), te = Symbol.iterator, ke = "@@iterator"; function Fe(e) { if (e === null || typeof e != "object") return null; var r = te && e[te] || e[ke]; return typeof r == "function" ? r : null; } var j = i.__SECRET_INTERNALS_DO_NOT_USE_OR_YOU_WILL_BE_FIRED; function R(e) { { for (var r = arguments.length, t = new Array(r > 1 ? r - 1 : 0), n = 1; n < r; n++) t[n - 1] = arguments[n]; De("error", e, t); } } function De(e, r, t) { { var n = j.ReactDebugCurrentFrame, s = n.getStackAddendum(); s !== "" && (r += "%s", t = t.concat([s])); var l = t.map(function(o) { return String(o); }); l.unshift("Warning: " + r), Function.prototype.apply.call(console[e], console, l); } } var Ae = !1, Ie = !1, $e = !1, We = !1, He = !1, ne; ne = Symbol.for("react.module.reference"); function Ne(e) { return !!(typeof e == "string" || typeof e == "function" || e === f || e === y || He || e === P || e === x || e === m || We || e === H || Ae || Ie || $e || typeof e == "object" && e !== null && (e.$$typeof === h || e.$$typeof === b || e.$$typeof === w || e.$$typeof === _ || e.$$typeof === v || // This needs to include all possible module reference object // types supported by any Flight configuration anywhere since // we don't know which Flight build this will end up being used // with. e.$$typeof === ne || e.getModuleId !== void 0)); } function Ye(e, r, t) { var n = e.displayName; if (n) return n; var s = r.displayName || r.name || ""; return s !== "" ? t + "(" + s + ")" : t; } function ae(e) { return e.displayName || "Context"; } function O(e) { if (e == null) return null; if (typeof e.tag == "number" && R("Received an unexpected object in getComponentNameFromType(). This is likely a bug in React. Please file an issue."), typeof e == "function") return e.displayName || e.name || null; if (typeof e == "string") return e; switch (e) { case f: return "Fragment"; case c: return "Portal"; case y: return "Profiler"; case P: return "StrictMode"; case x: return "Suspense"; case m: return "SuspenseList"; } if (typeof e == "object") switch (e.$$typeof) { case _: var r = e; return ae(r) + ".Consumer"; case w: var t = e; return ae(t._context) + ".Provider"; case v: return Ye(e, e.render, "ForwardRef"); case b: var n = e.displayName || null; return n !== null ? n : O(e.type) || "Memo"; case h: { var s = e, l = s._payload, o = s._init; try { return O(o(l)); } catch { return null; } } } return null; } var S = Object.assign, D = 0, oe, ie, ue, se, le, ce, fe; function de() { } de.__reactDisabledLog = !0; function Le() { { if (D === 0) { oe = console.log, ie = console.info, ue = console.warn, se = console.error, le = console.group, ce = console.groupCollapsed, fe = console.groupEnd; var e = { configurable: !0, enumerable: !0, value: de, writable: !0 }; Object.defineProperties(console, { info: e, log: e, warn: e, error: e, group: e, groupCollapsed: e, groupEnd: e }); } D++; } } function Me() { { if (D--, D === 0) { var e = { configurable: !0, enumerable: !0, writable: !0 }; Object.defineProperties(console, { log: S({}, e, { value: oe }), info: S({}, e, { value: ie }), warn: S({}, e, { value: ue }), error: S({}, e, { value: se }), group: S({}, e, { value: le }), groupCollapsed: S({}, e, { value: ce }), groupEnd: S({}, e, { value: fe }) }); } D < 0 && R("disabledDepth fell below zero. This is a bug in React. Please file an issue."); } } var q = j.ReactCurrentDispatcher, J; function N(e, r, t) { { if (J === void 0) try { throw Error(); } catch (s) { var n = s.stack.trim().match(/\n( *(at )?)/); J = n && n[1] || ""; } return ` ` + J + e; } } var G = !1, Y; { var Ve = typeof WeakMap == "function" ? WeakMap : Map; Y = new Ve(); } function pe(e, r) { if (!e || G) return ""; { var t = Y.get(e); if (t !== void 0) return t; } var n; G = !0; var s = Error.prepareStackTrace; Error.prepareStackTrace = void 0; var l; l = q.current, q.current = null, Le(); try { if (r) { var o = function() { throw Error(); }; if (Object.defineProperty(o.prototype, "props", { set: function() { throw Error(); } }), typeof Reflect == "object" && Reflect.construct) { try { Reflect.construct(o, []); } catch (T) { n = T; } Reflect.construct(e, [], o); } else { try { o.call(); } catch (T) { n = T; } e.call(o.prototype); } } else { try { throw Error(); } catch (T) { n = T; } e(); } } catch (T) { if (T && n && typeof T.stack == "string") { for (var a = T.stack.split(` `), g = n.stack.split(` `), d = a.length - 1, p = g.length - 1; d >= 1 && p >= 0 && a[d] !== g[p]; ) p--; for (; d >= 1 && p >= 0; d--, p--) if (a[d] !== g[p]) { if (d !== 1 || p !== 1) do if (d--, p--, p < 0 || a[d] !== g[p]) { var E = ` ` + a[d].replace(" at new ", " at "); return e.displayName && E.includes("<anonymous>") && (E = E.replace("<anonymous>", e.displayName)), typeof e == "function" && Y.set(e, E), E; } while (d >= 1 && p >= 0); break; } } } finally { G = !1, q.current = l, Me(), Error.prepareStackTrace = s; } var F = e ? e.displayName || e.name : "", Pe = F ? N(F) : ""; return typeof e == "function" && Y.set(e, Pe), Pe; } function Ue(e, r, t) { return pe(e, !1); } function Be(e) { var r = e.prototype; return !!(r && r.isReactComponent); } function L(e, r, t) { if (e == null) return ""; if (typeof e == "function") return pe(e, Be(e)); if (typeof e == "string") return N(e); switch (e) { case x: return N("Suspense"); case m: return N("SuspenseList"); } if (typeof e == "object") switch (e.$$typeof) { case v: return Ue(e.render); case b: return L(e.type, r, t); case h: { var n = e, s = n._payload, l = n._init; try { return L(l(s), r, t); } catch { } } } return ""; } var M = Object.prototype.hasOwnProperty, ve = {}, he = j.ReactDebugCurrentFrame; function V(e) { if (e) { var r = e._owner, t = L(e.type, e._source, r ? r.type : null); he.setExtraStackFrame(t); } else he.setExtraStackFrame(null); } function qe(e, r, t, n, s) { { var l = Function.call.bind(M); for (var o in e) if (l(e, o)) { var a = void 0; try { if (typeof e[o] != "function") { var g = Error((n || "React class") + ": " + t + " type `" + o + "` is invalid; it must be a function, usually from the `prop-types` package, but received `" + typeof e[o] + "`.This often happens because of typos such as `PropTypes.function` instead of `PropTypes.func`."); throw g.name = "Invariant Violation", g; } a = e[o](r, o, n, t, null, "SECRET_DO_NOT_PASS_THIS_OR_YOU_WILL_BE_FIRED"); } catch (d) { a = d; } a && !(a instanceof Error) && (V(s), R("%s: type specification of %s `%s` is invalid; the type checker function must return `null` or an `Error` but returned a %s. You may have forgotten to pass an argument to the type checker creator (arrayOf, instanceOf, objectOf, oneOf, oneOfType, and shape all require an argument).", n || "React class", t, o, typeof a), V(null)), a instanceof Error && !(a.message in ve) && (ve[a.message] = !0, V(s), R("Failed %s type: %s", t, a.message), V(null)); } } } var Je = Array.isArray; function z(e) { return Je(e); } function Ge(e) { { var r = typeof Symbol == "function" && Symbol.toStringTag, t = r && e[Symbol.toStringTag] || e.constructor.name || "Object"; return t; } } function ze(e) { try { return me(e), !1; } catch { return !0; } } function me(e) { return "" + e; } function Re(e) { if (ze(e)) return R("The provided key is an unsupported type %s. This value must be coerced to a string before before using it here.", Ge(e)), me(e); } var A = j.ReactCurrentOwner, Ke = { key: !0, ref: !0, __self: !0, __source: !0 }, ge, ye, K; K = {}; function Xe(e) { if (M.call(e, "ref")) { var r = Object.getOwnPropertyDescriptor(e, "ref").get; if (r && r.isReactWarning) return !1; } return e.ref !== void 0; } function Ze(e) { if (M.call(e, "key")) { var r = Object.getOwnPropertyDescriptor(e, "key").get; if (r && r.isReactWarning) return !1; } return e.key !== void 0; } function Qe(e, r) { if (typeof e.ref == "string" && A.current && r && A.current.stateNode !== r) { var t = O(A.current.type); K[t] || (R('Component "%s" contains the string ref "%s". Support for string refs will be removed in a future major release. This case cannot be automatically converted to an arrow function. We ask you to manually fix this case by using useRef() or createRef() instead. Learn more about using refs safely here: https://reactjs.org/link/strict-mode-string-ref', O(A.current.type), e.ref), K[t] = !0); } } function er(e, r) { { var t = function() { ge || (ge = !0, R("%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://reactjs.org/link/special-props)", r)); }; t.isReactWarning = !0, Object.defineProperty(e, "key", { get: t, configurable: !0 }); } } function rr(e, r) { { var t = function() { ye || (ye = !0, R("%s: `ref` 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://reactjs.org/link/special-props)", r)); }; t.isReactWarning = !0, Object.defineProperty(e, "ref", { get: t, configurable: !0 }); } } var tr = function(e, r, t, n, s, l, o) { var a = { // This tag allows us to uniquely identify this as a React Element $$typeof: u, // Built-in properties that belong on the element type: e, key: r, ref: t, props: o, // Record the component responsible for creating this element. _owner: l }; return a._store = {}, Object.defineProperty(a._store, "validated", { configurable: !1, enumerable: !1, writable: !0, value: !1 }), Object.defineProperty(a, "_self", { configurable: !1, enumerable: !1, writable: !1, value: n }), Object.defineProperty(a, "_source", { configurable: !1, enumerable: !1, writable: !1, value: s }), Object.freeze && (Object.freeze(a.props), Object.freeze(a)), a; }; function nr(e, r, t, n, s) { { var l, o = {}, a = null, g = null; t !== void 0 && (Re(t), a = "" + t), Ze(r) && (Re(r.key), a = "" + r.key), Xe(r) && (g = r.ref, Qe(r, s)); for (l in r) M.call(r, l) && !Ke.hasOwnProperty(l) && (o[l] = r[l]); if (e && e.defaultProps) { var d = e.defaultProps; for (l in d) o[l] === void 0 && (o[l] = d[l]); } if (a || g) { var p = typeof e == "function" ? e.displayName || e.name || "Unknown" : e; a && er(o, p), g && rr(o, p); } return tr(e, a, g, s, n, A.current, o); } } var X = j.ReactCurrentOwner, be = j.ReactDebugCurrentFrame; function k(e) { if (e) { var r = e._owner, t = L(e.type, e._source, r ? r.type : null); be.setExtraStackFrame(t); } else be.setExtraStackFrame(null); } var Z; Z = !1; function Q(e) { return typeof e == "object" && e !== null && e.$$typeof === u; } function Ee() { { if (X.current) { var e = O(X.current.type); if (e) return ` Check the render method of \`` + e + "`."; } return ""; } } function ar(e) { { if (e !== void 0) { var r = e.fileName.replace(/^.*[\\\/]/, ""), t = e.lineNumber; return ` Check your code at ` + r + ":" + t + "."; } return ""; } } var _e = {}; function or(e) { { var r = Ee(); if (!r) { var t = typeof e == "string" ? e : e.displayName || e.name; t && (r = ` Check the top-level render call using <` + t + ">."); } return r; } } function Ce(e, r) { { if (!e._store || e._store.validated || e.key != null) return; e._store.validated = !0; var t = or(r); if (_e[t]) return; _e[t] = !0; var n = ""; e && e._owner && e._owner !== X.current && (n = " It was passed a child from " + O(e._owner.type) + "."), k(e), R('Each child in a list should have a unique "key" prop.%s%s See https://reactjs.org/link/warning-keys for more information.', t, n), k(null); } } function Oe(e, r) { { if (typeof e != "object") return; if (z(e)) for (var t = 0; t < e.length; t++) { var n = e[t]; Q(n) && Ce(n, r); } else if (Q(e)) e._store && (e._store.validated = !0); else if (e) { var s = Fe(e); if (typeof s == "function" && s !== e.entries) for (var l = s.call(e), o; !(o = l.next()).done; ) Q(o.value) && Ce(o.value, r); } } } function ir(e) { { var r = e.type; if (r == null || typeof r == "string") return; var t; if (typeof r == "function") t = r.propTypes; else if (typeof r == "object" && (r.$$typeof === v || // Note: Memo only checks outer props here. // Inner props are checked in the reconciler. r.$$typeof === b)) t = r.propTypes; else return; if (t) { var n = O(r); qe(t, e.props, "prop", n, e); } else if (r.PropTypes !== void 0 && !Z) { Z = !0; var s = O(r); R("Component %s declared `PropTypes` instead of `propTypes`. Did you misspell the property assignment?", s || "Unknown"); } typeof r.getDefaultProps == "function" && !r.getDefaultProps.isReactClassApproved && R("getDefaultProps is only used on classic React.createClass definitions. Use a static property named `defaultProps` instead."); } } function ur(e) { { for (var r = Object.keys(e.props), t = 0; t < r.length; t++) { var n = r[t]; if (n !== "children" && n !== "key") { k(e), R("Invalid prop `%s` supplied to `React.Fragment`. React.Fragment can only have `key` and `children` props.", n), k(null); break; } } e.ref !== null && (k(e), R("Invalid attribute `ref` supplied to `React.Fragment`."), k(null)); } } function Te(e, r, t, n, s, l) { { var o = Ne(e); if (!o) { var a = ""; (e === void 0 || typeof e == "object" && e !== null && Object.keys(e).length === 0) && (a += " You likely forgot to export your component from the file it's defined in, or you might have mixed up default and named imports."); var g = ar(s); g ? a += g : a += Ee(); var d; e === null ? d = "null" : z(e) ? d = "array" : e !== void 0 && e.$$typeof === u ? (d = "<" + (O(e.type) || "Unknown") + " />", a = " Did you accidentally export a JSX literal instead of a component?") : d = typeof e, R("React.jsx: type is invalid -- expected a string (for built-in components) or a class/function (for composite components) but got: %s.%s", d, a); } var p = nr(e, r, t, s, l); if (p == null) return p; if (o) { var E = r.children; if (E !== void 0) if (n) if (z(E)) { for (var F = 0; F < E.length; F++) Oe(E[F], e); Object.freeze && Object.freeze(E); } else R("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 Oe(E, e); } return e === f ? ur(p) : ir(p), p; } } function sr(e, r, t) { return Te(e, r, t, !0); } function lr(e, r, t) { return Te(e, r, t, !1); } var cr = lr, fr = sr; $.Fragment = f, $.jsx = cr, $.jsxs = fr; }()), $; } (function(i) { process.env.NODE_ENV === "production" ? i.exports = Rr() : i.exports = gr(); })(mr); const W = C.createContext({ outlet: null, matches: [] }), re = C.createContext(null); process.env.NODE_ENV !== "production" && (W.displayName = "HashRoute", re.displayName = "HashOutlet"); process.env.NODE_ENV; function yr(i, u = []) { return i == null ? null : i.reduceRight((c, f, P) => /* @__PURE__ */ B.jsx( W.Provider, { children: f.route.element !== void 0 ? f.route.element : c, value: { outlet: c, matches: u.concat(i.slice(0, P + 1)) } } ), null); } function br(i, u) { var b; U( dr(), // TODO: This error is probably because they somehow have 2 versions of the // router loaded. We can help them understand how to avoid that. "useHashRoutes() may be used only in the context of a <HashRouter> component." ); let { matches: c } = C.useContext(W), f = c[c.length - 1], P = f ? f.params : {}; f && f.pathname; let y = f ? f.pathnameBase : "/"; f && f.route; let w = je(), _; if (u) { let h = typeof u == "string" ? vr(u) : u; U( y === "/" || ((b = h.pathname) == null ? void 0 : b.startsWith(y)), `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 "${y}" but pathname "${h.pathname}" was given in the \`location\` prop.` ), _ = h; } else _ = w; let v = _.hash || "/", x = y === "#" ? v : v.slice(y.length) || "/", m = pr(i, { pathname: x }); return yr( m && m.map( (h) => Object.assign({}, h, { params: Object.assign({}, P, h.params), pathname: xe([y, h.pathname]), pathnameBase: h.pathnameBase === "/" ? y : xe([y, h.pathnameBase]) }) ), c ); } function Tr({ children: i, location: u }) { const c = ee(i); return br(c, u); } function Pr() { return C.useContext(re); } function Er(i) { let u = C.useContext(W).outlet; return u && /* @__PURE__ */ B.jsx(re.Provider, { value: i, children: u }); } function xr() { let { matches: i } = C.useContext(W), u = i[i.length - 1]; return u ? u.params : {}; } function wr() { var u; const i = (u = je()) == null ? void 0 : u.hash; return i ? i.replace("#", "") : ""; } function Sr(i) { return Er(i.context); } export { Sr as HashOutlet, hr as HashRoute, Tr as HashRoutes, Er as useHashOutlet, Pr as useHashOutletContext, xr as useHashParams, wr as useHashPathname };