Skip to content

Commit a204106

Browse files
committed
migration
1 parent bbca1b6 commit a204106

File tree

5 files changed

+128
-178
lines changed

5 files changed

+128
-178
lines changed

front/src/components/backToTop/BackToTop.js

Lines changed: 0 additions & 130 deletions
This file was deleted.
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
/* eslint-disable no-undefined */
2+
import React, { useState, useEffect } from 'react';
3+
import BackToTopButton from './backToTopButton/BackToTopButton';
4+
import { Motion, spring, presets } from 'react-motion';
5+
6+
// #region types
7+
type Props = {
8+
minScrollY: number,
9+
scrollTo?: string,
10+
onScrollDone?: () => any,
11+
};
12+
// #endregion
13+
14+
function BackToTop({ minScrollY = 120, onScrollDone = () => {} }: Props) {
15+
const [showBackButton, setShowBackButton] = useState(false);
16+
const [windowScrollY, setWindowScrollY] = useState(0);
17+
const [tickingScollObserve, setTickingScollObserve] = useState(false);
18+
19+
// #region on windows scroll callback
20+
const handleWindowScroll = () => {
21+
if (!window) {
22+
return;
23+
}
24+
25+
/* eslint-disable no-undefined */
26+
const currentWindowScrollY =
27+
window.pageYOffset !== undefined
28+
? window.pageYOffset
29+
: (
30+
document.documentElement ||
31+
document.body.parentNode ||
32+
document.body
33+
).scrollTop;
34+
/* eslint-enable no-undefined */
35+
36+
// scroll event fires to often, using window.requestAnimationFrame to limit computations
37+
if (!tickingScollObserve) {
38+
window.requestAnimationFrame(() => {
39+
if (windowScrollY !== currentWindowScrollY) {
40+
const shouldShowBackButton =
41+
currentWindowScrollY >= minScrollY ? true : false;
42+
43+
setWindowScrollY(currentWindowScrollY);
44+
setShowBackButton(shouldShowBackButton);
45+
}
46+
setTickingScollObserve(false);
47+
});
48+
}
49+
50+
setTickingScollObserve(true);
51+
};
52+
// #endregion
53+
54+
// #region on button click (smooth scroll)
55+
const handlesOnBackButtonClick = (
56+
event: React.MouseEvent<HTMLButtonElement>,
57+
) => {
58+
event && event.preventDefault();
59+
if (window && windowScrollY && windowScrollY > minScrollY) {
60+
// using here smoothscroll-polyfill
61+
window.scroll({ top: 0, left: 0, behavior: 'smooth' });
62+
typeof onScrollDone === 'function' && onScrollDone();
63+
}
64+
};
65+
// #endregion
66+
67+
// #region mount and unmount subscrubstions
68+
useEffect(() => {
69+
if (typeof window !== 'undefined') {
70+
window.addEventListener('scroll', handleWindowScroll);
71+
}
72+
73+
return function unsubscribeEvents() {
74+
if (typeof window !== 'undefined') {
75+
window.removeEventListener('scroll', handleWindowScroll);
76+
}
77+
};
78+
});
79+
// #endregion
80+
81+
return (
82+
// @ts-ignore
83+
<Motion style={{ x: spring(showBackButton ? 0 : 120, presets.stiff) }}>
84+
{({ x }) => (
85+
<BackToTopButton
86+
position={'bottom-right'}
87+
onClick={handlesOnBackButtonClick}
88+
motionStyle={{
89+
WebkitTransform: `translate3d(${x}px, 0, 0)`,
90+
transform: `translate3d(${x}px, 0, 0)`,
91+
}}
92+
/>
93+
)}
94+
</Motion>
95+
);
96+
}
97+
98+
BackToTop.displayName = 'BackToTop';
99+
100+
export default BackToTop;
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
1-
// @flow
2-
3-
// #region imports
41
import React from 'react';
52
import cx from 'classnames';
63
import UpIcon from './UpIcon';
74
import WithRightMargin from './styled/WithRightMargin';
8-
// #endregion
95

10-
// #region flow types
6+
// #region types
7+
export type BackButtonPosition = 'bottom-left' | 'bottom-right';
118
type Props = {
12-
position: 'bottom-left' | 'bottom-right',
13-
onClick: (event: SyntheticEvent<>) => any,
14-
children: any,
15-
motionStyle: any,
16-
...any,
9+
position: BackButtonPosition;
10+
onClick: (event: React.MouseEvent<HTMLButtonElement>) => any;
11+
children?: any;
12+
motionStyle: any;
1713
};
1814
// #endregion
1915

@@ -34,11 +30,30 @@ const defaultStyle = {
3430
opacity: defaultOpacity,
3531
backgroundColor: defaultBackGroundColor,
3632
};
33+
34+
function setPosition(position = 'bottom-right', refStyle = defaultStyle): any {
35+
const style = { ...refStyle };
36+
37+
switch (position) {
38+
case 'bottom-right':
39+
style.right = sideOffset;
40+
style.left = '';
41+
return style;
42+
43+
case 'bottom-left':
44+
style.right = '';
45+
style.left = sideOffset;
46+
return style;
47+
48+
default:
49+
return refStyle;
50+
}
51+
}
3752
// #endregion
3853

3954
const BackToTopButton = ({
4055
onClick,
41-
position,
56+
position = 'bottom-right',
4257
children,
4358
motionStyle,
4459
}: Props) => {
@@ -65,33 +80,6 @@ const BackToTopButton = ({
6580
);
6681
};
6782

68-
// #region statics
69-
BackToTopButton.defaultProps = {
70-
position: 'bottom-right',
71-
};
72-
7383
BackToTopButton.displayName = 'BackToTopButton';
74-
// #endregion
75-
76-
// #region helpers
77-
function setPosition(position = 'bottom-right', refStyle = defaultStyle): any {
78-
const style = { ...refStyle };
79-
80-
switch (position) {
81-
case 'bottom-right':
82-
style.right = sideOffset;
83-
style.left = '';
84-
return style;
85-
86-
case 'bottom-left':
87-
style.right = '';
88-
style.left = sideOffset;
89-
return style;
90-
91-
default:
92-
return refStyle;
93-
}
94-
}
95-
// #endregion
9684

9785
export default BackToTopButton;

front/src/components/backToTop/backToTopButton/UpIcon.js renamed to front/src/components/backToTop/backToTopButton/UpIcon.tsx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1-
// @flow
2-
3-
// #region imports
41
import React from 'react';
5-
// #endregion
62

7-
// #region flow types
3+
// #region types
84
type Props = {
9-
color: string,
5+
color: string;
106
};
117
// #endregion
128

front/src/components/backToTop/backToTopButton/styled/WithRightMargin.js renamed to front/src/components/backToTop/backToTopButton/styled/WithRightMargin.tsx

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
// @flow
2-
3-
// #region imports
41
import styled from 'styled-components';
5-
// #endregion
62

73
const WithRightMargin = styled.div`
84
margin-right: 10px;

0 commit comments

Comments
 (0)