@tanstack/react-router 0.0.1-beta.76 → 0.0.1-beta.79

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.
@@ -7,7 +7,7 @@
7
7
  "name": "index.production.js",
8
8
  "children": [
9
9
  {
10
- "uid": "f796-109",
10
+ "uid": "9710-109",
11
11
  "name": "\u0000rollupPluginBabelHelpers.js"
12
12
  },
13
13
  {
@@ -15,11 +15,11 @@
15
15
  "children": [
16
16
  {
17
17
  "name": "tiny-invariant@1.3.1/node_modules/tiny-invariant/dist/esm/tiny-invariant.js",
18
- "uid": "f796-111"
18
+ "uid": "9710-111"
19
19
  },
20
20
  {
21
21
  "name": "tiny-warning@1.0.3/node_modules/tiny-warning/dist/tiny-warning.esm.js",
22
- "uid": "f796-113"
22
+ "uid": "9710-113"
23
23
  }
24
24
  ]
25
25
  },
@@ -28,19 +28,19 @@
28
28
  "children": [
29
29
  {
30
30
  "name": "store/build/esm/index.js",
31
- "uid": "f796-115"
31
+ "uid": "9710-115"
32
32
  },
33
33
  {
34
34
  "name": "router/build/esm/index.js",
35
- "uid": "f796-117"
35
+ "uid": "9710-117"
36
36
  },
37
37
  {
38
38
  "name": "react-store/build/esm/index.js",
39
- "uid": "f796-119"
39
+ "uid": "9710-119"
40
40
  },
41
41
  {
42
42
  "name": "react-router/src/index.tsx",
43
- "uid": "f796-121"
43
+ "uid": "9710-121"
44
44
  }
45
45
  ]
46
46
  }
@@ -50,176 +50,176 @@
50
50
  "isRoot": true
51
51
  },
