DocSpace-buildtools/packages/asc-web-common/src/components/Layout/MobileLayout.js

132 lines
3.2 KiB
JavaScript
Raw Normal View History

2020-11-17 08:36:49 +00:00
import React, { Component, createRef } from "react";
Merge branch 'develop' into feature/workspaces # Conflicts: # packages/asc-web-common/src/components/Layout/MobileLayout.js # packages/asc-web-common/src/components/Layout/ScrollToTop.js # packages/asc-web-common/src/components/Layout/context.js # packages/asc-web-common/src/components/Layout/index.js # packages/asc-web-common/src/components/NavMenu/index.js # packages/asc-web-common/src/components/NavMenu/sub-components/header-nav.js # packages/asc-web-common/src/components/PageLayout/index.js # packages/asc-web-common/src/components/PageLayout/sub-components/article.js # packages/asc-web-common/src/components/PageLayout/sub-components/section-header.js # packages/asc-web-common/src/components/PageLayout/sub-components/section.js # packages/asc-web-components/src/components/icons/svg/button.cancel.react.svg # packages/asc-web-components/src/components/icons/svg/clear.active.react.svg # packages/asc-web-components/src/components/icons/svg/load.error.react.svg # products/ASC.Files/Client/package.json # products/ASC.Files/Client/src/App.js # products/ASC.Files/Client/src/store/files/actions.js # products/ASC.People/Client/package.json # products/ASC.People/Client/src/App.js # products/ASC.People/Client/src/components/pages/Profile/index.js # products/ASC.People/Client/src/components/pages/ProfileAction/index.js # products/ASC.People/Client/yarn.lock # web/ASC.Web.Client/package.json # web/ASC.Web.Client/src/App.js # web/ASC.Web.Client/yarn.lock # web/ASC.Web.Common/package.json # web/ASC.Web.Common/yarn.lock # web/ASC.Web.Components/package.json # web/ASC.Web.Components/yarn.lock # yarn.lock
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";
import { isMobile, isSafari, isIOS, isChrome } from "react-device-detect";
const { isTouchDevice } = utils.device;
2020-11-17 08:36:49 +00:00
class MobileLayout extends Component {
constructor(props) {
super(props);
2020-11-17 08:36:49 +00:00
this.state = {
prevScrollPosition: window.pageYOffset,
visibleContent: true,
};
2020-11-17 08:36:49 +00:00
this.scrollRefPage = createRef();
}
2020-11-17 08:36:49 +00:00
componentDidMount() {
this.customScrollElm = document.querySelector(
"#customScrollBar > .scroll-body"
);
if (!isChrome) this.customScrollElm.scrollTo(0, 0);
this.customScrollElm.addEventListener(
2020-11-17 08:36:49 +00:00
"scroll",
this.scrolledTheVerticalAxis
);
// this.setState({ visibleContent: true });
2020-11-17 08:36:49 +00:00
}
componentWillUnmount() {
this.customScrollElm.removeEventListener(
2020-11-17 08:36:49 +00:00
"scroll",
this.scrolledTheVerticalAxis
);
}
scrolledTheVerticalAxis = () => {
const { prevScrollPosition, visibleContent } = this.state;
const currentScrollPosition =
this.customScrollElm.scrollTop > 0 ? this.customScrollElm.scrollTop : 0;
if (document.getElementsByClassName("backdrop-active").length > 0) {
const elements = document.getElementsByClassName("backdrop-active");
elements[0].click();
return;
}
if (visibleContent && isMobile && !isTouchDevice) {
return;
}
if (
(isSafari || isIOS) &&
this.customScrollElm.scrollHeight - this.customScrollElm.clientHeight <
112
) {
if (!this.state.visibleContent)
this.setState({
visibleContent: true,
});
return;
}
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) {
return;
}
if (prevScrollPosition === 0 && currentScrollPosition > 100) {
if (Math.abs(currentScrollPosition - prevScrollPosition) <= 104) {
return;
}
}
2020-12-03 08:02:19 +00:00
let isVisible = prevScrollPosition >= currentScrollPosition;
if (
(isSafari || isIOS) &&
currentScrollPosition >=
this.customScrollElm.scrollHeight - this.customScrollElm.clientHeight &&
this.customScrollElm.scrollHeight !== this.customScrollElm.clientHeight
) {
isVisible = false;
}
2020-11-17 08:36:49 +00:00
this.setState({
prevScrollPosition: currentScrollPosition,
visibleContent: isVisible,
2020-11-17 08:36:49 +00:00
});
};
render() {
const scrollProp = { ref: this.scrollRefPage };
const { children } = this.props;
return (
<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-17 08:36:49 +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;