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

186 lines
4.8 KiB
JavaScript
Raw Normal View History

import React, { useEffect, useState } from "react";
import styled, { css } from "styled-components";
2021-02-03 12:42:47 +00:00
import PropTypes from "prop-types";
2020-11-17 08:36:49 +00:00
import MobileLayout from "./MobileLayout";
2021-02-25 21:19:45 +00:00
import { size, isSmallTablet } from "@appserver/components/utils/device";
import {
isIOS,
isFirefox,
isSafari,
isMobile,
isMobileOnly,
isChrome,
isTablet,
} from "react-device-detect";
2021-02-03 12:42:47 +00:00
import { inject, observer } from "mobx-react";
2020-10-22 17:09:44 +00:00
const StyledContainer = styled.div`
2020-11-17 08:36:49 +00:00
width: 100%;
height: ${(props) =>
(props.isTabletView || isMobile) && !isFirefox
? `${props.contentHeight}px`
: "100vh"};
#articleScrollBar {
> .scroll-body {
position: ${(props) =>
isMobile && !isSmallTablet()
? props.isArticlePinned
? "static"
: "absolute"
: "absolute"} !important;
${(props) =>
isMobile &&
props.isArticlePinned &&
!isSmallTablet() &&
css`
overflow-y: hidden !important;
overflow-x: hidden !important;
width: 208px;
`}
}
.nav-thumb-horizontal {
${(props) =>
props.isTabletView &&
css`
height: 0 !important;
`}
}
}
2020-11-17 08:36:49 +00:00
`;
const Layout = (props) => {
const { children, isTabletView, setIsTabletView, isArticlePinned } = props;
2020-12-16 11:38:20 +00:00
const [contentHeight, setContentHeight] = useState();
const [isPortrait, setIsPortrait] = useState();
const intervalTime = 100;
const endTimeout = 300;
let intervalHandler;
let timeoutHandler;
useEffect(() => {
setIsPortrait(window.innerHeight > window.innerWidth);
});
useEffect(() => {
const isTablet = window.innerWidth <= size.tablet;
setIsTabletView(isTablet);
let mediaQuery = window.matchMedia("(max-width: 1024px)");
mediaQuery.addEventListener("change", onWidthChange);
return () => {
mediaQuery.removeEventListener("change", onWidthChange);
if (intervalHandler) clearInterval(intervalHandler);
if (timeoutHandler) clearTimeout(timeoutHandler);
};
}, []);
useEffect(() => {
if (isTabletView || isMobile) {
if (isIOS && isSafari) window.addEventListener("resize", onResize);
else window.addEventListener("orientationchange", onOrientationChange);
changeRootHeight();
}
return () => {
if (isTabletView || isMobile) {
if (isIOS && isSafari) window.removeEventListener("resize", onResize);
else
window.removeEventListener("orientationchange", onOrientationChange);
}
};
}, [isTabletView]);
const onWidthChange = (e) => {
const { matches } = e;
setIsTabletView(matches);
};
const onResize = () => {
changeRootHeight();
};
const onOrientationChange = () => {
changeRootHeight();
};
const changeRootHeight = () => {
intervalHandler && clearInterval(intervalHandler);
timeoutHandler && clearTimeout(timeoutHandler);
let lastInnerHeight, noChangeCount;
const updateHeight = () => {
const correctorMobileChrome = 57; // ios
const correctorTabletSafari = 71; // ios
clearInterval(intervalHandler);
clearTimeout(timeoutHandler);
intervalHandler = null;
timeoutHandler = null;
let height = window.innerHeight;
if (isMobileOnly && isIOS && isChrome) {
if (window.innerHeight < window.innerWidth && isPortrait) {
height = window.screen.availWidth - correctorMobileChrome;
}
}
if (isTablet && isIOS && isSafari) {
if (
window.innerHeight < window.innerWidth &&
window.innerWidth > 1024
) {
height = window.screen.availHeight - correctorTabletSafari;
}
}
setContentHeight(height);
};
intervalHandler = setInterval(() => {
//console.log("changeRootHeight setInterval"); TODO: need to refactoring
if (window.innerHeight === lastInnerHeight) {
noChangeCount++;
if (noChangeCount === intervalTime) {
updateHeight();
}
} else {
lastInnerHeight = window.innerHeight;
noChangeCount = 0;
}
});
timeoutHandler = setTimeout(() => {
updateHeight();
}, endTimeout);
};
return (
<StyledContainer
className="Layout"
isTabletView={isTabletView}
contentHeight={contentHeight}
isArticlePinned={isArticlePinned}
>
{isMobile ? <MobileLayout {...props} /> : children}
</StyledContainer>
);
};
2021-01-26 15:21:50 +00:00
Layout.propTypes = {
isTabletView: PropTypes.bool,
2021-02-03 12:42:47 +00:00
children: PropTypes.any,
2021-01-26 15:21:50 +00:00
setIsTabletView: PropTypes.func,
2021-02-20 17:17:27 +00:00
isArticlePinned: PropTypes.bool,
};
2021-02-15 19:43:07 +00:00
export default inject(({ auth }) => {
return {
2021-02-15 19:43:07 +00:00
isTabletView: auth.settingsStore.isTabletView,
Merge branch 'develop' into feature/mobx # Conflicts: # products/ASC.Files/Client/src/App.js # products/ASC.Files/Client/src/components/Article/Body/ThirdPartyList.js # products/ASC.Files/Client/src/components/pages/Home/Section/Body/index.js # products/ASC.Files/Client/src/components/pages/Home/Section/Header/index.js # products/ASC.Files/Client/src/components/pages/Home/index.js # products/ASC.Files/Client/src/store/files/selectors.js # products/ASC.People/Client/src/App.js # products/ASC.People/Client/src/components/pages/GroupAction/Section/Body/index.js # products/ASC.People/Client/src/components/pages/Home/Section/Body/index.js # products/ASC.People/Client/src/components/pages/Home/Section/Header/index.js # products/ASC.People/Client/src/components/pages/Home/index.js # products/ASC.People/Client/src/components/pages/Profile/Section/Body/index.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/src/store/group/actions.js # products/ASC.People/Client/src/store/people/actions.js # products/ASC.People/Client/src/store/people/selectors.js # products/ASC.People/Client/yarn.lock # web/ASC.Web.Client/src/App.js # web/ASC.Web.Client/src/components/pages/About/index.js # web/ASC.Web.Client/src/components/pages/Home/index.js # web/ASC.Web.Client/src/store/settings/actions.js # web/ASC.Web.Common/src/components/Layout/index.js # web/ASC.Web.Common/src/components/NavMenu/index.js # web/ASC.Web.Common/src/components/NavMenu/sub-components/header-nav.js # web/ASC.Web.Common/src/components/NavMenu/sub-components/header-unauth.js # web/ASC.Web.Common/src/components/NavMenu/sub-components/header.js # web/ASC.Web.Common/src/components/NavMenu/sub-components/nav-item.js # web/ASC.Web.Common/src/components/PageLayout/index.js # web/ASC.Web.Common/src/store/auth/actions.js # web/ASC.Web.Common/src/store/auth/reducer.js # web/ASC.Web.Common/src/store/auth/selectors.js # web/ASC.Web.Components/src/components/loader/types/rombs.js
2021-02-20 14:31:58 +00:00
isArticlePinned: auth.settingsStore.isArticlePinned,
setIsTabletView: auth.settingsStore.setIsTabletView,
};
2021-02-03 12:42:47 +00:00
})(observer(Layout));