@tanstack/solid-router 1.114.2 → 1.114.3
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/dist/cjs/scroll-restoration.cjs +3 -3
- package/dist/cjs/scroll-restoration.cjs.map +1 -1
- package/dist/cjs/scroll-restoration.d.cts +1 -2
- package/dist/esm/scroll-restoration.d.ts +1 -2
- package/dist/esm/scroll-restoration.js +3 -3
- package/dist/esm/scroll-restoration.js.map +1 -1
- package/dist/source/scroll-restoration.d.ts +1 -2
- package/dist/source/scroll-restoration.jsx +3 -3
- package/dist/source/scroll-restoration.jsx.map +1 -1
- package/package.json +3 -3
- package/src/scroll-restoration.tsx +8 -11
|
@@ -45,7 +45,7 @@ function getCssSelector(el) {
|
|
|
45
45
|
return `${path.join(" > ")}`.toLowerCase();
|
|
46
46
|
}
|
|
47
47
|
let ignoreScroll = false;
|
|
48
|
-
function restoreScroll(
|
|
48
|
+
function restoreScroll(storageKey2, key, behavior, shouldScrollRestoration, scrollToTopSelectors) {
|
|
49
49
|
var _a;
|
|
50
50
|
let byKey;
|
|
51
51
|
try {
|
|
@@ -77,7 +77,7 @@ function restoreScroll(routerHistory, storageKey2, key, behavior, shouldScrollRe
|
|
|
77
77
|
}
|
|
78
78
|
return;
|
|
79
79
|
}
|
|
80
|
-
const hash =
|
|
80
|
+
const hash = window.location.hash.split("#")[1];
|
|
81
81
|
if (hash) {
|
|
82
82
|
const hashScrollIntoViewOptions = (window.history.state || {}).__hashScrollIntoViewOptions ?? true;
|
|
83
83
|
if (hashScrollIntoViewOptions) {
|
|
@@ -154,7 +154,7 @@ function setupScrollRestoration(router, force) {
|
|
|
154
154
|
router.resetNextScroll = true;
|
|
155
155
|
return;
|
|
156
156
|
}
|
|
157
|
-
restoreScroll(
|
|
157
|
+
restoreScroll(storageKey, cacheKey, router.options.scrollRestorationBehavior || void 0, router.isScrollRestoring || void 0, router.options.scrollToTopSelectors || void 0);
|
|
158
158
|
if (router.isScrollRestoring) {
|
|
159
159
|
scrollRestorationCache.set((state) => {
|
|
160
160
|
state[cacheKey] = state[cacheKey] || {};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"scroll-restoration.cjs","sources":["../../src/scroll-restoration.tsx"],"sourcesContent":["import { functionalUpdate } from '@tanstack/router-core'\nimport { useRouter } from './useRouter'\nimport { ScriptOnce } from './ScriptOnce'\n\nimport type {\n AnyRouter,\n NonNullableUpdater,\n ParsedLocation,\n} from '@tanstack/router-core'\nimport type { RouterHistory } from '@tanstack/history'\n\nexport type ScrollRestorationEntry = { scrollX: number; scrollY: number }\n\nexport type ScrollRestorationByElement = Record<string, ScrollRestorationEntry>\n\nexport type ScrollRestorationByKey = Record<string, ScrollRestorationByElement>\n\nexport type ScrollRestorationCache = {\n state: ScrollRestorationByKey\n set: (updater: NonNullableUpdater<ScrollRestorationByKey>) => void\n}\nexport type ScrollRestorationOptions = {\n getKey?: (location: ParsedLocation) => string\n scrollBehavior?: ScrollToOptions['behavior']\n}\n\nexport const storageKey = 'tsr-scroll-restoration-v1_3'\nlet sessionsStorage = false\ntry {\n sessionsStorage =\n typeof window !== 'undefined' && typeof window.sessionStorage === 'object'\n} catch {}\nconst throttle = (fn: (...args: Array<any>) => void, wait: number) => {\n let timeout: any\n return (...args: Array<any>) => {\n if (!timeout) {\n timeout = setTimeout(() => {\n fn(...args)\n timeout = null\n }, wait)\n }\n }\n}\nexport const scrollRestorationCache: ScrollRestorationCache = sessionsStorage\n ? (() => {\n const state: ScrollRestorationByKey =\n JSON.parse(window.sessionStorage.getItem(storageKey) || 'null') || {}\n\n return {\n state,\n // This setter is simply to make sure that we set the sessionStorage right\n // after the state is updated. It doesn't necessarily need to be a functional\n // update.\n set: (updater) => (\n (scrollRestorationCache.state =\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n functionalUpdate(updater, scrollRestorationCache.state) ||\n scrollRestorationCache.state),\n window.sessionStorage.setItem(\n storageKey,\n JSON.stringify(scrollRestorationCache.state),\n )\n ),\n }\n })()\n : (undefined as any)\n/**\n * The default `getKey` function for `useScrollRestoration`.\n * It returns the `key` from the location state or the `href` of the location.\n *\n * The `location.href` is used as a fallback to support the use case where the location state is not available like the initial render.\n */\n\nexport const defaultGetScrollRestorationKey = (location: ParsedLocation) => {\n return location.state.key! || location.href\n}\n\nexport function getCssSelector(el: any): string {\n const path = []\n let parent\n while ((parent = el.parentNode)) {\n path.unshift(\n `${el.tagName}:nth-child(${([].indexOf as any).call(parent.children, el) + 1})`,\n )\n el = parent\n }\n return `${path.join(' > ')}`.toLowerCase()\n}\n\nlet ignoreScroll = false\n\n// NOTE: This function must remain pure and not use any outside variables\n// unless they are passed in as arguments. Why? Because we need to be able to\n// toString() it into a script tag to execute as early as possible in the browser\n// during SSR. Additionally, we also call it from within the router lifecycle\nexport function restoreScroll(\n routerHistory: RouterHistory,\n storageKey: string,\n key?: string,\n behavior?: ScrollToOptions['behavior'],\n shouldScrollRestoration?: boolean,\n scrollToTopSelectors?: Array<string>,\n) {\n let byKey: ScrollRestorationByKey\n\n try {\n byKey = JSON.parse(sessionStorage.getItem(storageKey) || '{}')\n } catch (error: any) {\n console.error(error)\n return\n }\n\n const resolvedKey = key || window.history.state?.key\n const elementEntries = byKey[resolvedKey]\n\n //\n ignoreScroll = true\n\n //\n ;(() => {\n // If we have a cached entry for this location state,\n // we always need to prefer that over the hash scroll.\n if (shouldScrollRestoration && elementEntries) {\n for (const elementSelector in elementEntries) {\n const entry = elementEntries[elementSelector]!\n if (elementSelector === 'window') {\n window.scrollTo({\n top: entry.scrollY,\n left: entry.scrollX,\n behavior,\n })\n } else if (elementSelector) {\n const element = document.querySelector(elementSelector)\n if (element) {\n element.scrollLeft = entry.scrollX\n element.scrollTop = entry.scrollY\n }\n }\n }\n\n return\n }\n\n // If we don't have a cached entry for the hash,\n // Which means we've never seen this location before,\n // we need to check if there is a hash in the URL.\n // If there is, we need to scroll it's ID into view.\n const hash = routerHistory.location.hash.split('#')[1]\n\n if (hash) {\n const hashScrollIntoViewOptions =\n (window.history.state || {}).__hashScrollIntoViewOptions ?? true\n\n if (hashScrollIntoViewOptions) {\n const el = document.getElementById(hash)\n if (el) {\n el.scrollIntoView(hashScrollIntoViewOptions)\n }\n }\n\n return\n }\n\n // If there is no cached entry for the hash and there is no hash in the URL,\n // we need to scroll to the top of the page for every scrollToTop element\n ;[\n 'window',\n ...(scrollToTopSelectors?.filter((d) => d !== 'window') ?? []),\n ].forEach((selector) => {\n const element =\n selector === 'window' ? window : document.querySelector(selector)\n if (element) {\n element.scrollTo({\n top: 0,\n left: 0,\n behavior,\n })\n }\n })\n })()\n\n //\n ignoreScroll = false\n}\n\nexport function setupScrollRestoration(router: AnyRouter, force?: boolean) {\n const shouldScrollRestoration =\n force ?? router.options.scrollRestoration ?? false\n\n if (shouldScrollRestoration) {\n router.isScrollRestoring = true\n }\n\n if (typeof document === 'undefined' || router.isScrollRestorationSetup) {\n return\n }\n\n router.isScrollRestorationSetup = true\n\n //\n ignoreScroll = false\n\n const getKey =\n router.options.getScrollRestorationKey || defaultGetScrollRestorationKey\n\n window.history.scrollRestoration = 'manual'\n\n // // Create a MutationObserver to monitor DOM changes\n // const mutationObserver = new MutationObserver(() => {\n // ;ignoreScroll = true\n // requestAnimationFrame(() => {\n // ;ignoreScroll = false\n\n // // Attempt to restore scroll position on each dom\n // // mutation until the user scrolls. We do this\n // // because dynamic content may come in at different\n // // ticks after the initial render and we want to\n // // keep up with that content as much as possible.\n // // As soon as the user scrolls, we no longer need\n // // to attempt router.\n // // console.log('mutation observer restoreScroll')\n // restoreScroll(\n // storageKey,\n // getKey(router.state.location),\n // router.options.scrollRestorationBehavior,\n // )\n // })\n // })\n\n // const observeDom = () => {\n // // Observe changes to the entire document\n // mutationObserver.observe(document, {\n // childList: true, // Detect added or removed child nodes\n // subtree: true, // Monitor all descendants\n // characterData: true, // Detect text content changes\n // })\n // }\n\n // const unobserveDom = () => {\n // mutationObserver.disconnect()\n // }\n\n // observeDom()\n\n const onScroll = (event: Event) => {\n // unobserveDom()\n\n if (ignoreScroll || !router.isScrollRestoring) {\n return\n }\n\n let elementSelector = ''\n\n if (event.target === document || event.target === window) {\n elementSelector = 'window'\n } else {\n const attrId = (event.target as Element).getAttribute(\n 'data-scroll-restoration-id',\n )\n\n if (attrId) {\n elementSelector = `[data-scroll-restoration-id=\"${attrId}\"]`\n } else {\n elementSelector = getCssSelector(event.target)\n }\n }\n\n const restoreKey = getKey(router.state.location)\n\n scrollRestorationCache.set((state) => {\n const keyEntry = (state[restoreKey] =\n state[restoreKey] || ({} as ScrollRestorationByElement))\n\n const elementEntry = (keyEntry[elementSelector] =\n keyEntry[elementSelector] || ({} as ScrollRestorationEntry))\n\n if (elementSelector === 'window') {\n elementEntry.scrollX = window.scrollX || 0\n elementEntry.scrollY = window.scrollY || 0\n } else if (elementSelector) {\n const element = document.querySelector(elementSelector)\n if (element) {\n elementEntry.scrollX = element.scrollLeft || 0\n elementEntry.scrollY = element.scrollTop || 0\n }\n }\n\n return state\n })\n }\n\n // Throttle the scroll event to avoid excessive updates\n if (typeof document !== 'undefined') {\n document.addEventListener('scroll', throttle(onScroll, 100), true)\n }\n\n router.subscribe('onRendered', (event) => {\n // unobserveDom()\n\n const cacheKey = getKey(event.toLocation)\n\n // If the user doesn't want to restore the scroll position,\n // we don't need to do anything.\n if (!router.resetNextScroll) {\n router.resetNextScroll = true\n return\n }\n\n restoreScroll(\n router.history,\n storageKey,\n cacheKey,\n router.options.scrollRestorationBehavior,\n router.isScrollRestoring,\n router.options.scrollToTopSelectors,\n )\n\n if (router.isScrollRestoring) {\n // Mark the location as having been seen\n scrollRestorationCache.set((state) => {\n state[cacheKey] = state[cacheKey] || ({} as ScrollRestorationByElement)\n\n return state\n })\n }\n })\n}\n\nexport function ScrollRestoration() {\n const router = useRouter()\n const getKey =\n router.options.getScrollRestorationKey || defaultGetScrollRestorationKey\n const userKey = getKey(router.latestLocation)\n const resolvedKey =\n userKey !== defaultGetScrollRestorationKey(router.latestLocation)\n ? userKey\n : null\n\n if (!router.isScrollRestoring || !router.isServer) {\n return null\n }\n\n return (\n <ScriptOnce\n children={`(${restoreScroll.toString()})(${JSON.stringify(storageKey)},${JSON.stringify(resolvedKey)}, undefined, true)`}\n log={false}\n />\n )\n}\n"],"names":["storageKey","sessionsStorage","window","sessionStorage","throttle","fn","wait","timeout","args","setTimeout","scrollRestorationCache","state","JSON","parse","getItem","set","updater","functionalUpdate","setItem","stringify","undefined","defaultGetScrollRestorationKey","location","key","href","getCssSelector","el","path","parent","parentNode","unshift","tagName","indexOf","call","children","join","toLowerCase","ignoreScroll","restoreScroll","routerHistory","behavior","shouldScrollRestoration","scrollToTopSelectors","byKey","error","console","resolvedKey","history","elementEntries","elementSelector","entry","scrollTo","top","scrollY","left","scrollX","element","document","querySelector","scrollLeft","scrollTop","hash","split","hashScrollIntoViewOptions","__hashScrollIntoViewOptions","getElementById","scrollIntoView","filter","d","forEach","selector","setupScrollRestoration","router","force","options","scrollRestoration","isScrollRestoring","isScrollRestorationSetup","getKey","getScrollRestorationKey","onScroll","event","target","attrId","getAttribute","restoreKey","keyEntry","elementEntry","addEventListener","subscribe","cacheKey","toLocation","resetNextScroll","scrollRestorationBehavior","ScrollRestoration","useRouter","userKey","latestLocation","isServer","_$createComponent","ScriptOnce","toString","log"],"mappings":";;;;;;AA0BO,MAAMA,aAAa;AAC1B,IAAIC,kBAAkB;AACtB,IAAI;AACFA,oBACE,OAAOC,WAAW,eAAe,OAAOA,OAAOC,mBAAmB;AACtE,QAAQ;AAAC;AACT,MAAMC,WAAWA,CAACC,IAAmCC,SAAiB;AAChEC,MAAAA;AACJ,SAAO,IAAIC,SAAqB;AAC9B,QAAI,CAACD,SAAS;AACZA,gBAAUE,WAAW,MAAM;AACzBJ,WAAG,GAAGG,IAAI;AACA,kBAAA;AAAA,SACTF,IAAI;AAAA,IAAA;AAAA,EAEX;AACF;AACaI,MAAAA,yBAAiDT,mBACzD,MAAM;AACCU,QAAAA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,OAAOoB,iBAAiB;AAC3BpB,aAAOoB,kBAAkB;AACzB;AAAA,IAAA;AAIApB,kBAAAA,OAAOzB,SACP/C,YACA0F,UACAlB,OAAOE,QAAQmB,2BACfrB,OAAOI,mBACPJ,OAAOE,QAAQhC,oBACjB;AAEA,QAAI8B,OAAOI,mBAAmB;AAE5BlE,6BAAuBK,IAAKJ,CAAU,UAAA;AACpCA,cAAM+E,QAAQ,IAAI/E,MAAM+E,QAAQ,KAAM,CAAC;AAEhC/E,eAAAA;AAAAA,MAAAA,CACR;AAAA,IAAA;AAAA,EACH,CACD;AACH;AAEO,SAASmF,oBAAoB;AAClC,QAAMtB,SAASuB,UAAAA,UAAU;AACnBjB,QAAAA,SACJN,OAAOE,QAAQK,2BAA2B1D;AACtC2E,QAAAA,UAAUlB,OAAON,OAAOyB,cAAc;AAC5C,QAAMnD,cACJkD,YAAY3E,+BAA+BmD,OAAOyB,cAAc,IAC5DD,UACA;AAEN,MAAI,CAACxB,OAAOI,qBAAqB,CAACJ,OAAO0B,UAAU;AAC1C,WAAA;AAAA,EAAA;AAGT,SAAAC,IAAAA,gBACGC,WAAAA,YAAU;AAAA,IAAA,IACTlE,WAAQ;AAAA,aAAE,IAAII,cAAc+D,SAAS,CAAC,KAAKzF,KAAKO,UAAUnB,UAAU,CAAC,IAAIY,KAAKO,UAAU2B,WAAW,CAAC;AAAA,IAAoB;AAAA,IACxHwD,KAAK;AAAA,EAAA,CAAK;AAGhB;;;;;;;;"}
|
|
1
|
+
{"version":3,"file":"scroll-restoration.cjs","sources":["../../src/scroll-restoration.tsx"],"sourcesContent":["import { functionalUpdate } from '@tanstack/router-core'\nimport { useRouter } from './useRouter'\nimport { ScriptOnce } from './ScriptOnce'\n\nimport type {\n AnyRouter,\n NonNullableUpdater,\n ParsedLocation,\n} from '@tanstack/router-core'\n\nexport type ScrollRestorationEntry = { scrollX: number; scrollY: number }\n\nexport type ScrollRestorationByElement = Record<string, ScrollRestorationEntry>\n\nexport type ScrollRestorationByKey = Record<string, ScrollRestorationByElement>\n\nexport type ScrollRestorationCache = {\n state: ScrollRestorationByKey\n set: (updater: NonNullableUpdater<ScrollRestorationByKey>) => void\n}\nexport type ScrollRestorationOptions = {\n getKey?: (location: ParsedLocation) => string\n scrollBehavior?: ScrollToOptions['behavior']\n}\n\nexport const storageKey = 'tsr-scroll-restoration-v1_3'\nlet sessionsStorage = false\ntry {\n sessionsStorage =\n typeof window !== 'undefined' && typeof window.sessionStorage === 'object'\n} catch {}\nconst throttle = (fn: (...args: Array<any>) => void, wait: number) => {\n let timeout: any\n return (...args: Array<any>) => {\n if (!timeout) {\n timeout = setTimeout(() => {\n fn(...args)\n timeout = null\n }, wait)\n }\n }\n}\nexport const scrollRestorationCache: ScrollRestorationCache = sessionsStorage\n ? (() => {\n const state: ScrollRestorationByKey =\n JSON.parse(window.sessionStorage.getItem(storageKey) || 'null') || {}\n\n return {\n state,\n // This setter is simply to make sure that we set the sessionStorage right\n // after the state is updated. It doesn't necessarily need to be a functional\n // update.\n set: (updater) => (\n (scrollRestorationCache.state =\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n functionalUpdate(updater, scrollRestorationCache.state) ||\n scrollRestorationCache.state),\n window.sessionStorage.setItem(\n storageKey,\n JSON.stringify(scrollRestorationCache.state),\n )\n ),\n }\n })()\n : (undefined as any)\n/**\n * The default `getKey` function for `useScrollRestoration`.\n * It returns the `key` from the location state or the `href` of the location.\n *\n * The `location.href` is used as a fallback to support the use case where the location state is not available like the initial render.\n */\n\nexport const defaultGetScrollRestorationKey = (location: ParsedLocation) => {\n return location.state.key! || location.href\n}\n\nexport function getCssSelector(el: any): string {\n const path = []\n let parent\n while ((parent = el.parentNode)) {\n path.unshift(\n `${el.tagName}:nth-child(${([].indexOf as any).call(parent.children, el) + 1})`,\n )\n el = parent\n }\n return `${path.join(' > ')}`.toLowerCase()\n}\n\nlet ignoreScroll = false\n\n// NOTE: This function must remain pure and not use any outside variables\n// unless they are passed in as arguments. Why? Because we need to be able to\n// toString() it into a script tag to execute as early as possible in the browser\n// during SSR. Additionally, we also call it from within the router lifecycle\nexport function restoreScroll(\n storageKey: string,\n key: string | undefined,\n behavior: ScrollToOptions['behavior'] | undefined,\n shouldScrollRestoration: boolean | undefined,\n scrollToTopSelectors: Array<string> | undefined,\n) {\n let byKey: ScrollRestorationByKey\n\n try {\n byKey = JSON.parse(sessionStorage.getItem(storageKey) || '{}')\n } catch (error: any) {\n console.error(error)\n return\n }\n\n const resolvedKey = key || window.history.state?.key\n const elementEntries = byKey[resolvedKey]\n\n //\n ignoreScroll = true\n\n //\n ;(() => {\n // If we have a cached entry for this location state,\n // we always need to prefer that over the hash scroll.\n if (shouldScrollRestoration && elementEntries) {\n for (const elementSelector in elementEntries) {\n const entry = elementEntries[elementSelector]!\n if (elementSelector === 'window') {\n window.scrollTo({\n top: entry.scrollY,\n left: entry.scrollX,\n behavior,\n })\n } else if (elementSelector) {\n const element = document.querySelector(elementSelector)\n if (element) {\n element.scrollLeft = entry.scrollX\n element.scrollTop = entry.scrollY\n }\n }\n }\n\n return\n }\n\n // If we don't have a cached entry for the hash,\n // Which means we've never seen this location before,\n // we need to check if there is a hash in the URL.\n // If there is, we need to scroll it's ID into view.\n const hash = window.location.hash.split('#')[1]\n\n if (hash) {\n const hashScrollIntoViewOptions =\n (window.history.state || {}).__hashScrollIntoViewOptions ?? true\n\n if (hashScrollIntoViewOptions) {\n const el = document.getElementById(hash)\n if (el) {\n el.scrollIntoView(hashScrollIntoViewOptions)\n }\n }\n\n return\n }\n\n // If there is no cached entry for the hash and there is no hash in the URL,\n // we need to scroll to the top of the page for every scrollToTop element\n ;[\n 'window',\n ...(scrollToTopSelectors?.filter((d) => d !== 'window') ?? []),\n ].forEach((selector) => {\n const element =\n selector === 'window' ? window : document.querySelector(selector)\n if (element) {\n element.scrollTo({\n top: 0,\n left: 0,\n behavior,\n })\n }\n })\n })()\n\n //\n ignoreScroll = false\n}\n\nexport function setupScrollRestoration(router: AnyRouter, force?: boolean) {\n const shouldScrollRestoration =\n force ?? router.options.scrollRestoration ?? false\n\n if (shouldScrollRestoration) {\n router.isScrollRestoring = true\n }\n\n if (typeof document === 'undefined' || router.isScrollRestorationSetup) {\n return\n }\n\n router.isScrollRestorationSetup = true\n\n //\n ignoreScroll = false\n\n const getKey =\n router.options.getScrollRestorationKey || defaultGetScrollRestorationKey\n\n window.history.scrollRestoration = 'manual'\n\n // // Create a MutationObserver to monitor DOM changes\n // const mutationObserver = new MutationObserver(() => {\n // ;ignoreScroll = true\n // requestAnimationFrame(() => {\n // ;ignoreScroll = false\n\n // // Attempt to restore scroll position on each dom\n // // mutation until the user scrolls. We do this\n // // because dynamic content may come in at different\n // // ticks after the initial render and we want to\n // // keep up with that content as much as possible.\n // // As soon as the user scrolls, we no longer need\n // // to attempt router.\n // // console.log('mutation observer restoreScroll')\n // restoreScroll(\n // storageKey,\n // getKey(router.state.location),\n // router.options.scrollRestorationBehavior,\n // )\n // })\n // })\n\n // const observeDom = () => {\n // // Observe changes to the entire document\n // mutationObserver.observe(document, {\n // childList: true, // Detect added or removed child nodes\n // subtree: true, // Monitor all descendants\n // characterData: true, // Detect text content changes\n // })\n // }\n\n // const unobserveDom = () => {\n // mutationObserver.disconnect()\n // }\n\n // observeDom()\n\n const onScroll = (event: Event) => {\n // unobserveDom()\n\n if (ignoreScroll || !router.isScrollRestoring) {\n return\n }\n\n let elementSelector = ''\n\n if (event.target === document || event.target === window) {\n elementSelector = 'window'\n } else {\n const attrId = (event.target as Element).getAttribute(\n 'data-scroll-restoration-id',\n )\n\n if (attrId) {\n elementSelector = `[data-scroll-restoration-id=\"${attrId}\"]`\n } else {\n elementSelector = getCssSelector(event.target)\n }\n }\n\n const restoreKey = getKey(router.state.location)\n\n scrollRestorationCache.set((state) => {\n const keyEntry = (state[restoreKey] =\n state[restoreKey] || ({} as ScrollRestorationByElement))\n\n const elementEntry = (keyEntry[elementSelector] =\n keyEntry[elementSelector] || ({} as ScrollRestorationEntry))\n\n if (elementSelector === 'window') {\n elementEntry.scrollX = window.scrollX || 0\n elementEntry.scrollY = window.scrollY || 0\n } else if (elementSelector) {\n const element = document.querySelector(elementSelector)\n if (element) {\n elementEntry.scrollX = element.scrollLeft || 0\n elementEntry.scrollY = element.scrollTop || 0\n }\n }\n\n return state\n })\n }\n\n // Throttle the scroll event to avoid excessive updates\n if (typeof document !== 'undefined') {\n document.addEventListener('scroll', throttle(onScroll, 100), true)\n }\n\n router.subscribe('onRendered', (event) => {\n // unobserveDom()\n\n const cacheKey = getKey(event.toLocation)\n\n // If the user doesn't want to restore the scroll position,\n // we don't need to do anything.\n if (!router.resetNextScroll) {\n router.resetNextScroll = true\n return\n }\n\n restoreScroll(\n storageKey,\n cacheKey,\n router.options.scrollRestorationBehavior || undefined,\n router.isScrollRestoring || undefined,\n router.options.scrollToTopSelectors || undefined,\n )\n\n if (router.isScrollRestoring) {\n // Mark the location as having been seen\n scrollRestorationCache.set((state) => {\n state[cacheKey] = state[cacheKey] || ({} as ScrollRestorationByElement)\n\n return state\n })\n }\n })\n}\n\nexport function ScrollRestoration() {\n const router = useRouter()\n const getKey =\n router.options.getScrollRestorationKey || defaultGetScrollRestorationKey\n const userKey = getKey(router.latestLocation)\n const resolvedKey =\n userKey !== defaultGetScrollRestorationKey(router.latestLocation)\n ? userKey\n : null\n\n if (!router.isScrollRestoring || !router.isServer) {\n return null\n }\n\n return (\n <ScriptOnce\n children={`(${restoreScroll.toString()})(${JSON.stringify(storageKey)},${JSON.stringify(resolvedKey)}, undefined, true)`}\n log={false}\n />\n )\n}\n"],"names":["storageKey","sessionsStorage","window","sessionStorage","throttle","fn","wait","timeout","args","setTimeout","scrollRestorationCache","state","JSON","parse","getItem","set","updater","functionalUpdate","setItem","stringify","undefined","defaultGetScrollRestorationKey","location","key","href","getCssSelector","el","path","parent","parentNode","unshift","tagName","indexOf","call","children","join","toLowerCase","ignoreScroll","restoreScroll","behavior","shouldScrollRestoration","scrollToTopSelectors","byKey","error","console","resolvedKey","history","elementEntries","elementSelector","entry","scrollTo","top","scrollY","left","scrollX","element","document","querySelector","scrollLeft","scrollTop","hash","split","hashScrollIntoViewOptions","__hashScrollIntoViewOptions","getElementById","scrollIntoView","filter","d","forEach","selector","setupScrollRestoration","router","force","options","scrollRestoration","isScrollRestoring","isScrollRestorationSetup","getKey","getScrollRestorationKey","onScroll","event","target","attrId","getAttribute","restoreKey","keyEntry","elementEntry","addEventListener","subscribe","cacheKey","toLocation","resetNextScroll","scrollRestorationBehavior","ScrollRestoration","useRouter","userKey","latestLocation","isServer","_$createComponent","ScriptOnce","toString","log"],"mappings":";;;;;;AAyBO,MAAMA,aAAa;AAC1B,IAAIC,kBAAkB;AACtB,IAAI;AACFA,oBACE,OAAOC,WAAW,eAAe,OAAOA,OAAOC,mBAAmB;AACtE,QAAQ;AAAC;AACT,MAAMC,WAAWA,CAACC,IAAmCC,SAAiB;AAChEC,MAAAA;AACJ,SAAO,IAAIC,SAAqB;AAC9B,QAAI,CAACD,SAAS;AACZA,gBAAUE,WAAW,MAAM;AACzBJ,WAAG,GAAGG,IAAI;AACA,kBAAA;AAAA,SACTF,IAAI;AAAA,IAAA;AAAA,EAEX;AACF;AACaI,MAAAA,yBAAiDT,mBACzD,MAAM;AACCU,QAAAA,QACJC,KAAKC,MAAMX,OAAOC,eAAeW,QAAQd,UAAU,KAAK,MAAM,KAAK,CAAC;AAE/D,SAAA;AAAA,IACLW;AAAAA;AAAAA;AAAAA;AAAAA,IAIAI,KAAMC,cACHN,uBAAuBC;AAAAA,IAEtBM,WAAiBD,iBAAAA,SAASN,uBAAuBC,KAAK,KACtDD,uBAAuBC,OACzBT,OAAOC,eAAee,QACpBlB,YACAY,KAAKO,UAAUT,uBAAuBC,KAAK,CAC7C;AAAA,EAEJ;AACF,OACCS;AAQQC,MAAAA,iCAAiCA,CAACC,aAA6B;AACnEA,SAAAA,SAASX,MAAMY,OAAQD,SAASE;AACzC;AAEO,SAASC,eAAeC,IAAiB;AAC9C,QAAMC,OAA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ACJN,OAAOE,QAAQK,2BAA2BzD;AACtC0E,QAAAA,UAAUlB,OAAON,OAAOyB,cAAc;AAC5C,QAAMnD,cACJkD,YAAY1E,+BAA+BkD,OAAOyB,cAAc,IAC5DD,UACA;AAEN,MAAI,CAACxB,OAAOI,qBAAqB,CAACJ,OAAO0B,UAAU;AAC1C,WAAA;AAAA,EAAA;AAGT,SAAAC,IAAAA,gBACGC,WAAAA,YAAU;AAAA,IAAA,IACTjE,WAAQ;AAAA,aAAE,IAAII,cAAc8D,SAAS,CAAC,KAAKxF,KAAKO,UAAUnB,UAAU,CAAC,IAAIY,KAAKO,UAAU0B,WAAW,CAAC;AAAA,IAAoB;AAAA,IACxHwD,KAAK;AAAA,EAAA,CAAK;AAGhB;;;;;;;;"}
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { AnyRouter, NonNullableUpdater, ParsedLocation } from '@tanstack/router-core';
|
|
2
|
-
import { RouterHistory } from '@tanstack/history';
|
|
3
2
|
export type ScrollRestorationEntry = {
|
|
4
3
|
scrollX: number;
|
|
5
4
|
scrollY: number;
|
|
@@ -24,6 +23,6 @@ export declare const scrollRestorationCache: ScrollRestorationCache;
|
|
|
24
23
|
*/
|
|
25
24
|
export declare const defaultGetScrollRestorationKey: (location: ParsedLocation) => string;
|
|
26
25
|
export declare function getCssSelector(el: any): string;
|
|
27
|
-
export declare function restoreScroll(
|
|
26
|
+
export declare function restoreScroll(storageKey: string, key: string | undefined, behavior: ScrollToOptions['behavior'] | undefined, shouldScrollRestoration: boolean | undefined, scrollToTopSelectors: Array<string> | undefined): void;
|
|
28
27
|
export declare function setupScrollRestoration(router: AnyRouter, force?: boolean): void;
|
|
29
28
|
export declare function ScrollRestoration(): import("solid-js").JSX.Element;
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { AnyRouter, NonNullableUpdater, ParsedLocation } from '@tanstack/router-core';
|
|
2
|
-
import { RouterHistory } from '@tanstack/history';
|
|
3
2
|
export type ScrollRestorationEntry = {
|
|
4
3
|
scrollX: number;
|
|
5
4
|
scrollY: number;
|
|
@@ -24,6 +23,6 @@ export declare const scrollRestorationCache: ScrollRestorationCache;
|
|
|
24
23
|
*/
|
|
25
24
|
export declare const defaultGetScrollRestorationKey: (location: ParsedLocation) => string;
|
|
26
25
|
export declare function getCssSelector(el: any): string;
|
|
27
|
-
export declare function restoreScroll(
|
|
26
|
+
export declare function restoreScroll(storageKey: string, key: string | undefined, behavior: ScrollToOptions['behavior'] | undefined, shouldScrollRestoration: boolean | undefined, scrollToTopSelectors: Array<string> | undefined): void;
|
|
28
27
|
export declare function setupScrollRestoration(router: AnyRouter, force?: boolean): void;
|
|
29
28
|
export declare function ScrollRestoration(): import("solid-js").JSX.Element;
|
|
@@ -43,7 +43,7 @@ function getCssSelector(el) {
|
|
|
43
43
|
return `${path.join(" > ")}`.toLowerCase();
|
|
44
44
|
}
|
|
45
45
|
let ignoreScroll = false;
|
|
46
|
-
function restoreScroll(
|
|
46
|
+
function restoreScroll(storageKey2, key, behavior, shouldScrollRestoration, scrollToTopSelectors) {
|
|
47
47
|
var _a;
|
|
48
48
|
let byKey;
|
|
49
49
|
try {
|
|
@@ -75,7 +75,7 @@ function restoreScroll(routerHistory, storageKey2, key, behavior, shouldScrollRe
|
|
|
75
75
|
}
|
|
76
76
|
return;
|
|
77
77
|
}
|
|
78
|
-
const hash =
|
|
78
|
+
const hash = window.location.hash.split("#")[1];
|
|
79
79
|
if (hash) {
|
|
80
80
|
const hashScrollIntoViewOptions = (window.history.state || {}).__hashScrollIntoViewOptions ?? true;
|
|
81
81
|
if (hashScrollIntoViewOptions) {
|
|
@@ -152,7 +152,7 @@ function setupScrollRestoration(router, force) {
|
|
|
152
152
|
router.resetNextScroll = true;
|
|
153
153
|
return;
|
|
154
154
|
}
|
|
155
|
-
restoreScroll(
|
|
155
|
+
restoreScroll(storageKey, cacheKey, router.options.scrollRestorationBehavior || void 0, router.isScrollRestoring || void 0, router.options.scrollToTopSelectors || void 0);
|
|
156
156
|
if (router.isScrollRestoring) {
|
|
157
157
|
scrollRestorationCache.set((state) => {
|
|
158
158
|
state[cacheKey] = state[cacheKey] || {};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"scroll-restoration.js","sources":["../../src/scroll-restoration.tsx"],"sourcesContent":["import { functionalUpdate } from '@tanstack/router-core'\nimport { useRouter } from './useRouter'\nimport { ScriptOnce } from './ScriptOnce'\n\nimport type {\n AnyRouter,\n NonNullableUpdater,\n ParsedLocation,\n} from '@tanstack/router-core'\nimport type { RouterHistory } from '@tanstack/history'\n\nexport type ScrollRestorationEntry = { scrollX: number; scrollY: number }\n\nexport type ScrollRestorationByElement = Record<string, ScrollRestorationEntry>\n\nexport type ScrollRestorationByKey = Record<string, ScrollRestorationByElement>\n\nexport type ScrollRestorationCache = {\n state: ScrollRestorationByKey\n set: (updater: NonNullableUpdater<ScrollRestorationByKey>) => void\n}\nexport type ScrollRestorationOptions = {\n getKey?: (location: ParsedLocation) => string\n scrollBehavior?: ScrollToOptions['behavior']\n}\n\nexport const storageKey = 'tsr-scroll-restoration-v1_3'\nlet sessionsStorage = false\ntry {\n sessionsStorage =\n typeof window !== 'undefined' && typeof window.sessionStorage === 'object'\n} catch {}\nconst throttle = (fn: (...args: Array<any>) => void, wait: number) => {\n let timeout: any\n return (...args: Array<any>) => {\n if (!timeout) {\n timeout = setTimeout(() => {\n fn(...args)\n timeout = null\n }, wait)\n }\n }\n}\nexport const scrollRestorationCache: ScrollRestorationCache = sessionsStorage\n ? (() => {\n const state: ScrollRestorationByKey =\n JSON.parse(window.sessionStorage.getItem(storageKey) || 'null') || {}\n\n return {\n state,\n // This setter is simply to make sure that we set the sessionStorage right\n // after the state is updated. It doesn't necessarily need to be a functional\n // update.\n set: (updater) => (\n (scrollRestorationCache.state =\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n functionalUpdate(updater, scrollRestorationCache.state) ||\n scrollRestorationCache.state),\n window.sessionStorage.setItem(\n storageKey,\n JSON.stringify(scrollRestorationCache.state),\n )\n ),\n }\n })()\n : (undefined as any)\n/**\n * The default `getKey` function for `useScrollRestoration`.\n * It returns the `key` from the location state or the `href` of the location.\n *\n * The `location.href` is used as a fallback to support the use case where the location state is not available like the initial render.\n */\n\nexport const defaultGetScrollRestorationKey = (location: ParsedLocation) => {\n return location.state.key! || location.href\n}\n\nexport function getCssSelector(el: any): string {\n const path = []\n let parent\n while ((parent = el.parentNode)) {\n path.unshift(\n `${el.tagName}:nth-child(${([].indexOf as any).call(parent.children, el) + 1})`,\n )\n el = parent\n }\n return `${path.join(' > ')}`.toLowerCase()\n}\n\nlet ignoreScroll = false\n\n// NOTE: This function must remain pure and not use any outside variables\n// unless they are passed in as arguments. Why? Because we need to be able to\n// toString() it into a script tag to execute as early as possible in the browser\n// during SSR. Additionally, we also call it from within the router lifecycle\nexport function restoreScroll(\n routerHistory: RouterHistory,\n storageKey: string,\n key?: string,\n behavior?: ScrollToOptions['behavior'],\n shouldScrollRestoration?: boolean,\n scrollToTopSelectors?: Array<string>,\n) {\n let byKey: ScrollRestorationByKey\n\n try {\n byKey = JSON.parse(sessionStorage.getItem(storageKey) || '{}')\n } catch (error: any) {\n console.error(error)\n return\n }\n\n const resolvedKey = key || window.history.state?.key\n const elementEntries = byKey[resolvedKey]\n\n //\n ignoreScroll = true\n\n //\n ;(() => {\n // If we have a cached entry for this location state,\n // we always need to prefer that over the hash scroll.\n if (shouldScrollRestoration && elementEntries) {\n for (const elementSelector in elementEntries) {\n const entry = elementEntries[elementSelector]!\n if (elementSelector === 'window') {\n window.scrollTo({\n top: entry.scrollY,\n left: entry.scrollX,\n behavior,\n })\n } else if (elementSelector) {\n const element = document.querySelector(elementSelector)\n if (element) {\n element.scrollLeft = entry.scrollX\n element.scrollTop = entry.scrollY\n }\n }\n }\n\n return\n }\n\n // If we don't have a cached entry for the hash,\n // Which means we've never seen this location before,\n // we need to check if there is a hash in the URL.\n // If there is, we need to scroll it's ID into view.\n const hash = routerHistory.location.hash.split('#')[1]\n\n if (hash) {\n const hashScrollIntoViewOptions =\n (window.history.state || {}).__hashScrollIntoViewOptions ?? true\n\n if (hashScrollIntoViewOptions) {\n const el = document.getElementById(hash)\n if (el) {\n el.scrollIntoView(hashScrollIntoViewOptions)\n }\n }\n\n return\n }\n\n // If there is no cached entry for the hash and there is no hash in the URL,\n // we need to scroll to the top of the page for every scrollToTop element\n ;[\n 'window',\n ...(scrollToTopSelectors?.filter((d) => d !== 'window') ?? []),\n ].forEach((selector) => {\n const element =\n selector === 'window' ? window : document.querySelector(selector)\n if (element) {\n element.scrollTo({\n top: 0,\n left: 0,\n behavior,\n })\n }\n })\n })()\n\n //\n ignoreScroll = false\n}\n\nexport function setupScrollRestoration(router: AnyRouter, force?: boolean) {\n const shouldScrollRestoration =\n force ?? router.options.scrollRestoration ?? false\n\n if (shouldScrollRestoration) {\n router.isScrollRestoring = true\n }\n\n if (typeof document === 'undefined' || router.isScrollRestorationSetup) {\n return\n }\n\n router.isScrollRestorationSetup = true\n\n //\n ignoreScroll = false\n\n const getKey =\n router.options.getScrollRestorationKey || defaultGetScrollRestorationKey\n\n window.history.scrollRestoration = 'manual'\n\n // // Create a MutationObserver to monitor DOM changes\n // const mutationObserver = new MutationObserver(() => {\n // ;ignoreScroll = true\n // requestAnimationFrame(() => {\n // ;ignoreScroll = false\n\n // // Attempt to restore scroll position on each dom\n // // mutation until the user scrolls. We do this\n // // because dynamic content may come in at different\n // // ticks after the initial render and we want to\n // // keep up with that content as much as possible.\n // // As soon as the user scrolls, we no longer need\n // // to attempt router.\n // // console.log('mutation observer restoreScroll')\n // restoreScroll(\n // storageKey,\n // getKey(router.state.location),\n // router.options.scrollRestorationBehavior,\n // )\n // })\n // })\n\n // const observeDom = () => {\n // // Observe changes to the entire document\n // mutationObserver.observe(document, {\n // childList: true, // Detect added or removed child nodes\n // subtree: true, // Monitor all descendants\n // characterData: true, // Detect text content changes\n // })\n // }\n\n // const unobserveDom = () => {\n // mutationObserver.disconnect()\n // }\n\n // observeDom()\n\n const onScroll = (event: Event) => {\n // unobserveDom()\n\n if (ignoreScroll || !router.isScrollRestoring) {\n return\n }\n\n let elementSelector = ''\n\n if (event.target === document || event.target === window) {\n elementSelector = 'window'\n } else {\n const attrId = (event.target as Element).getAttribute(\n 'data-scroll-restoration-id',\n )\n\n if (attrId) {\n elementSelector = `[data-scroll-restoration-id=\"${attrId}\"]`\n } else {\n elementSelector = getCssSelector(event.target)\n }\n }\n\n const restoreKey = getKey(router.state.location)\n\n scrollRestorationCache.set((state) => {\n const keyEntry = (state[restoreKey] =\n state[restoreKey] || ({} as ScrollRestorationByElement))\n\n const elementEntry = (keyEntry[elementSelector] =\n keyEntry[elementSelector] || ({} as ScrollRestorationEntry))\n\n if (elementSelector === 'window') {\n elementEntry.scrollX = window.scrollX || 0\n elementEntry.scrollY = window.scrollY || 0\n } else if (elementSelector) {\n const element = document.querySelector(elementSelector)\n if (element) {\n elementEntry.scrollX = element.scrollLeft || 0\n elementEntry.scrollY = element.scrollTop || 0\n }\n }\n\n return state\n })\n }\n\n // Throttle the scroll event to avoid excessive updates\n if (typeof document !== 'undefined') {\n document.addEventListener('scroll', throttle(onScroll, 100), true)\n }\n\n router.subscribe('onRendered', (event) => {\n // unobserveDom()\n\n const cacheKey = getKey(event.toLocation)\n\n // If the user doesn't want to restore the scroll position,\n // we don't need to do anything.\n if (!router.resetNextScroll) {\n router.resetNextScroll = true\n return\n }\n\n restoreScroll(\n router.history,\n storageKey,\n cacheKey,\n router.options.scrollRestorationBehavior,\n router.isScrollRestoring,\n router.options.scrollToTopSelectors,\n )\n\n if (router.isScrollRestoring) {\n // Mark the location as having been seen\n scrollRestorationCache.set((state) => {\n state[cacheKey] = state[cacheKey] || ({} as ScrollRestorationByElement)\n\n return state\n })\n }\n })\n}\n\nexport function ScrollRestoration() {\n const router = useRouter()\n const getKey =\n router.options.getScrollRestorationKey || defaultGetScrollRestorationKey\n const userKey = getKey(router.latestLocation)\n const resolvedKey =\n userKey !== defaultGetScrollRestorationKey(router.latestLocation)\n ? userKey\n : null\n\n if (!router.isScrollRestoring || !router.isServer) {\n return null\n }\n\n return (\n <ScriptOnce\n children={`(${restoreScroll.toString()})(${JSON.stringify(storageKey)},${JSON.stringify(resolvedKey)}, undefined, true)`}\n log={false}\n />\n )\n}\n"],"names":["storageKey","sessionsStorage","window","sessionStorage","throttle","fn","wait","timeout","args","setTimeout","scrollRestorationCache","state","JSON","parse","getItem","set","updater","functionalUpdate","setItem","stringify","undefined","defaultGetScrollRestorationKey","location","key","href","getCssSelector","el","path","parent","parentNode","unshift","tagName","indexOf","call","children","join","toLowerCase","ignoreScroll","restoreScroll","routerHistory","behavior","shouldScrollRestoration","scrollToTopSelectors","byKey","error","console","resolvedKey","history","elementEntries","elementSelector","entry","scrollTo","top","scrollY","left","scrollX","element","document","querySelector","scrollLeft","scrollTop","hash","split","hashScrollIntoViewOptions","__hashScrollIntoViewOptions","getElementById","scrollIntoView","filter","d","forEach","selector","setupScrollRestoration","router","force","options","scrollRestoration","isScrollRestoring","isScrollRestorationSetup","getKey","getScrollRestorationKey","onScroll","event","target","attrId","getAttribute","restoreKey","keyEntry","elementEntry","addEventListener","subscribe","cacheKey","toLocation","resetNextScroll","scrollRestorationBehavior","ScrollRestoration","useRouter","userKey","latestLocation","isServer","_$createComponent","ScriptOnce","toString","log"],"mappings":";;;;AA0BO,MAAMA,aAAa;AAC1B,IAAIC,kBAAkB;AACtB,IAAI;AACFA,oBACE,OAAOC,WAAW,eAAe,OAAOA,OAAOC,mBAAmB;AACtE,QAAQ;AAAC;AACT,MAAMC,WAAWA,CAACC,IAAmCC,SAAiB;AAChEC,MAAAA;AACJ,SAAO,IAAIC,SAAqB;AAC9B,QAAI,CAACD,SAAS;AACZA,gBAAUE,WAAW,MAAM;AACzBJ,WAAG,GAAGG,IAAI;AACA,kBAAA;AAAA,SACTF,IAAI;AAAA,IAAA;AAAA,EAEX;AACF;AACaI,MAAAA,yBAAiDT,mBACzD,MAAM;AACC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B,OAAOoB,iBAAiB;AAC3BpB,aAAOoB,kBAAkB;AACzB;AAAA,IAAA;AAIApB,kBAAAA,OAAOzB,SACP/C,YACA0F,UACAlB,OAAOE,QAAQmB,2BACfrB,OAAOI,mBACPJ,OAAOE,QAAQhC,oBACjB;AAEA,QAAI8B,OAAOI,mBAAmB;AAE5BlE,6BAAuBK,IAAKJ,CAAU,UAAA;AACpCA,cAAM+E,QAAQ,IAAI/E,MAAM+E,QAAQ,KAAM,CAAC;AAEhC/E,eAAAA;AAAAA,MAAAA,CACR;AAAA,IAAA;AAAA,EACH,CACD;AACH;AAEO,SAASmF,oBAAoB;AAClC,QAAMtB,SAASuB,UAAU;AACnBjB,QAAAA,SACJN,OAAOE,QAAQK,2BAA2B1D;AACtC2E,QAAAA,UAAUlB,OAAON,OAAOyB,cAAc;AAC5C,QAAMnD,cACJkD,YAAY3E,+BAA+BmD,OAAOyB,cAAc,IAC5DD,UACA;AAEN,MAAI,CAACxB,OAAOI,qBAAqB,CAACJ,OAAO0B,UAAU;AAC1C,WAAA;AAAA,EAAA;AAGT,SAAAC,gBACGC,YAAU;AAAA,IAAA,IACTlE,WAAQ;AAAA,aAAE,IAAII,cAAc+D,SAAS,CAAC,KAAKzF,KAAKO,UAAUnB,UAAU,CAAC,IAAIY,KAAKO,UAAU2B,WAAW,CAAC;AAAA,IAAoB;AAAA,IACxHwD,KAAK;AAAA,EAAA,CAAK;AAGhB;"}
|
|
1
|
+
{"version":3,"file":"scroll-restoration.js","sources":["../../src/scroll-restoration.tsx"],"sourcesContent":["import { functionalUpdate } from '@tanstack/router-core'\nimport { useRouter } from './useRouter'\nimport { ScriptOnce } from './ScriptOnce'\n\nimport type {\n AnyRouter,\n NonNullableUpdater,\n ParsedLocation,\n} from '@tanstack/router-core'\n\nexport type ScrollRestorationEntry = { scrollX: number; scrollY: number }\n\nexport type ScrollRestorationByElement = Record<string, ScrollRestorationEntry>\n\nexport type ScrollRestorationByKey = Record<string, ScrollRestorationByElement>\n\nexport type ScrollRestorationCache = {\n state: ScrollRestorationByKey\n set: (updater: NonNullableUpdater<ScrollRestorationByKey>) => void\n}\nexport type ScrollRestorationOptions = {\n getKey?: (location: ParsedLocation) => string\n scrollBehavior?: ScrollToOptions['behavior']\n}\n\nexport const storageKey = 'tsr-scroll-restoration-v1_3'\nlet sessionsStorage = false\ntry {\n sessionsStorage =\n typeof window !== 'undefined' && typeof window.sessionStorage === 'object'\n} catch {}\nconst throttle = (fn: (...args: Array<any>) => void, wait: number) => {\n let timeout: any\n return (...args: Array<any>) => {\n if (!timeout) {\n timeout = setTimeout(() => {\n fn(...args)\n timeout = null\n }, wait)\n }\n }\n}\nexport const scrollRestorationCache: ScrollRestorationCache = sessionsStorage\n ? (() => {\n const state: ScrollRestorationByKey =\n JSON.parse(window.sessionStorage.getItem(storageKey) || 'null') || {}\n\n return {\n state,\n // This setter is simply to make sure that we set the sessionStorage right\n // after the state is updated. It doesn't necessarily need to be a functional\n // update.\n set: (updater) => (\n (scrollRestorationCache.state =\n // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition\n functionalUpdate(updater, scrollRestorationCache.state) ||\n scrollRestorationCache.state),\n window.sessionStorage.setItem(\n storageKey,\n JSON.stringify(scrollRestorationCache.state),\n )\n ),\n }\n })()\n : (undefined as any)\n/**\n * The default `getKey` function for `useScrollRestoration`.\n * It returns the `key` from the location state or the `href` of the location.\n *\n * The `location.href` is used as a fallback to support the use case where the location state is not available like the initial render.\n */\n\nexport const defaultGetScrollRestorationKey = (location: ParsedLocation) => {\n return location.state.key! || location.href\n}\n\nexport function getCssSelector(el: any): string {\n const path = []\n let parent\n while ((parent = el.parentNode)) {\n path.unshift(\n `${el.tagName}:nth-child(${([].indexOf as any).call(parent.children, el) + 1})`,\n )\n el = parent\n }\n return `${path.join(' > ')}`.toLowerCase()\n}\n\nlet ignoreScroll = false\n\n// NOTE: This function must remain pure and not use any outside variables\n// unless they are passed in as arguments. Why? Because we need to be able to\n// toString() it into a script tag to execute as early as possible in the browser\n// during SSR. Additionally, we also call it from within the router lifecycle\nexport function restoreScroll(\n storageKey: string,\n key: string | undefined,\n behavior: ScrollToOptions['behavior'] | undefined,\n shouldScrollRestoration: boolean | undefined,\n scrollToTopSelectors: Array<string> | undefined,\n) {\n let byKey: ScrollRestorationByKey\n\n try {\n byKey = JSON.parse(sessionStorage.getItem(storageKey) || '{}')\n } catch (error: any) {\n console.error(error)\n return\n }\n\n const resolvedKey = key || window.history.state?.key\n const elementEntries = byKey[resolvedKey]\n\n //\n ignoreScroll = true\n\n //\n ;(() => {\n // If we have a cached entry for this location state,\n // we always need to prefer that over the hash scroll.\n if (shouldScrollRestoration && elementEntries) {\n for (const elementSelector in elementEntries) {\n const entry = elementEntries[elementSelector]!\n if (elementSelector === 'window') {\n window.scrollTo({\n top: entry.scrollY,\n left: entry.scrollX,\n behavior,\n })\n } else if (elementSelector) {\n const element = document.querySelector(elementSelector)\n if (element) {\n element.scrollLeft = entry.scrollX\n element.scrollTop = entry.scrollY\n }\n }\n }\n\n return\n }\n\n // If we don't have a cached entry for the hash,\n // Which means we've never seen this location before,\n // we need to check if there is a hash in the URL.\n // If there is, we need to scroll it's ID into view.\n const hash = window.location.hash.split('#')[1]\n\n if (hash) {\n const hashScrollIntoViewOptions =\n (window.history.state || {}).__hashScrollIntoViewOptions ?? true\n\n if (hashScrollIntoViewOptions) {\n const el = document.getElementById(hash)\n if (el) {\n el.scrollIntoView(hashScrollIntoViewOptions)\n }\n }\n\n return\n }\n\n // If there is no cached entry for the hash and there is no hash in the URL,\n // we need to scroll to the top of the page for every scrollToTop element\n ;[\n 'window',\n ...(scrollToTopSelectors?.filter((d) => d !== 'window') ?? []),\n ].forEach((selector) => {\n const element =\n selector === 'window' ? window : document.querySelector(selector)\n if (element) {\n element.scrollTo({\n top: 0,\n left: 0,\n behavior,\n })\n }\n })\n })()\n\n //\n ignoreScroll = false\n}\n\nexport function setupScrollRestoration(router: AnyRouter, force?: boolean) {\n const shouldScrollRestoration =\n force ?? router.options.scrollRestoration ?? false\n\n if (shouldScrollRestoration) {\n router.isScrollRestoring = true\n }\n\n if (typeof document === 'undefined' || router.isScrollRestorationSetup) {\n return\n }\n\n router.isScrollRestorationSetup = true\n\n //\n ignoreScroll = false\n\n const getKey =\n router.options.getScrollRestorationKey || defaultGetScrollRestorationKey\n\n window.history.scrollRestoration = 'manual'\n\n // // Create a MutationObserver to monitor DOM changes\n // const mutationObserver = new MutationObserver(() => {\n // ;ignoreScroll = true\n // requestAnimationFrame(() => {\n // ;ignoreScroll = false\n\n // // Attempt to restore scroll position on each dom\n // // mutation until the user scrolls. We do this\n // // because dynamic content may come in at different\n // // ticks after the initial render and we want to\n // // keep up with that content as much as possible.\n // // As soon as the user scrolls, we no longer need\n // // to attempt router.\n // // console.log('mutation observer restoreScroll')\n // restoreScroll(\n // storageKey,\n // getKey(router.state.location),\n // router.options.scrollRestorationBehavior,\n // )\n // })\n // })\n\n // const observeDom = () => {\n // // Observe changes to the entire document\n // mutationObserver.observe(document, {\n // childList: true, // Detect added or removed child nodes\n // subtree: true, // Monitor all descendants\n // characterData: true, // Detect text content changes\n // })\n // }\n\n // const unobserveDom = () => {\n // mutationObserver.disconnect()\n // }\n\n // observeDom()\n\n const onScroll = (event: Event) => {\n // unobserveDom()\n\n if (ignoreScroll || !router.isScrollRestoring) {\n return\n }\n\n let elementSelector = ''\n\n if (event.target === document || event.target === window) {\n elementSelector = 'window'\n } else {\n const attrId = (event.target as Element).getAttribute(\n 'data-scroll-restoration-id',\n )\n\n if (attrId) {\n elementSelector = `[data-scroll-restoration-id=\"${attrId}\"]`\n } else {\n elementSelector = getCssSelector(event.target)\n }\n }\n\n const restoreKey = getKey(router.state.location)\n\n scrollRestorationCache.set((state) => {\n const keyEntry = (state[restoreKey] =\n state[restoreKey] || ({} as ScrollRestorationByElement))\n\n const elementEntry = (keyEntry[elementSelector] =\n keyEntry[elementSelector] || ({} as ScrollRestorationEntry))\n\n if (elementSelector === 'window') {\n elementEntry.scrollX = window.scrollX || 0\n elementEntry.scrollY = window.scrollY || 0\n } else if (elementSelector) {\n const element = document.querySelector(elementSelector)\n if (element) {\n elementEntry.scrollX = element.scrollLeft || 0\n elementEntry.scrollY = element.scrollTop || 0\n }\n }\n\n return state\n })\n }\n\n // Throttle the scroll event to avoid excessive updates\n if (typeof document !== 'undefined') {\n document.addEventListener('scroll', throttle(onScroll, 100), true)\n }\n\n router.subscribe('onRendered', (event) => {\n // unobserveDom()\n\n const cacheKey = getKey(event.toLocation)\n\n // If the user doesn't want to restore the scroll position,\n // we don't need to do anything.\n if (!router.resetNextScroll) {\n router.resetNextScroll = true\n return\n }\n\n restoreScroll(\n storageKey,\n cacheKey,\n router.options.scrollRestorationBehavior || undefined,\n router.isScrollRestoring || undefined,\n router.options.scrollToTopSelectors || undefined,\n )\n\n if (router.isScrollRestoring) {\n // Mark the location as having been seen\n scrollRestorationCache.set((state) => {\n state[cacheKey] = state[cacheKey] || ({} as ScrollRestorationByElement)\n\n return state\n })\n }\n })\n}\n\nexport function ScrollRestoration() {\n const router = useRouter()\n const getKey =\n router.options.getScrollRestorationKey || defaultGetScrollRestorationKey\n const userKey = getKey(router.latestLocation)\n const resolvedKey =\n userKey !== defaultGetScrollRestorationKey(router.latestLocation)\n ? userKey\n : null\n\n if (!router.isScrollRestoring || !router.isServer) {\n return null\n }\n\n return (\n <ScriptOnce\n children={`(${restoreScroll.toString()})(${JSON.stringify(storageKey)},${JSON.stringify(resolvedKey)}, undefined, true)`}\n log={false}\n />\n )\n}\n"],"names":["storageKey","sessionsStorage","window","sessionStorage","throttle","fn","wait","timeout","args","setTimeout","scrollRestorationCache","state","JSON","parse","getItem","set","updater","functionalUpdate","setItem","stringify","undefined","defaultGetScrollRestorationKey","location","key","href","getCssSelector","el","path","parent","parentNode","unshift","tagName","indexOf","call","children","join","toLowerCase","ignoreScroll","restoreScroll","behavior","shouldScrollRestoration","scrollToTopSelectors","byKey","error","console","resolvedKey","history","elementEntries","elementSelector","entry","scrollTo","top","scrollY","left","scrollX","element","document","querySelector","scrollLeft","scrollTop","hash","split","hashScrollIntoViewOptions","__hashScrollIntoViewOptions","getElementById","scrollIntoView","filter","d","forEach","selector","setupScrollRestoration","router","force","options","scrollRestoration","isScrollRestoring","isScrollRestorationSetup","getKey","getScrollRestorationKey","onScroll","event","target","attrId","getAttribute","restoreKey","keyEntry","elementEntry","addEventListener","subscribe","cacheKey","toLocation","resetNextScroll","scrollRestorationBehavior","ScrollRestoration","useRouter","userKey","latestLocation","isServer","_$createComponent","ScriptOnce","toString","log"],"mappings":";;;;AAyBO,MAAMA,aAAa;AAC1B,IAAIC,kBAAkB;AACtB,IAAI;AACFA,oBACE,OAAOC,WAAW,eAAe,OAAOA,OAAOC,mBAAmB;AACtE,QAAQ;AAAC;AACT,MAAMC,WAAWA,CAACC,IAAmCC,SAAiB;AAChEC,MAAAA;AACJ,SAAO,IAAIC,SAAqB;AAC9B,QAAI,CAACD,SAAS;AACZA,gBAAUE,WAAW,MAAM;AACzBJ,WAAG,GAAGG,IAAI;AACA,kBAAA;AAAA,SACTF,IAAI;AAAA,IAAA;AAAA,EAEX;AACF;AACaI,MAAAA,yBAAiDT,mBACzD,MAAM;AACCU,QAAAA,QACJC,KAAKC,MAAMX,OAAOC,eAAeW,QAAQd,UAAU,KAAK,MAAM,KAAK,CAAC;AAE/D,SAAA;AAAA,IACLW;AAAAA;AAAAA;AAAAA;AAAAA,IAIAI,KAAMC,cACHN,uBAAuBC;AAAAA,IAEtBM,iBAAiBD,SAASN,uBAAuBC,KAAK,KACtDD,uBAAuBC,OACzBT,OAAOC,eAAee,QACpBlB,YACAY,KAAKO,UAAUT,uBAAuBC,KAAK,CAC7C;AAAA,EAEJ;AACF,OACCS;AAQQC,MAAAA,iCAAiCA,CAACC,aAA6B;AACnEA,SAAAA,SAASX,MAAMY,OAAQD,SAASE;AACzC;AAEO,SAASC,eAAeC,IAAiB;AAC9C,QAAMC,OAAO,CAAE;AACXC,MAAAA;AACIA,SAAAA,SAASF,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AUlB,OAAON,OAAOyB,cAAc;AAC5C,QAAMnD,cACJkD,YAAY1E,+BAA+BkD,OAAOyB,cAAc,IAC5DD,UACA;AAEN,MAAI,CAACxB,OAAOI,qBAAqB,CAACJ,OAAO0B,UAAU;AAC1C,WAAA;AAAA,EAAA;AAGT,SAAAC,gBACGC,YAAU;AAAA,IAAA,IACTjE,WAAQ;AAAA,aAAE,IAAII,cAAc8D,SAAS,CAAC,KAAKxF,KAAKO,UAAUnB,UAAU,CAAC,IAAIY,KAAKO,UAAU0B,WAAW,CAAC;AAAA,IAAoB;AAAA,IACxHwD,KAAK;AAAA,EAAA,CAAK;AAGhB;"}
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import type { AnyRouter, NonNullableUpdater, ParsedLocation } from '@tanstack/router-core';
|
|
2
|
-
import type { RouterHistory } from '@tanstack/history';
|
|
3
2
|
export type ScrollRestorationEntry = {
|
|
4
3
|
scrollX: number;
|
|
5
4
|
scrollY: number;
|
|
@@ -24,6 +23,6 @@ export declare const scrollRestorationCache: ScrollRestorationCache;
|
|
|
24
23
|
*/
|
|
25
24
|
export declare const defaultGetScrollRestorationKey: (location: ParsedLocation) => string;
|
|
26
25
|
export declare function getCssSelector(el: any): string;
|
|
27
|
-
export declare function restoreScroll(
|
|
26
|
+
export declare function restoreScroll(storageKey: string, key: string | undefined, behavior: ScrollToOptions['behavior'] | undefined, shouldScrollRestoration: boolean | undefined, scrollToTopSelectors: Array<string> | undefined): void;
|
|
28
27
|
export declare function setupScrollRestoration(router: AnyRouter, force?: boolean): void;
|
|
29
28
|
export declare function ScrollRestoration(): import("solid-js").JSX.Element;
|
|
@@ -58,7 +58,7 @@ let ignoreScroll = false;
|
|
|
58
58
|
// unless they are passed in as arguments. Why? Because we need to be able to
|
|
59
59
|
// toString() it into a script tag to execute as early as possible in the browser
|
|
60
60
|
// during SSR. Additionally, we also call it from within the router lifecycle
|
|
61
|
-
export function restoreScroll(
|
|
61
|
+
export function restoreScroll(storageKey, key, behavior, shouldScrollRestoration, scrollToTopSelectors) {
|
|
62
62
|
let byKey;
|
|
63
63
|
try {
|
|
64
64
|
byKey = JSON.parse(sessionStorage.getItem(storageKey) || '{}');
|
|
@@ -98,7 +98,7 @@ export function restoreScroll(routerHistory, storageKey, key, behavior, shouldSc
|
|
|
98
98
|
// Which means we've never seen this location before,
|
|
99
99
|
// we need to check if there is a hash in the URL.
|
|
100
100
|
// If there is, we need to scroll it's ID into view.
|
|
101
|
-
const hash =
|
|
101
|
+
const hash = window.location.hash.split('#')[1];
|
|
102
102
|
if (hash) {
|
|
103
103
|
const hashScrollIntoViewOptions = (window.history.state || {}).__hashScrollIntoViewOptions ?? true;
|
|
104
104
|
if (hashScrollIntoViewOptions) {
|
|
@@ -225,7 +225,7 @@ export function setupScrollRestoration(router, force) {
|
|
|
225
225
|
router.resetNextScroll = true;
|
|
226
226
|
return;
|
|
227
227
|
}
|
|
228
|
-
restoreScroll(
|
|
228
|
+
restoreScroll(storageKey, cacheKey, router.options.scrollRestorationBehavior || undefined, router.isScrollRestoring || undefined, router.options.scrollToTopSelectors || undefined);
|
|
229
229
|
if (router.isScrollRestoring) {
|
|
230
230
|
// Mark the location as having been seen
|
|
231
231
|
scrollRestorationCache.set((state) => {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"scroll-restoration.jsx","sourceRoot":"","sources":["../../src/scroll-restoration.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAA;AACxD,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;AACvC,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAA;
|
|
1
|
+
{"version":3,"file":"scroll-restoration.jsx","sourceRoot":"","sources":["../../src/scroll-restoration.tsx"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,MAAM,uBAAuB,CAAA;AACxD,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;AACvC,OAAO,E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,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tanstack/solid-router",
|
|
3
|
-
"version": "1.114.
|
|
3
|
+
"version": "1.114.3",
|
|
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/
|
|
64
|
-
"@tanstack/
|
|
63
|
+
"@tanstack/router-core": "1.114.3",
|
|
64
|
+
"@tanstack/history": "1.114.3"
|
|
65
65
|
},
|
|
66
66
|
"devDependencies": {
|
|
67
67
|
"@solidjs/testing-library": "^0.8.10",
|
|
@@ -7,7 +7,6 @@ import type {
|
|
|
7
7
|
NonNullableUpdater,
|
|
8
8
|
ParsedLocation,
|
|
9
9
|
} from '@tanstack/router-core'
|
|
10
|
-
import type { RouterHistory } from '@tanstack/history'
|
|
11
10
|
|
|
12
11
|
export type ScrollRestorationEntry = { scrollX: number; scrollY: number }
|
|
13
12
|
|
|
@@ -94,12 +93,11 @@ let ignoreScroll = false
|
|
|
94
93
|
// toString() it into a script tag to execute as early as possible in the browser
|
|
95
94
|
// during SSR. Additionally, we also call it from within the router lifecycle
|
|
96
95
|
export function restoreScroll(
|
|
97
|
-
routerHistory: RouterHistory,
|
|
98
96
|
storageKey: string,
|
|
99
|
-
key
|
|
100
|
-
behavior
|
|
101
|
-
shouldScrollRestoration
|
|
102
|
-
scrollToTopSelectors
|
|
97
|
+
key: string | undefined,
|
|
98
|
+
behavior: ScrollToOptions['behavior'] | undefined,
|
|
99
|
+
shouldScrollRestoration: boolean | undefined,
|
|
100
|
+
scrollToTopSelectors: Array<string> | undefined,
|
|
103
101
|
) {
|
|
104
102
|
let byKey: ScrollRestorationByKey
|
|
105
103
|
|
|
@@ -145,7 +143,7 @@ export function restoreScroll(
|
|
|
145
143
|
// Which means we've never seen this location before,
|
|
146
144
|
// we need to check if there is a hash in the URL.
|
|
147
145
|
// If there is, we need to scroll it's ID into view.
|
|
148
|
-
const hash =
|
|
146
|
+
const hash = window.location.hash.split('#')[1]
|
|
149
147
|
|
|
150
148
|
if (hash) {
|
|
151
149
|
const hashScrollIntoViewOptions =
|
|
@@ -307,12 +305,11 @@ export function setupScrollRestoration(router: AnyRouter, force?: boolean) {
|
|
|
307
305
|
}
|
|
308
306
|
|
|
309
307
|
restoreScroll(
|
|
310
|
-
router.history,
|
|
311
308
|
storageKey,
|
|
312
309
|
cacheKey,
|
|
313
|
-
router.options.scrollRestorationBehavior,
|
|
314
|
-
router.isScrollRestoring,
|
|
315
|
-
router.options.scrollToTopSelectors,
|
|
310
|
+
router.options.scrollRestorationBehavior || undefined,
|
|
311
|
+
router.isScrollRestoring || undefined,
|
|
312
|
+
router.options.scrollToTopSelectors || undefined,
|
|
316
313
|
)
|
|
317
314
|
|
|
318
315
|
if (router.isScrollRestoring) {
|