@tanstack/router-core 1.131.26 → 1.131.28

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.
@@ -28,7 +28,7 @@ function getSafeSessionStorage() {
28
28
  return window.sessionStorage
29
29
  }
30
30
  } catch {
31
- return undefined
31
+ // silent
32
32
  }
33
33
  return undefined
34
34
  }
@@ -85,14 +85,14 @@ export const defaultGetScrollRestorationKey = (location: ParsedLocation) => {
85
85
 
86
86
  export function getCssSelector(el: any): string {
87
87
  const path = []
88
- let parent
88
+ let parent: HTMLElement
89
89
  while ((parent = el.parentNode)) {
90
- path.unshift(
91
- `${el.tagName}:nth-child(${([].indexOf as any).call(parent.children, el) + 1})`,
90
+ path.push(
91
+ `${el.tagName}:nth-child(${Array.prototype.indexOf.call(parent.children, el) + 1})`,
92
92
  )
93
93
  el = parent
94
94
  }
95
- return `${path.join(' > ')}`.toLowerCase()
95
+ return `${path.reverse().join(' > ')}`.toLowerCase()
96
96
  }
97
97
 
98
98
  let ignoreScroll = false
@@ -120,7 +120,7 @@ export function restoreScroll({
120
120
 
121
121
  try {
122
122
  byKey = JSON.parse(sessionStorage.getItem(storageKey) || '{}')
123
- } catch (error: any) {
123
+ } catch (error) {
124
124
  console.error(error)
125
125
  return
126
126
  }
@@ -132,7 +132,7 @@ export function restoreScroll({
132
132
  ignoreScroll = true
133
133
 
134
134
  //
135
- ;(() => {
135
+ scroll: {
136
136
  // If we have a cached entry for this location state,
137
137
  // we always need to prefer that over the hash scroll.
138
138
  if (
@@ -157,18 +157,18 @@ export function restoreScroll({
157
157
  }
158
158
  }
159
159
 
160
- return
160
+ break scroll
161
161
  }
162
162
 
163
163
  // If we don't have a cached entry for the hash,
164
164
  // Which means we've never seen this location before,
165
165
  // we need to check if there is a hash in the URL.
166
166
  // If there is, we need to scroll it's ID into view.
167
- const hash = (location ?? window.location).hash.split('#')[1]
167
+ const hash = (location ?? window.location).hash.split('#', 2)[1]
168
168
 
169
169
  if (hash) {
170
170
  const hashScrollIntoViewOptions =
171
- (window.history.state || {}).__hashScrollIntoViewOptions ?? true
171
+ window.history.state?.__hashScrollIntoViewOptions ?? true
172
172
 
173
173
  if (hashScrollIntoViewOptions) {
174
174
  const el = document.getElementById(hash)
@@ -177,30 +177,24 @@ export function restoreScroll({
177
177
  }
178
178
  }
179
179
 
180
- return
180
+ break scroll
181
181
  }
182
182
 
183
183
  // If there is no cached entry for the hash and there is no hash in the URL,
184
184
  // we need to scroll to the top of the page for every scrollToTop element
185
- ;[
186
- 'window',
187
- ...(scrollToTopSelectors?.filter((d) => d !== 'window') ?? []),
188
- ].forEach((selector) => {
189
- const element =
190
- selector === 'window'
191
- ? window
192
- : typeof selector === 'function'
185
+ const scrollOptions = { top: 0, left: 0, behavior }
186
+ window.scrollTo(scrollOptions)
187
+ if (scrollToTopSelectors) {
188
+ for (const selector of scrollToTopSelectors) {
189
+ if (selector === 'window') continue
190
+ const element =
191
+ typeof selector === 'function'
193
192
  ? selector()
194
193
  : document.querySelector(selector)
195
- if (element) {
196
- element.scrollTo({
197
- top: 0,
198
- left: 0,
199
- behavior,
200
- })
194
+ if (element) element.scrollTo(scrollOptions)
201
195
  }
202
- })
203
- })()
196
+ }
197
+ }
204
198
 
205
199
  //
206
200
  ignoreScroll = false
@@ -294,11 +288,10 @@ export function setupScrollRestoration(router: AnyRouter, force?: boolean) {
294
288
  const restoreKey = getKey(router.state.location)
295
289
 
296
290
  scrollRestorationCache.set((state) => {
297
- const keyEntry = (state[restoreKey] =
298
- state[restoreKey] || ({} as ScrollRestorationByElement))
291
+ const keyEntry = (state[restoreKey] ||= {} as ScrollRestorationByElement)
299
292
 
300
- const elementEntry = (keyEntry[elementSelector] =
301
- keyEntry[elementSelector] || ({} as ScrollRestorationEntry))
293
+ const elementEntry = (keyEntry[elementSelector] ||=
294
+ {} as ScrollRestorationEntry)
302
295
 
303
296
  if (elementSelector === 'window') {
304
297
  elementEntry.scrollX = window.scrollX || 0
@@ -344,7 +337,7 @@ export function setupScrollRestoration(router: AnyRouter, force?: boolean) {
344
337
  if (router.isScrollRestoring) {
345
338
  // Mark the location as having been seen
346
339
  scrollRestorationCache.set((state) => {
347
- state[cacheKey] = state[cacheKey] || ({} as ScrollRestorationByElement)
340
+ state[cacheKey] ||= {} as ScrollRestorationByElement
348
341
 
349
342
  return state
350
343
  })
@@ -9,7 +9,7 @@ export const defaultStringifySearch = stringifySearchWith(
9
9
 
10
10
  export function parseSearchWith(parser: (str: string) => any) {
11
11
  return (searchStr: string): AnySchema => {
12
- if (searchStr.substring(0, 1) === '?') {
12
+ if (searchStr[0] === '?') {
13
13
  searchStr = searchStr.substring(1)
14
14
  }
15
15
 
@@ -21,8 +21,8 @@ export function parseSearchWith(parser: (str: string) => any) {
21
21
  if (typeof value === 'string') {
22
22
  try {
23
23
  query[key] = parser(value)
24
- } catch (err) {
25
- //
24
+ } catch (_err) {
25
+ // silent
26
26
  }
27
27
  }
28
28
  }
@@ -35,20 +35,21 @@ export function stringifySearchWith(
35
35
  stringify: (search: any) => string,
36
36
  parser?: (str: string) => any,
37
37
  ) {
38
+ const hasParser = typeof parser === 'function'
38
39
  function stringifyValue(val: any) {
39
40
  if (typeof val === 'object' && val !== null) {
40
41
  try {
41
42
  return stringify(val)
42
- } catch (err) {
43
+ } catch (_err) {
43
44
  // silent
44
45
  }
45
- } else if (typeof val === 'string' && typeof parser === 'function') {
46
+ } else if (hasParser && typeof val === 'string') {
46
47
  try {
47
48
  // Check if it's a valid parseable string.
48
49
  // If it is, then stringify it again.
49
50
  parser(val)
50
51
  return stringify(val)
51
- } catch (err) {
52
+ } catch (_err) {
52
53
  // silent
53
54
  }
54
55
  }
@@ -56,19 +57,7 @@ export function stringifySearchWith(
56
57
  }
57
58
 
58
59
  return (search: Record<string, any>) => {
59
- search = { ...search }
60
-
61
- Object.keys(search).forEach((key) => {
62
- const val = search[key]
63
- if (typeof val === 'undefined' || val === undefined) {
64
- delete search[key]
65
- } else {
66
- search[key] = stringifyValue(val)
67
- }
68
- })
69
-
70
- const searchStr = encode(search as Record<string, string>).toString()
71
-
60
+ const searchStr = encode(search, stringifyValue)
72
61
  return searchStr ? `?${searchStr}` : ''
73
62
  }
74
63
  }