@tanstack/react-router 0.0.1-beta.209 → 0.0.1-beta.210

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.
Files changed (72) hide show
  1. package/build/cjs/CatchBoundary.js +125 -0
  2. package/build/cjs/CatchBoundary.js.map +1 -0
  3. package/build/cjs/Matches.js +223 -0
  4. package/build/cjs/Matches.js.map +1 -0
  5. package/build/cjs/RouterProvider.js +99 -53
  6. package/build/cjs/RouterProvider.js.map +1 -1
  7. package/build/cjs/index.js +46 -37
  8. package/build/cjs/index.js.map +1 -1
  9. package/build/cjs/lazyRouteComponent.js +57 -0
  10. package/build/cjs/lazyRouteComponent.js.map +1 -0
  11. package/build/cjs/link.js +150 -0
  12. package/build/cjs/link.js.map +1 -0
  13. package/build/cjs/route.js +9 -5
  14. package/build/cjs/route.js.map +1 -1
  15. package/build/cjs/router.js.map +1 -1
  16. package/build/cjs/searchParams.js.map +1 -1
  17. package/build/cjs/useBlocker.js +64 -0
  18. package/build/cjs/useBlocker.js.map +1 -0
  19. package/build/cjs/useNavigate.js +78 -0
  20. package/build/cjs/useNavigate.js.map +1 -0
  21. package/build/cjs/useParams.js +28 -0
  22. package/build/cjs/useParams.js.map +1 -0
  23. package/build/cjs/useSearch.js +27 -0
  24. package/build/cjs/useSearch.js.map +1 -0
  25. package/build/cjs/utils.js +30 -1
  26. package/build/cjs/utils.js.map +1 -1
  27. package/build/esm/index.js +491 -514
  28. package/build/esm/index.js.map +1 -1
  29. package/build/stats-html.html +1 -1
  30. package/build/stats-react.json +484 -208
  31. package/build/types/CatchBoundary.d.ts +33 -0
  32. package/build/types/Matches.d.ts +31 -0
  33. package/build/types/RouterProvider.d.ts +42 -18
  34. package/build/types/fileRoute.d.ts +7 -7
  35. package/build/types/index.d.ts +13 -7
  36. package/build/types/injectHtml.d.ts +0 -0
  37. package/build/types/lazyRouteComponent.d.ts +2 -0
  38. package/build/types/link.d.ts +10 -3
  39. package/build/types/route.d.ts +39 -7
  40. package/build/types/router.d.ts +6 -7
  41. package/build/types/useBlocker.d.ts +8 -0
  42. package/build/types/useNavigate.d.ts +20 -0
  43. package/build/types/useParams.d.ts +7 -0
  44. package/build/types/useSearch.d.ts +7 -0
  45. package/build/types/utils.d.ts +17 -0
  46. package/build/umd/index.development.js +492 -513
  47. package/build/umd/index.development.js.map +1 -1
  48. package/build/umd/index.production.js +1 -1
  49. package/build/umd/index.production.js.map +1 -1
  50. package/package.json +2 -2
  51. package/src/CatchBoundary.tsx +97 -0
  52. package/src/Matches.tsx +315 -0
  53. package/src/RouterProvider.tsx +317 -251
  54. package/src/index.tsx +17 -8
  55. package/src/injectHtml.ts +28 -0
  56. package/src/lazyRouteComponent.tsx +33 -0
  57. package/src/{link.ts → link.tsx} +163 -3
  58. package/src/location.ts +1 -0
  59. package/src/route.ts +86 -16
  60. package/src/router.ts +6 -7
  61. package/src/searchParams.ts +1 -0
  62. package/src/useBlocker.tsx +34 -0
  63. package/src/useNavigate.tsx +109 -0
  64. package/src/useParams.tsx +25 -0
  65. package/src/useSearch.tsx +25 -0
  66. package/src/utils.ts +83 -3
  67. package/build/cjs/react.js +0 -620
  68. package/build/cjs/react.js.map +0 -1
  69. package/build/types/RouteMatch.d.ts +0 -23
  70. package/build/types/react.d.ts +0 -141
  71. package/src/RouteMatch.ts +0 -28
  72. package/src/react.tsx +0 -1013
package/src/utils.ts CHANGED
@@ -1,4 +1,10 @@
1
1
  import * as React from 'react'