52
52
  "nodeParts": {
53
- "f796-109": {
53
+ "9710-109": {
54
54
  "renderedLength": 429,
55
55
  "gzipLength": 238,
56
56
  "brotliLength": 0,
57
- "mainUid": "f796-108"
57
+ "mainUid": "9710-108"
58
58
  },
59
- "f796-111": {
59
+ "9710-111": {
60
60
  "renderedLength": 181,
61
61
  "gzipLength": 129,
62
62
  "brotliLength": 0,
63
- "mainUid": "f796-110"
63
+ "mainUid": "9710-110"
64
64
  },
65
- "f796-113": {
65
+ "9710-113": {
66
66
  "renderedLength": 44,
67
67
  "gzipLength": 62,
68
68
  "brotliLength": 0,
69
- "mainUid": "f796-112"
69
+ "mainUid": "9710-112"
70
70
  },
71
- "f796-115": {
71
+ "9710-115": {
72
72
  "renderedLength": 1288,
73
73
  "gzipLength": 497,
74
74
  "brotliLength": 0,
75
- "mainUid": "f796-114"
75
+ "mainUid": "9710-114"
76
76
  },
77
- "f796-117": {
78
- "renderedLength": 46874,
79
- "gzipLength": 11076,
77
+ "9710-117": {
78
+ "renderedLength": 49433,
79
+ "gzipLength": 11720,
80
80
  "brotliLength": 0,
81
- "mainUid": "f796-116"
81
+ "mainUid": "9710-116"
82
82
  },
83
- "f796-119": {
83
+ "9710-119": {
84
84
  "renderedLength": 1571,
85
85
  "gzipLength": 594,
86
86
  "brotliLength": 0,
87
- "mainUid": "f796-118"
87
+ "mainUid": "9710-118"
88
88
  },
89
- "f796-121": {
90
- "renderedLength": 12591,
91
- "gzipLength": 3262,
89
+ "9710-121": {
90
+ "renderedLength": 12277,
91
+ "gzipLength": 3110,
92
92
  "brotliLength": 0,
93
- "mainUid": "f796-120"
93
+ "mainUid": "9710-120"
94
94
  }
95
95
  },
96
96
  "nodeMetas": {
97
- "f796-108": {
97
+ "9710-108": {
98
98
  "id": "\u0000rollupPluginBabelHelpers.js",
99
99
  "moduleParts": {
100
- "index.production.js": "f796-109"
100
+ "index.production.js": "9710-109"
101
101
  },
102
102
  "imported": [],
103
103
  "importedBy": [
104
104
  {
105
- "uid": "f796-120"
105
+ "uid": "9710-120"
106
106
  }
107
107
  ]
108
108
  },
109
- "f796-110": {
109
+ "9710-110": {
110
110
  "id": "/node_modules/.pnpm/tiny-invariant@1.3.1/node_modules/tiny-invariant/dist/esm/tiny-invariant.js",
111
111
  "moduleParts": {
112
- "index.production.js": "f796-111"
112
+ "index.production.js": "9710-111"
113
113
  },
114
114
  "imported": [],
115
115
  "importedBy": [
116
116
  {
117
- "uid": "f796-116"
117
+ "uid": "9710-116"
118
118
  }
119
119
  ]
120
120
  },
121
- "f796-112": {
121
+ "9710-112": {
122
122
  "id": "/node_modules/.pnpm/tiny-warning@1.0.3/node_modules/tiny-warning/dist/tiny-warning.esm.js",
123
123
  "moduleParts": {
124
- "index.production.js": "f796-113"
124
+ "index.production.js": "9710-113"
125
125
  },
126
126
  "imported": [],
127
127
  "importedBy": [
128
128
  {
129
- "uid": "f796-116"
129
+ "uid": "9710-116"
130
130
  }
131
131
  ]
132
132
  },
133
- "f796-114": {
133
+ "9710-114": {
134
134
  "id": "/packages/store/build/esm/index.js",
135
135
  "moduleParts": {
136
- "index.production.js": "f796-115"
136
+ "index.production.js": "9710-115"
137
137
  },
138
138
  "imported": [],
139
139
  "importedBy": [
140
140
  {
141
- "uid": "f796-116"
141
+ "uid": "9710-116"
142
142
  }
143
143
  ]
144
144
  },
145
- "f796-116": {
145
+ "9710-116": {
146
146
  "id": "/packages/router/build/esm/index.js",
147
147
  "moduleParts": {
148
- "index.production.js": "f796-117"
148
+ "index.production.js": "9710-117"
149
149
  },
150
150
  "imported": [
151
151
  {
152
- "uid": "f796-110"
152
+ "uid": "9710-110"
153
153
  },
154
154
  {
155
- "uid": "f796-112"
155
+ "uid": "9710-112"
156
156
  },
157
157
  {
158
- "uid": "f796-114"
158
+ "uid": "9710-114"
159
159
  }
160
160
  ],
161
161
  "importedBy": [
162
162
  {
163
- "uid": "f796-120"
163
+ "uid": "9710-120"
164
164
  }
165
165
  ]
166
166
  },
167
- "f796-118": {
167
+ "9710-118": {
168
168
  "id": "/packages/react-store/build/esm/index.js",
169
169
  "moduleParts": {
170
- "index.production.js": "f796-119"
170
+ "index.production.js": "9710-119"
171
171
  },
172
172
  "imported": [
173
173
  {
174
- "uid": "f796-123"
174
+ "uid": "9710-123"
175
175
  }
176
176
  ],
177
177
  "importedBy": [
178
178
  {
179
- "uid": "f796-120"
179
+ "uid": "9710-120"
180
180
  }
181
181
  ]
182
182
  },
183
- "f796-120": {
183
+ "9710-120": {
184
184
  "id": "/packages/react-router/src/index.tsx",
185
185
  "moduleParts": {
186
- "index.production.js": "f796-121"
186
+ "index.production.js": "9710-121"
187
187
  },
188
188
  "imported": [
189
189
  {
190
- "uid": "f796-108"
190
+ "uid": "9710-108"
191
191
  },
192
192
  {
193
- "uid": "f796-122"
193
+ "uid": "9710-122"
194
194
  },
195
195
  {
196
- "uid": "f796-116"
196
+ "uid": "9710-116"
197
197
  },
198
198
  {
199
- "uid": "f796-118"
199
+ "uid": "9710-118"
200
200
  }
201
201
  ],
202
202
  "importedBy": [],
203
203
  "isEntry": true
204
204
  },
205
- "f796-122": {
205
+ "9710-122": {
206
206
  "id": "react",
207
207
  "moduleParts": {},
208
208
  "imported": [],
209
209
  "importedBy": [
210
210
  {
211
- "uid": "f796-120"
211
+ "uid": "9710-120"
212
212
  }
213
213
  ],
214
214
  "isExternal": true
215
215
  },
216
- "f796-123": {
216
+ "9710-123": {
217
217
  "id": "use-sync-external-store/shim/with-selector",
218
218
  "moduleParts": {},
219
219
  "imported": [],
220
220
  "importedBy": [
221
221
  {
222
- "uid": "f796-118"
222
+ "uid": "9710-118"
223
223
  }
224
224
  ],
225
225
  "isExternal": true
@@ -106,5 +106,7 @@ declare function Outlet(): JSX.Element | null;
106
106
  declare function ErrorComponent({ error }: {
107
107
  error: any;
108
108
  }): JSX.Element;
109
+ declare function useBlocker(message: string, when?: boolean | any): void;
110
+ declare function Block({ message, when, children }: PromptProps): any;
109
111
 
110
- export { ErrorComponent, Link, LinkFn, LinkPropsOptions, MakeLinkOptions, MakeLinkPropsOptions, MakeMatchRouteOptions, MakeUseMatchRouteOptions, MatchRoute, MatchesProviderProps, Navigate, Outlet, PromptProps, ReactRouter, RouteComponent, RouterProps, RouterProvider, SyncRouteComponent, lazy, matchesContext, routerContext, useLinkProps, useMatch, useMatchRoute, useMatches, useNavigate, useParams, useRoute, useRouter, useRouterContext, useSearch };
112
+ export { Block, ErrorComponent, Link, LinkFn, LinkPropsOptions, MakeLinkOptions, MakeLinkPropsOptions, MakeMatchRouteOptions, MakeUseMatchRouteOptions, MatchRoute, MatchesProviderProps, Navigate, Outlet, PromptProps, ReactRouter, RouteComponent, RouterProps, RouterProvider, SyncRouteComponent, lazy, matchesContext, routerContext, useBlocker, useLinkProps, useMatch, useMatchRoute, useMatches, useNavigate, useParams, useRoute, useRouter, useRouterContext, useSearch };
@@ -142,10 +142,40 @@
142
142
  // making assumptions about the way TanStack Router works
143
143
 
144
144
  const popStateEvent = 'popstate';
145
+ const beforeUnloadEvent = 'beforeunload';
146
+ const beforeUnloadListener = event => {
147
+ event.preventDefault();
148
+ // @ts-ignore
149
+ return event.returnValue = '';
150
+ };
151
+ const stopBlocking = () => {
152
+ removeEventListener(beforeUnloadEvent, beforeUnloadListener, {
153
+ capture: true
154
+ });
155
+ };
145
156
  function createHistory(opts) {
146
157
  let currentLocation = opts.getLocation();
147
158
  let unsub = () => {};
148
159
  let listeners = new Set();
160
+ let blockers = [];
161
+ let queue = [];
162
+ const tryFlush = () => {
163
+ if (blockers.length) {
164
+ blockers[0]?.(tryFlush, () => {
165
+ blockers = [];
166
+ stopBlocking();
167
+ });
168
+ return;
169
+ }
170
+ while (queue.length) {
171
+ queue.shift()?.();
172
+ }
173
+ onUpdate();
174
+ };
175
+ const queueTask = task => {
176
+ queue.push(task);
177
+ tryFlush();
178
+ };
149
179
  const onUpdate = () => {
150
180
  currentLocation = opts.getLocation();
151
181
  listeners.forEach(listener => listener());
@@ -167,26 +197,45 @@
167
197
  };
168
198
  },
169
199
  push: (path, state) => {
170
- opts.pushState(path, state);
171
- onUpdate();
200
+ queueTask(() => {
201
+ opts.pushState(path, state);
202
+ });
172
203
  },
173
204
  replace: (path, state) => {
174
- opts.replaceState(path, state);
175
- onUpdate();
205
+ queueTask(() => {
206
+ opts.replaceState(path, state);
207
+ });
176
208
  },
177
209
  go: index => {
178
- opts.go(index);
179
- onUpdate();
210
+ queueTask(() => {
211
+ opts.go(index);
212
+ });
180
213
  },
181
214
  back: () => {
182
- opts.back();
183
- onUpdate();
215
+ queueTask(() => {
216
+ opts.back();
217
+ });
184
218
  },
185
219
  forward: () => {
186
- opts.forward();
187
- onUpdate();
220
+ queueTask(() => {
221
+ opts.forward();
222
+ });
188
223
  },
189
- createHref: str => opts.createHref(str)
224
+ createHref: str => opts.createHref(str),
225
+ block: cb => {
226
+ blockers.push(cb);
227
+ if (blockers.length === 1) {
228
+ addEventListener(beforeUnloadEvent, beforeUnloadListener, {
229
+ capture: true
230
+ });
231
+ }
232
+ return () => {
233
+ blockers = blockers.filter(b => b !== cb);
234
+ if (!blockers.length) {
235
+ stopBlocking();
236
+ }
237
+ };
238
+ }
190
239
  };
191
240
  }
192
241
  function createBrowserHistory(opts) {
@@ -612,11 +661,11 @@
612
661
  this.router = opts.router;
613
662
  const allOptions = this.options;
614
663
  const isRoot = !allOptions?.path && !allOptions?.id;
615
- const parent = this.options?.getParentRoute?.();
664
+ this.parentRoute = this.options?.getParentRoute?.();
616
665
  if (isRoot) {
617
666
  this.path = rootRouteId;
618
667
  } else {
619
- invariant(parent, `Child Route instances must pass a 'getParentRoute: () => ParentRoute' option that returns a Route instance.`);
668
+ invariant(this.parentRoute, `Child Route instances must pass a 'getParentRoute: () => ParentRoute' option that returns a Route instance.`);
620
669
  }
621
670
  let path = isRoot ? rootRouteId : allOptions.path;
622
671
 
@@ -627,14 +676,14 @@
627
676
  const customId = allOptions?.id || path;
628
677
 
629
678
  // Strip the parentId prefix from the first level of children
630
- let id = isRoot ? rootRouteId : joinPaths([parent.id === rootRouteId ? '' : parent.id, customId]);
679
+ let id = isRoot ? rootRouteId : joinPaths([this.parentRoute.id === rootRouteId ? '' : this.parentRoute.id, customId]);
631
680
  if (path === rootRouteId) {
632
681
  path = '/';
633
682
  }
634
683
  if (id !== rootRouteId) {
635
684
  id = joinPaths(['/', id]);
636
685
  }
637
- const fullPath = id === rootRouteId ? '/' : trimPathRight(joinPaths([parent.fullPath, path]));
686
+ const fullPath = id === rootRouteId ? '/' : trimPathRight(joinPaths([this.parentRoute.fullPath, path]));
638
687
  this.path = path;
639
688
  this.id = id;
640
689
  // this.customId = customId as TCustomId
@@ -644,10 +693,34 @@
644
693
  this.children = children;
645
694
  return this;
646
695
  };
647
- generate = options => {
648
- invariant(false, `route.generate() is used by TanStack Router's file-based routing code generation and should not actually be called during runtime. `);
649
- };
696
+
697
+ // generate = (
698
+ // options: Omit<
699
+ // RouteOptions<
700
+ // TParentRoute,
701
+ // TCustomId,
702
+ // TPath,
703
+ // InferFullSearchSchema<TParentRoute>,
704
+ // TSearchSchema,
705
+ // TFullSearchSchema,
706
+ // TParentRoute['__types']['allParams'],
707
+ // TParams,
708
+ // TAllParams,
709
+ // TParentContext,
710
+ // TAllParentContext,
711
+ // TRouteContext,
712
+ // TContext
713
+ // >,
714
+ // 'path'
715
+ // >,
716
+ // ) => {
717
+ // invariant(
718
+ // false,
719
+ // `route.generate() is used by TanStack Router's file-based routing code generation and should not actually be called during runtime. `,
720
+ // )
721
+ // }
650
722
  }
723
+
651
724
  class RootRoute extends Route {
652
725
  constructor(options) {
653
726
  super(options);
@@ -1291,19 +1364,14 @@
1291
1364
  });
1292
1365
  };
1293
1366
  #buildRouteTree = routeTree => {
1294
- const recurseRoutes = routes => {
1367
+ const recurseRoutes = (routes, parentRoute) => {
1295
1368
  routes.forEach((route, i) => {
1296
1369
  route.init({
1297
1370
  originalIndex: i,
1298
1371
  router: this
1299
1372
  });
1300
1373
  const existingRoute = this.routesById[route.id];
1301
- if (existingRoute) {
1302
- {
1303
- console.warn(`Duplicate routes found with id: ${String(route.id)}`, this.routesById, route);
1304
- }
1305
- throw new Error();
1306
- }
1374
+ invariant(!existingRoute, `Duplicate routes found with id: ${String(route.id)}`);
1307
1375
  this.routesById[route.id] = route;
1308
1376
  const children = route.children;
1309
1377
  if (children?.length) {
@@ -1343,6 +1411,19 @@
1343
1411
  });
1344
1412
  };
1345
1413
  recurseRoutes([routeTree]);
1414
+ const recurceCheckRoutes = (routes, parentRoute) => {
1415
+ routes.forEach(route => {
1416
+ if (route.isRoot) {
1417
+ invariant(!parentRoute, 'Root routes can only be used as the root of a route tree.');
1418
+ } else {
1419
+ invariant(parentRoute ? route.parentRoute === parentRoute : true, `Expected a route with path "${route.path}" to be passed to its parent route "${route.parentRoute?.id}" in an addChildren() call, but was instead passed as a child of the "${parentRoute?.id}" route.`);
1420
+ }
1421
+ if (route.children) {
1422
+ recurceCheckRoutes(route.children, route);
1423
+ }
1424
+ });
1425
+ };
1426
+ recurceCheckRoutes([routeTree], undefined);
1346
1427
  return routeTree;
1347
1428
  };
1348
1429
  #parseLocation = previousLocation => {
@@ -2104,34 +2185,31 @@
2104
2185
  }
2105
2186
  }, error.message ? /*#__PURE__*/React__namespace.createElement("code", null, error.message) : null)));
2106
2187
  }
2188
+ function useBlocker(message, when = true) {
2189
+ const router = useRouter();
2190
+ React__namespace.useEffect(() => {
2191
+ if (!when) return;
2192
+ let unblock = router.history.block((retry, cancel) => {
2193
+ if (window.confirm(message)) {
2194
+ unblock();
2195
+ retry();
2196
+ } else {
2197
+ cancel();
2198
+ }
2199
+ });
2200
+ return unblock;
2201
+ });
2202
+ }
2203
+ function Block({
2204
+ message,
2205
+ when,
2206
+ children
2207
+ }) {
2208
+ useBlocker(message, when);
2209
+ return children ?? null;
2210
+ }
2107
2211
 
2108
- // TODO: While we migrate away from the history package, these need to be disabled
2109
- // export function usePrompt(message: string, when: boolean | any): void {
2110
- // const router = useRouter()
2111
-
2112
- // React.useEffect(() => {
2113
- // if (!when) return
2114
-
2115
- // let unblock = router.getHistory().block((transition) => {
2116
- // if (window.confirm(message)) {
2117
- // unblock()
2118
- // transition.retry()
2119
- // } else {
2120
- // router.setStore((s) => {
2121
- // s.currentLocation.pathname = window.location.pathname
2122
- // })
2123
- // }
2124
- // })
2125
-
2126
- // return unblock
2127
- // }, [when, message])
2128
- // }
2129
-
2130
- // export function Prompt({ message, when, children }: PromptProps) {
2131
- // usePrompt(message, when ?? true)
2132
- // return (children ?? null) as ReactNode
2133
- // }
2134
-
2212
+ exports.Block = Block;
2135
2213
  exports.ErrorComponent = ErrorComponent;
2136
2214
  exports.Link = Link;
2137
2215
  exports.MatchRoute = MatchRoute;
@@ -2176,6 +2254,7 @@
2176
2254
  exports.trimPath = trimPath;
2177
2255
  exports.trimPathLeft = trimPathLeft;
2178
2256
  exports.trimPathRight = trimPathRight;
2257
+ exports.useBlocker = useBlocker;
2179
2258
  exports.useLinkProps = useLinkProps;
2180
2259
  exports.useMatch = useMatch;
2181
2260
  exports.useMatchRoute = useMatchRoute;