2020-11-17 08:36:49 +00:00
|
|
|
import React, { Component, createRef } from "react";
|
2021-01-29 13:39:00 +00:00
|
|
|
import { Scrollbar, utils } from "@appserver/components/src";
|
2020-11-17 08:36:49 +00:00
|
|
|
import { LayoutContextProvider } from "./context";
|
2021-01-26 15:21:50 +00:00
|
|
|
import PropTypes from "prop-types";
|
2020-12-03 08:25:08 +00:00
|
|
|
import { isMobile, isSafari, isIOS, isChrome } from "react-device-detect";
|
2020-11-16 09:29:12 +00:00
|
|
|
|
2020-11-24 12:08:46 +00:00
|
|
|
const { isTouchDevice } = utils.device;
|
2020-11-17 08:36:49 +00:00
|
|
|
class MobileLayout extends Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
2020-11-16 09:29:12 +00:00
|
|
|
|
2020-11-17 08:36:49 +00:00
|
|
|
this.state = {
|
|
|
|
prevScrollPosition: window.pageYOffset,
|
|
|
|
visibleContent: true,
|
|
|
|
};
|
2020-11-16 09:29:12 +00:00
|
|
|
|
2020-11-17 08:36:49 +00:00
|
|
|
this.scrollRefPage = createRef();
|
|
|
|
}
|
2020-11-16 09:29:12 +00:00
|
|
|
|
2020-11-17 08:36:49 +00:00
|
|
|
componentDidMount() {
|
2020-12-28 12:34:05 +00:00
|
|
|
this.customScrollElm = document.querySelector(
|
|
|
|
"#customScrollBar > .scroll-body"
|
|
|
|
);
|
2020-11-24 12:08:46 +00:00
|
|
|
|
2020-12-28 12:34:05 +00:00
|
|
|
if (!isChrome) this.customScrollElm.scrollTo(0, 0);
|
2020-11-24 12:08:46 +00:00
|
|
|
|
2020-12-28 12:34:05 +00:00
|
|
|
this.customScrollElm.addEventListener(
|
2020-11-17 08:36:49 +00:00
|
|
|
"scroll",
|
|
|
|
this.scrolledTheVerticalAxis
|
|
|
|
);
|
2020-11-24 12:08:46 +00:00
|
|
|
|
|
|
|
// this.setState({ visibleContent: true });
|
2020-11-17 08:36:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
2020-12-28 12:34:05 +00:00
|
|
|
this.customScrollElm.removeEventListener(
|
2020-11-17 08:36:49 +00:00
|
|
|
"scroll",
|
|
|
|
this.scrolledTheVerticalAxis
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
scrolledTheVerticalAxis = () => {
|
2020-11-24 12:08:46 +00:00
|
|
|
const { prevScrollPosition, visibleContent } = this.state;
|
|
|
|
|
2020-12-01 17:24:35 +00:00
|
|
|
const currentScrollPosition =
|
2021-01-15 13:04:34 +00:00
|
|
|
this.customScrollElm.scrollTop > 0 ? this.customScrollElm.scrollTop : 0;
|
2021-01-19 14:48:03 +00:00
|
|
|
|
2021-01-25 13:34:56 +00:00
|
|
|
if (document.getElementsByClassName("backdrop-active").length > 0) {
|
|
|
|
const elements = document.getElementsByClassName("backdrop-active");
|
|
|
|
elements[0].click();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-11-24 12:08:46 +00:00
|
|
|
if (visibleContent && isMobile && !isTouchDevice) {
|
|
|
|
return;
|
|
|
|
}
|
2021-01-19 14:48:03 +00:00
|
|
|
if (
|
|
|
|
(isSafari || isIOS) &&
|
|
|
|
this.customScrollElm.scrollHeight - this.customScrollElm.clientHeight <
|
|
|
|
112
|
|
|
|
) {
|
|
|
|
if (!this.state.visibleContent)
|
|
|
|
this.setState({
|
|
|
|
visibleContent: true,
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
2020-11-18 15:34:36 +00:00
|
|
|
|
2021-01-15 13:04:34 +00:00
|
|
|
if (
|
|
|
|
(isSafari || isIOS) &&
|
|
|
|
Math.abs(currentScrollPosition - prevScrollPosition) <= 112 &&
|
|
|
|
currentScrollPosition === 0
|
|
|
|
) {
|
|
|
|
if (!this.state.visibleContent)
|
|
|
|
this.setState({
|
|
|
|
visibleContent: true,
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Math.abs(currentScrollPosition - prevScrollPosition) <= 112) {
|
2020-11-26 19:53:42 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (prevScrollPosition === 0 && currentScrollPosition > 100) {
|
|
|
|
if (Math.abs(currentScrollPosition - prevScrollPosition) <= 104) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2020-11-26 13:37:27 +00:00
|
|
|
|
2020-12-03 08:02:19 +00:00
|
|
|
let isVisible = prevScrollPosition >= currentScrollPosition;
|
2021-01-19 14:48:03 +00:00
|
|
|
|
2020-11-19 07:00:31 +00:00
|
|
|
if (
|
2020-11-25 16:47:09 +00:00
|
|
|
(isSafari || isIOS) &&
|
2020-11-19 07:00:31 +00:00
|
|
|
currentScrollPosition >=
|
2020-12-28 12:34:05 +00:00
|
|
|
this.customScrollElm.scrollHeight - this.customScrollElm.clientHeight &&
|
|
|
|
this.customScrollElm.scrollHeight !== this.customScrollElm.clientHeight
|
2020-11-19 07:00:31 +00:00
|
|
|
) {
|
2020-11-24 12:08:46 +00:00
|
|
|
isVisible = false;
|
2020-11-19 07:00:31 +00:00
|
|
|
}
|
|
|
|
|
2020-11-17 08:36:49 +00:00
|
|
|
this.setState({
|
|
|
|
prevScrollPosition: currentScrollPosition,
|
2020-11-24 12:08:46 +00:00
|
|
|
visibleContent: isVisible,
|
2020-11-17 08:36:49 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const scrollProp = { ref: this.scrollRefPage };
|
|
|
|
const { children } = this.props;
|
|
|
|
|
|
|
|
return (
|
2020-12-28 12:34:05 +00:00
|
|
|
<Scrollbar id="customScrollBar" {...scrollProp} stype="mediumBlack">
|
2020-11-17 08:36:49 +00:00
|
|
|
<LayoutContextProvider
|
|
|
|
value={{
|
|
|
|
scrollRefLayout: this.scrollRefPage,
|
|
|
|
isVisible: this.state.visibleContent,
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{children}
|
|
|
|
</LayoutContextProvider>
|
|
|
|
</Scrollbar>
|
|
|
|
);
|
2020-11-16 09:29:12 +00:00
|
|
|
}
|
2020-11-17 08:36:49 +00:00
|
|
|
}
|
2020-11-16 09:29:12 +00:00
|
|
|
|
2021-01-26 15:21:50 +00:00
|
|
|
MobileLayout.propTypes = {
|
|
|
|
children: PropTypes.any,
|
|
|
|
};
|
|
|
|
|
2020-11-17 08:36:49 +00:00
|
|
|
export default MobileLayout;
|