2020-10-16 13:16:01 +00:00
|
|
|
import React from "react";
|
|
|
|
import PropTypes from "prop-types";
|
|
|
|
import styled from "styled-components";
|
2019-07-04 16:01:56 +00:00
|
|
|
|
|
|
|
const StyledBackdrop = styled.div`
|
2020-11-25 09:32:28 +00:00
|
|
|
background-color: ${(props) =>
|
2020-12-10 16:40:23 +00:00
|
|
|
props.needBackground ? "rgba(6, 22, 38, 0.1)" : "unset"};
|
2020-10-16 13:16:01 +00:00
|
|
|
display: ${(props) => (props.visible ? "block" : "none")};
|
2019-07-04 16:01:56 +00:00
|
|
|
height: 100vh;
|
|
|
|
position: fixed;
|
2019-07-10 09:49:14 +00:00
|
|
|
width: 100vw;
|
2020-10-16 13:16:01 +00:00
|
|
|
z-index: ${(props) => props.zIndex};
|
2019-07-15 08:11:13 +00:00
|
|
|
left: 0;
|
|
|
|
top: 0;
|
2020-12-10 16:40:23 +00:00
|
|
|
cursor: ${(props) => (props.needBackground ? "pointer" : "default")}; ;
|
2019-07-04 16:01:56 +00:00
|
|
|
`;
|
|
|
|
|
2020-11-25 16:01:57 +00:00
|
|
|
class Backdrop extends React.Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
this.state = {
|
2020-12-10 16:40:23 +00:00
|
|
|
needBackdrop: false,
|
|
|
|
needBackground: false,
|
2020-11-25 16:01:57 +00:00
|
|
|
};
|
2020-12-10 16:40:23 +00:00
|
|
|
|
|
|
|
this.backdropRef = React.createRef();
|
2020-12-04 08:50:21 +00:00
|
|
|
}
|
|
|
|
|
2020-11-25 16:01:57 +00:00
|
|
|
componentDidUpdate(prevProps) {
|
2020-12-10 16:40:23 +00:00
|
|
|
if (
|
|
|
|
prevProps.visible !== this.props.visible ||
|
|
|
|
prevProps.isAside !== this.props.isAside ||
|
|
|
|
prevProps.withBackground !== this.props.withBackground
|
|
|
|
) {
|
|
|
|
this.checkingExistBackdrop();
|
2020-11-25 16:01:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-10 16:40:23 +00:00
|
|
|
componentDidMount() {
|
|
|
|
this.checkingExistBackdrop();
|
|
|
|
}
|
|
|
|
|
|
|
|
checkingExistBackdrop = () => {
|
|
|
|
const { visible, isAside, withBackground } = this.props;
|
|
|
|
if (visible) {
|
|
|
|
const isTablet = window.innerWidth < 1024;
|
|
|
|
const backdrops = document.querySelectorAll(".backdrop-active");
|
|
|
|
|
2020-12-11 10:25:02 +00:00
|
|
|
const needBackdrop =
|
|
|
|
backdrops.length < 1 || (isAside && backdrops.length <= 1);
|
2020-12-10 16:40:23 +00:00
|
|
|
|
2020-12-11 10:25:02 +00:00
|
|
|
let needBackground = needBackdrop && (isTablet || withBackground);
|
2020-12-10 16:40:23 +00:00
|
|
|
|
2020-12-11 10:25:02 +00:00
|
|
|
if (isAside && needBackdrop) needBackground = true;
|
2020-12-10 16:40:23 +00:00
|
|
|
|
|
|
|
this.setState({
|
|
|
|
needBackdrop: needBackdrop,
|
|
|
|
needBackground: needBackground,
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.setState({
|
|
|
|
needBackground: false,
|
|
|
|
needBackdrop: false,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-12-11 12:25:53 +00:00
|
|
|
modifyClassName = () => {
|
|
|
|
const { className } = this.props;
|
|
|
|
let modifiedClass = "backdrop-active";
|
|
|
|
|
|
|
|
if (className) {
|
|
|
|
if (typeof className !== "string") {
|
|
|
|
if (!className.includes(modifiedClass)) {
|
|
|
|
modifiedClass = className.push(modifiedClass);
|
|
|
|
} else {
|
|
|
|
modifiedClass = className;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
modifiedClass += ` ${className}`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return modifiedClass;
|
|
|
|
};
|
|
|
|
|
2021-01-25 16:40:52 +00:00
|
|
|
onTouchMoveHandler = (e) => {
|
|
|
|
this.backdropRef.current.click();
|
|
|
|
};
|
|
|
|
|
2020-11-25 16:01:57 +00:00
|
|
|
render() {
|
2020-12-10 16:40:23 +00:00
|
|
|
const { needBackdrop, needBackground } = this.state;
|
2020-12-11 12:25:53 +00:00
|
|
|
const { isAside, visible } = this.props;
|
2020-12-10 16:40:23 +00:00
|
|
|
|
2020-12-11 12:25:53 +00:00
|
|
|
const modifiedClassName = this.modifyClassName();
|
2020-12-10 16:40:23 +00:00
|
|
|
|
|
|
|
return visible && (needBackdrop || isAside) ? (
|
2020-12-03 21:20:29 +00:00
|
|
|
<StyledBackdrop
|
|
|
|
{...this.props}
|
2020-12-10 16:40:23 +00:00
|
|
|
ref={this.backdropRef}
|
2020-12-11 12:25:53 +00:00
|
|
|
className={modifiedClassName}
|
2020-12-08 10:33:58 +00:00
|
|
|
needBackground={needBackground}
|
2020-12-10 16:40:23 +00:00
|
|
|
visible={visible}
|
2021-01-25 16:40:52 +00:00
|
|
|
onTouchMove={this.onTouchMoveHandler}
|
2020-12-03 21:20:29 +00:00
|
|
|
/>
|
2020-12-10 16:40:23 +00:00
|
|
|
) : null;
|
2020-11-25 16:01:57 +00:00
|
|
|
}
|
|
|
|
}
|
2019-07-04 16:01:56 +00:00
|
|
|
|
2019-07-15 08:11:13 +00:00
|
|
|
Backdrop.propTypes = {
|
|
|
|
visible: PropTypes.bool,
|
2019-11-26 08:58:57 +00:00
|
|
|
zIndex: PropTypes.number,
|
2020-12-11 12:25:53 +00:00
|
|
|
className: PropTypes.oneOfType([PropTypes.string, PropTypes.array]),
|
2019-11-26 08:58:57 +00:00
|
|
|
id: PropTypes.string,
|
2020-10-16 13:16:01 +00:00
|
|
|
style: PropTypes.oneOfType([PropTypes.object, PropTypes.array]),
|
2020-12-10 16:40:23 +00:00
|
|
|
withBackground: PropTypes.bool,
|
|
|
|
isAside: PropTypes.bool,
|
2019-07-15 08:11:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Backdrop.defaultProps = {
|
|
|
|
visible: false,
|
2020-10-16 13:16:01 +00:00
|
|
|
zIndex: 200,
|
2020-12-10 16:40:23 +00:00
|
|
|
withBackground: false,
|
|
|
|
isAside: false,
|
2019-07-15 08:11:13 +00:00
|
|
|
};
|
|
|
|
|
2020-10-16 13:16:01 +00:00
|
|
|
export default Backdrop;
|