2021-02-04 19:15:23 +01:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import React, { Component } from 'react';
|
|
|
|
import { findDOMNode } from 'react-dom';
|
|
|
|
import ReactCSSTransitionGroup from 'react-transition-group/CSSTransitionGroup';
|
2017-08-04 20:47:36 +02:00
|
|
|
|
2020-01-10 16:13:21 +01:00
|
|
|
export default class MenuDroppoComponent extends Component {
|
|
|
|
static propTypes = {
|
|
|
|
isOpen: PropTypes.bool.isRequired,
|
|
|
|
innerStyle: PropTypes.object,
|
|
|
|
children: PropTypes.node.isRequired,
|
2020-01-16 22:48:09 +01:00
|
|
|
onClickOutside: PropTypes.func,
|
2020-01-10 16:13:21 +01:00
|
|
|
containerClassName: PropTypes.string,
|
|
|
|
zIndex: PropTypes.number,
|
|
|
|
style: PropTypes.object.isRequired,
|
|
|
|
useCssTransition: PropTypes.bool,
|
|
|
|
speed: PropTypes.string,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2017-08-04 20:47:36 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
renderPrimary() {
|
2021-02-04 19:15:23 +01:00
|
|
|
const { isOpen } = this.props;
|
2020-01-10 16:13:21 +01:00
|
|
|
if (!isOpen) {
|
2021-02-04 19:15:23 +01:00
|
|
|
return null;
|
2020-01-10 16:13:21 +01:00
|
|
|
}
|
2017-08-04 20:47:36 +02:00
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
const innerStyle = this.props.innerStyle || {};
|
2020-01-10 16:13:21 +01:00
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="menu-droppo" key="menu-droppo-drawer" style={innerStyle}>
|
|
|
|
{this.props.children}
|
|
|
|
</div>
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2020-01-10 16:13:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
globalClickOccurred = (event) => {
|
2021-02-04 19:15:23 +01:00
|
|
|
const { target } = event;
|
2020-01-10 16:13:21 +01:00
|
|
|
// eslint-disable-next-line react/no-find-dom-node
|
2021-02-04 19:15:23 +01:00
|
|
|
const container = findDOMNode(this);
|
2020-01-10 16:13:21 +01:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
if (
|
2021-01-30 16:42:55 +01:00
|
|
|
this.props.isOpen &&
|
2020-11-03 00:41:28 +01:00
|
|
|
target !== container &&
|
2020-01-10 16:13:21 +01:00
|
|
|
!isDescendant(this.container, event.target) &&
|
2021-01-29 21:34:15 +01:00
|
|
|
this.props.onClickOutside
|
2020-11-03 00:41:28 +01:00
|
|
|
) {
|
2021-02-04 19:15:23 +01:00
|
|
|
this.props.onClickOutside(event);
|
2020-01-10 16:13:21 +01:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2020-01-10 16:13:21 +01:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
componentDidMount() {
|
2020-01-10 16:13:21 +01:00
|
|
|
if (this && document.body) {
|
2021-02-04 19:15:23 +01:00
|
|
|
document.body.addEventListener('click', this.globalClickOccurred);
|
2020-01-10 16:13:21 +01:00
|
|
|
// eslint-disable-next-line react/no-find-dom-node
|
2021-02-04 19:15:23 +01:00
|
|
|
const container = findDOMNode(this);
|
|
|
|
this.container = container;
|
2020-01-10 16:13:21 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
componentWillUnmount() {
|
2020-01-10 16:13:21 +01:00
|
|
|
if (this && document.body) {
|
2021-02-04 19:15:23 +01:00
|
|
|
document.body.removeEventListener('click', this.globalClickOccurred);
|
2020-01-10 16:13:21 +01:00
|
|
|
}
|
|
|
|
}
|
2017-08-04 20:47:36 +02:00
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
render() {
|
2021-02-04 19:15:23 +01:00
|
|
|
const { containerClassName = '', style } = this.props;
|
|
|
|
const speed = this.props.speed || '300ms';
|
|
|
|
const { useCssTransition } = this.props;
|
|
|
|
const zIndex = 'zIndex' in this.props ? this.props.zIndex : 0;
|
2017-08-04 20:47:36 +02:00
|
|
|
|
2020-08-19 18:27:05 +02:00
|
|
|
const baseStyle = {
|
|
|
|
position: 'fixed',
|
|
|
|
...style,
|
|
|
|
zIndex,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2017-08-04 20:47:36 +02:00
|
|
|
|
2020-01-10 16:13:21 +01:00
|
|
|
return (
|
2020-11-03 00:41:28 +01:00
|
|
|
<div
|
|
|
|
style={baseStyle}
|
|
|
|
className={`menu-droppo-container ${containerClassName}`}
|
|
|
|
>
|
|
|
|
<style>
|
|
|
|
{`
|
2019-11-23 17:19:36 +01:00
|
|
|
.menu-droppo-enter {
|
|
|
|
transition: transform ${speed} ease-in-out;
|
|
|
|
transform: translateY(-200%);
|
|
|
|
}
|
|
|
|
|
|
|
|
.menu-droppo-enter.menu-droppo-enter-active {
|
|
|
|
transition: transform ${speed} ease-in-out;
|
|
|
|
transform: translateY(0%);
|
|
|
|
}
|
|
|
|
|
|
|
|
.menu-droppo-leave {
|
|
|
|
transition: transform ${speed} ease-in-out;
|
|
|
|
transform: translateY(0%);
|
|
|
|
}
|
|
|
|
|
|
|
|
.menu-droppo-leave.menu-droppo-leave-active {
|
|
|
|
transition: transform ${speed} ease-in-out;
|
|
|
|
transform: translateY(-200%);
|
|
|
|
}
|
|
|
|
`}
|
2020-01-10 16:13:21 +01:00
|
|
|
</style>
|
2020-11-03 00:41:28 +01:00
|
|
|
{useCssTransition ? (
|
|
|
|
<ReactCSSTransitionGroup
|
|
|
|
className="css-transition-group"
|
|
|
|
transitionName="menu-droppo"
|
|
|
|
transitionEnterTimeout={parseInt(speed, 10)}
|
|
|
|
transitionLeaveTimeout={parseInt(speed, 10)}
|
|
|
|
>
|
|
|
|
{this.renderPrimary()}
|
|
|
|
</ReactCSSTransitionGroup>
|
|
|
|
) : (
|
|
|
|
this.renderPrimary()
|
|
|
|
)}
|
2020-01-10 16:13:21 +01:00
|
|
|
</div>
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
2017-08-04 20:47:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-03 00:41:28 +01:00
|
|
|
function isDescendant(parent, child) {
|
2021-02-04 19:15:23 +01:00
|
|
|
let node = child.parentNode;
|
2017-08-04 22:31:18 +02:00
|
|
|
while (node !== null) {
|
|
|
|
if (node === parent) {
|
2021-02-04 19:15:23 +01:00
|
|
|
return true;
|
2017-08-04 22:31:18 +02:00
|
|
|
}
|
2021-02-04 19:15:23 +01:00
|
|
|
node = node.parentNode;
|
2017-08-04 22:31:18 +02:00
|
|
|
}
|
|
|
|
|
2021-02-04 19:15:23 +01:00
|
|
|
return false;
|
2017-08-04 20:47:36 +02:00
|
|
|
}
|