@tanstack/react-router 0.0.1-beta.211 → 0.0.1-beta.213

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/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@tanstack/react-router",
3
3
  "author": "Tanner Linsley",
4
- "version": "0.0.1-beta.211",
4
+ "version": "0.0.1-beta.213",
5
5
  "license": "MIT",
6
6
  "repository": "tanstack/router",
7
7
  "homepage": "https://tanstack.com/router",
@@ -42,7 +42,7 @@
42
42
  "@babel/runtime": "^7.16.7",
43
43
  "tiny-invariant": "^1.3.1",
44
44
  "tiny-warning": "^1.0.3",
45
- "@tanstack/history": "0.0.1-beta.211"
45
+ "@tanstack/history": "0.0.1-beta.213"
46
46
  },
47
47
  "scripts": {
48
48
  "build": "rollup --config rollup.config.js"
@@ -127,6 +127,8 @@ export type RouterContext<
127
127
  history: RouterHistory
128
128
  load: LoadFn
129
129
  buildLocation: BuildLocationFn<TRouteTree>
130
+ subscribe: Router<TRouteTree>['subscribe']
131
+ resetNextScrollRef: React.MutableRefObject<boolean>
130
132
  }
131
133
 
132
134
  export const routerContext = React.createContext<RouterContext<any>>(null!)
@@ -150,8 +152,8 @@ export function getInitialRouterState(
150
152
  ): RouterState<any> {
151
153
  return {
152
154
  status: 'idle',
153
- resolvedLocation: undefined,
154
- location: location!,
155
+ resolvedLocation: location,
156
+ location,
155
157
  matches: [],
156
158
  pendingMatches: [],
157
159
  lastUpdated: Date.now(),
@@ -181,10 +183,8 @@ export function RouterProvider<
181
183
  const tempLocationKeyRef = React.useRef<string | undefined>(
182
184
  `${Math.round(Math.random() * 10000000)}`,
183
185
  )
184
- const resetNextScrollRef = React.useRef<boolean>(false)
185
-
186
+ const resetNextScrollRef = React.useRef<boolean>(true)
186
187
  const navigateTimeoutRef = React.useRef<NodeJS.Timeout | null>(null)
187
-
188
188
  const latestLoadPromiseRef = React.useRef<Promise<void>>(Promise.resolve())
189
189
 
190
190
  const checkLatest = (promise: Promise<void>): undefined | Promise<void> => {
@@ -245,8 +245,9 @@ export function RouterProvider<
245
245
  },
246
246
  )
247
247
 
248
+ const latestLocationRef = React.useRef<ParsedLocation>(parseLocation())
248
249
  const [preState, setState] = React.useState<RouterState<TRouteTree>>(() =>
249
- getInitialRouterState(parseLocation()),
250
+ getInitialRouterState(latestLocationRef.current),
250
251
  )
251
252
  const [isTransitioning, startReactTransition] = React.useTransition()
252
253
 
@@ -254,12 +255,19 @@ export function RouterProvider<
254
255
  () => ({
255
256
  ...preState,
256
257
  status: isTransitioning ? 'pending' : 'idle',
258
+ location: isTransitioning ? latestLocationRef.current : preState.location,
257
259
  }),
258
260
  [preState, isTransitioning],
259
261
  )
260
262
 
