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

Commit

Permalink
Merge pull request #13 from TylerBarnes/prevent-scroll-jumping-on-save
Browse files Browse the repository at this point in the history
Prevent scroll jumping on save during development by adding a wrapper…
  • Loading branch information
TylerBarnes committed Nov 22, 2018
2 parents 42c0746 + 46cc6c8 commit d8ca0c5
Show file tree
Hide file tree
Showing 8 changed files with 2,418 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "gatsby-plugin-transition-link",
"version": "1.2.5",
"version": "1.3.0",
"description": "A link component for page transitions in gatsby.",
"repository": "https://github.com/TylerBarnes/gatsby-plugin-transition-link",
"homepage": "https://gatsby-plugin-transition-link.netlify.com/",
Expand Down
1 change: 1 addition & 0 deletions src/AniLink/PaintDrip.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export default class PaintDrip extends Component {
rgb = color ? convert.keyword.rgb(color) : rgb;

canvas.style.zIndex = 10000;
canvas.style.top = 0;
canvas.style.position = "fixed";

let vw = (canvas.width = window.innerWidth);
Expand Down
30 changes: 30 additions & 0 deletions src/components/TransitionHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,35 @@ import { getMs } from "../utils/secondsMs";
import { onEnter } from "../functions/onEnter";
import { onExit } from "../functions/onExit";
import { LayoutComponent as Layout } from "./Layout";
import pageMinHeight from "../functions/pageMinHeight";

const DelayedTransition = delayTransitionRender(Transition);
export default class TransitionHandler extends Component {
constructor(props) {
super(props);

this.wrapper = React.createRef();
this.updatePageMinHeight = this.updatePageMinHeight.bind(this);
}
componentDidMount() {
this.updatePageMinHeight();
window.addEventListener("resize", this.updatePageMinHeight);
}

componentWillUnmount() {
window.removeEventListener("resize", this.updatePageMinHeight);
}

updatePageMinHeight() {
setTimeout(
() =>
pageMinHeight({
node: this.wrapper
}),
1
);
}

render() {
const { props } = this;
const { children } = props;
Expand All @@ -30,6 +56,7 @@ export default class TransitionHandler extends Component {
exitProps,
transitionIdHistory,
inTransition,
updateContext,
e
}) => {
return (
Expand All @@ -53,6 +80,8 @@ export default class TransitionHandler extends Component {
entryProps,
exitProps,
pathname,
updateContext,
pageMinHeight,
e
})
}
Expand Down Expand Up @@ -99,6 +128,7 @@ export default class TransitionHandler extends Component {

return (
<div
ref={n => (this.wrapper = n)}
className="tl-wrapper"
style={{
position: "absolute",
Expand Down
1 change: 1 addition & 0 deletions src/context/InternalProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class InternalProvider extends Component {
state = {
inTransition: false,
transitionIdHistory: [],
wrapperMinHeight: false,
// event
e: false,
// exit
Expand Down
4 changes: 4 additions & 0 deletions src/functions/onEnter.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ const onEnter = ({
entryProps,
exitProps,
pathname,
pageMinHeight,
updateContext,
e
}) => {
pageMinHeight({ node, updateContext });

if (inTransition) {
window.scrollTo(0, 0);
} else {
Expand Down
8 changes: 8 additions & 0 deletions src/functions/pageMinHeight.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// this function is used to set the outerwrapper height to keep the page from jumping up on save during development.
export default function pageMinHeight({ node, updateContext }) {
const minHeight = node.offsetHeight;
if (minHeight > 0) {
sessionStorage.setItem("wrapperMinHeight", node.offsetHeight);
updateContext && updateContext({ wrapperMinHeight: node.offsetHeight });
}
}
18 changes: 17 additions & 1 deletion src/wrap-page.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,28 @@
const React = require("react");
const TransitionHandler = require("./components/TransitionHandler").default;
const InternalProvider = require("./context/InternalProvider").default;
const Consumer = require("./context/createTransitionContext").Consumer;

// eslint-disable-next-line react/prop-types,react/display-name
module.exports = ({ element, props }) => {
const minHeight = sessionStorage.getItem("wrapperMinHeight");
return (
<InternalProvider>
<TransitionHandler {...props}>{element}</TransitionHandler>
<Consumer>
{({ wrapperMinHeight }) => (
<div
style={{
position: "relative",
zIndex: 0,
minHeight: wrapperMinHeight
? `${wrapperMinHeight}px`
: `${minHeight}px`
}}
>
<TransitionHandler {...props}>{element}</TransitionHandler>
</div>
)}
</Consumer>
</InternalProvider>
);
};

0 comments on commit d8ca0c5

Please sign in to comment.