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

89 lines
2.1 KiB
JavaScript
Raw Normal View History

2020-11-17 08:36:49 +00:00
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";
2020-12-03 08:02:19 +00:00
import { isIOS, isFirefox } from "react-device-detect";
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: ${isIOS && !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) => {
2020-11-17 08:36:49 +00:00
const { children } = props;
const isTablet = window.innerWidth <= size.tablet;
2020-10-22 17:09:44 +00:00
const [windowWidth, setWindowWidth] = useState({
matches: isTablet,
});
useEffect(() => {
let mediaQuery = window.matchMedia("(max-width: 1024px)");
mediaQuery.addListener(setWindowWidth);
return () => mediaQuery.removeListener(setWindowWidth);
}, []);
useEffect(() => {
2020-12-03 08:02:19 +00:00
if (isIOS && !isFirefox) {
window.addEventListener("resize", resizeHandler);
resizeHandler();
}
return () => {
2020-12-03 08:02:19 +00:00
if (isIOS && !isFirefox) {
window.removeEventListener("resize", resizeHandler);
}
};
}, []);
const resizeHandler = () => {
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;
const vh = (window.innerHeight - 57) * 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">
2020-11-17 08:36:49 +00:00
{windowWidth && windowWidth.matches ? (
<MobileLayout {...props} />
2020-11-17 08:36:49 +00:00
) : (
children
)}
</StyledContainer>
);
2020-11-17 08:36:49 +00:00
};
export default Layout;