@tanstack/router-core 1.168.3 → 1.168.5

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 (46) hide show
  1. package/dist/cjs/hash-scroll.cjs +20 -0
  2. package/dist/cjs/hash-scroll.cjs.map +1 -0
  3. package/dist/cjs/hash-scroll.d.cts +7 -0
  4. package/dist/cjs/index.cjs +3 -3
  5. package/dist/cjs/index.d.cts +2 -1
  6. package/dist/cjs/scroll-restoration-inline.cjs +6 -0
  7. package/dist/cjs/scroll-restoration-inline.cjs.map +1 -0
  8. package/dist/cjs/scroll-restoration-inline.d.cts +6 -0
  9. package/dist/cjs/scroll-restoration-script/client.cjs +9 -0
  10. package/dist/cjs/scroll-restoration-script/client.cjs.map +1 -0
  11. package/dist/cjs/scroll-restoration-script/client.d.cts +2 -0
  12. package/dist/cjs/scroll-restoration-script/server.cjs +30 -0
  13. package/dist/cjs/scroll-restoration-script/server.cjs.map +1 -0
  14. package/dist/cjs/scroll-restoration-script/server.d.cts +2 -0
  15. package/dist/cjs/scroll-restoration.cjs +130 -142
  16. package/dist/cjs/scroll-restoration.cjs.map +1 -1
  17. package/dist/cjs/scroll-restoration.d.cts +15 -38
  18. package/dist/cjs/ssr/transformStreamWithRouter.cjs.map +1 -1
  19. package/dist/cjs/ssr/tsrScript.cjs +1 -1
  20. package/dist/esm/hash-scroll.d.ts +7 -0
  21. package/dist/esm/hash-scroll.js +20 -0
  22. package/dist/esm/hash-scroll.js.map +1 -0
  23. package/dist/esm/index.d.ts +2 -1
  24. package/dist/esm/index.js +3 -2
  25. package/dist/esm/scroll-restoration-inline.d.ts +6 -0
  26. package/dist/esm/scroll-restoration-inline.js +6 -0
  27. package/dist/esm/scroll-restoration-inline.js.map +1 -0
  28. package/dist/esm/scroll-restoration-script/client.d.ts +2 -0
  29. package/dist/esm/scroll-restoration-script/client.js +8 -0
  30. package/dist/esm/scroll-restoration-script/client.js.map +1 -0
  31. package/dist/esm/scroll-restoration-script/server.d.ts +2 -0
  32. package/dist/esm/scroll-restoration-script/server.js +29 -0
  33. package/dist/esm/scroll-restoration-script/server.js.map +1 -0
  34. package/dist/esm/scroll-restoration.d.ts +15 -38
  35. package/dist/esm/scroll-restoration.js +131 -141
  36. package/dist/esm/scroll-restoration.js.map +1 -1
  37. package/dist/esm/ssr/transformStreamWithRouter.js.map +1 -1
  38. package/dist/esm/ssr/tsrScript.js +1 -1
  39. package/package.json +25 -4
  40. package/src/hash-scroll.ts +21 -0
  41. package/src/index.ts +3 -3
  42. package/src/scroll-restoration-inline.ts +81 -0
  43. package/src/scroll-restoration-script/client.ts +5 -0
  44. package/src/scroll-restoration-script/server.ts +64 -0
  45. package/src/scroll-restoration.ts +243 -271
  46. package/src/ssr/transformStreamWithRouter.ts +2 -2
