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

Commit d8ca0c5

Browse files
authored
Merge pull request #13 from TylerBarnes/prevent-scroll-jumping-on-save
Prevent scroll jumping on save during development by adding a wrapper…
2 parents 42c0746 + 46cc6c8 commit d8ca0c5

File tree

8 files changed

+2418
-2
lines changed

8 files changed

+2418
-2
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "gatsby-plugin-transition-link",
3-
"version": "1.2.5",
3+
"version": "1.3.0",
44
"description": "A link component for page transitions in gatsby.",
55
"repository": "https://github.com/TylerBarnes/gatsby-plugin-transition-link",
66
"homepage": "https://gatsby-plugin-transition-link.netlify.com/",

src/AniLink/PaintDrip.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export default class PaintDrip extends Component {
2222
rgb = color ? convert.keyword.rgb(color) : rgb;
2323

2424
canvas.style.zIndex = 10000;
25+
canvas.style.top = 0;
2526
canvas.style.position = "fixed";
2627

2728
let vw = (canvas.width = window.innerWidth);

src/components/TransitionHandler.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,35 @@ import { getMs } from "../utils/secondsMs";
99
import { onEnter } from "../functions/onEnter";
1010
import { onExit } from "../functions/onExit";
1111
import { LayoutComponent as Layout } from "./Layout";
12+
import pageMinHeight from "../functions/pageMinHeight";
1213

1314
const DelayedTransition = delayTransitionRender(Transition);
1415
export default class TransitionHandler extends Component {
16+
constructor(props) {
17+
super(props);
18+
19+
this.wrapper = React.createRef();
20+
this.updatePageMinHeight = this.updatePageMinHeight.bind(this);
21+
}
22+
componentDidMount() {
23+
this.updatePageMinHeight();
24+
window.addEventListener("resize", this.updatePageMinHeight);
25+
}
26+
27+
componentWillUnmount() {
28+
window.removeEventListener("resize", this.updatePageMinHeight);
29+
}
30+
31+
updatePageMinHeight() {
32+
setTimeout(
33+
() =>
34+
pageMinHeight({
35+
node: this.wrapper
36+
}),
37+
1
38+
);
39+
}
40+
1541
render() {
1642
const { props } = this;
1743
const { children } = props;
@@ -30,6 +56,7 @@ export default class TransitionHandler extends Component {
3056
exitProps,
3157
transitionIdHistory,
3258
inTransition,
59+
updateContext,
3360
e
3461
}) => {
3562
return (
@@ -53,6 +80,8 @@ export default class TransitionHandler extends Component {
5380
entryProps,
5481
exitProps,
5582
pathname,
83+
updateContext,
84+
pageMinHeight,
5685
e
5786
})
5887
}
@@ -99,6 +128,7 @@ export default class TransitionHandler extends Component {
99128

100129
return (
101130
<div
131+
ref={n => (this.wrapper = n)}
102132
className="tl-wrapper"
103133
style={{
104134
position: "absolute",

src/context/InternalProvider.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ class InternalProvider extends Component {
66
state = {
77
inTransition: false,
88
transitionIdHistory: [],
9+
wrapperMinHeight: false,
910
// event
1011
e: false,
1112
// exit

src/functions/onEnter.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,12 @@ const onEnter = ({
55
entryProps,
66
exitProps,
77
pathname,
8+
pageMinHeight,
9+
updateContext,
810
e
911
}) => {
12+
pageMinHeight({ node, updateContext });
13+
1014
if (inTransition) {
1115
window.scrollTo(0, 0);
1216
} else {

src/functions/pageMinHeight.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// this function is used to set the outerwrapper height to keep the page from jumping up on save during development.
2+
export default function pageMinHeight({ node, updateContext }) {
3+
const minHeight = node.offsetHeight;
4+
if (minHeight > 0) {
5+
sessionStorage.setItem("wrapperMinHeight", node.offsetHeight);
6+
updateContext && updateContext({ wrapperMinHeight: node.offsetHeight });
7+
}
8+
}

src/wrap-page.js

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,28 @@
11
const React = require("react");
22
const TransitionHandler = require("./components/TransitionHandler").default;
33
const InternalProvider = require("./context/InternalProvider").default;
4+
const Consumer = require("./context/createTransitionContext").Consumer;
45

56
// eslint-disable-next-line react/prop-types,react/display-name
67
module.exports = ({ element, props }) => {
8+
const minHeight = sessionStorage.getItem("wrapperMinHeight");
79
return (
810
<InternalProvider>
9-
<TransitionHandler {...props}>{element}</TransitionHandler>
11+
<Consumer>
12+
{({ wrapperMinHeight }) => (
13+
<div
14+
style={{
15+
position: "relative",
16+
zIndex: 0,
17+
minHeight: wrapperMinHeight
18+
? `${wrapperMinHeight}px`
19+
: `${minHeight}px`
20+
}}
21+
>
22+
<TransitionHandler {...props}>{element}</TransitionHandler>
23+
</div>
24+
)}
25+
</Consumer>
1026
</InternalProvider>
1127
);
1228
};

0 commit comments

Comments
 (0)