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-08 10:33:58 +00:00
|
|
|
props.withBackdrop && props.needBackground
|
2020-11-25 16:01:57 +00:00
|
|
|
? "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-11-25 09:32:28 +00:00
|
|
|
cursor: ${(props) => (props.withBackdrop ? "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-08 10:33:58 +00:00
|
|
|
needBackground: true,
|
2020-11-25 16:01:57 +00:00
|
|
|
};
|
2020-12-04 08:50:21 +00:00
|
|
|
}
|
|
|
|
|
2020-11-25 16:01:57 +00:00
|
|
|
componentDidUpdate(prevProps) {
|
|
|
|
if (prevProps.visible !== this.props.visible) {
|
2020-12-08 10:33:58 +00:00
|
|
|
const needBackground =
|
|
|
|
document.querySelectorAll(".backdrop-active").length <= 1;
|
|
|
|
this.setState({ needBackground: needBackground });
|
2020-11-25 16:01:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-08 06:49:07 +00:00
|
|
|
onClickHandler = (e) => {
|
|
|
|
const { target, clientX, clientY } = e;
|
|
|
|
const nearOpenDropdown = target.closest("[open]");
|
|
|
|
const nearAside = target.closest(".aside-container");
|
|
|
|
|
|
|
|
if ((nearOpenDropdown || nearAside) && (clientX !== 0 || clientY !== 0)) {
|
|
|
|
let rects;
|
|
|
|
if (nearOpenDropdown) {
|
|
|
|
rects = nearOpenDropdown.getBoundingClientRect();
|
|
|
|
}
|
|
|
|
if (nearAside) {
|
|
|
|
rects = nearAside.getBoundingClientRect();
|
|
|
|
}
|
|
|
|
const { x, y, width, height } = rects;
|
|
|
|
|
|
|
|
if (
|
|
|
|
clientX < x ||
|
|
|
|
clientX > x + width ||
|
|
|
|
clientY < y ||
|
|
|
|
clientY > y + height
|
|
|
|
) {
|
|
|
|
const backdrops = document.querySelectorAll(".backdrop-active");
|
|
|
|
backdrops.forEach((item) => item.click());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.props.onClick && this.props.onClick(e);
|
|
|
|
};
|
|
|
|
|
2020-11-25 16:01:57 +00:00
|
|
|
render() {
|
2020-12-08 10:33:58 +00:00
|
|
|
const { needBackground } = this.state;
|
2020-12-04 08:41:56 +00:00
|
|
|
const { className } = this.props;
|
2020-11-25 16:01:57 +00:00
|
|
|
|
2020-12-04 08:41:56 +00:00
|
|
|
const classNameStr = this.props.visible
|
|
|
|
? this.props.className
|
|
|
|
? `backdrop-active ${className}`
|
|
|
|
: "backdrop-active"
|
|
|
|
: this.props.className
|
2020-12-08 06:49:07 +00:00
|
|
|
? `backdrop-inactive ${className}`
|
2020-12-04 08:41:56 +00:00
|
|
|
: "backdrop-inactive";
|
|
|
|
|
|
|
|
return (
|
2020-12-03 21:20:29 +00:00
|
|
|
<StyledBackdrop
|
2020-12-04 08:41:56 +00:00
|
|
|
className={classNameStr}
|
2020-12-03 21:20:29 +00:00
|
|
|
{...this.props}
|
2020-12-08 10:33:58 +00:00
|
|
|
needBackground={needBackground}
|
2020-12-08 06:49:07 +00:00
|
|
|
onClick={this.onClickHandler}
|
2020-12-03 21:20:29 +00:00
|
|
|
/>
|
|
|
|
);
|
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,
|
|
|
|
className: PropTypes.string,
|
|
|
|
id: PropTypes.string,
|
2020-10-16 13:16:01 +00:00
|
|
|
style: PropTypes.oneOfType([PropTypes.object, PropTypes.array]),
|
2020-11-25 09:32:28 +00:00
|
|
|
withBackdrop: 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-11-25 09:32:28 +00:00
|
|
|
withBackdrop: true,
|
2019-07-15 08:11:13 +00:00
|
|
|
};
|
|
|
|
|
2020-10-16 13:16:01 +00:00
|
|
|
export default Backdrop;
|