@tanstack/react-router 0.0.1-beta.210 → 0.0.1-beta.212
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/build/cjs/RouterProvider.js +42 -47
- package/build/cjs/RouterProvider.js.map +1 -1
- package/build/cjs/index.js +3 -0
- package/build/cjs/index.js.map +1 -1
- package/build/cjs/link.js +1 -0
- package/build/cjs/link.js.map +1 -1
- package/build/cjs/router.js.map +1 -1
- package/build/cjs/scroll-restoration.js +186 -0
- package/build/cjs/scroll-restoration.js.map +1 -0
- package/build/esm/index.js +191 -50
- package/build/esm/index.js.map +1 -1
- package/build/stats-html.html +1 -1
- package/build/stats-react.json +300 -256
- package/build/types/RouterProvider.d.ts +2 -0
- package/build/types/index.d.ts +1 -0
- package/build/types/router.d.ts +11 -5
- package/build/types/scroll-restoration.d.ts +6 -0
- package/build/umd/index.development.js +197 -52
- package/build/umd/index.development.js.map +1 -1
- package/build/umd/index.production.js +1 -1
- package/build/umd/index.production.js.map +1 -1
- package/package.json +2 -2
- package/src/RouterProvider.tsx +56 -47
- package/src/index.tsx +1 -1
- package/src/link.tsx +1 -0
- package/src/router.ts +11 -5
- package/src/scroll-restoration.tsx +192 -205
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.
|
|
4
|
+
"version": "0.0.1-beta.212",
|
|
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.
|
|
45
|
+
"@tanstack/history": "0.0.1-beta.212"
|
|
46
46
|
},
|
|
47
47
|
"scripts": {
|
|
48
48
|
"build": "rollup --config rollup.config.js"
|
package/src/RouterProvider.tsx
CHANGED
|
@@ -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:
|
|
154
|
-
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>(
|
|
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(
|
|
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,
|
|
@@ -892,7 +900,7 @@ export function RouterProvider<
|
|
|
892
900
|
params: match.params,
|
|
893
901
|
preload: !!preload,
|
|
894
902
|
context: parentContext,
|
|
895
|
-
location: state.location,
|
|
903
|
+
location: state.location,
|
|
896
904
|
navigate: (opts) =>
|
|
897
905
|
navigate({ ...opts, from: match.pathname } as any),
|
|
898
906
|
buildLocation,
|
|
@@ -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
|
|
1044
|
-
const pathDidChange =
|
|
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
|
-
|
|
1053
|
-
|
|
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
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
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
|
-
//
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
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
|
-
|
|
1122
|
-
|
|
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
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
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
|
-
|
|
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
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:
|
|
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
|
-
|
|
152
|
-
|
|
151
|
+
fromLocation: ParsedLocation
|
|
152
|
+
toLocation: ParsedLocation
|
|
153
153
|
pathChanged: boolean
|
|
154
154
|
}
|
|
155
155
|
onLoad: {
|
|
156
156
|
type: 'onLoad'
|
|
157
|
-
|
|
158
|
-
|
|
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
|
}
|