@tanstack/solid-router 1.114.14 → 1.114.16

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.
Files changed (38) hide show
  1. package/dist/cjs/ScrollRestoration.cjs +5 -5
  2. package/dist/cjs/ScrollRestoration.cjs.map +1 -1
  3. package/dist/cjs/ScrollRestoration.d.cts +2 -3
  4. package/dist/cjs/index.d.cts +2 -1
  5. package/dist/cjs/router.cjs +1 -2
  6. package/dist/cjs/router.cjs.map +1 -1
  7. package/dist/cjs/scroll-restoration.cjs +3 -168
  8. package/dist/cjs/scroll-restoration.cjs.map +1 -1
  9. package/dist/cjs/scroll-restoration.d.cts +0 -27
  10. package/dist/cjs/typePrimitives.d.cts +1 -59
  11. package/dist/esm/ScrollRestoration.d.ts +2 -3
  12. package/dist/esm/ScrollRestoration.js +1 -1
  13. package/dist/esm/ScrollRestoration.js.map +1 -1
  14. package/dist/esm/index.d.ts +2 -1
  15. package/dist/esm/router.js +1 -2
  16. package/dist/esm/router.js.map +1 -1
  17. package/dist/esm/scroll-restoration.d.ts +0 -27
  18. package/dist/esm/scroll-restoration.js +2 -167
  19. package/dist/esm/scroll-restoration.js.map +1 -1
  20. package/dist/esm/typePrimitives.d.ts +1 -59
  21. package/dist/source/ScrollRestoration.d.ts +2 -3
  22. package/dist/source/ScrollRestoration.jsx +1 -1
  23. package/dist/source/ScrollRestoration.jsx.map +1 -1
  24. package/dist/source/index.d.ts +2 -1
  25. package/dist/source/index.jsx +0 -1
  26. package/dist/source/index.jsx.map +1 -1
  27. package/dist/source/router.js +1 -2
  28. package/dist/source/router.js.map +1 -1
  29. package/dist/source/scroll-restoration.d.ts +0 -27
  30. package/dist/source/scroll-restoration.jsx +1 -235
  31. package/dist/source/scroll-restoration.jsx.map +1 -1
  32. package/dist/source/typePrimitives.d.ts +1 -59
  33. package/package.json +3 -3
  34. package/src/ScrollRestoration.tsx +8 -5
  35. package/src/index.tsx +27 -1
  36. package/src/router.ts +1 -1
  37. package/src/scroll-restoration.tsx +5 -322
  38. package/src/typePrimitives.ts +7 -177
@@ -1,68 +1,10 @@
1
1
  import type { LinkComponentProps } from './link';
2
2
  import type { UseParamsOptions } from './useParams';
3
3
  import type { UseSearchOptions } from './useSearch';
