@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
@@ -12,14 +12,15 @@
12
12
 
13
13
  Object.defineProperty(exports, '__esModule', { value: true });
14
14
 
15
+ var history = require('@tanstack/history');
15
16
  var React = require('react');
16
- var router = require('./router.js');
17
+ var invariant = require('tiny-invariant');
18
+ var warning = require('tiny-warning');
19
+ var Matches = require('./Matches.js');
20
+ var path = require('./path.js');
17
21
  var redirects = require('./redirects.js');
22
+ var router = require('./router.js');
18
23
  var utils = require('./utils.js');
19
- var react = require('./react.js');
20
- var path = require('./path.js');
21
- var invariant = require('tiny-invariant');
22
- var history = require('@tanstack/history');
23
24
 
24
25
  function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }
25
26
 
@@ -43,17 +44,22 @@ function _interopNamespace(e) {
43
44
 
44
45
  var React__namespace = /*#__PURE__*/_interopNamespace(React);
45
46
  var invariant__default = /*#__PURE__*/_interopDefaultLegacy(invariant);
47
+ var warning__default = /*#__PURE__*/_interopDefaultLegacy(warning);
46
48
 
47
- const preloadWarning = 'Error preloading route! ☝️';
48
49
  const routerContext = /*#__PURE__*/React__namespace.createContext(null);
49
50
  if (typeof document !== 'undefined') {
50
51
  window.__TSR_ROUTER_CONTEXT__ = routerContext;
51
52
  }
53
+ const preloadWarning = 'Error preloading route! ☝️';
54
+ function isCtrlEvent(e) {
55
+ return !!(e.metaKey || e.altKey || e.ctrlKey || e.shiftKey);
56
+ }
57
+ class SearchParamError extends Error {}
58
+ class PathParamError extends Error {}
52
59
  function getInitialRouterState(location) {
53
60
  return {
54
61
  status: 'idle',
55
- isFetching: false,
56
- resolvedLocation: location,
62
+ resolvedLocation: undefined,
57
63
  location: location,
58
64
  matches: [],
59
65
  pendingMatches: [],
@@ -114,7 +120,20 @@ function RouterProvider({
114
120
  }
115
121
  return location;
116
122
  });
117
- const [state, setState] = React__namespace.useState(() => getInitialRouterState(parseLocation()));
123
+ const [preState, setState] = React__namespace.useState(() => getInitialRouterState(parseLocation()));
124
+ const [isTransitioning, startReactTransition] = React__namespace.useTransition();
125
+ const state = React__namespace.useMemo(() => ({
126
+ ...preState,
127
+ status: isTransitioning ? 'pending' : 'idle'
128
+ }), [preState, isTransitioning]);
129
+ React__namespace.useLayoutEffect(() => {
130
+ if (!isTransitioning && state.resolvedLocation !== state.location) {
131
+ setState(s => ({
132
+ ...s,
133
+ resolvedLocation: s.location
134
+ }));
135
+ }
136
+ });
118
137
  const basepath = `/${path.trimPath(options.basepath ?? '') ?? ''}`;
119
138
  const resolvePathWithBase = utils.useStableCallback((from, path$1) => {
120
139
  return path.resolvePath(basepath, from, path.cleanPath(path$1));
@@ -333,7 +352,7 @@ function RouterProvider({
333
352
  cancelMatch(match.id);
334
353
  });
335
354
  });
336
- const buildLocation = utils.useStableCallback((opts = {}) => {
355
+ const buildLocation = utils.useStableCallback(opts => {
337
356
  const build = (dest = {}, matches) => {
338
357
  const from = latestLocationRef.current;
339
358
  const fromPathname = dest.from ?? from.pathname;
@@ -426,7 +445,10 @@ function RouterProvider({
426
445
  }
427
446
  return buildWithMatches(opts);
428
447
  });
429
- const commitLocation = utils.useStableCallback(async next => {
448
+ const commitLocation = utils.useStableCallback(async ({
449
+ startTransition,
450
+ ...next
451
+ }) => {
430
452
  if (navigateTimeoutRef.current) clearTimeout(navigateTimeoutRef.current);
431
453
  const isSameUrl = latestLocationRef.current.href === next.href;
432
454
 
@@ -459,7 +481,14 @@ function RouterProvider({
459
481
  nextHistory.state.__tempKey = tempLocationKeyRef.current;
460
482
  }
461
483
  }
462
- history$1[next.replace ? 'replace' : 'push'](nextHistory.href, nextHistory.state);
484
+ const apply = () => {
485
+ history$1[next.replace ? 'replace' : 'push'](nextHistory.href, nextHistory.state);
486
+ };
487
+ if (startTransition ?? true) {
488
+ startReactTransition(apply);
489
+ } else {
490
+ apply();
491
+ }
463
492
  }
464
493
  resetNextScrollRef.current = next.resetScroll ?? true;
465
494
  return latestLoadPromiseRef.current;
@@ -467,11 +496,13 @@ function RouterProvider({
467
496
  const buildAndCommitLocation = utils.useStableCallback(({
468
497
  replace,
469
498
  resetScroll,
499
+ startTransition,
470
500
  ...rest
471
501
  } = {}) => {
472
502
  const location = buildLocation(rest);
473
503
  return commitLocation({
474
504
  ...location,
505
+ startTransition,
475
506
  replace,
476
507
  resetScroll
477
508
  });
@@ -553,7 +584,8 @@ function RouterProvider({
553
584
  navigate: opts => navigate({
554
585
  ...opts,
555
586
  from: match.pathname
556
- })
587
+ }),
588
+ buildLocation
557
589
  })) ?? {};
558
590
  const context = {
559
591
  ...parentContext,
@@ -614,7 +646,7 @@ function RouterProvider({
614
646
  from: match.pathname
615
647
  })
616
648
  });
617
- await Promise.all([componentsPromise, loaderPromise]);
649
+ const [_, loaderContext] = await Promise.all([componentsPromise, loaderPromise]);
618
650
  if (latestPromise = checkLatest()) return await latestPromise;
619
651
  matches[index] = match = {
620
652
  ...match,
@@ -668,7 +700,7 @@ function RouterProvider({
668
700
  const load = utils.useStableCallback(async () => {
669
701
  const promise = new Promise(async (resolve, reject) => {
670
702
  const next = latestLocationRef.current;
671
- const prevLocation = state.resolvedLocation;
703
+ const prevLocation = state.resolvedLocation || state.location;
672
704
  const pathDidChange = !!(next && prevLocation.href !== next.href);
673
705
  let latestPromise;
674
706
 
@@ -681,19 +713,14 @@ function RouterProvider({
681
713
  pathChanged: pathDidChange
682
714
  });
683
715
 
684
- // Ingest the new location
685
- setState(s => ({
686
- ...s,
687
- location: next
688
- }));
689
-
690
716
  // Match the routes
691
717
  let matches = matchRoutes(next.pathname, next.search, {
692
718
  debug: true
693
719
  });
720
+
721
+ // Ingest the new matches
694
722
  setState(s => ({
695
723
  ...s,
696
- status: 'pending',
697
724
  matches
698
725
  }));
699
726
  try {
@@ -724,11 +751,11 @@ function RouterProvider({
724
751
  // state.pendingMatches.includes(id),
725
752
  // )
726
753
 
727
- setState(s => ({
728
- ...s,
729
- status: 'idle',
730
- resolvedLocation: s.location
731
- }));
754
+ // setState((s) => ({
755
+ // ...s,
756
+ // status: 'idle',
757
+ // resolvedLocation: s.location,
758
+ // }))
732
759
 
733
760
  // TODO:
734
761
  // ;(
@@ -773,7 +800,7 @@ function RouterProvider({
773
800
  });
774
801
  return [utils.last(matches), matches];
775
802
  });
776
- const buildLink = utils.useStableCallback((state, dest) => {
803
+ const buildLink = utils.useStableCallback(dest => {
777
804
  // If this link simply reloads the current route,
778
805
  // make sure it has a new key so it will trigger a data refresh
779
806
 
@@ -788,7 +815,8 @@ function RouterProvider({
788
815
  disabled,
789
816
  target,
790
817
  replace,
791
- resetScroll
818
+ resetScroll,
819
+ startTransition
792
820
  } = dest;
793
821
  try {
794
822
  new URL(`${to}`);
@@ -823,7 +851,8 @@ function RouterProvider({
823
851
  commitLocation({
824
852
  ...next,
825
853
  replace,
826
- resetScroll
854
+ resetScroll,
855
+ startTransition
827
856
  });
828
857
  }
829
858
  };
@@ -881,15 +910,17 @@ function RouterProvider({
881
910
  React__namespace.useLayoutEffect(() => {
882
911
  const unsub = history$1.subscribe(() => {
883
912
  latestLocationRef.current = parseLocation(latestLocationRef.current);
884
- React__namespace.startTransition(() => {
885
- if (state.location !== latestLocationRef.current) {
886
- try {
887
- load();
888
- } catch (err) {
889
- console.error(err);
890
- }
913
+ setState(s => ({
914
+ ...s,
915
+ status: 'pending'
916
+ }));
917
+ if (state.location !== latestLocationRef.current) {
918
+ try {
919
+ load();
920
+ } catch (err) {
921
+ console.error(err);
891
922
  }
892
- });
923
+ }
893
924
  });
894
925
  const nextLocation = buildLocation({
895
926
  search: true,
@@ -910,14 +941,15 @@ function RouterProvider({
910
941
  const initialLoad = React__namespace.useRef(true);
911
942
  if (initialLoad.current) {
912
943
  initialLoad.current = false;
913
- try {
914
- load();
915
- } catch (err) {
916
- console.error(err);
917
- }
944
+ startReactTransition(() => {
945
+ try {
946
+ load();
947
+ } catch (err) {
948
+ console.error(err);
949
+ }
950
+ });
918
951
  }
919
- React__namespace.useMemo(() => [...state.matches, ...state.pendingMatches].some(d => d.isFetching), [state.matches, state.pendingMatches]);
920
- const matchRoute = utils.useStableCallback((state, location, opts) => {
952
+ const matchRoute = utils.useStableCallback((location, opts) => {
921
953
  location = {
922
954
  ...location,
923
955
  to: location.to ? resolvePathWithBase(location.from || '', location.to) : undefined
@@ -927,6 +959,9 @@ function RouterProvider({
927
959
  return false;
928
960
  }
929
961
  const baseLocation = opts?.pending ? latestLocationRef.current : state.resolvedLocation;
962
+
963
+ // const baseLocation = state.resolvedLocation
964
+
930
965
  if (!baseLocation) {
931
966
  return false;
932
967
  }
@@ -937,7 +972,7 @@ function RouterProvider({
937
972
  if (!match) {
938
973
  return false;
939
974
  }
940
- if (opts?.includeSearch ?? true) {
975
+ if (match && (opts?.includeSearch ?? true)) {
941
976
  return utils.partialDeepEqual(baseLocation.search, next.search) ? match : false;
942
977
  }
943
978
  return match;
@@ -951,20 +986,29 @@ function RouterProvider({
951
986
  routesById,
952
987
  options,
953
988
  history: history$1,
954
- load
989
+ load,
990
+ buildLocation
955
991
  };
956
992
  return /*#__PURE__*/React__namespace.createElement(routerContext.Provider, {
957
993
  value: routerContextValue
958
- }, /*#__PURE__*/React__namespace.createElement(react.Matches, null));
994
+ }, /*#__PURE__*/React__namespace.createElement(Matches.Matches, null));
959
995
  }
960
- function isCtrlEvent(e) {
961
- return !!(e.metaKey || e.altKey || e.ctrlKey || e.shiftKey);
962
- }
963
- class SearchParamError extends Error {}
964
- class PathParamError extends Error {}
965
996
  function getRouteMatch(state, id) {
966
997
  return [...state.pendingMatches, ...state.matches].find(d => d.id === id);
967
998
  }
999
+ function useRouterState(opts) {
1000
+ const {
1001
+ state
1002
+ } = useRouter();
1003
+ // return useStore(router.__store, opts?.select as any)
1004
+ return opts?.select ? opts.select(state) : state;
1005
+ }
1006
+ function useRouter() {
1007
+ const resolvedContext = window.__TSR_ROUTER_CONTEXT__ || routerContext;
1008
+ const value = React__namespace.useContext(resolvedContext);
1009
+ warning__default["default"](value, 'useRouter must be used inside a <RouterProvider> component!');
1010
+ return value;
1011
+ }
968
1012
 
969
1013
  exports.PathParamError = PathParamError;
970
1014
  exports.RouterProvider = RouterProvider;
@@ -972,4 +1016,6 @@ exports.SearchParamError = SearchParamError;
972
1016
  exports.getInitialRouterState = getInitialRouterState;
973
1017
  exports.getRouteMatch = getRouteMatch;
974
1018
  exports.routerContext = routerContext;
1019
+ exports.useRouter = useRouter;
1020
+ exports.useRouterState = useRouterState;
975
1021
  //# sourceMappingURL=RouterProvider.js.map