2
+ import { useMatch } from './Matches'
3
+ import { RouteMatch } from './RouterProvider'
4
+ import { AnyRoute } from './route'
5
+ import { ParseRoute, RouteIds, RoutesById, RouteById } from './routeInfo'
6
+ import { RegisteredRouter } from './router'
7
+
2
8
  export type NoInfer<T> = [T][T extends any ? 0 : never]
3
9
  export type IsAny<T, Y, N = T> = 1 extends 0 & T ? Y : N
4
10
  export type IsAnyBoolean<T> = 1 extends 0 & T ? true : false
@@ -239,9 +245,9 @@ export function partialDeepEqual(a: any, b: any): boolean {
239
245
  }
240
246
 
241
247
  if (Array.isArray(a) && Array.isArray(b)) {
242
- return (
243
- a.length === b.length &&
244
- a.every((item, index) => partialDeepEqual(item, b[index]))
248
+ return !(
249
+ a.length !== b.length ||
250
+ a.some((item, index) => !partialDeepEqual(item, b[index]))
245
251
  )
246
252
  }
247
253
 
@@ -255,3 +261,77 @@ export function useStableCallback<T extends (...args: any[]) => any>(fn: T): T {
255
261
  const ref = React.useRef((...args: any[]) => fnRef.current(...args))
256
262
  return ref.current as T
257
263
  }
264
+
265
+ export function shallow<T>(objA: T, objB: T) {
266
+ if (Object.is(objA, objB)) {
267
+ return true
268
+ }
269
+
270
+ if (
271
+ typeof objA !== 'object' ||
272
+ objA === null ||
273
+ typeof objB !== 'object' ||
274
+ objB === null
275
+ ) {
276
+ return false
277
+ }
278
+
279
+ const keysA = Object.keys(objA)
280
+ if (keysA.length !== Object.keys(objB).length) {
281
+ return false
282
+ }
283
+
284
+ for (let i = 0; i < keysA.length; i++) {
285
+ if (
286
+ !Object.prototype.hasOwnProperty.call(objB, keysA[i] as string) ||
287
+ !Object.is(objA[keysA[i] as keyof T], objB[keysA[i] as keyof T])
288
+ ) {
289
+ return false
290
+ }
291
+ }
292
+ return true
293
+ }
294
+
295
+ export type StrictOrFrom<TFrom> =
296
+ | {
297
+ from: TFrom
298
+ strict?: true
299
+ }
300
+ | {
301
+ from?: never
302
+ strict: false
303
+ }
304
+
305
+ export type RouteFromIdOrRoute<
306
+ T,
307
+ TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
308
+ > = T extends ParseRoute<TRouteTree>
309
+ ? T
310
+ : T extends RouteIds<TRouteTree>
311
+ ? RoutesById<TRouteTree>[T]
312
+ : T extends string
313
+ ? RouteIds<TRouteTree>
314
+ : never
315
+
316
+ export function useRouteContext<
317
+ TRouteTree extends AnyRoute = RegisteredRouter['routeTree'],
318
+ TFrom extends RouteIds<TRouteTree> = RouteIds<TRouteTree>,
319
+ TStrict extends boolean = true,
320
+ TRouteContext = RouteById<TRouteTree, TFrom>['types']['allContext'],
321
+ TSelected = TRouteContext,
322
+ >(
323
+ opts: StrictOrFrom<TFrom> & {
324
+ select?: (search: TRouteContext) => TSelected
325
+ },
326
+ ): TStrict extends true ? TSelected : TSelected | undefined {
327
+ return useMatch({
328
+ ...(opts as any),
329
+ select: (match: RouteMatch) =>
330
+ opts?.select
331
+ ? opts.select(match.context as TRouteContext)
332
+ : match.context,
333
+ })
334
+ }
335
+
336
+ export const useLayoutEffect =
337
+ typeof window !== 'undefined' ? React.useLayoutEffect : React.useEffect
@@ -1,620 +0,0 @@
1
- /**
2
- * @tanstack/react-router/src/index.tsx
3
- *
4
- * Copyright (c) TanStack
5
- *
6
- * This source code is licensed under the MIT license found in the
7
- * LICENSE.md file in the root directory of this source tree.
8
- *
9
- * @license MIT
10
- */
11
- 'use strict';
12
-
13
- Object.defineProperty(exports, '__esModule', { value: true });
14
-
15
- var _rollupPluginBabelHelpers = require('./_virtual/_rollupPluginBabelHelpers.js');
16
- var React = require('react');
17
- var invariant = require('tiny-invariant');
18
- var warning = require('tiny-warning');
19
- var route = require('./route.js');
20
- var utils = require('./utils.js');
21
- var RouterProvider = require('./RouterProvider.js');
22
-
23
- function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
24
-
25
- function _interopNamespace(e) {
26
- if (e && e.__esModule) return e;
27
- var n = Object.create(null);
28
- if (e) {
29
- Object.keys(e).forEach(function (k) {
30
- if (k !== 'default') {
31
- var d = Object.getOwnPropertyDescriptor(e, k);
32
- Object.defineProperty(n, k, d.get ? d : {
33
- enumerable: true,
34
- get: function () { return e[k]; }
35
- });
36
- }
37
- });
38
- }
39
- n["default"] = e;
40
- return Object.freeze(n);
41
- }
42
-
43
- var React__namespace = /*#__PURE__*/_interopNamespace(React);
44
- var invariant__default = /*#__PURE__*/_interopDefaultLegacy(invariant);
45
- var warning__default = /*#__PURE__*/_interopDefaultLegacy(warning);
46
-
47
- const useLayoutEffect = typeof window !== 'undefined' ? React__namespace.useLayoutEffect : React__namespace.useEffect;
48
-
49
- //
50
-
51
- function lazyRouteComponent(importer, exportName) {
52
- let loadPromise;
53
- const load = () => {
54
- if (!loadPromise) {
55
- loadPromise = importer();
56
- }
57
- return loadPromise;
58
- };
59
- const lazyComp = /*#__PURE__*/React__namespace.lazy(async () => {
60
- const moduleExports = await load();
61
- const comp = moduleExports[exportName ?? 'default'];
62
- return {
63
- default: comp
64
- };
65
- });
66
- lazyComp.preload = load;
67
- return lazyComp;
68
- }
69
- //
70
-
71
- function useLinkProps(options) {
72
- const {
73
- buildLink,
74
- state: routerState
75
- } = useRouter();
76
- const match = useMatch({
77
- strict: false
78
- });
79
- const {
80
- // custom props
81
- type,
82
- children,
83
- target,
84
- activeProps = () => ({
85
- className: 'active'
86
- }),
87
- inactiveProps = () => ({}),
88
- activeOptions,
89
- disabled,
90
- hash,
91
- search,
92
- params,
93
- to,
94
- state,
95
- mask,
96
- preload,
97
- preloadDelay,
98
- replace,
99
- // element props
100
- style,
101
- className,
102
- onClick,
103
- onFocus,
104
- onMouseEnter,
105
- onMouseLeave,
106
- onTouchStart,
107
- ...rest
108
- } = options;
109
- const linkInfo = buildLink(routerState, {
110
- from: options.to ? match.pathname : undefined,
111
- ...options
112
- });
113
- if (linkInfo.type === 'external') {
114
- const {
115
- href
116
- } = linkInfo;
117
- return {
118
- href
119
- };
120
- }
121
- const {
122
- handleClick,
123
- handleFocus,
124
- handleEnter,
125
- handleLeave,
126
- handleTouchStart,
127
- isActive,
128
- next
129
- } = linkInfo;
130
- const handleReactClick = e => {
131
- if (options.startTransition ?? true) {
132
- (React__namespace.startTransition || (d => d))(() => {
133
- handleClick(e);
134
- });
135
- }
136
- };
137
- const composeHandlers = handlers => e => {
138
- if (e.persist) e.persist();
139
- handlers.filter(Boolean).forEach(handler => {
140
- if (e.defaultPrevented) return;
141
- handler(e);
142
- });
143
- };
144
-
145
- // Get the active props
146
- const resolvedActiveProps = isActive ? utils.functionalUpdate(activeProps, {}) ?? {} : {};
147
-
148
- // Get the inactive props
149
- const resolvedInactiveProps = isActive ? {} : utils.functionalUpdate(inactiveProps, {}) ?? {};
150
- return {
151
- ...resolvedActiveProps,
152
- ...resolvedInactiveProps,
153
- ...rest,
154
- href: disabled ? undefined : next.maskedLocation ? next.maskedLocation.href : next.href,
155
- onClick: composeHandlers([onClick, handleReactClick]),
156
- onFocus: composeHandlers([onFocus, handleFocus]),
157
- onMouseEnter: composeHandlers([onMouseEnter, handleEnter]),
158
- onMouseLeave: composeHandlers([onMouseLeave, handleLeave]),
159
- onTouchStart: composeHandlers([onTouchStart, handleTouchStart]),
160
- target,
161
- style: {
162
- ...style,
163
- ...resolvedActiveProps.style,
164
- ...resolvedInactiveProps.style
165
- },
166
- className: [className, resolvedActiveProps.className, resolvedInactiveProps.className].filter(Boolean).join(' ') || undefined,
167
- ...(disabled ? {
168
- role: 'link',
169
- 'aria-disabled': true
170
- } : undefined),
171
- ['data-status']: isActive ? 'active' : undefined
172
- };
173
- }
174
- const Link = /*#__PURE__*/React__namespace.forwardRef((props, ref) => {
175
- const linkProps = useLinkProps(props);
176
- return /*#__PURE__*/React__namespace.createElement("a", _rollupPluginBabelHelpers["extends"]({
177
- ref: ref
178
- }, linkProps, {
179
- children: typeof props.children === 'function' ? props.children({
180
- isActive: linkProps['data-status'] === 'active'
181
- }) : props.children
182
- }));
183
- });
184
- function Navigate(props) {
185
- const {
186
- navigate
187
- } = useRouter();
188
- const match = useMatch({
189
- strict: false
190
- });
191
- useLayoutEffect(() => {
192
- navigate({
193
- from: props.to ? match.pathname : undefined,
194
- ...props
195
- });
196
- }, []);
197
- return null;
198
- }
199
- const matchesContext = /*#__PURE__*/React__namespace.createContext(null);
200
- function useRouter() {
201
- const resolvedContext = window.__TSR_ROUTER_CONTEXT__ || RouterProvider.routerContext;
202
- const value = React__namespace.useContext(resolvedContext);
203
- warning__default["default"](value, 'useRouter must be used inside a <RouterProvider> component!');
204
- return value;
205
- }
206
- function useRouterState(opts) {
207
- const {
208
- state
209
- } = useRouter();
210
- // return useStore(router.__store, opts?.select as any)
211
- return opts?.select ? opts.select(state) : state;
212
- }
213
- function useMatches(opts) {
214
- const contextMatches = React__namespace.useContext(matchesContext);
215
- return useRouterState({
216
- select: state => {
217
- const matches = state.matches.slice(state.matches.findIndex(d => d.id === contextMatches[0]?.id));
218
- return opts?.select ? opts.select(matches) : matches;
219
- }
220
- });
221
- }
222
- function useMatch(opts) {
223
- const nearestMatch = React__namespace.useContext(matchesContext)[0];
224
- const nearestMatchRouteId = nearestMatch?.routeId;
225
- const matchRouteId = useRouterState({
226
- select: state => {
227
- const match = opts?.from ? state.matches.find(d => d.routeId === opts?.from) : state.matches.find(d => d.id === nearestMatch.id);
228
- return match.routeId;
229
- }
230
- });
231
- if (opts?.strict ?? true) {
232
- invariant__default["default"](nearestMatchRouteId == matchRouteId, `useMatch("${matchRouteId}") is being called in a component that is meant to render the '${nearestMatchRouteId}' route. Did you mean to 'useMatch("${matchRouteId}", { strict: false })' or 'useRoute("${matchRouteId}")' instead?`);
233
- }
234
- const matchSelection = useRouterState({
235
- select: state => {
236
- const match = opts?.from ? state.matches.find(d => d.routeId === opts?.from) : state.matches.find(d => d.id === nearestMatch.id);
237
- invariant__default["default"](match, `Could not find ${opts?.from ? `an active match from "${opts.from}"` : 'a nearest match!'}`);
238
- return opts?.select ? opts.select(match) : match;
239
- }
240
- });
241
- return matchSelection;
242
- }
243
- function useRouteContext(opts) {
244
- return useMatch({
245
- ...opts,
246
- select: match => opts?.select ? opts.select(match.context) : match.context
247
- });
248
- }
249
- function useSearch(opts) {
250
- return useMatch({
251
- ...opts,
252
- select: match => {
253
- return opts?.select ? opts.select(match.search) : match.search;
254
- }
255
- });
256
- }
257
- function useParams(opts) {
258
- return useRouterState({
259
- select: state => {
260
- const params = utils.last(state.matches)?.params;
261
- return opts?.select ? opts.select(params) : params;
262
- }
263
- });
264
- }
265
- function useNavigate(defaultOpts) {
266
- const {
267
- navigate
268
- } = useRouter();
269
- const match = useMatch({
270
- strict: false
271
- });
272
- return React__namespace.useCallback(opts => {
273
- return navigate({
274
- from: opts?.to ? match.pathname : undefined,
275
- ...defaultOpts,
276
- ...opts
277
- });
278
- }, []);
279
- }
280
- function typedNavigate(navigate) {
281
- return navigate;
282
- }
283
- function useMatchRoute() {
284
- const {
285
- state,
286
- matchRoute
287
- } = useRouter();
288
- return React__namespace.useCallback(opts => {
289
- const {
290
- pending,
291
- caseSensitive,
292
- ...rest
293
- } = opts;
294
- return matchRoute(state, rest, {
295
- pending,
296
- caseSensitive
297
- });
298
- }, []);
299
- }
300
- function Matches() {
301
- const {
302
- routesById,
303
- state
304
- } = useRouter();
305
-
306
- // const matches = useRouterState({
307
- // select: (state) => {
308
- // return state.matches
309
- // },
310
- // })
311
-
312
- const {
313
- matches
314
- } = state;
315
- const locationKey = useRouterState({
316
- select: d => d.resolvedLocation.state?.key
317
- });
318
- const route$1 = routesById[route.rootRouteId];
319
- const errorComponent = React__namespace.useCallback(props => {
320
- return /*#__PURE__*/React__namespace.createElement(ErrorComponent, {
321
- ...props,
322
- useMatch: route$1.useMatch,
323
- useRouteContext: route$1.useRouteContext,
324
- useSearch: route$1.useSearch,
325
- useParams: route$1.useParams
326
- });
327
- }, [route$1]);
328
- return /*#__PURE__*/React__namespace.createElement(matchesContext.Provider, {
329
- value: matches
330
- }, /*#__PURE__*/React__namespace.createElement(CatchBoundary, {
331
- resetKey: locationKey,
332
- errorComponent: errorComponent,
333
- onCatch: () => {
334
- warning__default["default"](false, `Error in router! Consider setting an 'errorComponent' in your RootRoute! 👍`);
335
- }
336
- }, matches.length ? /*#__PURE__*/React__namespace.createElement(Match, {
337
- matches: matches
338
- }) : null));
339
- }
340
- function MatchRoute(props) {
341
- const matchRoute = useMatchRoute();
342
- const params = matchRoute(props);
343
- if (typeof props.children === 'function') {
344
- return props.children(params);
345
- }
346
- return !!params ? props.children : null;
347
- }
348
- function Outlet() {
349
- const matches = React__namespace.useContext(matchesContext).slice(1);
350
- if (!matches[0]) {
351
- return null;
352
- }
353
- return /*#__PURE__*/React__namespace.createElement(Match, {
354
- matches: matches
355
- });
356
- }
357
- const defaultPending = () => null;
358
- function Match({
359
- matches
360
- }) {
361
- const {
362
- options,
363
- routesById
364
- } = useRouter();
365
- const match = matches[0];
366
- const routeId = match?.routeId;
367
- const route = routesById[routeId];
368
- const locationKey = useRouterState({
369
- select: s => s.resolvedLocation.state?.key
370
- });
371
- const PendingComponent = route.options.pendingComponent ?? options.defaultPendingComponent ?? defaultPending;
372
- const routeErrorComponent = route.options.errorComponent ?? options.defaultErrorComponent ?? ErrorComponent;
373
- const ResolvedSuspenseBoundary = route.options.wrapInSuspense ?? !route.isRoot ? React__namespace.Suspense : SafeFragment;
374
- const ResolvedCatchBoundary = !!routeErrorComponent ? CatchBoundary : SafeFragment;
375
- const errorComponent = React__namespace.useCallback(props => {
376
- return /*#__PURE__*/React__namespace.createElement(routeErrorComponent, {
377
- ...props,
378
- useMatch: route.useMatch,
379
- useRouteContext: route.useRouteContext,
380
- useSearch: route.useSearch,
381
- useParams: route.useParams
382
- });
383
- }, [route]);
384
- return /*#__PURE__*/React__namespace.createElement(matchesContext.Provider, {
385
- value: matches
386
- }, /*#__PURE__*/React__namespace.createElement(ResolvedSuspenseBoundary, {
387
- fallback: /*#__PURE__*/React__namespace.createElement(PendingComponent, {
388
- useMatch: route.useMatch,
389
- useRouteContext: route.useRouteContext,
390
- useSearch: route.useSearch,
391
- useParams: route.useParams
392
- })
393
- }, /*#__PURE__*/React__namespace.createElement(ResolvedCatchBoundary, {
394
- resetKey: locationKey,
395
- errorComponent: errorComponent,
396
- onCatch: () => {
397
- warning__default["default"](false, `Error in route match: ${match.id}`);
398
- }
399
- }, /*#__PURE__*/React__namespace.createElement(MatchInner, {
400
- match: match
401
- }))));
402
- }
403
- function MatchInner({
404
- match
405
- }) {
406
- const {
407
- options,
408
- routesById
409
- } = useRouter();
410
- const route = routesById[match.routeId];
411
- if (match.status === 'error') {
412
- throw match.error;
413
- }
414
- if (match.status === 'pending') {
415
- throw match.loadPromise;
416
- }
417
- if (match.status === 'success') {
418
- let comp = route.options.component ?? options.defaultComponent;
419
- if (comp) {
420
- return /*#__PURE__*/React__namespace.createElement(comp, {
421
- useMatch: route.useMatch,
422
- useRouteContext: route.useRouteContext,
423
- useSearch: route.useSearch,
424
- useParams: route.useParams
425
- });
426
- }
427
- return /*#__PURE__*/React__namespace.createElement(Outlet, null);
428
- }
429
- invariant__default["default"](false, 'Idle routeMatch status encountered during rendering! You should never see this. File an issue!');
430
- }
431
- function SafeFragment(props) {
432
- return /*#__PURE__*/React__namespace.createElement(React__namespace.Fragment, null, props.children);
433
- }
434
-
435
- // export function useInjectHtml() {
436
- // const { } = useRouter()
437
-
438
- // return React.useCallback(
439
- // (html: string | (() => Promise<string> | string)) => {
440
- // router.injectHtml(html)
441
- // },
442
- // [],
443
- // )
444
- // }
445
-
446
- // export function useDehydrate() {
447
- // const { } = useRouter()
448
-
449
- // return React.useCallback(function dehydrate<T>(
450
- // key: any,
451
- // data: T | (() => Promise<T> | T),
452
- // ) {
453
- // return router.dehydrateData(key, data)
454
- // },
455
- // [])
456
- // }
457
-
458
- // export function useHydrate() {
459
- // const { } = useRouter()
460
-
461
- // return function hydrate<T = unknown>(key: any) {
462
- // return router.hydrateData(key) as T
463
- // }
464
- // }
465
-
466
- // This is the messiest thing ever... I'm either seriously tired (likely) or
467
- // there has to be a better way to reset error boundaries when the
468
- // router's location key changes.
469
-
470
- function CatchBoundary(props) {
471
- const errorComponent = props.errorComponent ?? ErrorComponent;
472
- return /*#__PURE__*/React__namespace.createElement(CatchBoundaryImpl, {
473
- resetKey: props.resetKey,
474
- onCatch: props.onCatch,
475
- children: ({
476
- error
477
- }) => {
478
- if (error) {
479
- return /*#__PURE__*/React__namespace.createElement(errorComponent, {
480
- error
481
- });
482
- }
483
- return props.children;
484
- }
485
- });
486
- }
487
- class CatchBoundaryImpl extends React__namespace.Component {
488
- state = {
489
- error: null
490
- };
491
- static getDerivedStateFromError(error) {
492
- return {
493
- error
494
- };
495
- }
496
- componentDidUpdate(prevProps, prevState) {
497
- if (prevState.error && prevProps.resetKey !== this.props.resetKey) {
498
- this.setState({
499
- error: null
500
- });
501
- }
502
- }
503
- componentDidCatch(error) {
504
- this.props.onCatch?.(error);
505
- }
506
- render() {
507
- return this.props.children(this.state);
508
- }
509
- }
510
- function ErrorComponent({
511
- error
512
- }) {
513
- const [show, setShow] = React__namespace.useState(process.env.NODE_ENV !== 'production');
514
- return /*#__PURE__*/React__namespace.createElement("div", {
515
- style: {
516
- padding: '.5rem',
517
- maxWidth: '100%'
518
- }
519
- }, /*#__PURE__*/React__namespace.createElement("div", {
520
- style: {
521
- display: 'flex',
522
- alignItems: 'center',
523
- gap: '.5rem'
524
- }
525
- }, /*#__PURE__*/React__namespace.createElement("strong", {
526
- style: {
527
- fontSize: '1rem'
528
- }
529
- }, "Something went wrong!"), /*#__PURE__*/React__namespace.createElement("button", {
530
- style: {
531
- appearance: 'none',
532
- fontSize: '.6em',
533
- border: '1px solid currentColor',
534
- padding: '.1rem .2rem',
535
- fontWeight: 'bold',
536
- borderRadius: '.25rem'
537
- },
538
- onClick: () => setShow(d => !d)
539
- }, show ? 'Hide Error' : 'Show Error')), /*#__PURE__*/React__namespace.createElement("div", {
540
- style: {
541
- height: '.25rem'
542
- }
543
- }), show ? /*#__PURE__*/React__namespace.createElement("div", null, /*#__PURE__*/React__namespace.createElement("pre", {
544
- style: {
545
- fontSize: '.7em',
546
- border: '1px solid red',
547
- borderRadius: '.25rem',
548
- padding: '.3rem',
549
- color: 'red',
550
- overflow: 'auto'
551
- }
552
- }, error.message ? /*#__PURE__*/React__namespace.createElement("code", null, error.message) : null)) : null);
553
- }
554
- function useBlocker(message, condition = true) {
555
- const {
556
- history
557
- } = useRouter();
558
- React__namespace.useEffect(() => {
559
- if (!condition) return;
560
- let unblock = history.block((retry, cancel) => {
561
- if (window.confirm(message)) {
562
- unblock();
563
- retry();
564
- }
565
- });
566
- return unblock;
567
- });
568
- }
569
- function Block({
570
- message,
571
- condition,
572
- children
573
- }) {
574
- useBlocker(message, condition);
575
- return children ?? null;
576
- }
577
- function shallow(objA, objB) {
578
- if (Object.is(objA, objB)) {
579
- return true;
580
- }
581
- if (typeof objA !== 'object' || objA === null || typeof objB !== 'object' || objB === null) {
582
- return false;
583
- }
584
- const keysA = Object.keys(objA);
585
- if (keysA.length !== Object.keys(objB).length) {
586
- return false;
587
- }
588
- for (let i = 0; i < keysA.length; i++) {
589
- if (!Object.prototype.hasOwnProperty.call(objB, keysA[i]) || !Object.is(objA[keysA[i]], objB[keysA[i]])) {
590
- return false;
591
- }
592
- }
593
- return true;
594
- }
595
-
596
- exports.Block = Block;
597
- exports.CatchBoundary = CatchBoundary;
598
- exports.CatchBoundaryImpl = CatchBoundaryImpl;
599
- exports.ErrorComponent = ErrorComponent;
600
- exports.Link = Link;
601
- exports.MatchRoute = MatchRoute;
602
- exports.Matches = Matches;
603
- exports.Navigate = Navigate;
604
- exports.Outlet = Outlet;
605
- exports.lazyRouteComponent = lazyRouteComponent;
606
- exports.matchesContext = matchesContext;
607
- exports.shallow = shallow;
608
- exports.typedNavigate = typedNavigate;
609
- exports.useBlocker = useBlocker;
610
- exports.useLinkProps = useLinkProps;
611
- exports.useMatch = useMatch;
612
- exports.useMatchRoute = useMatchRoute;
613
- exports.useMatches = useMatches;
614
- exports.useNavigate = useNavigate;
615
- exports.useParams = useParams;
616
- exports.useRouteContext = useRouteContext;
617
- exports.useRouter = useRouter;
618
- exports.useRouterState = useRouterState;
619
- exports.useSearch = useSearch;
620
- //# sourceMappingURL=react.js.map