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

96 lines
2.5 KiB
JavaScript
Raw Normal View History

import React, { Component, createRef, useRef, useEffect} from "react"
2020-10-22 17:09:44 +00:00
import styled from "styled-components";
import { Scrollbar} from "asc-web-components";
import {LayoutContextProvider} from "./context"
2020-10-22 17:09:44 +00:00
const StyledContainer = styled.div`
width:100%;
height:100vh;
2020-10-22 17:09:44 +00:00
`
class ExpandLayout extends Component{
constructor(props) {
super(props);
2020-10-22 17:09:44 +00:00
this.windowWidth = window.matchMedia( "(max-width: 1024px)" );
this.state = {
isTablet: window.innerWidth < 1024,
prevScrollPosition: window.pageYOffset,
visibleContent: true,
};
this.scrollRefPage = createRef();
}
componentDidMount() {
this.getElementById = document.getElementById("scroll");
this.getElementById.addEventListener("scroll", this.scrolledTheVerticalAxis);
}
componentWillUnmount() {
this.getElementById.removeEventListener("scroll", this.scrolledTheVerticalAxis);
}
scrolledTheVerticalAxis = () => {
const { prevScrollPosition } = this.state;
const currentScrollPosition = this.getElementById.scrollTop || window.pageYOffset ;
let visibleContent = prevScrollPosition >= currentScrollPosition;
if (!visibleContent && (this.getElementById.scrollHeight - this.getElementById.clientHeight < 57)) {
visibleContent = true
}
this.setState({
prevScrollPosition: currentScrollPosition,
visibleContent
});
};
render() {
const scrollProp = { ref: this.scrollRefPage } ;
const { children, windowWidth } = this.props
return(
<StyledContainer className="Layout">
{windowWidth && windowWidth.matches
? <Scrollbar {...scrollProp} stype="mediumBlack">
<LayoutContextProvider value={{scrollRefLayout: this.scrollRefPage, isVisible:this.state.visibleContent}}>
{ children }
</LayoutContextProvider>
</Scrollbar>
: children
}
</StyledContainer>
)
}
}
2020-10-27 08:13:42 +00:00
const Layout = (props) => {
// const scrollRefPage = useRef(null)
// const scrollProp = { ref: scrollRefPage } ;
const isTablet = window.innerWidth < 1024;
2020-10-22 17:09:44 +00:00
const [windowWidth, setWindowWidth] = React.useState({
matches: isTablet,
});
useEffect(() => {
let mediaQuery = window.matchMedia("(max-width: 1024px)");
mediaQuery.addListener(setWindowWidth);
return () => mediaQuery.removeListener(setWindowWidth);
}, []);
return <ExpandLayout windowWidth={windowWidth} {...props}/>;
}
2020-10-22 17:09:44 +00:00
export default Layout;