@tanstack/react-router 0.0.1-beta.230 → 0.0.1-beta.232
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 +34 -37
- 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 +4 -3
- package/build/cjs/index.js.map +1 -1
- package/build/cjs/router.js +70 -70
- 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 +155 -133
- package/build/esm/index.js.map +1 -1
- package/build/stats-html.html +1 -1
- package/build/stats-react.json +293 -293
- package/build/types/RouterProvider.d.ts +0 -5
- package/build/types/router.d.ts +14 -2
- package/build/types/scroll-restoration.d.ts +12 -0
- package/build/umd/index.development.js +154 -131
- 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/Matches.tsx +3 -1
- package/src/RouterProvider.tsx +42 -46
- package/src/awaited.tsx +1 -1
- package/src/router.ts +106 -84
- 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,GAAG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A;AACR,CAAA;AAEO,SAASQ,iBAAiBA,CAACC,KAA+B,EAAE;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":";;;;;;;;;;;;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A,EAA+BX,OAAO,CAAC2C,EAAG,CAAG,EAAA,CAAA,CAAA;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
|
@@ -8,7 +8,7 @@
|
|
|
8
8
|
*
|
|
9
9
|
* @license MIT
|
|
10
10
|
*/
|
|
11
|
-
import { createBrowserHistory } from '@tanstack/history';
|
|
11
|
+
import { createBrowserHistory, createMemoryHistory } from '@tanstack/history';
|
|
12
12
|
export * from '@tanstack/history';
|
|
13
13
|
import invariant from 'tiny-invariant';
|
|
14
14
|
export { default as invariant } from 'tiny-invariant';
|
|
@@ -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,
|
|
@@ -794,24 +797,12 @@ const routerContext = /*#__PURE__*/React.createContext(null);
|
|
|
794
797
|
if (typeof document !== 'undefined') {
|
|
795
798
|
window.__TSR_ROUTER_CONTEXT__ = routerContext;
|
|
796
799
|
}
|
|
797
|
-
class SearchParamError extends Error {}
|
|
798
|
-
class PathParamError extends Error {}
|
|
799
|
-
function getInitialRouterState(location) {
|
|
800
|
-
return {
|
|
801
|
-
status: 'idle',
|
|
802
|
-
resolvedLocation: location,
|
|
803
|
-
location,
|
|
804
|
-
matches: [],
|
|
805
|
-
pendingMatches: [],
|
|
806
|
-
lastUpdated: Date.now()
|
|
807
|
-
};
|
|
808
|
-
}
|
|
809
800
|
function RouterProvider({
|
|
810
801
|
router,
|
|
811
802
|
...rest
|
|
812
803
|
}) {
|
|
813
804
|
// Allow the router to update options on the router instance
|
|
814
|
-
router.
|
|
805
|
+
router.update({
|
|
815
806
|
...router.options,
|
|
816
807
|
...rest,
|
|
817
808
|
context: {
|
|
@@ -819,29 +810,44 @@ function RouterProvider({
|
|
|
819
810
|
...rest?.context
|
|
820
811
|
}
|
|
821
812
|
});
|
|
813
|
+
const inner = /*#__PURE__*/React.createElement(RouterProviderInner, {
|
|
814
|
+
router: router
|
|
815
|
+
});
|
|
816
|
+
if (router.options.Wrap) {
|
|
817
|
+
return /*#__PURE__*/React.createElement(router.options.Wrap, null, inner);
|
|
818
|
+
}
|
|
819
|
+
return inner;
|
|
820
|
+
}
|
|
821
|
+
function RouterProviderInner({
|
|
822
|
+
router
|
|
823
|
+
}) {
|
|
822
824
|
const [preState, setState] = React.useState(() => router.state);
|
|
823
825
|
const [isTransitioning, startReactTransition] = React.useTransition();
|
|
826
|
+
const isAnyTransitioning = isTransitioning || preState.matches.some(d => d.status === 'pending');
|
|
824
827
|
const state = React.useMemo(() => ({
|
|
825
828
|
...preState,
|
|
826
|
-
status:
|
|
829
|
+
status: isAnyTransitioning ? 'pending' : 'idle',
|
|
827
830
|
location: isTransitioning ? router.latestLocation : preState.location,
|
|
828
831
|
pendingMatches: router.pendingMatches
|
|
829
832
|
}), [preState, isTransitioning]);
|
|
830
833
|
router.setState = setState;
|
|
831
834
|
router.state = state;
|
|
832
835
|
router.startReactTransition = startReactTransition;
|
|
833
|
-
|
|
836
|
+
const tryLoad = () => {
|
|
837
|
+
if (state.location !== router.latestLocation) {
|
|
838
|
+
startReactTransition(() => {
|
|
839
|
+
try {
|
|
840
|
+
router.load();
|
|
841
|
+
} catch (err) {
|
|
842
|
+
console.error(err);
|
|
843
|
+
}
|
|
844
|
+
});
|
|
845
|
+
}
|
|
846
|
+
};
|
|
847
|
+
useLayoutEffect$1(() => {
|
|
834
848
|
const unsub = router.history.subscribe(() => {
|
|
835
849
|
router.latestLocation = router.parseLocation(router.latestLocation);
|
|
836
|
-
|
|
837
|
-
startReactTransition(() => {
|
|
838
|
-
try {
|
|
839
|
-
router.load();
|
|
840
|
-
} catch (err) {
|
|
841
|
-
console.error(err);
|
|
842
|
-
}
|
|
843
|
-
});
|
|
844
|
-
}
|
|
850
|
+
tryLoad();
|
|
845
851
|
});
|
|
846
852
|
const nextLocation = router.buildLocation({
|
|
847
853
|
search: true,
|
|
@@ -859,7 +865,7 @@ function RouterProvider({
|
|
|
859
865
|
unsub();
|
|
860
866
|
};
|
|
861
867
|
}, [router.history]);
|
|
862
|
-
|
|
868
|
+
useLayoutEffect$1(() => {
|
|
863
869
|
if (!isTransitioning && state.resolvedLocation !== state.location) {
|
|
864
870
|
router.emit({
|
|
865
871
|
type: 'onResolved',
|
|
@@ -874,14 +880,10 @@ function RouterProvider({
|
|
|
874
880
|
}));
|
|
875
881
|
}
|
|
876
882
|
});
|
|
877
|
-
|
|
878
|
-
|
|
879
|
-
|
|
880
|
-
|
|
881
|
-
} catch (err) {
|
|
882
|
-
console.error(err);
|
|
883
|
-
}
|
|
884
|
-
});
|
|
883
|
+
useLayoutEffect$1(() => {
|
|
884
|
+
if (!window.__TSR_DEHYDRATED__) {
|
|
885
|
+
tryLoad();
|
|
886
|
+
}
|
|
885
887
|
}, []);
|
|
886
888
|
return /*#__PURE__*/React.createElement(routerContext.Provider, {
|
|
887
889
|
value: router
|
|
@@ -898,7 +900,7 @@ function useRouterState(opts) {
|
|
|
898
900
|
return opts?.select ? opts.select(state) : state;
|
|
899
901
|
}
|
|
900
902
|
function useRouter() {
|
|
901
|
-
const resolvedContext = window.__TSR_ROUTER_CONTEXT__ || routerContext;
|
|
903
|
+
const resolvedContext = typeof document !== 'undefined' ? window.__TSR_ROUTER_CONTEXT__ || routerContext : routerContext;
|
|
902
904
|
const value = React.useContext(resolvedContext);
|
|
903
905
|
warning(value, 'useRouter must be used inside a <RouterProvider> component!');
|
|
904
906
|
return value;
|
|
@@ -938,7 +940,7 @@ function useAwaited({
|
|
|
938
940
|
promise.__deferredState = state;
|
|
939
941
|
}
|
|
940
942
|
if (state.status === 'pending') {
|
|
941
|
-
throw promise;
|
|
943
|
+
throw new Promise(r => setTimeout(r, 1)).then(() => promise);
|
|
942
944
|
}
|
|
943
945
|
if (state.status === 'error') {
|
|
944
946
|
throw state.error;
|
|
@@ -1224,6 +1226,8 @@ function stringifySearchWith(stringify, parser) {
|
|
|
1224
1226
|
};
|
|
1225
1227
|
}
|
|
1226
1228
|
|
|
1229
|
+
// import warning from 'tiny-warning'
|
|
1230
|
+
|
|
1227
1231
|
//
|
|
1228
1232
|
|
|
1229
1233
|
const componentTypes = ['component', 'errorComponent', 'pendingComponent'];
|
|
@@ -1241,7 +1245,7 @@ class Router {
|
|
|
1241
1245
|
// Must build in constructor
|
|
1242
1246
|
|
|
1243
1247
|
constructor(options) {
|
|
1244
|
-
this.
|
|
1248
|
+
this.update({
|
|
1245
1249
|
defaultPreloadDelay: 50,
|
|
1246
1250
|
defaultPendingMs: 1000,
|
|
1247
1251
|
defaultPendingMinMs: 500,
|
|
@@ -1251,20 +1255,22 @@ class Router {
|
|
|
1251
1255
|
parseSearch: options?.parseSearch ?? defaultParseSearch
|
|
1252
1256
|
});
|
|
1253
1257
|
}
|
|
1254
|
-
|
|
1255
|
-
|
|
1256
|
-
|
|
1257
|
-
|
|
1258
|
-
|
|
1258
|
+
|
|
1259
|
+
// These are default implementations that can optionally be overridden
|
|
1260
|
+
// by the router provider once rendered. We provide these so that the
|
|
1261
|
+
// router can be used in a non-react environment if necessary
|
|
1262
|
+
startReactTransition = fn => fn();
|
|
1263
|
+
setState = updater => {
|
|
1264
|
+
this.state = functionalUpdate(updater, this.state);
|
|
1259
1265
|
};
|
|
1260
|
-
|
|
1266
|
+
update = newOptions => {
|
|
1261
1267
|
this.options = {
|
|
1262
1268
|
...this.options,
|
|
1263
1269
|
...newOptions
|
|
1264
1270
|
};
|
|
1265
1271
|
this.basepath = `/${trimPath(newOptions.basepath ?? '') ?? ''}`;
|
|
1266
1272
|
if (!this.history || this.options.history && this.options.history !== this.history) {
|
|
1267
|
-
this.history = this.options.history ?? createBrowserHistory();
|
|
1273
|
+
this.history = this.options.history ?? (typeof document !== 'undefined' ? createBrowserHistory() : createMemoryHistory());
|
|
1268
1274
|
this.latestLocation = this.parseLocation();
|
|
1269
1275
|
}
|
|
1270
1276
|
if (this.options.routeTree !== this.routeTree) {
|
|
@@ -1904,6 +1910,7 @@ class Router {
|
|
|
1904
1910
|
// forcefully show the pending component
|
|
1905
1911
|
if (pendingPromise) {
|
|
1906
1912
|
pendingPromise.then(() => {
|
|
1913
|
+
if (latestPromise = checkLatest()) return;
|
|
1907
1914
|
didShowPending = true;
|
|
1908
1915
|
matches[index] = match = {
|
|
1909
1916
|
...match,
|
|
@@ -1923,6 +1930,7 @@ class Router {
|
|
|
1923
1930
|
if (didShowPending && pendingMinMs) {
|
|
1924
1931
|
await new Promise(r => setTimeout(r, pendingMinMs));
|
|
1925
1932
|
}
|
|
1933
|
+
if (latestPromise = checkLatest()) return await latestPromise;
|
|
1926
1934
|
matches[index] = match = {
|
|
1927
1935
|
...match,
|
|
1928
1936
|
error: undefined,
|
|
@@ -1991,7 +1999,7 @@ class Router {
|
|
|
1991
1999
|
// Ingest the new matches
|
|
1992
2000
|
this.setState(s => ({
|
|
1993
2001
|
...s,
|
|
1994
|
-
status: 'pending',
|
|
2002
|
+
// status: 'pending',
|
|
1995
2003
|
location: next,
|
|
1996
2004
|
matches
|
|
1997
2005
|
}));
|
|
@@ -2020,6 +2028,7 @@ class Router {
|
|
|
2020
2028
|
// ...s,
|
|
2021
2029
|
// status: 'idle',
|
|
2022
2030
|
// resolvedLocation: s.location,
|
|
2031
|
+
// matches,
|
|
2023
2032
|
// }))
|
|
2024
2033
|
|
|
2025
2034
|
//
|
|
@@ -2220,63 +2229,42 @@ class Router {
|
|
|
2220
2229
|
}
|
|
2221
2230
|
return undefined;
|
|
2222
2231
|
};
|
|
2223
|
-
|
|
2224
|
-
|
|
2225
|
-
|
|
2226
|
-
|
|
2227
|
-
|
|
2228
|
-
|
|
2229
|
-
|
|
2230
|
-
|
|
2231
|
-
|
|
2232
|
-
|
|
2233
|
-
|
|
2234
|
-
|
|
2235
|
-
|
|
2236
|
-
|
|
2237
|
-
|
|
2238
|
-
|
|
2239
|
-
|
|
2240
|
-
|
|
2241
|
-
|
|
2242
|
-
|
|
2243
|
-
|
|
2244
|
-
|
|
2245
|
-
|
|
2246
|
-
|
|
2247
|
-
|
|
2248
|
-
|
|
2249
|
-
|
|
2250
|
-
|
|
2251
|
-
|
|
2252
|
-
|
|
2253
|
-
|
|
2254
|
-
|
|
2255
|
-
|
|
2256
|
-
|
|
2257
|
-
|
|
2258
|
-
|
|
2259
|
-
// invariant(
|
|
2260
|
-
// dehydratedMatch,
|
|
2261
|
-
// `Could not find a client-side match for dehydrated match with id: ${match.id}!`,
|
|
2262
|
-
// )
|
|
2263
|
-
|
|
2264
|
-
// if (dehydratedMatch) {
|
|
2265
|
-
// return {
|
|
2266
|
-
// ...match,
|
|
2267
|
-
// ...dehydratedMatch,
|
|
2268
|
-
// }
|
|
2269
|
-
// }
|
|
2270
|
-
// return match
|
|
2271
|
-
// })
|
|
2272
|
-
|
|
2273
|
-
// this.setState((s) => {
|
|
2274
|
-
// return {
|
|
2275
|
-
// ...s,
|
|
2276
|
-
// matches: dehydratedState.dehydratedMatches as any,
|
|
2277
|
-
// }
|
|
2278
|
-
// })
|
|
2279
|
-
// }
|
|
2232
|
+
dehydrate = () => {
|
|
2233
|
+
return {
|
|
2234
|
+
state: {
|
|
2235
|
+
dehydratedMatches: this.state.matches.map(d => pick(d, ['fetchedAt', 'invalid', 'id', 'status', 'updatedAt', 'loaderData']))
|
|
2236
|
+
}
|
|
2237
|
+
};
|
|
2238
|
+
};
|
|
2239
|
+
hydrate = async __do_not_use_server_ctx => {
|
|
2240
|
+
let _ctx = __do_not_use_server_ctx;
|
|
2241
|
+
// Client hydrates from window
|
|
2242
|
+
if (typeof document !== 'undefined') {
|
|
2243
|
+
_ctx = window.__TSR_DEHYDRATED__;
|
|
2244
|
+
}
|
|
2245
|
+
invariant(_ctx, 'Expected to find a __TSR_DEHYDRATED__ property on window... but we did not. Did you forget to render <DehydrateRouter /> in your app?');
|
|
2246
|
+
const ctx = _ctx;
|
|
2247
|
+
this.dehydratedData = ctx.payload;
|
|
2248
|
+
this.options.hydrate?.(ctx.payload);
|
|
2249
|
+
const dehydratedState = ctx.router.state;
|
|
2250
|
+
let matches = this.matchRoutes(this.state.location.pathname, this.state.location.search).map(match => {
|
|
2251
|
+
const dehydratedMatch = dehydratedState.dehydratedMatches.find(d => d.id === match.id);
|
|
2252
|
+
invariant(dehydratedMatch, `Could not find a client-side match for dehydrated match with id: ${match.id}!`);
|
|
2253
|
+
if (dehydratedMatch) {
|
|
2254
|
+
return {
|
|
2255
|
+
...match,
|
|
2256
|
+
...dehydratedMatch
|
|
2257
|
+
};
|
|
2258
|
+
}
|
|
2259
|
+
return match;
|
|
2260
|
+
});
|
|
2261
|
+
this.setState(s => {
|
|
2262
|
+
return {
|
|
2263
|
+
...s,
|
|
2264
|
+
matches: matches
|
|
2265
|
+
};
|
|
2266
|
+
});
|
|
2267
|
+
};
|
|
2280
2268
|
|
|
2281
2269
|
// resolveMatchPromise = (matchId: string, key: string, value: any) => {
|
|
2282
2270
|
// state.matches
|
|
@@ -2297,36 +2285,43 @@ function lazyFn(fn, key) {
|
|
|
2297
2285
|
function isCtrlEvent(e) {
|
|
2298
2286
|
return !!(e.metaKey || e.altKey || e.ctrlKey || e.shiftKey);
|
|
2299
2287
|
}
|
|
2288
|
+
class SearchParamError extends Error {}
|
|
2289
|
+
class PathParamError extends Error {}
|
|
2290
|
+
function getInitialRouterState(location) {
|
|
2291
|
+
return {
|
|
2292
|
+
status: 'idle',
|
|
2293
|
+
resolvedLocation: location,
|
|
2294
|
+
location,
|
|
2295
|
+
matches: [],
|
|
2296
|
+
pendingMatches: [],
|
|
2297
|
+
lastUpdated: Date.now()
|
|
2298
|
+
};
|
|
2299
|
+
}
|
|
2300
2300
|
|
|
2301
2301
|
const useLayoutEffect = typeof window !== 'undefined' ? React.useLayoutEffect : React.useEffect;
|
|
2302
2302
|
const windowKey = 'window';
|
|
2303
2303
|
const delimiter = '___';
|
|
2304
2304
|
let weakScrolledElements = new WeakSet();
|
|
2305
|
-
let cache;
|
|
2306
2305
|
const sessionsStorage = typeof window !== 'undefined' && window.sessionStorage;
|
|
2306
|
+
let cache = sessionsStorage ? (() => {
|
|
2307
|
+
const storageKey = 'tsr-scroll-restoration-v2';
|
|
2308
|
+
const state = JSON.parse(window.sessionStorage.getItem(storageKey) || 'null') || {
|
|
2309
|
+
cached: {},
|
|
2310
|
+
next: {}
|
|
2311
|
+
};
|
|
2312
|
+
return {
|
|
2313
|
+
state,
|
|
2314
|
+
set: updater => {
|
|
2315
|
+
cache.state = functionalUpdate(updater, cache.state);
|
|
2316
|
+
window.sessionStorage.setItem(storageKey, JSON.stringify(cache.state));
|
|
2317
|
+
}
|
|
2318
|
+
};
|
|
2319
|
+
})() : undefined;
|
|
2307
2320
|
const defaultGetKey = location => location.state.key;
|
|
2308
2321
|
function useScrollRestoration(options) {
|
|
2309
2322
|
const router = useRouter();
|
|
2310
2323
|
useLayoutEffect(() => {
|
|
2311
2324
|
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
2325
|
const {
|
|
2331
2326
|
history
|
|
2332
2327
|
} = window;
|
|
@@ -2336,7 +2331,17 @@ function useScrollRestoration(options) {
|
|
|
2336
2331
|
const onScroll = event => {
|
|
2337
2332
|
if (weakScrolledElements.has(event.target)) return;
|
|
2338
2333
|
weakScrolledElements.add(event.target);
|
|
2339
|
-
|
|
2334
|
+
let elementSelector = '';
|
|
2335
|
+
if (event.target === document || event.target === window) {
|
|
2336
|
+
elementSelector = windowKey;
|
|
2337
|
+
} else {
|
|
2338
|
+
const attrId = event.target.getAttribute('data-scroll-restoration-id');
|
|
2339
|
+
if (attrId) {
|
|
2340
|
+
elementSelector = `[data-scroll-restoration-id="${attrId}"]`;
|
|
2341
|
+
} else {
|
|
2342
|
+
elementSelector = getCssSelector(event.target);
|
|
2343
|
+
}
|
|
2344
|
+
}
|
|
2340
2345
|
if (!cache.state.next[elementSelector]) {
|
|
2341
2346
|
cache.set(c => ({
|
|
2342
2347
|
...c,
|
|
@@ -2350,15 +2355,6 @@ function useScrollRestoration(options) {
|
|
|
2350
2355
|
}));
|
|
2351
2356
|
}
|
|
2352
2357
|
};
|
|
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
2358
|
if (typeof document !== 'undefined') {
|
|
2363
2359
|
document.addEventListener('scroll', onScroll, true);
|
|
2364
2360
|
}
|
|
@@ -2438,6 +2434,32 @@ function ScrollRestoration(props) {
|
|
|
2438
2434
|
useScrollRestoration(props);
|
|
2439
2435
|
return null;
|
|
2440
2436
|
}
|
|
2437
|
+
function useElementScrollRestoration(options) {
|
|
2438
|
+
const router = useRouter();
|
|
2439
|
+
const getKey = options?.getKey || defaultGetKey;
|
|
2440
|
+
let elementSelector = '';
|
|
2441
|
+
if (options.id) {
|
|
2442
|
+
elementSelector = `[data-scroll-restoration-id="${options.id}"]`;
|
|
2443
|
+
} else {
|
|
2444
|
+
const element = options.getElement?.();
|
|
2445
|
+
if (!element) {
|
|
2446
|
+
return;
|
|
2447
|
+
}
|
|
2448
|
+
elementSelector = getCssSelector(element);
|
|
2449
|
+
}
|
|
2450
|
+
const restoreKey = getKey(router.latestLocation);
|
|
2451
|
+
const cacheKey = [restoreKey, elementSelector].join(delimiter);
|
|
2452
|
+
return cache.state.cached[cacheKey];
|
|
2453
|
+
}
|
|
2454
|
+
function getCssSelector(el) {
|
|
2455
|
+
let path = [],
|
|
2456
|
+
parent;
|
|
2457
|
+
while (parent = el.parentNode) {
|
|
2458
|
+
path.unshift(`${el.tagName}:nth-child(${[].indexOf.call(parent.children, el) + 1})`);
|
|
2459
|
+
el = parent;
|
|
2460
|
+
}
|
|
2461
|
+
return `${path.join(' > ')}`.toLowerCase();
|
|
2462
|
+
}
|
|
2441
2463
|
|
|
2442
2464
|
function useBlocker(message, condition = true) {
|
|
2443
2465
|
const {
|
|
@@ -2498,5 +2520,5 @@ function Navigate(props) {
|
|
|
2498
2520
|
return null;
|
|
2499
2521
|
}
|
|
2500
2522
|
|
|
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 };
|
|
2523
|
+
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
2524
|
//# sourceMappingURL=index.js.map
|