4
- import type { AnyRouter, Constrain, ConstrainLiteral, FromPathOption, NavigateOptions, PathParamOptions, Redirect, RegisteredRouter, RouteIds, SearchParamOptions, ToPathOption, UseParamsResult, UseSearchResult } from '@tanstack/router-core';
5
- export type ValidateFromPath<TRouter extends AnyRouter = RegisteredRouter, TFrom = string> = FromPathOption<TRouter, TFrom>;
6
- export type ValidateToPath<TRouter extends AnyRouter = RegisteredRouter, TTo extends string | undefined = undefined, TFrom extends string = string> = ToPathOption<TRouter, TFrom, TTo>;
7
- export type ValidateSearch<TRouter extends AnyRouter = RegisteredRouter, TTo extends string | undefined = undefined, TFrom extends string = string> = SearchParamOptions<TRouter, TFrom, TTo>;
8
- export type ValidateParams<TRouter extends AnyRouter = RegisteredRouter, TTo extends string | undefined = undefined, TFrom extends string = string> = PathParamOptions<TRouter, TFrom, TTo>;
9
- /**
10
- * @internal
11
- */
12
- export type InferFrom<TOptions, TDefaultFrom extends string = string> = TOptions extends {
13
- from: infer TFrom extends string;
14
- } ? TFrom : TDefaultFrom;
15
- /**
16
- * @internal
17
- */
18
- export type InferTo<TOptions> = TOptions extends {
19
- to: infer TTo extends string;
20
- } ? TTo : undefined;
21
- /**
22
- * @internal
23
- */
24
- export type InferMaskTo<TOptions> = TOptions extends {
25
- mask: {
26
- to: infer TTo extends string;
27
- };
28
- } ? TTo : '';
29
- export type InferMaskFrom<TOptions> = TOptions extends {
30
- mask: {
31
- from: infer TFrom extends string;
32
- };
33
- } ? TFrom : string;
34
- export type ValidateNavigateOptions<TRouter extends AnyRouter = RegisteredRouter, TOptions = unknown, TDefaultFrom extends string = string> = Constrain<TOptions, NavigateOptions<TRouter, InferFrom<TOptions, TDefaultFrom>, InferTo<TOptions>, InferMaskFrom<TOptions>, InferMaskTo<TOptions>>>;
35
- export type ValidateNavigateOptionsArray<TRouter extends AnyRouter = RegisteredRouter, TOptions extends ReadonlyArray<any> = ReadonlyArray<unknown>, TDefaultFrom extends string = string> = {
36
- [K in keyof TOptions]: ValidateNavigateOptions<TRouter, TOptions[K], TDefaultFrom>;
37
- };
38
- export type ValidateRedirectOptions<TRouter extends AnyRouter = RegisteredRouter, TOptions = unknown, TDefaultFrom extends string = string> = Constrain<TOptions, Redirect<TRouter, InferFrom<TOptions, TDefaultFrom>, InferTo<TOptions>, InferMaskFrom<TOptions>, InferMaskTo<TOptions>>>;
39
- export type ValidateRedirectOptionsArray<TRouter extends AnyRouter = RegisteredRouter, TOptions extends ReadonlyArray<any> = ReadonlyArray<unknown>, TDefaultFrom extends string = string> = {
40
- [K in keyof TOptions]: ValidateRedirectOptions<TRouter, TOptions[K], TDefaultFrom>;
41
- };
4
+ import type { AnyRouter, Constrain, InferFrom, InferMaskFrom, InferMaskTo, InferSelected, InferShouldThrow, InferStrict, InferTo, RegisteredRouter } from '@tanstack/router-core';
42
5
  export type ValidateLinkOptions<TRouter extends AnyRouter = RegisteredRouter, TOptions = unknown, TDefaultFrom extends string = string, TComp = 'a'> = Constrain<TOptions, LinkComponentProps<TComp, TRouter, InferFrom<TOptions, TDefaultFrom>, InferTo<TOptions>, InferMaskFrom<TOptions>, InferMaskTo<TOptions>>>;
43
6
  export type ValidateLinkOptionsArray<TRouter extends AnyRouter = RegisteredRouter, TOptions extends ReadonlyArray<any> = ReadonlyArray<unknown>, TDefaultFrom extends string = string, TComp = 'a'> = {
44
7
  [K in keyof TOptions]: ValidateLinkOptions<TRouter, TOptions[K], TDefaultFrom, TComp>;
45
8
  };
46
- export type ValidateId<TRouter extends AnyRouter = RegisteredRouter, TId extends string = string> = ConstrainLiteral<TId, RouteIds<TRouter['routeTree']>>;
47
- /**
48
- * @internal
49
- */
50
- export type InferStrict<TOptions> = TOptions extends {
51
- strict: infer TStrict extends boolean;
52
- } ? TStrict : true;
53
- /**
54
- * @internal
55
- */
56
- export type InferShouldThrow<TOptions> = TOptions extends {
57
- shouldThrow: infer TShouldThrow extends boolean;
58
- } ? TShouldThrow : true;
59
- /**
60
- * @internal
61
- */
62
- export type InferSelected<TOptions> = TOptions extends {
63
- select: (...args: Array<any>) => infer TSelected;
64
- } ? TSelected : unknown;
65
9
  export type ValidateUseSearchOptions<TOptions, TRouter extends AnyRouter = RegisteredRouter> = Constrain<TOptions, UseSearchOptions<TRouter, InferFrom<TOptions>, InferStrict<TOptions>, InferShouldThrow<TOptions>, InferSelected<TOptions>>>;
