DocSpace-client/web/ASC.Web.Common/src/components/PageLayout/sub-components/section-header.js

115 lines
2.5 KiB
JavaScript
Raw Normal View History

import React from "react";
2020-02-10 14:34:56 +00:00
import styled from "styled-components";
import { utils } from "asc-web-components";
import isEqual from "lodash/isEqual";
import classnames from "classnames";
const { tablet } = utils.device;
const StyledSectionHeader = styled.div`
border-bottom: 1px solid #eceef1;
height: 56px;
margin-right: 24px;
@media ${tablet} {
margin-right: 16px;
2020-08-18 14:02:20 +00:00
border-bottom: none;
height: 44px;
}
.section-header--hidden {
@media ${tablet} {
top: -50px;
}
}
.section-header {
width: calc(100% - 76px);
@media ${tablet} {
width: 100%;
background-color: #fff;
position: fixed;
top: 56px;
padding-right:100%;
transition: top 0.3s;
z-index:1;
}
h1,
h2,
h3,
h4,
h5,
h6 {
max-width: calc(100vw - 435px);
@media ${tablet} {
max-width: ${(props) =>
props.isArticlePinned ? `calc(100vw - 320px)` : `calc(100vw - 96px)`};
}
}
}
`;
class SectionHeader extends React.Component {
constructor(props) {
super(props);
this.state = {
2020-10-29 10:23:21 +00:00
prevScrollPosition: window.pageYOffset || document.documentElement.scrollTop,
visibleContent: true
};
this.focusRef = React.createRef();
}
componentDidMount() {
2020-10-29 10:23:21 +00:00
window.addEventListener("scroll", this.scrolledTheVerticalAxis,false);
}
componentWillUnmount() {
2020-10-29 10:23:21 +00:00
window.removeEventListener("scroll", this.scrolledTheVerticalAxis,false);
}
scrolledTheVerticalAxis = () => {
2020-10-29 10:23:21 +00:00
const { prevScrollPosition } = this.state;
// const first = document.documentElement.offsetTop
// const second = document.body.scrollTop || document.documentElement.scrollTop
const currentScrollPosition = window.pageYOffset || document.documentElement.scrollTop;
const visibleContent = prevScrollPosition > currentScrollPosition;
this.setState({
2020-10-29 10:23:21 +00:00
prevScrollPosition: currentScrollPosition,
visibleContent
});
};
shouldComponentUpdate(nextProps) {
return !isEqual(this.props, nextProps);
}
render() {
//console.log("PageLayout SectionHeader render");
// eslint-disable-next-line react/prop-types
const { isArticlePinned, ...rest } = this.props;
return (
<StyledSectionHeader isArticlePinned={isArticlePinned}>
<div id="scroll" className={classnames("section-header" , {
2020-10-29 10:23:21 +00:00
"section-header--hidden": !this.state.visibleContent
})} {...rest} />
</StyledSectionHeader>
);
}
}
SectionHeader.displayName = "SectionHeader";
export default SectionHeader;