2020-10-16 13:16:01 +00:00
|
|
|
import React from "react";
|
|
|
|
import PropTypes from "prop-types";
|
2019-07-04 16:01:56 +00:00
|
|
|
|
2021-02-04 09:28:04 +00:00
|
|
|
import StyledBackdrop from "./styled-backdrop";
|
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 = () => {
|
2022-05-11 09:16:39 +00:00
|
|
|
const { visible, isAside, withBackground, withoutBlur } = this.props;
|
2020-12-10 16:40:23 +00:00
|
|
|
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
|
|
|
|
2022-05-04 12:38:35 +00:00
|
|
|
let needBackground =
|
2022-05-11 09:16:39 +00:00
|
|
|
needBackdrop && ((isTablet && !withoutBlur) || 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;
|
2022-04-19 12:42:38 +00:00
|
|
|
let modifiedClass = "backdrop-active not-selectable";
|
2020-12-11 12:25:53 +00:00
|
|
|
|
|
|
|
if (className) {
|
|
|
|
if (typeof className !== "string") {
|
|
|
|
if (!className.includes(modifiedClass)) {
|
|
|
|
modifiedClass = className.push(modifiedClass);
|
|
|
|
} else {
|
|
|
|
modifiedClass = className;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
modifiedClass += ` ${className}`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return modifiedClass;
|
|
|
|
};
|
|
|
|
|
2021-02-12 12:48:15 +00:00
|
|
|
onTouchHandler = (e) => {
|
2021-03-19 15:08:10 +00:00
|
|
|
const { isModalDialog } = this.props;
|
|
|
|
!isModalDialog && e.preventDefault();
|
2021-01-25 16:40:52 +00:00
|
|
|
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-02-12 12:48:15 +00:00
|
|
|
onTouchMove={this.onTouchHandler}
|
|
|
|
onTouchEnd={this.onTouchHandler}
|
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 = {
|
2021-03-07 11:57:26 +00:00
|
|
|
/** Display or not */
|
2019-07-15 08:11:13 +00:00
|
|
|
visible: PropTypes.bool,
|
2021-03-07 11:57:26 +00:00
|
|
|
/** CSS z-index */
|
2019-11-26 08:58:57 +00:00
|
|
|
zIndex: PropTypes.number,
|
2021-03-07 11:57:26 +00:00
|
|
|
/** Accepts class */
|
2020-12-11 12:25:53 +00:00
|
|
|
className: PropTypes.oneOfType([PropTypes.string, PropTypes.array]),
|
2021-03-07 11:57:26 +00:00
|
|
|
/** Accepts id */
|
2019-11-26 08:58:57 +00:00
|
|
|
id: PropTypes.string,
|
2021-03-07 11:57:26 +00:00
|
|
|
/** Accepts css style */
|
2020-10-16 13:16:01 +00:00
|
|
|
style: PropTypes.oneOfType([PropTypes.object, PropTypes.array]),
|
2021-03-07 11:57:26 +00:00
|
|
|
/** The background is not displayed if the viewport width is less than 1024,
|
|
|
|
* set it to true for display */
|
2020-12-10 16:40:23 +00:00
|
|
|
withBackground: PropTypes.bool,
|
2021-03-07 11:57:26 +00:00
|
|
|
/** Must be true if used with Aside component */
|
2020-12-10 16:40:23 +00:00
|
|
|
isAside: PropTypes.bool,
|
2022-05-04 12:38:35 +00:00
|
|
|
/** Must be true if used with Context menu */
|
2022-05-11 09:16:39 +00:00
|
|
|
withoutBlur: PropTypes.bool,
|
2019-07-15 08:11:13 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Backdrop.defaultProps = {
|
|
|
|
visible: false,
|
2022-05-04 09:08:48 +00:00
|
|
|
zIndex: 203,
|
2020-12-10 16:40:23 +00:00
|
|
|
withBackground: false,
|
|
|
|
isAside: false,
|
2021-03-19 15:08:10 +00:00
|
|
|
isModalDialog: false,
|
2022-05-11 09:16:39 +00:00
|
|
|
withoutBlur: false,
|
2019-07-15 08:11:13 +00:00
|
|
|
};
|
|
|
|
|
2020-10-16 13:16:01 +00:00
|
|
|
export default Backdrop;
|