2016-07-16 02:06:10 +00:00
|
|
|
import React, {Component, PropTypes} from 'react';
|
|
|
|
import classnames from 'classnames';
|
2016-11-10 02:40:53 +00:00
|
|
|
import {isMac} from '../../../common/constants';
|
2016-04-07 03:09:14 +00:00
|
|
|
|
2016-11-07 20:24:38 +00:00
|
|
|
// Keep global z-index reference so that every modal will
|
|
|
|
// appear over top of an existing one.
|
|
|
|
let globalZIndex = 1000;
|
2016-07-06 22:11:37 +00:00
|
|
|
|
2016-04-07 03:09:14 +00:00
|
|
|
class Modal extends Component {
|
2016-11-29 21:28:22 +00:00
|
|
|
state = {
|
|
|
|
open: false,
|
|
|
|
forceRefreshCounter: 0,
|
|
|
|
zIndex: globalZIndex
|
|
|
|
};
|
2016-07-06 20:18:26 +00:00
|
|
|
|
2016-11-29 21:28:22 +00:00
|
|
|
_handleSetNodeRef = n => this._node = n;
|
|
|
|
|
|
|
|
_handleClick = e => {
|
2016-04-07 03:09:14 +00:00
|
|
|
// Did we click a close button. Let's check a few parent nodes up as well
|
|
|
|
// because some buttons might have nested elements. Maybe there is a better
|
|
|
|
// way to check this?
|
|
|
|
let target = e.target;
|
2016-07-06 20:18:26 +00:00
|
|
|
let shouldHide = false;
|
2016-04-08 04:05:08 +00:00
|
|
|
|
2016-04-07 03:09:14 +00:00
|
|
|
for (let i = 0; i < 5; i++) {
|
|
|
|
if (target.hasAttribute('data-close-modal')) {
|
2016-07-06 20:18:26 +00:00
|
|
|
shouldHide = true;
|
2016-04-07 03:09:14 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
target = target.parentNode;
|
|
|
|
}
|
2016-04-08 04:05:08 +00:00
|
|
|
|
2016-07-06 20:18:26 +00:00
|
|
|
if (shouldHide) {
|
|
|
|
this.hide();
|
2016-04-07 03:27:45 +00:00
|
|
|
}
|
2016-11-29 21:28:22 +00:00
|
|
|
};
|
2016-11-23 23:57:34 +00:00
|
|
|
|
2016-07-07 20:10:55 +00:00
|
|
|
show () {
|
2016-11-29 21:28:22 +00:00
|
|
|
const {freshState} = this.props;
|
|
|
|
const {forceRefreshCounter} = this.state;
|
|
|
|
|
|
|
|
this.setState({
|
|
|
|
open: true,
|
|
|
|
zIndex: globalZIndex++,
|
|
|
|
forceRefreshCounter: forceRefreshCounter + (freshState ? 1 : 0),
|
|
|
|
});
|
2016-08-25 21:34:03 +00:00
|
|
|
|
|
|
|
if (this.props.dontFocus) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-08-15 17:04:36 +00:00
|
|
|
setTimeout(() => {
|
2016-11-09 03:18:25 +00:00
|
|
|
this._node && this._node.focus();
|
2016-07-06 22:11:37 +00:00
|
|
|
});
|
2016-07-06 20:18:26 +00:00
|
|
|
}
|
|
|
|
|
2016-07-07 20:10:55 +00:00
|
|
|
toggle () {
|
2016-07-06 20:18:26 +00:00
|
|
|
if (this.state.open) {
|
|
|
|
this.hide();
|
|
|
|
} else {
|
|
|
|
this.show();
|
2016-04-09 19:24:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-07 20:10:55 +00:00
|
|
|
hide () {
|
2016-07-06 20:18:26 +00:00
|
|
|
this.setState({open: false});
|
2016-04-07 03:09:14 +00:00
|
|
|
}
|
|
|
|
|
2016-08-15 17:04:36 +00:00
|
|
|
componentDidMount () {
|
|
|
|
// In order for this to work, there needs to be tabIndex of -1 on the modal container
|
2016-09-07 22:21:10 +00:00
|
|
|
this._keydownCallback = e => {
|
2016-09-02 05:45:12 +00:00
|
|
|
if (!this.state.open) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-10-02 20:57:00 +00:00
|
|
|
// Don't bubble up meta events up past the modal no matter what
|
|
|
|
// Example: ctrl+Enter to send requests
|
|
|
|
const isMeta = isMac() ? e.metaKey : e.ctrlKey;
|
|
|
|
if (isMeta) {
|
|
|
|
e.stopPropagation();
|
|
|
|
}
|
2016-09-13 17:29:09 +00:00
|
|
|
|
2016-09-02 05:45:12 +00:00
|
|
|
const closeOnKeyCodes = this.props.closeOnKeyCodes || [];
|
|
|
|
const pressedEscape = e.keyCode === 27;
|
|
|
|
const pressedElse = closeOnKeyCodes.find(c => c === e.keyCode);
|
|
|
|
|
|
|
|
if (pressedEscape || pressedElse) {
|
2016-08-15 17:04:36 +00:00
|
|
|
e.preventDefault();
|
|
|
|
// Pressed escape
|
|
|
|
this.hide();
|
|
|
|
}
|
2016-09-07 22:21:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
this._node.addEventListener('keydown', this._keydownCallback);
|
2016-08-15 17:04:36 +00:00
|
|
|
}
|
|
|
|
|
2016-09-02 05:45:12 +00:00
|
|
|
componentWillUnmount () {
|
2016-09-07 22:21:10 +00:00
|
|
|
this._node.removeEventListener('keydown', this._keydownCallback);
|
2016-09-02 05:45:12 +00:00
|
|
|
}
|
|
|
|
|
2016-07-07 20:10:55 +00:00
|
|
|
render () {
|
2016-08-15 17:04:36 +00:00
|
|
|
const {tall, top, wide, className} = this.props;
|
2016-11-29 21:28:22 +00:00
|
|
|
const {open, zIndex, forceRefreshCounter} = this.state;
|
2016-04-07 03:09:14 +00:00
|
|
|
|
2016-07-07 20:10:55 +00:00
|
|
|
const classes = classnames(
|
|
|
|
'modal',
|
|
|
|
className,
|
|
|
|
{'modal--open': open},
|
2016-07-20 18:35:08 +00:00
|
|
|
{'modal--fixed-height': tall},
|
2016-08-15 17:04:36 +00:00
|
|
|
{'modal--fixed-top': top},
|
2016-10-21 17:20:36 +00:00
|
|
|
{'modal--wide': wide},
|
2016-07-14 22:48:56 +00:00
|
|
|
);
|
|
|
|
|
2016-04-07 03:09:14 +00:00
|
|
|
return (
|
2016-11-29 21:28:22 +00:00
|
|
|
<div ref={this._handleSetNodeRef}
|
|
|
|
tabIndex="-1"
|
|
|
|
className={classes}
|
2016-11-07 20:24:38 +00:00
|
|
|
style={{zIndex: zIndex}}
|
2016-11-29 21:28:22 +00:00
|
|
|
onClick={this._handleClick}>
|
|
|
|
<div className="modal__content" key={forceRefreshCounter}>
|
2016-07-14 22:48:56 +00:00
|
|
|
<div className="modal__backdrop" onClick={() => this.hide()}></div>
|
2016-04-07 03:09:14 +00:00
|
|
|
{this.props.children}
|
|
|
|
</div>
|
2016-07-06 22:11:37 +00:00
|
|
|
</div>
|
2016-04-07 03:09:14 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Modal.propTypes = {
|
2016-07-20 18:35:08 +00:00
|
|
|
tall: PropTypes.bool,
|
2016-08-15 17:04:36 +00:00
|
|
|
top: PropTypes.bool,
|
2016-08-25 21:34:03 +00:00
|
|
|
wide: PropTypes.bool,
|
2016-09-02 05:45:12 +00:00
|
|
|
dontFocus: PropTypes.bool,
|
2016-11-29 21:28:22 +00:00
|
|
|
closeOnKeyCodes: PropTypes.array,
|
|
|
|
freshState: PropTypes.bool,
|
2016-04-07 03:09:14 +00:00
|
|
|
};
|
|
|
|
|
2016-04-15 05:23:54 +00:00
|
|
|
export default Modal;
|
|
|
|
|