DocSpace-buildtools/web/ASC.Web.Common/src/components/Layout/index.js

126 lines
3.2 KiB
JavaScript
Raw Normal View History

import React, { useEffect, useState } from "react";
2020-10-22 17:09:44 +00:00
import styled from "styled-components";
2020-11-17 08:36:49 +00:00
import MobileLayout from "./MobileLayout";
import { utils } from "asc-web-components";
import { isIOS, isFirefox, isChrome, isSafari } from "react-device-detect";
import { connect } from "react-redux";
import store from "../../store";
const { setIsTabletView } = store.auth.actions;
const { getIsTabletView } = store.auth.selectors;
const { size } = utils.device;
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 && !isFirefox
? "calc(var(--vh, 1vh) * 100 + 57px)"
: "100vh"};
2020-11-17 08:36:49 +00:00
`;
2020-10-27 08:13:42 +00:00
const Layout = (props) => {
const { children, isTabletView, setIsTabletView } = props;
const [isInitPortrait, setInitOrientation] = useState(
window.innerHeight > window.innerWidth
);
useEffect(() => {
const isTablet = window.innerWidth <= size.tablet;
setIsTabletView(isTablet);
let mediaQuery = window.matchMedia("(max-width: 1024px)");
mediaQuery.addEventListener("change", isViewChangeHandler);
return () => mediaQuery.removeEventListener("change", isViewChangeHandler);
}, []);
useEffect(() => {
if (isTabletView) {
if (isIOS && isSafari)
window.addEventListener("resize", orientationChangeHandler);
else
window.addEventListener("orientationchange", orientationChangeHandler);
orientationChangeHandler();
}
return () => {
if (isTabletView) {
2020-12-14 14:20:26 +00:00
if (isIOS && isSafari)
window.removeEventListener("resize", orientationChangeHandler);
else
window.removeEventListener(
"orientationchange",
orientationChangeHandler
);
}
};
}, [isTabletView]);
const isViewChangeHandler = (e) => {
const { matches } = e;
setIsTabletView(matches);
};
const orientationChangeHandler = () => {
const intervalTime = 100;
2020-12-03 08:02:19 +00:00
const endTimeout = 300;
let interval, timeout, lastInnerHeight, noChangeCount;
const updateHeight = () => {
clearInterval(interval);
clearTimeout(timeout);
interval = null;
timeout = null;
let vh = (window.innerHeight - 57) * 0.01;
if (
isChrome &&
isInitPortrait &&
window.innerHeight < window.innerWidth
) {
vh = window.innerHeight * 0.01;
}
document.documentElement.style.setProperty("--vh", `${vh}px`);
};
interval = setInterval(() => {
if (window.innerHeight === lastInnerHeight) {
noChangeCount++;
if (noChangeCount === intervalTime) {
updateHeight();
}
} else {
lastInnerHeight = window.innerHeight;
noChangeCount = 0;
}
});
timeout = setTimeout(() => {
updateHeight();
}, endTimeout);
};
2020-11-17 08:36:49 +00:00
return (
<StyledContainer className="Layout" isTabletView={isTabletView}>
{isTabletView ? <MobileLayout {...props} /> : children}
</StyledContainer>
);
2020-11-17 08:36:49 +00:00
};
const mapStateToProps = (state) => {
return {
isTabletView: getIsTabletView(state),
};
};
const mapDispatchToProps = (dispatch) => {
return {
setIsTabletView: (isTabletView) => dispatch(setIsTabletView(isTabletView)),
};
};
export default connect(mapStateToProps, mapDispatchToProps)(Layout);