@tanstack/router-core 0.0.1-beta.163 → 0.0.1-beta.165
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/index.js +3 -0
- package/build/cjs/index.js.map +1 -1
- package/build/cjs/router.js.map +1 -1
- package/build/cjs/scroll-restoration.js +136 -0
- package/build/cjs/scroll-restoration.js.map +1 -0
- package/build/esm/index.js +120 -1
- package/build/esm/index.js.map +1 -1
- package/build/stats-html.html +1 -1
- package/build/stats-react.json +139 -114
- package/build/types/index.d.ts +10 -4
- package/build/umd/index.development.js +121 -0
- package/build/umd/index.development.js.map +1 -1
- package/build/umd/index.production.js +2 -2
- package/build/umd/index.production.js.map +1 -1
- package/package.json +2 -2
- package/src/index.ts +1 -0
- package/src/router.ts +3 -3
- package/src/scroll-restoration.ts +175 -0
|
@@ -1911,6 +1911,125 @@
|
|
|
1911
1911
|
};
|
|
1912
1912
|
}
|
|
1913
1913
|
|
|
1914
|
+
const windowKey = 'window';
|
|
1915
|
+
const delimiter = '___';
|
|
1916
|
+
let weakScrolledElementsByRestoreKey = {};
|
|
1917
|
+
let cache;
|
|
1918
|
+
let pathDidChange = false;
|
|
1919
|
+
const sessionsStorage = typeof window !== 'undefined' && window.sessionStorage;
|
|
1920
|
+
const defaultGetKey = location => location.key;
|
|
1921
|
+
function watchScrollPositions(router, opts) {
|
|
1922
|
+
const getKey = opts?.getKey || defaultGetKey;
|
|
1923
|
+
if (sessionsStorage) {
|
|
1924
|
+
if (!cache) {
|
|
1925
|
+
cache = (() => {
|
|
1926
|
+
const storageKey = 'tsr-scroll-restoration-v1';
|
|
1927
|
+
const current = JSON.parse(window.sessionStorage.getItem(storageKey) || '{}');
|
|
1928
|
+
return {
|
|
1929
|
+
current,
|
|
1930
|
+
set: (key, value) => {
|
|
1931
|
+
current[key] = value;
|
|
1932
|
+
window.sessionStorage.setItem(storageKey, JSON.stringify(cache));
|
|
1933
|
+
}
|
|
1934
|
+
};
|
|
1935
|
+
})();
|
|
1936
|
+
}
|
|
1937
|
+
}
|
|
1938
|
+
const {
|
|
1939
|
+
history
|
|
1940
|
+
} = window;
|
|
1941
|
+
if (history.scrollRestoration) {
|
|
1942
|
+
history.scrollRestoration = 'manual';
|
|
1943
|
+
}
|
|
1944
|
+
const onScroll = event => {
|
|
1945
|
+
const restoreKey = getKey(router.state.resolvedLocation);
|
|
1946
|
+
if (!weakScrolledElementsByRestoreKey[restoreKey]) {
|
|
1947
|
+
weakScrolledElementsByRestoreKey[restoreKey] = new WeakSet();
|
|
1948
|
+
}
|
|
1949
|
+
const set = weakScrolledElementsByRestoreKey[restoreKey];
|
|
1950
|
+
if (set.has(event.target)) return;
|
|
1951
|
+
set.add(event.target);
|
|
1952
|
+
const cacheKey = [restoreKey, event.target === document || event.target === window ? windowKey : getCssSelector(event.target)].join(delimiter);
|
|
1953
|
+
if (!cache.current[cacheKey]) {
|
|
1954
|
+
cache.set(cacheKey, {
|
|
1955
|
+
scrollX: NaN,
|
|
1956
|
+
scrollY: NaN
|
|
1957
|
+
});
|
|
1958
|
+
}
|
|
1959
|
+
};
|
|
1960
|
+
const getCssSelector = el => {
|
|
1961
|
+
let path = [],
|
|
1962
|
+
parent;
|
|
1963
|
+
while (parent = el.parentNode) {
|
|
1964
|
+
path.unshift(`${el.tagName}:nth-child(${[].indexOf.call(parent.children, el) + 1})`);
|
|
1965
|
+
el = parent;
|
|
1966
|
+
}
|
|
1967
|
+
return `${path.join(' > ')}`.toLowerCase();
|
|
1968
|
+
};
|
|
1969
|
+
const onPathWillChange = from => {
|
|
1970
|
+
const restoreKey = getKey(from);
|
|
1971
|
+
for (const cacheKey in cache.current) {
|
|
1972
|
+
const entry = cache.current[cacheKey];
|
|
1973
|
+
const [key, elementSelector] = cacheKey.split(delimiter);
|
|
1974
|
+
if (restoreKey === key) {
|
|
1975
|
+
if (elementSelector === windowKey) {
|
|
1976
|
+
entry.scrollX = window.scrollX || 0;
|
|
1977
|
+
entry.scrollY = window.scrollY || 0;
|
|
1978
|
+
} else if (elementSelector) {
|
|
1979
|
+
const element = document.querySelector(elementSelector);
|
|
1980
|
+
entry.scrollX = element?.scrollLeft || 0;
|
|
1981
|
+
entry.scrollY = element?.scrollTop || 0;
|
|
1982
|
+
}
|
|
1983
|
+
cache.set(cacheKey, entry);
|
|
1984
|
+
}
|
|
1985
|
+
}
|
|
1986
|
+
};
|
|
1987
|
+
const onPathChange = () => {
|
|
1988
|
+
pathDidChange = true;
|
|
1989
|
+
};
|
|
1990
|
+
if (typeof document !== 'undefined') {
|
|
1991
|
+
document.addEventListener('scroll', onScroll, true);
|
|
1992
|
+
}
|
|
1993
|
+
const unsubOnBeforeLoad = router.subscribe('onBeforeLoad', event => {
|
|
1994
|
+
if (event.pathChanged) onPathWillChange(event.from);
|
|
1995
|
+
});
|
|
1996
|
+
const unsubOnLoad = router.subscribe('onLoad', event => {
|
|
1997
|
+
if (event.pathChanged) onPathChange();
|
|
1998
|
+
});
|
|
1999
|
+
return () => {
|
|
2000
|
+
document.removeEventListener('scroll', onScroll);
|
|
2001
|
+
unsubOnBeforeLoad();
|
|
2002
|
+
unsubOnLoad();
|
|
2003
|
+
};
|
|
2004
|
+
}
|
|
2005
|
+
function restoreScrollPositions(router, opts) {
|
|
2006
|
+
if (pathDidChange) {
|
|
2007
|
+
const getKey = opts?.getKey || defaultGetKey;
|
|
2008
|
+
pathDidChange = false;
|
|
2009
|
+
const restoreKey = getKey(router.state.location);
|
|
2010
|
+
let windowRestored = false;
|
|
2011
|
+
for (const cacheKey in cache.current) {
|
|
2012
|
+
const entry = cache.current[cacheKey];
|
|
2013
|
+
const [key, elementSelector] = cacheKey.split(delimiter);
|
|
2014
|
+
if (key === restoreKey) {
|
|
2015
|
+
if (elementSelector === windowKey) {
|
|
2016
|
+
windowRestored = true;
|
|
2017
|
+
window.scrollTo(entry.scrollX, entry.scrollY);
|
|
2018
|
+
} else if (elementSelector) {
|
|
2019
|
+
const element = document.querySelector(elementSelector);
|
|
2020
|
+
if (element) {
|
|
2021
|
+
element.scrollLeft = entry.scrollX;
|
|
2022
|
+
element.scrollTop = entry.scrollY;
|
|
2023
|
+
}
|
|
2024
|
+
}
|
|
2025
|
+
}
|
|
2026
|
+
}
|
|
2027
|
+
if (!windowRestored) {
|
|
2028
|
+
window.scrollTo(0, 0);
|
|
2029
|
+
}
|
|
2030
|
+
}
|
|
2031
|
+
}
|
|
2032
|
+
|
|
1914
2033
|
exports.FileRoute = FileRoute;
|
|
1915
2034
|
exports.PathParamError = PathParamError;
|
|
1916
2035
|
exports.RootRoute = RootRoute;
|
|
@@ -1944,12 +2063,14 @@
|
|
|
1944
2063
|
exports.redirect = redirect;
|
|
1945
2064
|
exports.replaceEqualDeep = replaceEqualDeep;
|
|
1946
2065
|
exports.resolvePath = resolvePath;
|
|
2066
|
+
exports.restoreScrollPositions = restoreScrollPositions;
|
|
1947
2067
|
exports.rootRouteId = rootRouteId;
|
|
1948
2068
|
exports.stringifySearchWith = stringifySearchWith;
|
|
1949
2069
|
exports.trimPath = trimPath;
|
|
1950
2070
|
exports.trimPathLeft = trimPathLeft;
|
|
1951
2071
|
exports.trimPathRight = trimPathRight;
|
|
1952
2072
|
exports.warning = warning;
|
|
2073
|
+
exports.watchScrollPositions = watchScrollPositions;
|
|
1953
2074
|
|
|
1954
2075
|
Object.defineProperty(exports, '__esModule', { value: true });
|
|
1955
2076
|
|