66
- export type ValidateUseSearchResult<TOptions, TRouter extends AnyRouter = RegisteredRouter> = UseSearchResult<TRouter, InferFrom<TOptions>, InferStrict<TOptions>, InferSelected<TOptions>>;
67
10
  export type ValidateUseParamsOptions<TOptions, TRouter extends AnyRouter = RegisteredRouter> = Constrain<TOptions, UseParamsOptions<TRouter, InferFrom<TOptions>, InferStrict<TOptions>, InferShouldThrow<TOptions>, InferSelected<TOptions>>>;
68
- export type ValidateUseParamsResult<TOptions, TRouter extends AnyRouter = RegisteredRouter> = Constrain<TOptions, UseParamsResult<TRouter, InferFrom<TOptions>, InferStrict<TOptions>, InferSelected<TOptions>>>;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanstack/solid-router",
3
- "version": "1.114.14",
3
+ "version": "1.114.16",
4
4
  "description": "Modern and scalable routing for Solid applications",
5
5
  "author": "Tanner Linsley",
6
6
  "license": "MIT",
@@ -60,8 +60,8 @@
60
60
  "jsesc": "^3.0.2",
61
61
  "tiny-invariant": "^1.3.3",
62
62
  "tiny-warning": "^1.0.3",
63
- "@tanstack/history": "1.114.12",
64
- "@tanstack/router-core": "1.114.12"
63
+ "@tanstack/router-core": "1.114.16",
64
+ "@tanstack/history": "1.114.12"
65
65
  },
66
66
  "devDependencies": {
67
67
  "@solidjs/testing-library": "^0.8.10",
@@ -1,12 +1,15 @@
1
- import { useRouter } from './useRouter'
2
1
  import {
3
2
  defaultGetScrollRestorationKey,
4
3
  getCssSelector,
5
4
  scrollRestorationCache,
6
5
  setupScrollRestoration,
7
- } from './scroll-restoration'
8
- import type { ScrollRestorationOptions } from './scroll-restoration'
9
- import type { ParsedLocation } from '@tanstack/router-core'
6
+ } from '@tanstack/router-core'
7
+ import { useRouter } from './useRouter'
8
+ import type {
9
+ ParsedLocation,
10
+ ScrollRestorationEntry,
11
+ ScrollRestorationOptions,
12
+ } from '@tanstack/router-core'
10
13
 
