insomnia/app/components/base/Modal.js

68 lines
1.4 KiB
JavaScript
Raw Normal View History

2016-04-15 05:23:54 +00:00
import React, {Component, PropTypes} from 'react'
import classnames from 'classnames'
2016-04-07 03:09:14 +00:00
class Modal extends Component {
_handleClick (e) {
// 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-04-07 03:27:45 +00:00
let close = false;
2016-04-08 04:05:08 +00:00
2016-04-07 03:27:45 +00:00
if (target === this.refs.modal) {
close = true;
}
2016-04-07 03:09:14 +00:00
for (let i = 0; i < 5; i++) {
if (target.hasAttribute('data-close-modal')) {
2016-04-07 03:27:45 +00:00
close = true;
2016-04-07 03:09:14 +00:00
break;
}
target = target.parentNode;
}
2016-04-08 04:05:08 +00:00
2016-04-07 03:27:45 +00:00
if (close) {
this.close();
}
}
2016-04-08 04:05:08 +00:00
2016-04-09 19:24:33 +00:00
_keyDown (e) {
if (e.keyCode === 27) {
// We pressed ESC
this.close();
}
}
2016-04-07 03:27:45 +00:00
close () {
this.props.onClose && this.props.onClose();
2016-04-07 03:09:14 +00:00
}
2016-04-10 02:58:48 +00:00
componentDidMount () {
2016-04-15 05:23:54 +00:00
this.refs.modal && this.refs.modal.focus();
2016-04-10 02:58:48 +00:00
}
2016-04-07 03:09:14 +00:00
2016-04-10 02:58:48 +00:00
render () {
2016-04-07 03:09:14 +00:00
return (
2016-04-08 04:05:08 +00:00
<div ref="modal"
2016-04-10 02:58:48 +00:00
tabIndex="-1"
className={classnames('modal', 'grid', 'grid--center', this.props.className)}
2016-04-09 19:24:33 +00:00
onKeyDown={this._keyDown.bind(this)}
2016-04-08 04:05:08 +00:00
onClick={this._handleClick.bind(this)}>
2016-04-15 05:23:54 +00:00
<div
className={classnames('modal__content', 'grid--v', 'bg-super-light', {tall: this.props.tall})}>
2016-04-07 03:09:14 +00:00
{this.props.children}
</div>
</div>
)
}
}
Modal.propTypes = {
2016-04-10 02:58:48 +00:00
onClose: PropTypes.func,
tall: PropTypes.bool
2016-04-07 03:09:14 +00:00
};
2016-04-15 05:23:54 +00:00
export default Modal;