Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/jaredpalmer/after.js
Browse files Browse the repository at this point in the history
  • Loading branch information
nimaa77 committed Jun 29, 2020
2 parents 568e40c + f7be13a commit 1a0415b
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions packages/after.js/src/After.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ class Afterparty extends React.Component<AfterpartyProps, AfterpartyState> {
history,
routes,
data,
transitionBehavior,
// we don't want to pass these
// to loadInitialProps()
match,
Expand All @@ -87,6 +88,19 @@ class Afterparty extends React.Component<AfterpartyProps, AfterpartyState> {

const { scrollToTop } = data.afterData;

const instantMode = isInstantTransition(transitionBehavior)

// Only for page changes, prevent scroll up for anchor links
if (
(prevState.currentLocation &&
prevState.currentLocation.pathname) !== location.pathname &&
// Only Scroll if scrollToTop is not false
scrollToTop.current === true &&
instantMode === true
) {
window.scrollTo(0, 0);
}

loadInitialProps(routes, location.pathname, {
location,
history,
Expand All @@ -101,10 +115,12 @@ class Afterparty extends React.Component<AfterpartyProps, AfterpartyState> {
(prevState.currentLocation &&
prevState.currentLocation.pathname) !== location.pathname &&
// Only Scroll if scrollToTop is not false
scrollToTop.current
scrollToTop.current === true &&
instantMode === false
) {
window.scrollTo(0, 0);
}

this.setState({ previousLocation: null, data, isLoading: false });
})
.catch(e => {
Expand Down Expand Up @@ -132,12 +148,12 @@ class Afterparty extends React.Component<AfterpartyProps, AfterpartyState> {
const { location: currentLocation, transitionBehavior } = this.props;
const initialData = this.prefetcherCache[currentLocation.pathname] || data;

const instantTransition = isInstantTransition(transitionBehavior)
const instantMode = isInstantTransition(transitionBehavior)

// when we are in the instant mode we want to pass the right location prop
// to the <Route /> otherwise it will render previous matche component
const location =
instantTransition
instantMode
? currentLocation
: previousLocation || currentLocation;

Expand Down

0 comments on commit 1a0415b

Please sign in to comment.