11
14
  function useScrollRestoration() {
12
15
  const router = useRouter()
@@ -41,7 +44,7 @@ export function useElementScrollRestoration(
41
44
  ) & {
42
45
  getKey?: (location: ParsedLocation) => string
43
46
  },
44
- ) {
47
+ ): ScrollRestorationEntry | undefined {
45
48
  useScrollRestoration()
46
49
 
47
50
  const router = useRouter()
package/src/index.tsx CHANGED
@@ -335,7 +335,33 @@ export { CatchNotFound, DefaultGlobalNotFound } from './not-found'
335
335
  export { notFound, isNotFound } from '@tanstack/router-core'
336
336
  export type { NotFoundError } from '@tanstack/router-core'
337
337
 
338
- export * from './typePrimitives'
338
+ export type {
339
+ ValidateLinkOptions,
340
+ ValidateUseSearchOptions,
341
+ ValidateUseParamsOptions,
342
+ ValidateLinkOptionsArray,
343
+ } from './typePrimitives'
344
+
345
+ export type {
346
+ ValidateFromPath,
347
+ ValidateToPath,
348
+ ValidateSearch,
349
+ ValidateParams,
350
+ InferFrom,
351
+ InferTo,
352
+ InferMaskTo,
353
+ InferMaskFrom,
354
+ ValidateNavigateOptions,
355
+ ValidateNavigateOptionsArray,
356
+ ValidateRedirectOptions,
357
+ ValidateRedirectOptionsArray,
358
+ ValidateId,
359
+ InferStrict,
360
+ InferShouldThrow,
361
+ InferSelected,
362
+ ValidateUseSearchResult,
363
+ ValidateUseParamsResult,
364
+ } from '@tanstack/router-core'
339
365
 
340
366
  export { ScriptOnce } from './ScriptOnce'
341
367
 
package/src/router.ts CHANGED
@@ -25,11 +25,11 @@ import {
25
25
  replaceEqualDeep,
26
26
  resolvePath,
27
27
  rootRouteId,
28
+ setupScrollRestoration,
28
29
  trimPath,
29
30
  trimPathLeft,
30
31
  trimPathRight,
31
32
  } from '@tanstack/router-core'
32
- import { setupScrollRestoration } from './scroll-restoration'
33
33
  import type * as Solid from 'solid-js'
34
34
  import type { HistoryLocation, RouterHistory } from '@tanstack/history'
35
35
 
@@ -1,328 +1,11 @@
1
- import { functionalUpdate } from '@tanstack/router-core'
1
+ import {
2
+ defaultGetScrollRestorationKey,
3
+ restoreScroll,
4
+ storageKey,
5
+ } from '@tanstack/router-core'
2
6
  import { useRouter } from './useRouter'
3
7
  import { ScriptOnce } from './ScriptOnce'
4
8
 
5
- import type {
6
- AnyRouter,
7
- NonNullableUpdater,
8
- ParsedLocation,
9
- } from '@tanstack/router-core'
10
-
11
- export type ScrollRestorationEntry = { scrollX: number; scrollY: number }
12
-
13
- export type ScrollRestorationByElement = Record<string, ScrollRestorationEntry>
14
-
15
- export type ScrollRestorationByKey = Record<string, ScrollRestorationByElement>
16
-
17
- export type ScrollRestorationCache = {
18
- state: ScrollRestorationByKey
19
- set: (updater: NonNullableUpdater<ScrollRestorationByKey>) => void
20
- }
21
- export type ScrollRestorationOptions = {
22
- getKey?: (location: ParsedLocation) => string
23
- scrollBehavior?: ScrollToOptions['behavior']
24
- }
25
-
26
- export const storageKey = 'tsr-scroll-restoration-v1_3'
27
- let sessionsStorage = false
28
- try {
29
- sessionsStorage =
30
- typeof window !== 'undefined' && typeof window.sessionStorage === 'object'
31
- } catch {}
32
- const throttle = (fn: (...args: Array<any>) => void, wait: number) => {
33
- let timeout: any
34
- return (...args: Array<any>) => {
35
- if (!timeout) {
36
- timeout = setTimeout(() => {
37
- fn(...args)
38
- timeout = null
39
- }, wait)
40
- }
41
- }
42
- }
43
- export const scrollRestorationCache: ScrollRestorationCache = sessionsStorage
44
- ? (() => {
45
- const state: ScrollRestorationByKey =
46
- JSON.parse(window.sessionStorage.getItem(storageKey) || 'null') || {}
47
-
48
- return {
49
- state,
50
- // This setter is simply to make sure that we set the sessionStorage right
51
- // after the state is updated. It doesn't necessarily need to be a functional
52
- // update.
53
- set: (updater) => (
54
- (scrollRestorationCache.state =
55
- // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
56
- functionalUpdate(updater, scrollRestorationCache.state) ||
57
- scrollRestorationCache.state),
58
- window.sessionStorage.setItem(
59
- storageKey,
60
- JSON.stringify(scrollRestorationCache.state),
61
- )
62
- ),
63
- }
64
- })()
65
- : (undefined as any)
66
- /**
67
- * The default `getKey` function for `useScrollRestoration`.
68
- * It returns the `key` from the location state or the `href` of the location.
69
- *
70
- * The `location.href` is used as a fallback to support the use case where the location state is not available like the initial render.
71
- */
72
-
73
- export const defaultGetScrollRestorationKey = (location: ParsedLocation) => {
74
- return location.state.key! || location.href
75
- }
76
-
77
- export function getCssSelector(el: any): string {
78
- const path = []
79
- let parent
80
- while ((parent = el.parentNode)) {
81
- path.unshift(
82
- `${el.tagName}:nth-child(${([].indexOf as any).call(parent.children, el) + 1})`,
83
- )
84
- el = parent
85
- }
86
- return `${path.join(' > ')}`.toLowerCase()
87
- }
88
-
89
- let ignoreScroll = false
90
-
91
- // NOTE: This function must remain pure and not use any outside variables
92
- // unless they are passed in as arguments. Why? Because we need to be able to
93
- // toString() it into a script tag to execute as early as possible in the browser
94
- // during SSR. Additionally, we also call it from within the router lifecycle
95
- export function restoreScroll(
96
- storageKey: string,
97
- key: string | undefined,
98
- behavior: ScrollToOptions['behavior'] | undefined,
99
- shouldScrollRestoration: boolean | undefined,
100
- scrollToTopSelectors: Array<string> | undefined,
101
- ) {
102
- let byKey: ScrollRestorationByKey
103
-
104
- try {
105
- byKey = JSON.parse(sessionStorage.getItem(storageKey) || '{}')
106
- } catch (error: any) {
107
- console.error(error)
108
- return
109
- }
110
-
111
- const resolvedKey = key || window.history.state?.key
112
- const elementEntries = byKey[resolvedKey]
113
-
114
- //
115
- ignoreScroll = true
116
-
117
- //
118
- ;(() => {
119
- // If we have a cached entry for this location state,
120
- // we always need to prefer that over the hash scroll.
121
- if (shouldScrollRestoration && elementEntries) {
122
- for (const elementSelector in elementEntries) {
123
- const entry = elementEntries[elementSelector]!
124
- if (elementSelector === 'window') {
125
- window.scrollTo({
126
- top: entry.scrollY,
127
- left: entry.scrollX,
128
- behavior,
129
- })
130
- } else if (elementSelector) {
131
- const element = document.querySelector(elementSelector)
132
- if (element) {
133
- element.scrollLeft = entry.scrollX
134
- element.scrollTop = entry.scrollY
135
- }
136
- }
137
- }
138
-
139
- return
140
- }
141
-
142
- // If we don't have a cached entry for the hash,
143
- // Which means we've never seen this location before,
144
- // we need to check if there is a hash in the URL.
145
- // If there is, we need to scroll it's ID into view.
146
- const hash = window.location.hash.split('#')[1]
147
-
148
- if (hash) {
149
- const hashScrollIntoViewOptions =
150
- (window.history.state || {}).__hashScrollIntoViewOptions ?? true
151
-
152
- if (hashScrollIntoViewOptions) {
153
- const el = document.getElementById(hash)
154
- if (el) {
155
- el.scrollIntoView(hashScrollIntoViewOptions)
156
- }
157
- }
158
-
159
- return
160
- }
161
-
162
- // If there is no cached entry for the hash and there is no hash in the URL,
163
- // we need to scroll to the top of the page for every scrollToTop element
164
- ;[
165
- 'window',
166
- ...(scrollToTopSelectors?.filter((d) => d !== 'window') ?? []),
167
- ].forEach((selector) => {
168
- const element =
169
- selector === 'window' ? window : document.querySelector(selector)
170
- if (element) {
171
- element.scrollTo({
172
- top: 0,
173
- left: 0,
174
- behavior,
175
- })
176
- }
177
- })
178
- })()
179
-
180
- //
181
- ignoreScroll = false
182
- }
183
-
184
- export function setupScrollRestoration(router: AnyRouter, force?: boolean) {
185
- const shouldScrollRestoration =
186
- force ?? router.options.scrollRestoration ?? false
187
-
188
- if (shouldScrollRestoration) {
189
- router.isScrollRestoring = true
190
- }
191
-
192
- if (typeof document === 'undefined' || router.isScrollRestorationSetup) {
193
- return
194
- }
195
-
196
- router.isScrollRestorationSetup = true
197
-
198
- //
199
- ignoreScroll = false
200
-
201
- const getKey =
202
- router.options.getScrollRestorationKey || defaultGetScrollRestorationKey
203
-
204
- window.history.scrollRestoration = 'manual'
205
-
206
- // // Create a MutationObserver to monitor DOM changes
207
- // const mutationObserver = new MutationObserver(() => {
208
- // ;ignoreScroll = true
209
- // requestAnimationFrame(() => {
210
- // ;ignoreScroll = false
211
-
212
- // // Attempt to restore scroll position on each dom
213
- // // mutation until the user scrolls. We do this
214
- // // because dynamic content may come in at different
215
- // // ticks after the initial render and we want to
216
- // // keep up with that content as much as possible.
217
- // // As soon as the user scrolls, we no longer need
218
- // // to attempt router.
219
- // // console.log('mutation observer restoreScroll')
220
- // restoreScroll(
221
- // storageKey,
222
- // getKey(router.state.location),
223
- // router.options.scrollRestorationBehavior,
224
- // )
225
- // })
226
- // })
227
-
228
- // const observeDom = () => {
229
- // // Observe changes to the entire document
230
- // mutationObserver.observe(document, {
231
- // childList: true, // Detect added or removed child nodes
232
- // subtree: true, // Monitor all descendants
233
- // characterData: true, // Detect text content changes
234
- // })
235
- // }
236
-
237
- // const unobserveDom = () => {
238
- // mutationObserver.disconnect()
239
- // }
240
-
241
- // observeDom()
242
-
243
- const onScroll = (event: Event) => {
244
- // unobserveDom()
245
-
246
- if (ignoreScroll || !router.isScrollRestoring) {
247
- return
248
- }
249
-
250
- let elementSelector = ''
251
-
252
- if (event.target === document || event.target === window) {
253
- elementSelector = 'window'
254
- } else {
255
- const attrId = (event.target as Element).getAttribute(
256
- 'data-scroll-restoration-id',
257
- )
258
-
259
- if (attrId) {
260
- elementSelector = `[data-scroll-restoration-id="${attrId}"]`
261
- } else {
262
- elementSelector = getCssSelector(event.target)
263
- }
264
- }
265
-
266
- const restoreKey = getKey(router.state.location)
267
-
268
- scrollRestorationCache.set((state) => {
269
- const keyEntry = (state[restoreKey] =
270
- state[restoreKey] || ({} as ScrollRestorationByElement))
271
-
272
- const elementEntry = (keyEntry[elementSelector] =
273
- keyEntry[elementSelector] || ({} as ScrollRestorationEntry))
274
-
275
- if (elementSelector === 'window') {
276
- elementEntry.scrollX = window.scrollX || 0
277
- elementEntry.scrollY = window.scrollY || 0
278
- } else if (elementSelector) {
279
- const element = document.querySelector(elementSelector)
280
- if (element) {
281
- elementEntry.scrollX = element.scrollLeft || 0
282
- elementEntry.scrollY = element.scrollTop || 0
283
- }
284
- }
285
-
286
- return state
287
- })
288
- }
289
-
290
- // Throttle the scroll event to avoid excessive updates
291
- if (typeof document !== 'undefined') {
292
- document.addEventListener('scroll', throttle(onScroll, 100), true)
293
- }
294
-
295
- router.subscribe('onRendered', (event) => {
296
- // unobserveDom()
297
-
298
- const cacheKey = getKey(event.toLocation)
299
-
300
- // If the user doesn't want to restore the scroll position,
301
- // we don't need to do anything.
302
- if (!router.resetNextScroll) {
303
- router.resetNextScroll = true
304
- return
305
- }
306
-
307
- restoreScroll(
308
- storageKey,
309
- cacheKey,
310
- router.options.scrollRestorationBehavior || undefined,
311
- router.isScrollRestoring || undefined,
312
- router.options.scrollToTopSelectors || undefined,
313
- )
314
-
315
- if (router.isScrollRestoring) {
316
- // Mark the location as having been seen
317
- scrollRestorationCache.set((state) => {
318
- state[cacheKey] = state[cacheKey] || ({} as ScrollRestorationByElement)
319
-
320
- return state
321
- })
322
- }
323
- })
324
- }
325
-
326
9
  export function ScrollRestoration() {
327
10
  const router = useRouter()
328
11
  const getKey =