@tanstack/react-router 0.0.1-beta.230 → 0.0.1-beta.231
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/cjs/Matches.js +4 -1
- package/build/cjs/Matches.js.map +1 -1
- package/build/cjs/RouterProvider.js +2 -1
- package/build/cjs/RouterProvider.js.map +1 -1
- package/build/cjs/awaited.js +1 -1
- package/build/cjs/awaited.js.map +1 -1
- package/build/cjs/index.js +1 -0
- package/build/cjs/index.js.map +1 -1
- package/build/cjs/router.js +3 -1
- package/build/cjs/router.js.map +1 -1
- package/build/cjs/scroll-restoration.js +52 -29
- package/build/cjs/scroll-restoration.js.map +1 -1
- package/build/esm/index.js +62 -34
- package/build/esm/index.js.map +1 -1
- package/build/stats-html.html +1 -1
- package/build/stats-react.json +289 -289
- package/build/types/scroll-restoration.d.ts +12 -0
- package/build/umd/index.development.js +62 -33
- package/build/umd/index.development.js.map +1 -1
- package/build/umd/index.production.js +1 -1
- package/build/umd/index.production.js.map +1 -1
- package/package.json +2 -2
- package/src/Matches.tsx +3 -1
- package/src/RouterProvider.tsx +3 -1
- package/src/awaited.tsx +1 -1
- package/src/router.ts +5 -1
- package/src/scroll-restoration.tsx +81 -43
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"scroll-restoration.js","sources":["../../src/scroll-restoration.tsx"],"sourcesContent":["import * as React from 'react'\n\nconst useLayoutEffect =\n typeof window !== 'undefined' ? React.useLayoutEffect : React.useEffect\n\nimport { ParsedLocation } from './location'\nimport { useRouter } from './RouterProvider'\nimport { NonNullableUpdater, functionalUpdate } from './utils'\n\nconst windowKey = 'window'\nconst delimiter = '___'\n\nlet weakScrolledElements = new WeakSet<any>()\n\ntype CacheValue = Record<string, { scrollX: number; scrollY: number }>\ntype CacheState = {\n cached: CacheValue\n next: CacheValue\n}\n\ntype Cache = {\n state: CacheState\n set: (updater: NonNullableUpdater<CacheState>) => void\n}\n\nlet cache: Cache\n\nconst sessionsStorage = typeof window !== 'undefined' && window.sessionStorage\n\nexport type ScrollRestorationOptions = {\n getKey?: (location: ParsedLocation) => string\n}\n\nconst defaultGetKey = (location: ParsedLocation) => location.state.key!\n\nexport function useScrollRestoration(options?: ScrollRestorationOptions) {\n const router = useRouter()\n\n useLayoutEffect(() => {\n const getKey = options?.getKey || defaultGetKey\n\n if (sessionsStorage) {\n if (!cache) {\n cache = (() => {\n const storageKey = 'tsr-scroll-restoration-v2'\n\n const state: CacheState = JSON.parse(\n window.sessionStorage.getItem(storageKey) || 'null',\n ) || { cached: {}, next: {} }\n\n return {\n state,\n set: (updater) => {\n cache.state = functionalUpdate(updater, cache.state)\n window.sessionStorage.setItem(\n storageKey,\n JSON.stringify(cache.state),\n )\n },\n }\n })()\n }\n }\n\n const { history } = window\n if (history.scrollRestoration) {\n history.scrollRestoration = 'manual'\n }\n\n const onScroll = (event: Event) => {\n if (weakScrolledElements.has(event.target)) return\n weakScrolledElements.add(event.target)\n\n const elementSelector =\n event.target === document || event.target === window\n ? windowKey\n : getCssSelector(event.target)\n\n if (!cache.state.next[elementSelector]) {\n cache.set((c) => ({\n ...c,\n next: {\n ...c.next,\n [elementSelector]: {\n scrollX: NaN,\n scrollY: NaN,\n },\n },\n }))\n }\n }\n\n const getCssSelector = (el: any): string => {\n let path = [],\n parent\n while ((parent = el.parentNode)) {\n path.unshift(\n `${el.tagName}:nth-child(${\n ([].indexOf as any).call(parent.children, el) + 1\n })`,\n )\n el = parent\n }\n return `${path.join(' > ')}`.toLowerCase()\n }\n\n if (typeof document !== 'undefined') {\n document.addEventListener('scroll', onScroll, true)\n }\n\n const unsubOnBeforeLoad = router.subscribe('onBeforeLoad', (event) => {\n if (event.pathChanged) {\n const restoreKey = getKey(event.fromLocation)\n for (const elementSelector in cache.state.next) {\n const entry = cache.state.next[elementSelector]!\n if (elementSelector === windowKey) {\n entry.scrollX = window.scrollX || 0\n entry.scrollY = window.scrollY || 0\n } else if (elementSelector) {\n const element = document.querySelector(elementSelector)\n entry.scrollX = element?.scrollLeft || 0\n entry.scrollY = element?.scrollTop || 0\n }\n\n cache.set((c) => {\n const next = { ...c.next }\n delete next[elementSelector]\n\n return {\n ...c,\n next,\n cached: {\n ...c.cached,\n [[restoreKey, elementSelector].join(delimiter)]: entry,\n },\n }\n })\n }\n }\n })\n\n const unsubOnResolved = router.subscribe('onResolved', (event) => {\n if (event.pathChanged) {\n if (!router.resetNextScroll) {\n return\n }\n\n router.resetNextScroll = true\n\n const getKey = options?.getKey || defaultGetKey\n\n const restoreKey = getKey(event.toLocation)\n let windowRestored = false\n\n for (const cacheKey in cache.state.cached) {\n const entry = cache.state.cached[cacheKey]!\n const [key, elementSelector] = cacheKey.split(delimiter)\n if (key === restoreKey) {\n if (elementSelector === windowKey) {\n windowRestored = true\n window.scrollTo(entry.scrollX, entry.scrollY)\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\n if (!windowRestored) {\n window.scrollTo(0, 0)\n }\n\n cache.set((c) => ({ ...c, next: {} }))\n weakScrolledElements = new WeakSet<any>()\n }\n })\n\n return () => {\n document.removeEventListener('scroll', onScroll)\n unsubOnBeforeLoad()\n unsubOnResolved()\n }\n }, [])\n}\n\nexport function ScrollRestoration(props: ScrollRestorationOptions) {\n useScrollRestoration(props)\n return null\n}\n"],"names":["useLayoutEffect","window","React","useEffect","windowKey","delimiter","weakScrolledElements","WeakSet","cache","sessionsStorage","sessionStorage","defaultGetKey","location","state","key","useScrollRestoration","options","router","useRouter","getKey","storageKey","JSON","parse","getItem","cached","next","set","updater","functionalUpdate","setItem","stringify","history","scrollRestoration","onScroll","event","has","target","add","elementSelector","document","getCssSelector","c","scrollX","NaN","scrollY","el","path","parent","parentNode","unshift","tagName","indexOf","call","children","join","toLowerCase","addEventListener","unsubOnBeforeLoad","subscribe","pathChanged","restoreKey","fromLocation","entry","element","querySelector","scrollLeft","scrollTop","unsubOnResolved","resetNextScroll","toLocation","windowRestored","cacheKey","split","scrollTo","removeEventListener","ScrollRestoration","props"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEA,MAAMA,eAAe,GACnB,OAAOC,MAAM,KAAK,WAAW,GAAGC,gBAAK,CAACF,eAAe,GAAGE,gBAAK,CAACC,SAAS,CAAA;AAMzE,MAAMC,SAAS,GAAG,QAAQ,CAAA;AAC1B,MAAMC,SAAS,GAAG,KAAK,CAAA;AAEvB,IAAIC,oBAAoB,GAAG,IAAIC,OAAO,EAAO,CAAA;AAa7C,IAAIC,KAAY,CAAA;AAEhB,MAAMC,eAAe,GAAG,OAAOR,MAAM,KAAK,WAAW,IAAIA,MAAM,CAACS,cAAc,CAAA;AAM9E,MAAMC,aAAa,GAAIC,QAAwB,IAAKA,QAAQ,CAACC,KAAK,CAACC,GAAI,CAAA;AAEhE,SAASC,oBAAoBA,CAACC,OAAkC,EAAE;AACvE,EAAA,MAAMC,MAAM,GAAGC,wBAAS,EAAE,CAAA;AAE1BlB,EAAAA,eAAe,CAAC,MAAM;AACpB,IAAA,MAAMmB,MAAM,GAAGH,OAAO,EAAEG,MAAM,IAAIR,aAAa,CAAA;AAE/C,IAAA,IAAIF,eAAe,EAAE;MACnB,IAAI,CAACD,KAAK,EAAE;QACVA,KAAK,GAAG,CAAC,MAAM;UACb,MAAMY,UAAU,GAAG,2BAA2B,CAAA;AAE9C,UAAA,MAAMP,KAAiB,GAAGQ,IAAI,CAACC,KAAK,CAClCrB,MAAM,CAACS,cAAc,CAACa,OAAO,CAACH,UAAU,CAAC,IAAI,MAC/C,CAAC,IAAI;YAAEI,MAAM,EAAE,EAAE;AAAEC,YAAAA,IAAI,EAAE,EAAC;WAAG,CAAA;UAE7B,OAAO;YACLZ,KAAK;YACLa,GAAG,EAAGC,OAAO,IAAK;cAChBnB,KAAK,CAACK,KAAK,GAAGe,sBAAgB,CAACD,OAAO,EAAEnB,KAAK,CAACK,KAAK,CA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AE;EACjE7D,oBAAoB,CAAC6D,KAAK,CAAC,CAAA;AAC3B,EAAA,OAAO,IAAI,CAAA;AACb;;;;;"}
|
|
1
|
+
{"version":3,"file":"scroll-restoration.js","sources":["../../src/scroll-restoration.tsx"],"sourcesContent":["import * as React from 'react'\n\nconst useLayoutEffect =\n typeof window !== 'undefined' ? React.useLayoutEffect : React.useEffect\n\nimport { ParsedLocation } from './location'\nimport { useRouter } from './RouterProvider'\nimport { NonNullableUpdater, functionalUpdate } from './utils'\n\nconst windowKey = 'window'\nconst delimiter = '___'\n\nlet weakScrolledElements = new WeakSet<any>()\n\ntype CacheValue = Record<string, { scrollX: number; scrollY: number }>\ntype CacheState = {\n cached: CacheValue\n next: CacheValue\n}\n\ntype Cache = {\n state: CacheState\n set: (updater: NonNullableUpdater<CacheState>) => void\n}\n\nconst sessionsStorage = typeof window !== 'undefined' && window.sessionStorage\n\nlet cache: Cache = sessionsStorage\n ? (() => {\n const storageKey = 'tsr-scroll-restoration-v2'\n\n const state: CacheState = JSON.parse(\n window.sessionStorage.getItem(storageKey) || 'null',\n ) || { cached: {}, next: {} }\n\n return {\n state,\n set: (updater) => {\n cache.state = functionalUpdate(updater, cache.state)\n window.sessionStorage.setItem(storageKey, JSON.stringify(cache.state))\n },\n }\n })()\n : (undefined as any)\n\nexport type ScrollRestorationOptions = {\n getKey?: (location: ParsedLocation) => string\n}\n\nconst defaultGetKey = (location: ParsedLocation) => location.state.key!\n\nexport function useScrollRestoration(options?: ScrollRestorationOptions) {\n const router = useRouter()\n\n useLayoutEffect(() => {\n const getKey = options?.getKey || defaultGetKey\n\n const { history } = window\n if (history.scrollRestoration) {\n history.scrollRestoration = 'manual'\n }\n\n const onScroll = (event: Event) => {\n if (weakScrolledElements.has(event.target)) return\n weakScrolledElements.add(event.target)\n\n let elementSelector = ''\n\n if (event.target === document || event.target === window) {\n elementSelector = windowKey\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 if (!cache.state.next[elementSelector]) {\n cache.set((c) => ({\n ...c,\n next: {\n ...c.next,\n [elementSelector]: {\n scrollX: NaN,\n scrollY: NaN,\n },\n },\n }))\n }\n }\n\n if (typeof document !== 'undefined') {\n document.addEventListener('scroll', onScroll, true)\n }\n\n const unsubOnBeforeLoad = router.subscribe('onBeforeLoad', (event) => {\n if (event.pathChanged) {\n const restoreKey = getKey(event.fromLocation)\n for (const elementSelector in cache.state.next) {\n const entry = cache.state.next[elementSelector]!\n if (elementSelector === windowKey) {\n entry.scrollX = window.scrollX || 0\n entry.scrollY = window.scrollY || 0\n } else if (elementSelector) {\n const element = document.querySelector(elementSelector)\n entry.scrollX = element?.scrollLeft || 0\n entry.scrollY = element?.scrollTop || 0\n }\n\n cache.set((c) => {\n const next = { ...c.next }\n delete next[elementSelector]\n\n return {\n ...c,\n next,\n cached: {\n ...c.cached,\n [[restoreKey, elementSelector].join(delimiter)]: entry,\n },\n }\n })\n }\n }\n })\n\n const unsubOnResolved = router.subscribe('onResolved', (event) => {\n if (event.pathChanged) {\n if (!router.resetNextScroll) {\n return\n }\n\n router.resetNextScroll = true\n\n const getKey = options?.getKey || defaultGetKey\n\n const restoreKey = getKey(event.toLocation)\n let windowRestored = false\n\n for (const cacheKey in cache.state.cached) {\n const entry = cache.state.cached[cacheKey]!\n const [key, elementSelector] = cacheKey.split(delimiter)\n if (key === restoreKey) {\n if (elementSelector === windowKey) {\n windowRestored = true\n window.scrollTo(entry.scrollX, entry.scrollY)\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\n if (!windowRestored) {\n window.scrollTo(0, 0)\n }\n\n cache.set((c) => ({ ...c, next: {} }))\n weakScrolledElements = new WeakSet<any>()\n }\n })\n\n return () => {\n document.removeEventListener('scroll', onScroll)\n unsubOnBeforeLoad()\n unsubOnResolved()\n }\n }, [])\n}\n\nexport function ScrollRestoration(props: ScrollRestorationOptions) {\n useScrollRestoration(props)\n return null\n}\n\nexport function useElementScrollRestoration(\n options: (\n | {\n id: string\n getElement?: () => Element | undefined | null\n }\n | {\n id?: string\n getElement: () => Element | undefined | null\n }\n ) & {\n getKey?: (location: ParsedLocation) => string\n },\n) {\n const router = useRouter()\n const getKey = options?.getKey || defaultGetKey\n\n let elementSelector = ''\n\n if (options.id) {\n elementSelector = `[data-scroll-restoration-id=\"${options.id}\"]`\n } else {\n const element = options.getElement?.()\n if (!element) {\n return\n }\n elementSelector = getCssSelector(element)\n }\n\n const restoreKey = getKey(router.latestLocation)\n const cacheKey = [restoreKey, elementSelector].join(delimiter)\n return cache.state.cached[cacheKey]\n}\n\nfunction getCssSelector(el: any): string {\n let path = [],\n parent\n while ((parent = el.parentNode)) {\n path.unshift(\n `${el.tagName}:nth-child(${\n ([].indexOf as any).call(parent.children, el) + 1\n })`,\n )\n el = parent\n }\n return `${path.join(' > ')}`.toLowerCase()\n}\n"],"names":["useLayoutEffect","window","React","useEffect","windowKey","delimiter","weakScrolledElements","WeakSet","sessionsStorage","sessionStorage","cache","storageKey","state","JSON","parse","getItem","cached","next","set","updater","functionalUpdate","setItem","stringify","undefined","defaultGetKey","location","key","useScrollRestoration","options","router","useRouter","getKey","history","scrollRestoration","onScroll","event","has","target","add","elementSelector","document","attrId","getAttribute","getCssSelector","c","scrollX","NaN","scrollY","addEventListener","unsubOnBeforeLoad","subscribe","pathChanged","restoreKey","fromLocation","entry","element","querySelector","scrollLeft","scrollTop","join","unsubOnResolved","resetNextScroll","toLocation","windowRestored","cacheKey","split","scrollTo","removeEventListener","ScrollRestoration","props","useElementScrollRestoration","id","getElement","latestLocation","el","path","parent","parentNode","unshift","tagName","indexOf","call","children","toLowerCase"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAEA,MAAMA,eAAe,GACnB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AA;AAClE,GAAC,MAAM;AACL,IAAA,MAAMhB,OAAO,GAAG3B,OAAO,CAAC4C,UAAU,IAAI,CAAA;IACtC,IAAI,CAACjB,OAAO,EAAE;AACZ,MAAA,OAAA;AACF,KAAA;AACAhB,IAAAA,eAAe,GAAGI,cAAc,CAACY,OAAO,CAAC,CAAA;AAC3C,GAAA;AAEA,EAAA,MAAMH,UAAU,GAAGrB,MAAM,CAACF,MAAM,CAAC4C,cAAc,CAAC,CAAA;EAChD,MAAMT,QAAQ,GAAG,CAACZ,UAAU,EAAEb,eAAe,CAAC,CAACoB,IAAI,CAACtD,SAAS,CAAC,CAAA;AAC9D,EAAA,OAAOK,KAAK,CAACE,KAAK,CAACI,MAAM,CAACgD,QAAQ,CAAC,CAAA;AACrC,CAAA;AAEA,SAASrB,cAAcA,CAAC+B,EAAO,EAAU;EACvC,IAAIC,IAAI,GAAG,EAAE;IACXC,MAAM,CAAA;AACR,EAAA,OAAQA,MAAM,GAAGF,EAAE,CAACG,UAAU,EAAG;IAC/BF,IAAI,CAACG,OAAO,CACT,CAAA,EAAEJ,EAAE,CAACK,OAAQ,CACX,WAAA,EAAA,EAAE,CAACC,OAAO,CAASC,IAAI,CAACL,MAAM,CAACM,QAAQ,EAAER,EAAE,CAAC,GAAG,CACjD,CAAA,CAAA,CACH,CAAC,CAAA;AACDA,IAAAA,EAAE,GAAGE,MAAM,CAAA;AACb,GAAA;EACA,OAAQ,CAAA,EAAED,IAAI,CAAChB,IAAI,CAAC,KAAK,CAAE,CAAC,CAAA,CAACwB,WAAW,EAAE,CAAA;AAC5C;;;;;;"}
|
package/build/esm/index.js
CHANGED
|
@@ -650,7 +650,10 @@ function Match({
|
|
|
650
650
|
const match = matches[0];
|
|
651
651
|
const routeId = match?.routeId;
|
|
652
652
|
const route = routesById[routeId];
|
|
653
|
-
const
|
|
653
|
+
const router = useRouter();
|
|
654
|
+
const locationKey = router.latestLocation.state?.key;
|
|
655
|
+
// const locationKey = useRouterState().location.state?.key
|
|
656
|
+
|
|
654
657
|
const PendingComponent = route.options.pendingComponent ?? options.defaultPendingComponent;
|
|
655
658
|
const pendingElement = PendingComponent ? /*#__PURE__*/React.createElement(PendingComponent, {
|
|
656
659
|
useMatch: route.useMatch,
|
|
@@ -821,9 +824,10 @@ function RouterProvider({
|
|
|
821
824
|
});
|
|
822
825
|
const [preState, setState] = React.useState(() => router.state);
|
|
823
826
|
const [isTransitioning, startReactTransition] = React.useTransition();
|
|
827
|
+
const isAnyTransitioning = isTransitioning || preState.matches.some(d => d.status === 'pending');
|
|
824
828
|
const state = React.useMemo(() => ({
|
|
825
829
|
...preState,
|
|
826
|
-
status:
|
|
830
|
+
status: isAnyTransitioning ? 'pending' : 'idle',
|
|
827
831
|
location: isTransitioning ? router.latestLocation : preState.location,
|
|
828
832
|
pendingMatches: router.pendingMatches
|
|
829
833
|
}), [preState, isTransitioning]);
|
|
@@ -938,7 +942,7 @@ function useAwaited({
|
|
|
938
942
|
promise.__deferredState = state;
|
|
939
943
|
}
|
|
940
944
|
if (state.status === 'pending') {
|
|
941
|
-
throw promise;
|
|
945
|
+
throw new Promise(r => setTimeout(r, 1)).then(() => promise);
|
|
942
946
|
}
|
|
943
947
|
if (state.status === 'error') {
|
|
944
948
|
throw state.error;
|
|
@@ -1904,6 +1908,7 @@ class Router {
|
|
|
1904
1908
|
// forcefully show the pending component
|
|
1905
1909
|
if (pendingPromise) {
|
|
1906
1910
|
pendingPromise.then(() => {
|
|
1911
|
+
if (latestPromise = checkLatest()) return;
|
|
1907
1912
|
didShowPending = true;
|
|
1908
1913
|
matches[index] = match = {
|
|
1909
1914
|
...match,
|
|
@@ -1923,6 +1928,7 @@ class Router {
|
|
|
1923
1928
|
if (didShowPending && pendingMinMs) {
|
|
1924
1929
|
await new Promise(r => setTimeout(r, pendingMinMs));
|
|
1925
1930
|
}
|
|
1931
|
+
if (latestPromise = checkLatest()) return await latestPromise;
|
|
1926
1932
|
matches[index] = match = {
|
|
1927
1933
|
...match,
|
|
1928
1934
|
error: undefined,
|
|
@@ -1991,7 +1997,7 @@ class Router {
|
|
|
1991
1997
|
// Ingest the new matches
|
|
1992
1998
|
this.setState(s => ({
|
|
1993
1999
|
...s,
|
|
1994
|
-
status: 'pending',
|
|
2000
|
+
// status: 'pending',
|
|
1995
2001
|
location: next,
|
|
1996
2002
|
matches
|
|
1997
2003
|
}));
|
|
@@ -2302,31 +2308,26 @@ const useLayoutEffect = typeof window !== 'undefined' ? React.useLayoutEffect :
|
|
|
2302
2308
|
const windowKey = 'window';
|
|
2303
2309
|
const delimiter = '___';
|
|
2304
2310
|
let weakScrolledElements = new WeakSet();
|
|
2305
|
-
let cache;
|
|
2306
2311
|
const sessionsStorage = typeof window !== 'undefined' && window.sessionStorage;
|
|
2312
|
+
let cache = sessionsStorage ? (() => {
|
|
2313
|
+
const storageKey = 'tsr-scroll-restoration-v2';
|
|
2314
|
+
const state = JSON.parse(window.sessionStorage.getItem(storageKey) || 'null') || {
|
|
2315
|
+
cached: {},
|
|
2316
|
+
next: {}
|
|
2317
|
+
};
|
|
2318
|
+
return {
|
|
2319
|
+
state,
|
|
2320
|
+
set: updater => {
|
|
2321
|
+
cache.state = functionalUpdate(updater, cache.state);
|
|
2322
|
+
window.sessionStorage.setItem(storageKey, JSON.stringify(cache.state));
|
|
2323
|
+
}
|
|
2324
|
+
};
|
|
2325
|
+
})() : undefined;
|
|
2307
2326
|
const defaultGetKey = location => location.state.key;
|
|
2308
2327
|
function useScrollRestoration(options) {
|
|
2309
2328
|
const router = useRouter();
|
|
2310
2329
|
useLayoutEffect(() => {
|
|
2311
2330
|
const getKey = options?.getKey || defaultGetKey;
|
|
2312
|
-
if (sessionsStorage) {
|
|
2313
|
-
if (!cache) {
|
|
2314
|
-
cache = (() => {
|
|
2315
|
-
const storageKey = 'tsr-scroll-restoration-v2';
|
|
2316
|
-
const state = JSON.parse(window.sessionStorage.getItem(storageKey) || 'null') || {
|
|
2317
|
-
cached: {},
|
|
2318
|
-
next: {}
|
|
2319
|
-
};
|
|
2320
|
-
return {
|
|
2321
|
-
state,
|
|
2322
|
-
set: updater => {
|
|
2323
|
-
cache.state = functionalUpdate(updater, cache.state);
|
|
2324
|
-
window.sessionStorage.setItem(storageKey, JSON.stringify(cache.state));
|
|
2325
|
-
}
|
|
2326
|
-
};
|
|
2327
|
-
})();
|
|
2328
|
-
}
|
|
2329
|
-
}
|
|
2330
2331
|
const {
|
|
2331
2332
|
history
|
|
2332
2333
|
} = window;
|
|
@@ -2336,7 +2337,17 @@ function useScrollRestoration(options) {
|
|
|
2336
2337
|
const onScroll = event => {
|
|
2337
2338
|
if (weakScrolledElements.has(event.target)) return;
|
|
2338
2339
|
weakScrolledElements.add(event.target);
|
|
2339
|
-
|
|
2340
|
+
let elementSelector = '';
|
|
2341
|
+
if (event.target === document || event.target === window) {
|
|
2342
|
+
elementSelector = windowKey;
|
|
2343
|
+
} else {
|
|
2344
|
+
const attrId = event.target.getAttribute('data-scroll-restoration-id');
|
|
2345
|
+
if (attrId) {
|
|
2346
|
+
elementSelector = `[data-scroll-restoration-id="${attrId}"]`;
|
|
2347
|
+
} else {
|
|
2348
|
+
elementSelector = getCssSelector(event.target);
|
|
2349
|
+
}
|
|
2350
|
+
}
|
|
2340
2351
|
if (!cache.state.next[elementSelector]) {
|
|
2341
2352
|
cache.set(c => ({
|
|
2342
2353
|
...c,
|
|
@@ -2350,15 +2361,6 @@ function useScrollRestoration(options) {
|
|
|
2350
2361
|
}));
|
|
2351
2362
|
}
|
|
2352
2363
|
};
|
|
2353
|
-
const getCssSelector = el => {
|
|
2354
|
-
let path = [],
|
|
2355
|
-
parent;
|
|
2356
|
-
while (parent = el.parentNode) {
|
|
2357
|
-
path.unshift(`${el.tagName}:nth-child(${[].indexOf.call(parent.children, el) + 1})`);
|
|
2358
|
-
el = parent;
|
|
2359
|
-
}
|
|
2360
|
-
return `${path.join(' > ')}`.toLowerCase();
|
|
2361
|
-
};
|
|
2362
2364
|
if (typeof document !== 'undefined') {
|
|
2363
2365
|
document.addEventListener('scroll', onScroll, true);
|
|
2364
2366
|
}
|
|
@@ -2438,6 +2440,32 @@ function ScrollRestoration(props) {
|
|
|
2438
2440
|
useScrollRestoration(props);
|
|
2439
2441
|
return null;
|
|
2440
2442
|
}
|
|
2443
|
+
function useElementScrollRestoration(options) {
|
|
2444
|
+
const router = useRouter();
|
|
2445
|
+
const getKey = options?.getKey || defaultGetKey;
|
|
2446
|
+
let elementSelector = '';
|
|
2447
|
+
if (options.id) {
|
|
2448
|
+
elementSelector = `[data-scroll-restoration-id="${options.id}"]`;
|
|
2449
|
+
} else {
|
|
2450
|
+
const element = options.getElement?.();
|
|
2451
|
+
if (!element) {
|
|
2452
|
+
return;
|
|
2453
|
+
}
|
|
2454
|
+
elementSelector = getCssSelector(element);
|
|
2455
|
+
}
|
|
2456
|
+
const restoreKey = getKey(router.latestLocation);
|
|
2457
|
+
const cacheKey = [restoreKey, elementSelector].join(delimiter);
|
|
2458
|
+
return cache.state.cached[cacheKey];
|
|
2459
|
+
}
|
|
2460
|
+
function getCssSelector(el) {
|
|
2461
|
+
let path = [],
|
|
2462
|
+
parent;
|
|
2463
|
+
while (parent = el.parentNode) {
|
|
2464
|
+
path.unshift(`${el.tagName}:nth-child(${[].indexOf.call(parent.children, el) + 1})`);
|
|
2465
|
+
el = parent;
|
|
2466
|
+
}
|
|
2467
|
+
return `${path.join(' > ')}`.toLowerCase();
|
|
2468
|
+
}
|
|
2441
2469
|
|
|
2442
2470
|
function useBlocker(message, condition = true) {
|
|
2443
2471
|
const {
|
|
@@ -2498,5 +2526,5 @@ function Navigate(props) {
|
|
|
2498
2526
|
return null;
|
|
2499
2527
|
}
|
|
2500
2528
|
|
|
2501
|
-
export { Await, Block, CatchBoundary, CatchBoundaryImpl, ErrorComponent, FileRoute, Link, Match, MatchRoute, Matches, Navigate, Outlet, PathParamError, RootRoute, Route, Router, RouterProvider, ScrollRestoration, SearchParamError, cleanPath, componentTypes, createRouteMask, decode, deepEqual, defaultParseSearch, defaultStringifySearch, defer, encode, escapeJSON, functionalUpdate, getInitialRouterState, getRouteMatch, interpolatePath, isDehydratedDeferred, isPlainObject, isRedirect, isServer, joinPaths, last, lazyFn, lazyRouteComponent, matchByPath, matchPathname, matchesContext, parsePathname, parseSearchWith, pick, redirect, replaceEqualDeep, resolvePath, rootRouteId, rootRouteWithContext, routerContext, shallow, stringifySearchWith, trimPath, trimPathLeft, trimPathRight, typedNavigate, useAwaited, useBlocker, useLayoutEffect$1 as useLayoutEffect, useLinkProps, useLoaderData, useMatch, useMatchRoute, useMatches, useNavigate, useParams, useRouteContext, useRouter, useRouterState, useScrollRestoration, useSearch, useStableCallback };
|
|
2529
|
+
export { Await, Block, CatchBoundary, CatchBoundaryImpl, ErrorComponent, FileRoute, Link, Match, MatchRoute, Matches, Navigate, Outlet, PathParamError, RootRoute, Route, Router, RouterProvider, ScrollRestoration, SearchParamError, cleanPath, componentTypes, createRouteMask, decode, deepEqual, defaultParseSearch, defaultStringifySearch, defer, encode, escapeJSON, functionalUpdate, getInitialRouterState, getRouteMatch, interpolatePath, isDehydratedDeferred, isPlainObject, isRedirect, isServer, joinPaths, last, lazyFn, lazyRouteComponent, matchByPath, matchPathname, matchesContext, parsePathname, parseSearchWith, pick, redirect, replaceEqualDeep, resolvePath, rootRouteId, rootRouteWithContext, routerContext, shallow, stringifySearchWith, trimPath, trimPathLeft, trimPathRight, typedNavigate, useAwaited, useBlocker, useElementScrollRestoration, useLayoutEffect$1 as useLayoutEffect, useLinkProps, useLoaderData, useMatch, useMatchRoute, useMatches, useNavigate, useParams, useRouteContext, useRouter, useRouterState, useScrollRestoration, useSearch, useStableCallback };
|
|
2502
2530
|
//# sourceMappingURL=index.js.map
|