insomnia/app/components/base/Modal.js

97 lines
2.2 KiB
JavaScript
Raw Normal View History

2016-04-07 03:09:14 +00:00
import React, {Component, PropTypes} from 'react';
2016-04-10 02:58:48 +00:00
import classnames from 'classnames';
2016-04-07 03:09:14 +00:00
const ModalHeader = (props) => (
2016-04-07 03:27:45 +00:00
<div className="modal__header bg-light">
2016-04-07 03:09:14 +00:00
<div className="grid">
<div className="grid__cell pad">
2016-04-08 04:05:08 +00:00
<div className={props.className}>
{props.children}
</div>
2016-04-07 03:09:14 +00:00
</div>
<div className="grid--v">
<button className="btn btn--compact txt-lg" data-close-modal="true">
<i className="fa fa-times"></i>
</button>
</div>
</div>
</div>
);
const ModalBody = (props) => (
2016-04-10 02:58:48 +00:00
<div className={classnames('modal__body', 'grid__cell', 'scrollable', props.className)}>
{props.children}
2016-04-07 03:09:14 +00:00
</div>
);
const ModalFooter = (props) => (
2016-04-08 04:05:08 +00:00
<div className="modal__footer">
<div className={props.className}>
{props.children}
</div>
2016-04-07 03:09:14 +00:00
</div>
);
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 () {
this.refs.modal.focus();
}
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-10 02:58:48 +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
};
export {Modal, ModalHeader, ModalBody, ModalFooter};