@@ -0,0 +1,81 @@
1
+ export default function (options: {
2
+ storageKey: string
3
+ key?: string
4
+ behavior?: ScrollToOptions['behavior']
5
+ shouldScrollRestoration?: boolean
6
+ }) {
7
+ let byKey
8
+
9
+ try {
10
+ byKey = JSON.parse(sessionStorage.getItem(options.storageKey) || '{}')
11
+ } catch (error) {
12
+ console.error(error)
13
+ return
14
+ }
15
+
16
+ const resolvedKey = options.key || window.history.state?.__TSR_key
17
+ const elementEntries = resolvedKey ? byKey[resolvedKey] : undefined
18
+
19
+ if (
20
+ options.shouldScrollRestoration &&
21
+ elementEntries &&
22
+ typeof elementEntries === 'object' &&
23
+ Object.keys(elementEntries).length > 0
24
+ ) {
25
+ for (const elementSelector in elementEntries) {
26
+ const entry = elementEntries[elementSelector]
27
+
28
+ if (!entry || typeof entry !== 'object') {
29
+ continue
30
+ }
31
+
32
+ const scrollX = entry.scrollX
33
+ const scrollY = entry.scrollY
34
+
35
+ if (!Number.isFinite(scrollX) || !Number.isFinite(scrollY)) {
36
+ continue
37
+ }
38
+
39
+ if (elementSelector === 'window') {
40
+ window.scrollTo({
41
+ top: scrollY,
42
+ left: scrollX,
43
+ behavior: options.behavior,
44
+ })
45
+ } else if (elementSelector) {
46
+ let element
47
+
48
+ try {
49
+ element = document.querySelector(elementSelector)
50
+ } catch {
51
+ continue
52
+ }
53
+
54
+ if (element) {
55
+ element.scrollLeft = scrollX
56
+ element.scrollTop = scrollY
57
+ }
58
+ }
59
+ }
60
+
61
+ return
62
+ }
63
+
64
+ const hash = window.location.hash.split('#', 2)[1]
65
+
66
+ if (hash) {
67
+ const hashScrollIntoViewOptions =
68
+ window.history.state?.__hashScrollIntoViewOptions ?? true
69
+
70
+ if (hashScrollIntoViewOptions) {
71
+ const el = document.getElementById(hash)
72
+ if (el) {
73
+ el.scrollIntoView(hashScrollIntoViewOptions)
74
+ }
75
+ }
76
+
77
+ return
78
+ }
79
+
80
+ window.scrollTo({ top: 0, left: 0, behavior: options.behavior })
81
+ }
@@ -0,0 +1,5 @@
1
+ import type { AnyRouter } from '../router'
2
+
3
+ export function getScrollRestorationScriptForRouter(_router: AnyRouter) {
4
+ return null
5
+ }
@@ -0,0 +1,64 @@
1
+ import minifiedScrollRestorationScript from '../scroll-restoration-inline?script-string'
2
+ import {
3
+ defaultGetScrollRestorationKey,
4
+ storageKey,
5
+ } from '../scroll-restoration'
6
+ import { escapeHtml } from '../utils'
7
+ import type { AnyRouter } from '../router'
8
+
9
+ type InlineScrollRestorationScriptOptions = {
10
+ storageKey: string
11
+ key?: string
12
+ behavior?: ScrollToOptions['behavior']
13
+ shouldScrollRestoration?: boolean
14
+ }
15
+
16
+ const defaultInlineScrollRestorationScript = `(${minifiedScrollRestorationScript})(${escapeHtml(
17
+ JSON.stringify({
18
+ storageKey,
19
+ shouldScrollRestoration: true,
20
+ } satisfies InlineScrollRestorationScriptOptions),
21
+ )})`
22
+
23
+ function getScrollRestorationScript(
24
+ options: InlineScrollRestorationScriptOptions,
25
+ ) {
26
+ if (
27
+ options.storageKey === storageKey &&
28
+ options.shouldScrollRestoration === true &&
29
+ options.key === undefined &&
30
+ options.behavior === undefined
31
+ ) {
32
+ return defaultInlineScrollRestorationScript
33
+ }
34
+
35
+ return `(${minifiedScrollRestorationScript})(${escapeHtml(JSON.stringify(options))})`
36
+ }
37
+
38
+ export function getScrollRestorationScriptForRouter(router: AnyRouter) {
39
+ if (
40
+ typeof router.options.scrollRestoration === 'function' &&
41
+ !router.options.scrollRestoration({ location: router.latestLocation })
42
+ ) {
43
+ return null
44
+ }
45
+
46
+ const getKey = router.options.getScrollRestorationKey
47
+ if (!getKey) {
48
+ return defaultInlineScrollRestorationScript
49
+ }
50
+
51
+ const location = router.latestLocation
52
+ const userKey = getKey(location)
53
+ const defaultKey = defaultGetScrollRestorationKey(location)
54
+
55
+ if (userKey === defaultKey) {
56
+ return defaultInlineScrollRestorationScript
57
+ }
58
+
59
+ return getScrollRestorationScript({
60
+ storageKey,
61
+ shouldScrollRestoration: true,
62
+ key: userKey,
63
+ })
64
+ }