261
263
  React.useLayoutEffect(() => {
262
264
  if (!isTransitioning && state.resolvedLocation !== state.location) {
265
+ router.emit({
266
+ type: 'onResolved',
267
+ fromLocation: state.resolvedLocation,
268
+ toLocation: state.location,
269
+ pathChanged: state.location!.href !== state.resolvedLocation?.href,
270
+ })
263
271
  setState((s) => ({
264
272
  ...s,
265
273
  resolvedLocation: s.location,
@@ -1040,8 +1048,8 @@ export function RouterProvider<
1040
1048
  const load = useStableCallback<LoadFn>(async () => {
1041
1049
  const promise = new Promise<void>(async (resolve, reject) => {
1042
1050
  const next = latestLocationRef.current
1043
- const prevLocation = state.resolvedLocation || state.location
1044
- const pathDidChange = !!(next && prevLocation!.href !== next.href)
1051
+ const prevLocation = state.resolvedLocation
1052
+ const pathDidChange = prevLocation!.href !== next.href
1045
1053
  let latestPromise: Promise<void> | undefined | null
1046
1054
 
1047
1055
  // Cancel any pending matches
@@ -1049,8 +1057,8 @@ export function RouterProvider<
1049
1057
 
1050
1058
  router.emit({
1051
1059
  type: 'onBeforeLoad',
1052
- from: prevLocation,
1053
- to: next ?? state.location,
1060
+ fromLocation: prevLocation,
1061
+ toLocation: next,
1054
1062
  pathChanged: pathDidChange,
1055
1063
  })
1056
1064
 
@@ -1063,9 +1071,13 @@ export function RouterProvider<
1063
1071
  },
1064
1072
  )
1065
1073
 
1074
+ const previousMatches = state.matches
1075
+
1066
1076
  // Ingest the new matches
1067
1077
  setState((s) => ({
1068
1078
  ...s,
1079
+ status: 'pending',
1080
+ location: next,
1069
1081
  matches,
1070
1082
  }))
1071
1083
 
@@ -1086,16 +1098,15 @@ export function RouterProvider<
1086
1098
  return latestPromise
1087
1099
  }
1088
1100
 
1089
- // TODO:
1090
- // const exitingMatchIds = previousMatches.filter(
1091
- // (id) => !state.pendingMatches.includes(id),
1092
- // )
1093
- // const enteringMatchIds = state.pendingMatches.filter(
1094
- // (id) => !previousMatches.includes(id),
1095
- // )
1096
- // const stayingMatchIds = previousMatches.filter((id) =>
1097
- // state.pendingMatches.includes(id),
1098
- // )
1101
+ const exitingMatchIds = previousMatches.filter(
1102
+ (id) => !state.pendingMatches.includes(id),
1103
+ )
1104
+ const enteringMatchIds = state.pendingMatches.filter(
1105
+ (id) => !previousMatches.includes(id),
1106
+ )
1107
+ const stayingMatchIds = previousMatches.filter((id) =>
1108
+ state.pendingMatches.includes(id),
1109
+ )
1099
1110
 
1100
1111
  // setState((s) => ({
1101
1112
  // ...s,
@@ -1103,23 +1114,23 @@ export function RouterProvider<
1103
1114
  // resolvedLocation: s.location,
1104
1115
  // }))
1105
1116
 
1106
- // TODO:
1107
- // ;(
1108
- // [
1109
- // [exitingMatchIds, 'onLeave'],
1110
- // [enteringMatchIds, 'onEnter'],
1111
- // [stayingMatchIds, 'onTransition'],
1112
- // ] as const
1113
- // ).forEach(([matches, hook]) => {
1114
- // matches.forEach((match) => {
1115
- // const route = this.getRoute(match.routeId)
1116
- // route.options[hook]?.(match)
1117
- // })
1118
- // })
1117
+ //
1118
+ ;(
1119
+ [
1120
+ [exitingMatchIds, 'onLeave'],
1121
+ [enteringMatchIds, 'onEnter'],
1122
+ [stayingMatchIds, 'onTransition'],
1123
+ ] as const
1124
+ ).forEach(([matches, hook]) => {
1125
+ matches.forEach((match) => {
1126
+ looseRoutesById[match.routeId]!.options[hook]?.(match)
1127
+ })
1128
+ })
1129
+
1119
1130
  router.emit({
1120
1131
  type: 'onLoad',
1121
- from: prevLocation,
1122
- to: next,
1132
+ fromLocation: prevLocation,
1133
+ toLocation: next,
1123
1134
  pathChanged: pathDidChange,
1124
1135
  })
1125
1136
 
@@ -1284,22 +1295,18 @@ export function RouterProvider<
1284
1295
  }
1285
1296
  })
1286
1297
 
1287
- const latestLocationRef = React.useRef(state.location)
1288
-
1289
1298
  React.useLayoutEffect(() => {
1290
1299
  const unsub = history.subscribe(() => {
1291
1300
  latestLocationRef.current = parseLocation(latestLocationRef.current)
1292
1301
 
1293
- setState((s) => ({
1294
- ...s,
1295
- status: 'pending',
1296
- }))
1297
1302
  if (state.location !== latestLocationRef.current) {
1298
- try {
1299
- load()
1300
- } catch (err) {
1301
- console.error(err)
1302
- }
1303
+ startReactTransition(() => {
1304
+ try {
1305
+ load()
1306
+ } catch (err) {
1307
+ console.error(err)
1308
+ }
1309
+ })
1303
1310
  }
1304
1311
  })
1305
1312
 
@@ -1387,6 +1394,8 @@ export function RouterProvider<
1387
1394
  history,
1388
1395
  load,
1389
1396
  buildLocation,
1397
+ subscribe: router.subscribe,
1398
+ resetNextScrollRef,
1390
1399
  }
1391
1400
 
1392
1401
  return (
package/src/index.tsx CHANGED
@@ -20,7 +20,7 @@ export * from './route'
20
20
  export * from './routeInfo'
21
21
  export * from './router'
22
22
  export * from './RouterProvider'
23
- // export * from './scroll-restoration'
23
+ export * from './scroll-restoration'
24
24
  export * from './searchParams'
25
25
  export * from './useBlocker'
26
26
  export * from './useNavigate'
package/src/link.tsx CHANGED
@@ -385,6 +385,7 @@ export function useLinkProps<
385
385
  preloadDelay,
386
386
  replace,
387
387
  startTransition,
388
+ resetScroll,
388
389
  // element props
389
390
  style,
390
391
  className,
package/src/router.ts CHANGED
@@ -97,7 +97,7 @@ export interface RouterState<TRouteTree extends AnyRoute = AnyRoute> {
97
97
  matches: RouteMatch<TRouteTree>[]
98
98
  pendingMatches: RouteMatch<TRouteTree>[]
99
99
  location: ParsedLocation<FullSearchSchema<TRouteTree>>
100
- resolvedLocation: undefined | ParsedLocation<FullSearchSchema<TRouteTree>>
100
+ resolvedLocation: ParsedLocation<FullSearchSchema<TRouteTree>>
101
101
  lastUpdated: number
102
102
  }
103
103
 
@@ -148,14 +148,20 @@ export const componentTypes = [
148
148
  export type RouterEvents = {
149
149
  onBeforeLoad: {
150
150
  type: 'onBeforeLoad'
151
- from: undefined | ParsedLocation
152
- to: ParsedLocation
151
+ fromLocation: ParsedLocation
152
+ toLocation: ParsedLocation
153
153
  pathChanged: boolean
154
154
  }
155
155
  onLoad: {
156
156
  type: 'onLoad'
157
- from: undefined | ParsedLocation
158
- to: ParsedLocation
157
+ fromLocation: ParsedLocation
158
+ toLocation: ParsedLocation
159
+ pathChanged: boolean
160
+ }
161
+ onResolved: {
162
+ type: 'onResolved'
163
+ fromLocation: ParsedLocation
164
+ toLocation: ParsedLocation
159
165
  pathChanged: boolean
160
166
  }
161
167
  }