Skip to content

Commit

Permalink
feat: add transition mode for pending data
Browse files Browse the repository at this point in the history
* feat: render fallback if route data is loading
* feat: add new transition mode for pending data
* style: transition mode names

---------

Co-authored-by: Mikhail Tsipotan <[email protected]>
  • Loading branch information
Mikhail Tsipotan and Mikhail Tsipotan authored Dec 7, 2023
1 parent 6a521dc commit 94c4b66
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
4 changes: 2 additions & 2 deletions .size-limit.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
[
{
"path": "dist/client/index.js",
"limit": "6.95 KB"
"limit": "7.01 KB"
},
{
"path": "dist/server/index.js",
"limit": "6.97 KB"
"limit": "7.02 KB"
}
]
9 changes: 7 additions & 2 deletions src/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ export type GetMetaData = Loader<MetaData, {data: InitialData}>
* export default MainPage
* ```
*/
export type PageComponent<P = any> = React.ComponentType<P> & {

export type PageComponent<P = any> = React.ComponentType<
P & {isLoading: boolean}
> & {
getMetaData?: GetMetaData
getInitialData?: GetInitialData
}
Expand All @@ -96,7 +99,9 @@ export type PageRoute = RouteProps & {
export enum TransitionMode {
/** Wait for `getInitialData` to get completed, and show the next page */
BLOCKED = 'blocked',
/** Show the next page with spinner or skeleton while `getInitialData` is pending */
/** Show Fallback component while `getInitialData` is pending */
WAIT_FOR_DATA = 'wait-for-data',
/** Show Fallback component while Suspense is pending */
INSTANT = 'instant'
}

Expand Down
23 changes: 19 additions & 4 deletions src/components/routes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,16 @@ export const Routes: React.FC<RoutesProps> = ({

setRouteData((prevState) => ({...prevState, isLoading: true}))

if (scrollToTop && transition === TransitionMode.INSTANT) {
if (
scrollToTop &&
(transition === TransitionMode.WAIT_FOR_DATA ||
transition === TransitionMode.INSTANT)
) {
window.scrollTo(0, 0)
}

loadRouteData({pathname, routes, context}).then((routeData) => {
if (scrollToTop && transition !== TransitionMode.INSTANT) {
if (scrollToTop && transition === TransitionMode.BLOCKED) {
window.scrollTo(0, 0)
}

Expand All @@ -76,7 +80,12 @@ export const Routes: React.FC<RoutesProps> = ({
}, [location, currentLocation, scrollToTop]) // eslint-disable-line react-hooks/exhaustive-deps

const routerLocation =
transition === TransitionMode.INSTANT ? location : currentLocation
transition === TransitionMode.WAIT_FOR_DATA ||
transition === TransitionMode.INSTANT
? location
: currentLocation

const pageData = {...routeData.data, isLoading: routeData.isLoading}

return (
<BaseRoutes location={routerLocation}>
Expand All @@ -88,7 +97,13 @@ export const Routes: React.FC<RoutesProps> = ({
path={path}
element={
<Suspense fallback={Fallback ? <Fallback /> : null}>
<Component {...rest} {...routeData.data} />
{transition === TransitionMode.WAIT_FOR_DATA &&
routeData.isLoading &&
Fallback ? (
<Fallback />
) : (
<Component {...rest} {...pageData} />
)}
</Suspense>
}
/>
Expand Down

0 comments on commit 94c4b66

Please sign in to comment.