clear-react-router 1.0.13 → 1.0.15
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.
- package/dist/hooks/useHandleNavigation.d.ts +1 -1
- package/dist/index.js +54 -54
- package/dist/utils/redirect.d.ts +1 -7
- package/package.json +1 -1
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { BlockerState, Location, RouteItem, UpdateBlockedRouteProps } from '../types/global';
|
|
1
|
+
import type { BlockerState, Location, RouteItem, UpdateBlockedRouteProps } from '../types/global.ts';
|
|
2
2
|
type UseHandleNavigation = {
|
|
3
3
|
routeList: RouteItem[];
|
|
4
4
|
setLocation: (arg: Location) => void;
|
package/dist/index.js
CHANGED
|
@@ -199,29 +199,24 @@ var l = (e, t) => () => (t || (e((t = { exports: {} }).exports, t), e = null), t
|
|
|
199
199
|
children: e
|
|
200
200
|
})
|
|
201
201
|
})
|
|
202
|
-
}), v = (
|
|
203
|
-
let t = s(e);
|
|
204
|
-
return a(() => {
|
|
205
|
-
t.current = e;
|
|
206
|
-
}, [e]), t;
|
|
207
|
-
}, y = (t, r) => {
|
|
202
|
+
}), v = (t, r) => {
|
|
208
203
|
let i = n(() => t().then((e) => ({ default: e.default || e })));
|
|
209
204
|
return () => /* @__PURE__ */ (0, g.jsx)(e, {
|
|
210
205
|
fallback: typeof r == "function" ? r() : r || null,
|
|
211
206
|
children: /* @__PURE__ */ (0, g.jsx)(i, {})
|
|
212
207
|
});
|
|
213
|
-
},
|
|
208
|
+
}, y = (e) => typeof e.element == "function" && e.element.toString().includes("import("), b = (e, t = [], n = "") => {
|
|
214
209
|
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) => ({
|
|
215
210
|
key: i[t],
|
|
216
211
|
value: e.slice(1)
|
|
217
|
-
}))] : t, s = o.length ? `${n}${a.slice(0, a.length - 1).join("/")}` : e.path, c =
|
|
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;
|
|
218
213
|
return [{
|
|
219
214
|
...e,
|
|
220
215
|
path: s,
|
|
221
216
|
params: o,
|
|
222
217
|
element: c
|
|
223
|
-
}, ...e.children?.flatMap((e) =>
|
|
224
|
-
},
|
|
218
|
+
}, ...e.children?.flatMap((e) => b(e, o, s)) || []];
|
|
219
|
+
}, x = (e) => e.flatMap((e) => b(e, [])), S = (e) => {
|
|
225
220
|
let { pathname: t } = window.location, n = t.split("/");
|
|
226
221
|
return (e || []).map((e) => ({
|
|
227
222
|
index: n.findIndex((t) => t === e.key),
|
|
@@ -230,32 +225,31 @@ var l = (e, t) => () => (t || (e((t = { exports: {} }).exports, t), e = null), t
|
|
|
230
225
|
...e,
|
|
231
226
|
[t.value]: n[t.index + 1]
|
|
232
227
|
}), {});
|
|
233
|
-
},
|
|
228
|
+
}, C = (e) => ({
|
|
234
229
|
pathname: e.pathname,
|
|
235
230
|
search: e.search
|
|
236
|
-
}),
|
|
231
|
+
}), w = (e, t) => {
|
|
237
232
|
let n = e.path.split("/").filter(Boolean), r = e.params ? Object.keys(e.params).length : 0, i = t.split("/").filter(Boolean);
|
|
238
233
|
return n.every((e, t) => e === i[t + +!!t]) && i.length === n.length + r;
|
|
239
|
-
},
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
}
|
|
246
|
-
}, D = (e, t) => Promise.reject(new E(e, t)), O = ({ setLocation: e, routeList: t, context: n, revalidateCache: i }) => {
|
|
234
|
+
}, T = (e) => {
|
|
235
|
+
let t = s(e);
|
|
236
|
+
return a(() => {
|
|
237
|
+
t.current = e;
|
|
238
|
+
}, [e]), t;
|
|
239
|
+
}, E = (e) => typeof e == "object" && !!e && e.cause === "redirect", D = ({ setLocation: e, routeList: t, context: n, revalidateCache: i }) => {
|
|
247
240
|
let [l, u] = c({
|
|
248
241
|
from: "",
|
|
249
242
|
to: ""
|
|
250
|
-
}), d = s(""), f =
|
|
243
|
+
}), d = s(""), f = T(r(async (r) => {
|
|
251
244
|
try {
|
|
252
|
-
let a = t.find((e) =>
|
|
253
|
-
|
|
245
|
+
let a = t.find((e) => w(e, r.pathname));
|
|
246
|
+
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
247
|
} catch (t) {
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
248
|
+
let n = t;
|
|
249
|
+
if (!E(n)) return n;
|
|
250
|
+
history.replaceState(null, "", n.url), e({
|
|
251
|
+
pathname: n.url,
|
|
252
|
+
search: n.search
|
|
259
253
|
});
|
|
260
254
|
}
|
|
261
255
|
}, [
|
|
@@ -280,7 +274,7 @@ var l = (e, t) => () => (t || (e((t = { exports: {} }).exports, t), e = null), t
|
|
|
280
274
|
}, [l.from, f]);
|
|
281
275
|
return a(() => {
|
|
282
276
|
let e = async (e) => {
|
|
283
|
-
let t =
|
|
277
|
+
let t = C(e.target.location);
|
|
284
278
|
d.current === l.from ? (u({
|
|
285
279
|
from: d.current,
|
|
286
280
|
to: t.pathname
|
|
@@ -288,14 +282,14 @@ var l = (e, t) => () => (t || (e((t = { exports: {} }).exports, t), e = null), t
|
|
|
288
282
|
};
|
|
289
283
|
return window.addEventListener("popstate", e), () => window.removeEventListener("popstate", e);
|
|
290
284
|
}, [l.from, f]), a(() => {
|
|
291
|
-
let e =
|
|
285
|
+
let e = C(window.location);
|
|
292
286
|
f.current(e), d.current = e.pathname;
|
|
293
287
|
}, [f]), {
|
|
294
288
|
blockerState: o(() => l.from && l.to ? "blocked" : l.from ? "charged" : "unblocked", [l]),
|
|
295
289
|
updateLocation: m,
|
|
296
290
|
updateBlockedRoute: p
|
|
297
291
|
};
|
|
298
|
-
},
|
|
292
|
+
}, O = (e) => {
|
|
299
293
|
let [t, n] = c({}), [i, a] = c(!1), [o, l] = c({}), u = s({}), d = r(({ key: e, value: t }) => n((n) => ({
|
|
300
294
|
...n,
|
|
301
295
|
[e]: t
|
|
@@ -332,7 +326,7 @@ var l = (e, t) => () => (t || (e((t = { exports: {} }).exports, t), e = null), t
|
|
|
332
326
|
}
|
|
333
327
|
}
|
|
334
328
|
}, [f, d]), m = r(async (t) => {
|
|
335
|
-
let n = e.find((e) =>
|
|
329
|
+
let n = e.find((e) => w(e, t));
|
|
336
330
|
n && await p(n);
|
|
337
331
|
}, [p, e]);
|
|
338
332
|
return {
|
|
@@ -342,13 +336,13 @@ var l = (e, t) => () => (t || (e((t = { exports: {} }).exports, t), e = null), t
|
|
|
342
336
|
revalidateCache: p,
|
|
343
337
|
isLoading: o[window.location.pathname]
|
|
344
338
|
};
|
|
345
|
-
},
|
|
346
|
-
let [n, r] = c(
|
|
339
|
+
}, k = (e) => e ? typeof e == "function" ? /* @__PURE__ */ (0, g.jsx)(e, {}) : e : null, A = "error 404. Page not found", j = "*", M = ({ routeList: e, context: t = {} }) => {
|
|
340
|
+
let [n, r] = c(C(window.location)), [i, a] = c(t), s = o(() => e.find((e) => e.path === j || w(e, n.pathname)), [n.pathname, e]), { loaderError: l, loaderCache: u, prefetchLoader: d, revalidateCache: f, isLoading: p } = O(e), { blockerState: m, updateLocation: h, updateBlockedRoute: v } = D({
|
|
347
341
|
setLocation: r,
|
|
348
342
|
routeList: e,
|
|
349
343
|
context: i,
|
|
350
344
|
revalidateCache: f
|
|
351
|
-
}), y = o(() => s?.params ?
|
|
345
|
+
}), y = o(() => s?.params ? S(s.params) : {}, [s]), b = o(() => ({
|
|
352
346
|
location: n,
|
|
353
347
|
updateLocation: h,
|
|
354
348
|
params: y,
|
|
@@ -370,45 +364,45 @@ var l = (e, t) => () => (t || (e((t = { exports: {} }).exports, t), e = null), t
|
|
|
370
364
|
]);
|
|
371
365
|
return s?.loader && !l && p ? /* @__PURE__ */ (0, g.jsx)(_, {
|
|
372
366
|
...b,
|
|
373
|
-
children:
|
|
367
|
+
children: k(s?.loaderFallback)
|
|
374
368
|
}) : l ? /* @__PURE__ */ (0, g.jsx)(_, {
|
|
375
369
|
...b,
|
|
376
|
-
children:
|
|
370
|
+
children: k(s?.errorElement)
|
|
377
371
|
}) : /* @__PURE__ */ (0, g.jsx)(_, {
|
|
378
372
|
...b,
|
|
379
|
-
children:
|
|
373
|
+
children: k(s?.element) || A
|
|
380
374
|
});
|
|
381
|
-
},
|
|
375
|
+
}, N = () => {
|
|
382
376
|
let e = i(p);
|
|
383
377
|
if (!Object.keys(e).length) throw Error("useNavigationState must be used within Router component");
|
|
384
378
|
return e;
|
|
385
|
-
},
|
|
379
|
+
}, P = () => {
|
|
386
380
|
let e = i(d);
|
|
387
381
|
if (!Object.keys(e).length) throw Error("useRouterActions must be used within Router component");
|
|
388
382
|
return e;
|
|
389
|
-
},
|
|
383
|
+
}, F = () => {
|
|
390
384
|
let e = i(f);
|
|
391
385
|
if (!Object.keys(e).length) throw Error("useRouterData must be used within Router component");
|
|
392
386
|
return e;
|
|
393
|
-
},
|
|
394
|
-
let { updateLocation: e } =
|
|
387
|
+
}, I = () => {
|
|
388
|
+
let { updateLocation: e } = P();
|
|
395
389
|
return r(async (t) => t === -1 ? history.go(-1) : await e(t), [e]);
|
|
396
|
-
},
|
|
397
|
-
let { prefetchLoader: r } =
|
|
390
|
+
}, L = ({ children: e, to: t, prefetch: n = !0 }) => {
|
|
391
|
+
let { prefetchLoader: r } = P(), i = I();
|
|
398
392
|
return /* @__PURE__ */ (0, g.jsx)("a", {
|
|
399
393
|
style: { cursor: "pointer" },
|
|
400
394
|
onClick: () => i({ pathname: t }),
|
|
401
395
|
onMouseOver: () => n && r(t),
|
|
402
396
|
children: e
|
|
403
397
|
});
|
|
404
|
-
},
|
|
405
|
-
let { params: e } =
|
|
398
|
+
}, R = () => {
|
|
399
|
+
let { params: e } = N();
|
|
406
400
|
return e;
|
|
407
|
-
},
|
|
408
|
-
let { loaderCache: e } =
|
|
401
|
+
}, z = () => N().location, B = () => {
|
|
402
|
+
let { loaderCache: e } = F();
|
|
409
403
|
return e;
|
|
410
|
-
},
|
|
411
|
-
let { location: { pathname: t }, blockerState: n } =
|
|
404
|
+
}, V = (e) => {
|
|
405
|
+
let { location: { pathname: t }, blockerState: n } = N(), { updateBlockedRoute: r } = P(), i = e();
|
|
412
406
|
return a(() => r(i ? {
|
|
413
407
|
type: "charge",
|
|
414
408
|
payload: t
|
|
@@ -421,19 +415,25 @@ var l = (e, t) => () => (t || (e((t = { exports: {} }).exports, t), e = null), t
|
|
|
421
415
|
process: () => r({ type: "process" }),
|
|
422
416
|
reset: () => r({ type: "reset" })
|
|
423
417
|
};
|
|
424
|
-
},
|
|
418
|
+
}, H = (e) => {
|
|
425
419
|
a(() => {
|
|
426
420
|
let t = (t) => {
|
|
427
421
|
e && (t.preventDefault(), e());
|
|
428
422
|
};
|
|
429
423
|
return window.addEventListener("beforeunload", t), () => window.removeEventListener("beforeunload", t);
|
|
430
424
|
}, [e]);
|
|
431
|
-
},
|
|
432
|
-
let { context: e } =
|
|
425
|
+
}, U = () => {
|
|
426
|
+
let { context: e } = F(), { setContext: t } = P();
|
|
433
427
|
return {
|
|
434
428
|
context: e,
|
|
435
429
|
setContext: t
|
|
436
430
|
};
|
|
431
|
+
}, W = (e, t) => {
|
|
432
|
+
let n = /* @__PURE__ */ Error();
|
|
433
|
+
throw n.cause = "redirect", Object.assign(n, {
|
|
434
|
+
url: e,
|
|
435
|
+
search: t || ""
|
|
436
|
+
});
|
|
437
437
|
};
|
|
438
438
|
//#endregion
|
|
439
|
-
export {
|
|
439
|
+
export { L as Link, M as Router, x as createRouter, W as redirect, H as useBeforeUnload, V as useBlocker, B as useLoaderState, z as useLocation, I as useNavigate, R as useParams, U as useRouterContext };
|
package/dist/utils/redirect.d.ts
CHANGED
|
@@ -1,7 +1 @@
|
|
|
1
|
-
export declare
|
|
2
|
-
url: string;
|
|
3
|
-
search?: string;
|
|
4
|
-
cause: 'redirect';
|
|
5
|
-
constructor(url: string, search?: string);
|
|
6
|
-
}
|
|
7
|
-
export declare const redirect: (url: string, search?: string) => Promise<never>;
|
|
1
|
+
export declare const redirect: (url: string, search?: string) => never;
|