Skip to content
This repository has been archived by the owner on Sep 20, 2023. It is now read-only.

Commit

Permalink
feat: prevent the back button during transitions
Browse files Browse the repository at this point in the history
Previously pressing the back button during transitions could cause the page to look glitchy or elements to dissapear. This prevents that.
  • Loading branch information
Tyler Barnes committed Mar 25, 2019
1 parent 7a5b683 commit f879140
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
2 changes: 2 additions & 0 deletions src/components/TransitionHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export default class TransitionHandler extends Component {
}}
onEnter={node =>
!!node &&
!window.__tl_back_button_pressed &&
onEnter({
node,
action,
Expand All @@ -64,6 +65,7 @@ export default class TransitionHandler extends Component {
}
onExit={node =>
!!node &&
!window.__tl_back_button_pressed &&
onExit({
node,
inTransition,
Expand Down
13 changes: 13 additions & 0 deletions src/gatsby-browser.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
const { navigate } = require("gatsby");

exports.wrapPageElement = require(`./wrap-page`);

exports.shouldUpdateScroll = () => false;

exports.onPreRouteUpdate = ({ location }) => {
// prevent the back button during transitions as it breaks pages
if (
window.__tl_inTransition &&
location.pathname !== window.__tl_desiredPathname
) {
window.__tl_back_button_pressed = true;
navigate(window.__tl_desiredPathname);
}
};
27 changes: 17 additions & 10 deletions src/utils/triggerTransition.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ const triggerTransition = ({

if (inTransition) return false;

// these globals prevent the back button from being pressed during a transition as that can have unexpected results
window.__tl_inTransition = true;
window.__tl_desiredPathname = to;

updateContext({
inTransition: true,
exitDelay: 0,
Expand Down Expand Up @@ -80,16 +84,19 @@ const triggerTransition = ({

// reset exit animation times so they dont apply when using browser back/forward.
// this will be replaced with a better solution in the future
setTimeout(
() =>
updateContext({
exitDelay: 0,
exitLength: 0,
// Once all animation is finished, it's safe to start a new animation since we're no longer inTransition.
inTransition: false
}),
getMs(finalResetSeconds) + 1
);
setTimeout(() => {
// these globals prevent the back button from being pressed during a transition as that can have unexpected results
window.__tl_inTransition = false;
window.__tl_desiredPathname = false;
window.__tl_back_button_pressed = false;

updateContext({
exitDelay: 0,
exitLength: 0,
// Once all animation is finished, it's safe to start a new animation since we're no longer inTransition.
inTransition: false
});
}, getMs(finalResetSeconds) + 1);
};

export { triggerTransition };

0 comments on commit f879140

Please sign in to comment.