clear-react-router 1.0.8 → 1.0.10

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,4 +1,4 @@
1
- import type { BlockerState, Location, RouteItem, UpdateBlockedRouteProps } from '../types/global.ts';
1
+ import type { BlockerState, Location, RouteItem, UpdateBlockedRouteProps } from '../types/global';
2
2
  type UseHandleNavigation = {
3
3
  routeList: RouteItem[];
4
4
  setLocation: (arg: Location) => void;
package/dist/index.js CHANGED
@@ -199,24 +199,29 @@ var l = (e, t) => () => (t || (e((t = { exports: {} }).exports, t), e = null), t
199
199
  children: e
200
200
  })
201
201
  })
202
- }), v = (t, r) => {
202
+ }), v = (e) => {
203
+ let t = s(e);
204
+ return a(() => {
205
+ t.current = e;
206
+ }, [e]), t;
207
+ }, y = (t, r) => {
203
208
  let i = n(() => t().then((e) => ({ default: e.default || e })));
204
209
  return () => /* @__PURE__ */ (0, g.jsx)(e, {
205
210
  fallback: typeof r == "function" ? r() : r || null,
206
211
  children: /* @__PURE__ */ (0, g.jsx)(i, {})
207
212
  });
208
- }, y = (e) => typeof e.element == "function" && e.element.toString().includes("import("), b = (e, t = [], n = "") => {
213
+ }, b = (e) => typeof e.element == "function" && e.element.toString().includes("import("), x = (e, t = [], n = "") => {
209
214
  let r = e.path.match(/:[^/]+/g), i = e.path.replaceAll(/:[^/]+(\/|$)/g, "").split("/").filter(Boolean), a = e.path.split("/"), o = r ? [...t, ...r.map((e, t) => ({
210
215
  key: i[t],
211
216
  value: e.slice(1)
212
- }))] : t, s = o.length ? `${n}${a.slice(0, a.length - 1).join("/")}` : e.path, c = y(e) ? v(e.element, e.fallback) : e.element;
217
+ }))] : t, s = o.length ? `${n}${a.slice(0, a.length - 1).join("/")}` : e.path, c = b(e) ? y(e.element, e.fallback) : e.element;
213
218
  return [{
214
219
  ...e,
215
220
  path: s,
216
221
  params: o,
217
222
  element: c
218
- }, ...e.children?.flatMap((e) => b(e, o, s)) || []];
219
- }, x = (e) => e.flatMap((e) => b(e, [])), S = (e) => {
223
+ }, ...e.children?.flatMap((e) => x(e, o, s)) || []];
224
+ }, S = (e) => e.flatMap((e) => x(e, [])), C = (e) => {
220
225
  let { pathname: t } = window.location, n = t.split("/");
221
226
  return (e || []).map((e) => ({
222
227
  index: n.findIndex((t) => t === e.key),
@@ -225,34 +230,29 @@ var l = (e, t) => () => (t || (e((t = { exports: {} }).exports, t), e = null), t
225
230
  ...e,
226
231
  [t.value]: n[t.index + 1]
227
232
  }), {});
228
- }, C = (e) => ({
233
+ }, w = (e) => ({
229
234
  pathname: e.pathname,
230
235
  search: e.search
231
- }), w = (e, t) => {
236
+ }), T = (e, t) => {
232
237
  let n = e.path.split("/").filter(Boolean), r = e.params ? Object.keys(e.params).length : 0, i = t.split("/").filter(Boolean);
233
238
  return n.every((e, t) => e === i[t + +!!t]) && i.length === n.length + r;
234
- }, T = class {
239
+ }, E = class {
235
240
  url;
236
241
  search;
237
242
  cause;
238
243
  constructor(e, t) {
239
244
  this.url = e, this.search = t, this.cause = "redirect";
240
245
  }
241
- }, E = (e, t) => Promise.reject(new T(e, t)), D = (e) => {
242
- let t = s(e);
243
- return a(() => {
244
- t.current = e;
245
- }, [e]), t;
246
- }, O = ({ setLocation: e, routeList: t, context: n, revalidateCache: i }) => {
246
+ }, D = (e, t) => Promise.reject(new E(e, t)), O = ({ setLocation: e, routeList: t, context: n, revalidateCache: i }) => {
247
247
  let [l, u] = c({
248
248
  from: "",
249
249
  to: ""
250
- }), d = s(""), f = D(r(async (r) => {
250
+ }), d = s(""), f = v(r(async (r) => {
251
251
  try {
252
- let a = t.find((e) => w(e, r.pathname));
252
+ let a = t.find((e) => T(e, r.pathname));
253
253
  a?.beforeLoad && await a?.beforeLoad(n), e(r), r.pathname !== window.location.pathname && (history.pushState(null, "", r.pathname), d.current = r.pathname), await i(a), a?.afterLoad && await a?.afterLoad(n);
254
254
  } catch (t) {
255
- if (!(t instanceof T)) return t;
255
+ if (!(t instanceof E)) return t;
256
256
  history.replaceState(null, "", `${t.url}${t.search || ""}`), e({
257
257
  pathname: t.url,
258
258
  search: t.search
@@ -280,7 +280,7 @@ var l = (e, t) => () => (t || (e((t = { exports: {} }).exports, t), e = null), t
280
280
  }, [l.from, f]);
281
281
  return a(() => {
282
282
  let e = async (e) => {
283
- let t = C(e.target.location);
283
+ let t = w(e.target.location);
284
284
  d.current === l.from ? (u({
285
285
  from: d.current,
286
286
  to: t.pathname
@@ -288,7 +288,7 @@ var l = (e, t) => () => (t || (e((t = { exports: {} }).exports, t), e = null), t
288
288
  };
289
289
  return window.addEventListener("popstate", e), () => window.removeEventListener("popstate", e);
290
290
  }, [l.from, f]), a(() => {
291
- let e = C(window.location);
291
+ let e = w(window.location);
292
292
  f.current(e), d.current = e.pathname;
293
293
  }, [f]), {
294
294
  blockerState: o(() => l.from && l.to ? "blocked" : l.from ? "charged" : "unblocked", [l]),
@@ -332,7 +332,7 @@ var l = (e, t) => () => (t || (e((t = { exports: {} }).exports, t), e = null), t
332
332
  }
333
333
  }
334
334
  }, [f, d]), m = r(async (t) => {
335
- let n = e.find((e) => w(e, t));
335
+ let n = e.find((e) => T(e, t));
336
336
  n && await p(n);
337
337
  }, [p, e]);
338
338
  return {
@@ -343,12 +343,12 @@ var l = (e, t) => () => (t || (e((t = { exports: {} }).exports, t), e = null), t
343
343
  isLoading: o[window.location.pathname]
344
344
  };
345
345
  }, A = (e) => e ? typeof e == "function" ? /* @__PURE__ */ (0, g.jsx)(e, {}) : e : null, j = "error 404. Page not found", M = "*", N = ({ routeList: e, context: t = {} }) => {
346
- let [n, r] = c(C(window.location)), [i, a] = c(t), s = o(() => e.find((e) => e.path === M || w(e, n.pathname)), [n.pathname, e]), { loaderError: l, loaderCache: u, prefetchLoader: d, revalidateCache: f, isLoading: p } = k(e), { blockerState: m, updateLocation: h, updateBlockedRoute: v } = O({
346
+ let [n, r] = c(w(window.location)), [i, a] = c(t), s = o(() => e.find((e) => e.path === M || T(e, n.pathname)), [n.pathname, e]), { loaderError: l, loaderCache: u, prefetchLoader: d, revalidateCache: f, isLoading: p } = k(e), { blockerState: m, updateLocation: h, updateBlockedRoute: v } = O({
347
347
  setLocation: r,
348
348
  routeList: e,
349
349
  context: i,
350
350
  revalidateCache: f
351
- }), y = o(() => s?.params ? S(s.params) : {}, [s]), b = o(() => ({
351
+ }), y = o(() => s?.params ? C(s.params) : {}, [s]), b = o(() => ({
352
352
  location: n,
353
353
  updateLocation: h,
354
354
  params: y,
@@ -436,4 +436,4 @@ var l = (e, t) => () => (t || (e((t = { exports: {} }).exports, t), e = null), t
436
436
  };
437
437
  };
438
438
  //#endregion
439
- export { R as Link, N as Router, x as createRouter, E as redirect, U as useBeforeUnload, H as useBlocker, V as useLoaderState, B as useLocation, L as useNavigate, z as useParams, W as useRouterContext };
439
+ export { R as Link, N as Router, S as createRouter, D as redirect, U as useBeforeUnload, H as useBlocker, V as useLoaderState, B as useLocation, L as useNavigate, z as useParams, W as useRouterContext };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "clear-react-router",
3
- "version": "1.0.8",
3
+ "version": "1.0.10",
4
4
  "description": "A lightweight, type-safe routing library for React applications",
5
5
  "author": "Andrew Bubnov",
6
6
  "main": "./dist/index.js",