@tanstack/react-router 0.0.1-beta.230 → 0.0.1-beta.231
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/Matches.js +4 -1
- package/build/cjs/Matches.js.map +1 -1
- package/build/cjs/RouterProvider.js +2 -1
- package/build/cjs/RouterProvider.js.map +1 -1
- package/build/cjs/awaited.js +1 -1
- package/build/cjs/awaited.js.map +1 -1
- package/build/cjs/index.js +1 -0
- package/build/cjs/index.js.map +1 -1
- package/build/cjs/router.js +3 -1
- package/build/cjs/router.js.map +1 -1
- package/build/cjs/scroll-restoration.js +52 -29
- package/build/cjs/scroll-restoration.js.map +1 -1
- package/build/esm/index.js +62 -34
- package/build/esm/index.js.map +1 -1
- package/build/stats-html.html +1 -1
- package/build/stats-react.json +289 -289
- package/build/types/scroll-restoration.d.ts +12 -0
- package/build/umd/index.development.js +62 -33
- 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/Matches.tsx +3 -1
- package/src/RouterProvider.tsx +3 -1
- package/src/awaited.tsx +1 -1
- package/src/router.ts +5 -1
- package/src/scroll-restoration.tsx +81 -43
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.231",
|
|
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.231"
|
|
46
46
|
},
|
|
47
47
|
"scripts": {
|
|
48
48
|
"build": "rollup --config rollup.config.js"
|
package/src/Matches.tsx
CHANGED
|
@@ -94,7 +94,9 @@ export function Match({ matches }: { matches: RouteMatch[] }) {
|
|
|
94
94
|
const match = matches[0]!
|
|
95
95
|
const routeId = match?.routeId
|
|
96
96
|
const route = routesById[routeId]!
|
|
97
|
-
const
|
|
97
|
+
const router = useRouter()
|
|
98
|
+
const locationKey = router.latestLocation.state?.key
|
|
99
|
+
// const locationKey = useRouterState().location.state?.key
|
|
98
100
|
|
|
99
101
|
const PendingComponent = (route.options.pendingComponent ??
|
|
100
102
|
options.defaultPendingComponent) as any
|
package/src/RouterProvider.tsx
CHANGED
|
@@ -109,11 +109,13 @@ export function RouterProvider<
|
|
|
109
109
|
|
|
110
110
|
const [preState, setState] = React.useState(() => router.state)
|
|
111
111
|
const [isTransitioning, startReactTransition] = React.useTransition()
|
|
112
|
+
const isAnyTransitioning =
|
|
113
|
+
isTransitioning || preState.matches.some((d) => d.status === 'pending')
|
|
112
114
|
|
|
113
115
|
const state = React.useMemo<RouterState<TRouteTree>>(
|
|
114
116
|
() => ({
|
|
115
117
|
...preState,
|
|
116
|
-
status:
|
|
118
|
+
status: isAnyTransitioning ? 'pending' : 'idle',
|
|
117
119
|
location: isTransitioning ? router.latestLocation : preState.location,
|
|
118
120
|
pendingMatches: router.pendingMatches,
|
|
119
121
|
}),
|
package/src/awaited.tsx
CHANGED
package/src/router.ts
CHANGED
|
@@ -1168,6 +1168,8 @@ export class Router<
|
|
|
1168
1168
|
// forcefully show the pending component
|
|
1169
1169
|
if (pendingPromise) {
|
|
1170
1170
|
pendingPromise.then(() => {
|
|
1171
|
+
if ((latestPromise = checkLatest())) return
|
|
1172
|
+
|
|
1171
1173
|
didShowPending = true
|
|
1172
1174
|
matches[index] = match = {
|
|
1173
1175
|
...match,
|
|
@@ -1195,6 +1197,8 @@ export class Router<
|
|
|
1195
1197
|
await new Promise((r) => setTimeout(r, pendingMinMs))
|
|
1196
1198
|
}
|
|
1197
1199
|
|
|
1200
|
+
if ((latestPromise = checkLatest())) return await latestPromise
|
|
1201
|
+
|
|
1198
1202
|
matches[index] = match = {
|
|
1199
1203
|
...match,
|
|
1200
1204
|
error: undefined,
|
|
@@ -1279,7 +1283,7 @@ export class Router<
|
|
|
1279
1283
|
// Ingest the new matches
|
|
1280
1284
|
this.setState((s) => ({
|
|
1281
1285
|
...s,
|
|
1282
|
-
status: 'pending',
|
|
1286
|
+
// status: 'pending',
|
|
1283
1287
|
location: next,
|
|
1284
1288
|
matches,
|
|
1285
1289
|
}))
|
|
@@ -23,10 +23,26 @@ type Cache = {
|
|
|
23
23
|
set: (updater: NonNullableUpdater<CacheState>) => void
|
|
24
24
|
}
|
|
25
25
|
|
|
26
|
-
let cache: Cache
|
|
27
|
-
|
|
28
26
|
const sessionsStorage = typeof window !== 'undefined' && window.sessionStorage
|
|
29
27
|
|
|
28
|
+
let cache: Cache = sessionsStorage
|
|
29
|
+
? (() => {
|
|
30
|
+
const storageKey = 'tsr-scroll-restoration-v2'
|
|
31
|
+
|
|
32
|
+
const state: CacheState = JSON.parse(
|
|
33
|
+
window.sessionStorage.getItem(storageKey) || 'null',
|
|
34
|
+
) || { cached: {}, next: {} }
|
|
35
|
+
|
|
36
|
+
return {
|
|
37
|
+
state,
|
|
38
|
+
set: (updater) => {
|
|
39
|
+
cache.state = functionalUpdate(updater, cache.state)
|
|
40
|
+
window.sessionStorage.setItem(storageKey, JSON.stringify(cache.state))
|
|
41
|
+
},
|
|
42
|
+
}
|
|
43
|
+
})()
|
|
44
|
+
: (undefined as any)
|
|
45
|
+
|
|
30
46
|
export type ScrollRestorationOptions = {
|
|
31
47
|
getKey?: (location: ParsedLocation) => string
|
|
32
48
|
}
|
|
@@ -39,29 +55,6 @@ export function useScrollRestoration(options?: ScrollRestorationOptions) {
|
|
|
39
55
|
useLayoutEffect(() => {
|
|
40
56
|
const getKey = options?.getKey || defaultGetKey
|
|
41
57
|
|
|
42
|
-
if (sessionsStorage) {
|
|
43
|
-
if (!cache) {
|
|
44
|
-
cache = (() => {
|
|
45
|
-
const storageKey = 'tsr-scroll-restoration-v2'
|
|
46
|
-
|
|
47
|
-
const state: CacheState = JSON.parse(
|
|
48
|
-
window.sessionStorage.getItem(storageKey) || 'null',
|
|
49
|
-
) || { cached: {}, next: {} }
|
|
50
|
-
|
|
51
|
-
return {
|
|
52
|
-
state,
|
|
53
|
-
set: (updater) => {
|
|
54
|
-
cache.state = functionalUpdate(updater, cache.state)
|
|
55
|
-
window.sessionStorage.setItem(
|
|
56
|
-
storageKey,
|
|
57
|
-
JSON.stringify(cache.state),
|
|
58
|
-
)
|
|
59
|
-
},
|
|
60
|
-
}
|
|
61
|
-
})()
|
|
62
|
-
}
|
|
63
|
-
}
|
|
64
|
-
|
|
65
58
|
const { history } = window
|
|
66
59
|
if (history.scrollRestoration) {
|
|
67
60
|
history.scrollRestoration = 'manual'
|
|
@@ -71,10 +64,21 @@ export function useScrollRestoration(options?: ScrollRestorationOptions) {
|
|
|
71
64
|
if (weakScrolledElements.has(event.target)) return
|
|
72
65
|
weakScrolledElements.add(event.target)
|
|
73
66
|
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
67
|
+
let elementSelector = ''
|
|
68
|
+
|
|
69
|
+
if (event.target === document || event.target === window) {
|
|
70
|
+
elementSelector = windowKey
|
|
71
|
+
} else {
|
|
72
|
+
const attrId = (event.target as Element).getAttribute(
|
|
73
|
+
'data-scroll-restoration-id',
|
|
74
|
+
)
|
|
75
|
+
|
|
76
|
+
if (attrId) {
|
|
77
|
+
elementSelector = `[data-scroll-restoration-id="${attrId}"]`
|
|
78
|
+
} else {
|
|
79
|
+
elementSelector = getCssSelector(event.target)
|
|
80
|
+
}
|
|
81
|
+
}
|
|
78
82
|
|
|
79
83
|
if (!cache.state.next[elementSelector]) {
|
|
80
84
|
cache.set((c) => ({
|
|
@@ -90,20 +94,6 @@ export function useScrollRestoration(options?: ScrollRestorationOptions) {
|
|
|
90
94
|
}
|
|
91
95
|
}
|
|
92
96
|
|
|
93
|
-
const getCssSelector = (el: any): string => {
|
|
94
|
-
let path = [],
|
|
95
|
-
parent
|
|
96
|
-
while ((parent = el.parentNode)) {
|
|
97
|
-
path.unshift(
|
|
98
|
-
`${el.tagName}:nth-child(${
|
|
99
|
-
([].indexOf as any).call(parent.children, el) + 1
|
|
100
|
-
})`,
|
|
101
|
-
)
|
|
102
|
-
el = parent
|
|
103
|
-
}
|
|
104
|
-
return `${path.join(' > ')}`.toLowerCase()
|
|
105
|
-
}
|
|
106
|
-
|
|
107
97
|
if (typeof document !== 'undefined') {
|
|
108
98
|
document.addEventListener('scroll', onScroll, true)
|
|
109
99
|
}
|
|
@@ -190,3 +180,51 @@ export function ScrollRestoration(props: ScrollRestorationOptions) {
|
|
|
190
180
|
useScrollRestoration(props)
|
|
191
181
|
return null
|
|
192
182
|
}
|
|
183
|
+
|
|
184
|
+
export function useElementScrollRestoration(
|
|
185
|
+
options: (
|
|
186
|
+
| {
|
|
187
|
+
id: string
|
|
188
|
+
getElement?: () => Element | undefined | null
|
|
189
|
+
}
|
|
190
|
+
| {
|
|
191
|
+
id?: string
|
|
192
|
+
getElement: () => Element | undefined | null
|
|
193
|
+
}
|
|
194
|
+
) & {
|
|
195
|
+
getKey?: (location: ParsedLocation) => string
|
|
196
|
+
},
|
|
197
|
+
) {
|
|
198
|
+
const router = useRouter()
|
|
199
|
+
const getKey = options?.getKey || defaultGetKey
|
|
200
|
+
|
|
201
|
+
let elementSelector = ''
|
|
202
|
+
|
|
203
|
+
if (options.id) {
|
|
204
|
+
elementSelector = `[data-scroll-restoration-id="${options.id}"]`
|
|
205
|
+
} else {
|
|
206
|
+
const element = options.getElement?.()
|
|
207
|
+
if (!element) {
|
|
208
|
+
return
|
|
209
|
+
}
|
|
210
|
+
elementSelector = getCssSelector(element)
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
const restoreKey = getKey(router.latestLocation)
|
|
214
|
+
const cacheKey = [restoreKey, elementSelector].join(delimiter)
|
|
215
|
+
return cache.state.cached[cacheKey]
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
function getCssSelector(el: any): string {
|
|
219
|
+
let path = [],
|
|
220
|
+
parent
|
|
221
|
+
while ((parent = el.parentNode)) {
|
|
222
|
+
path.unshift(
|
|
223
|
+
`${el.tagName}:nth-child(${
|
|
224
|
+
([].indexOf as any).call(parent.children, el) + 1
|
|
225
|
+
})`,
|
|
226
|
+
)
|
|
227
|
+
el = parent
|
|
228
|
+
}
|
|
229
|
+
return `${path.join(' > ')}`.toLowerCase()
|
|
230
|
+
}
|