|
| 1 | +import React, { PropTypes } from 'react'; |
| 2 | +import ReactDOM from 'react-dom'; |
| 3 | +import classNames from 'classnames'; |
| 4 | +import Fade from './Fade'; |
| 5 | + |
| 6 | +const propTypes = { |
| 7 | + isOpen: PropTypes.bool, |
| 8 | + size: PropTypes.string, |
| 9 | + toggle: PropTypes.func.isRequired, |
| 10 | + onEnter: PropTypes.func, |
| 11 | + onExit: PropTypes.func |
| 12 | +}; |
| 13 | + |
| 14 | +const defaultProps = { |
| 15 | + isOpen: false |
| 16 | +}; |
| 17 | + |
| 18 | +class Modal extends React.Component { |
| 19 | + constructor(props) { |
| 20 | + super(props); |
| 21 | + |
| 22 | + this.handleProps = this.handleProps.bind(this); |
| 23 | + this.handleBackdropClick = this.handleBackdropClick.bind(this); |
| 24 | + this.handleEscape = this.handleEscape.bind(this); |
| 25 | + this.destroy = this.destroy.bind(this); |
| 26 | + this.onEnter = this.onEnter.bind(this); |
| 27 | + this.onExit = this.onExit.bind(this); |
| 28 | + } |
| 29 | + |
| 30 | + componentDidMount() { |
| 31 | + if (this.props.isOpen) { |
| 32 | + this.show(); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + componentDidUpdate(prevProps) { |
| 37 | + if (this.props.isOpen !== prevProps.isOpen) { |
| 38 | + this.handleProps(); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + componentWillUnmount() { |
| 43 | + this.hide(); |
| 44 | + } |
| 45 | + |
| 46 | + onEnter() { |
| 47 | + this._modal.fadeIn(); |
| 48 | + if (this.props.onEnter) { |
| 49 | + this.props.onEnter(); |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + onExit() { |
| 54 | + this.destroy(); |
| 55 | + if (this.props.onExit) { |
| 56 | + this.props.onExit(); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + handleEscape(e) { |
| 61 | + if (e.keyCode === 27) { |
| 62 | + this.props.toggle(); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + handleBackdropClick(e) { |
| 67 | + const container = ReactDOM.findDOMNode(this._dialog); |
| 68 | + |
| 69 | + if (e.target && !container.contains(e.target)) { |
| 70 | + this.props.toggle(); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + handleProps() { |
| 75 | + if (this.props.isOpen) { |
| 76 | + this.show(); |
| 77 | + } else { |
| 78 | + this.hide(); |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + destroy() { |
| 83 | + let classes = document.body.className.replace('modal-open', ''); |
| 84 | + this.removeEvents(); |
| 85 | + |
| 86 | + if (this._element) { |
| 87 | + ReactDOM.unmountComponentAtNode(this._element); |
| 88 | + document.body.removeChild(this._element); |
| 89 | + this._element = null; |
| 90 | + } |
| 91 | + |
| 92 | + document.body.className = classNames(classes).trim(); |
| 93 | + } |
| 94 | + |
| 95 | + removeEvents() { |
| 96 | + document.removeEventListener('click', this.handleBackdropClick, false); |
| 97 | + document.removeEventListener('keyup', this.handleEscape, false); |
| 98 | + } |
| 99 | + |
| 100 | + hide() { |
| 101 | + this.removeEvents(); |
| 102 | + |
| 103 | + if (this._modal) { |
| 104 | + this._modal.fadeOut(); |
| 105 | + } |
| 106 | + if (this._backdrop) { |
| 107 | + this._backdrop.fadeOut(this.onExit, 250); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + show() { |
| 112 | + const classes = document.body.className; |
| 113 | + this._element = document.createElement('div'); |
| 114 | + this._element.setAttribute('tabindex', '-1'); |
| 115 | + |
| 116 | + document.body.appendChild(this._element); |
| 117 | + document.addEventListener('click', this.handleBackdropClick, false); |
| 118 | + document.addEventListener('keyup', this.handleEscape, false); |
| 119 | + |
| 120 | + document.body.className = classNames( |
| 121 | + classes, |
| 122 | + 'modal-open' |
| 123 | + ); |
| 124 | + |
| 125 | + ReactDOM.unstable_renderSubtreeIntoContainer( |
| 126 | + this, |
| 127 | + this.renderChildren(), |
| 128 | + this._element |
| 129 | + ); |
| 130 | + |
| 131 | + this._element.focus(); |
| 132 | + this._backdrop.fadeIn(this.onEnter, 100); |
| 133 | + } |
| 134 | + |
| 135 | + renderChildren() { |
| 136 | + return ( |
| 137 | + <div> |
| 138 | + <Fade className="modal" style={{ display: 'block' }} tabIndex="-1" ref={(c) => this._modal = c }> |
| 139 | + <div className="modal-dialog" role="document" ref={(c) => this._dialog = c }> |
| 140 | + <div className="modal-content"> |
| 141 | + { this.props.children } |
| 142 | + </div> |
| 143 | + </div> |
| 144 | + </Fade> |
| 145 | + <Fade className="modal-backdrop" ref={(c) => this._backdrop = c }/> |
| 146 | + </div> |
| 147 | + ); |
| 148 | + } |
| 149 | + |
| 150 | + render() { |
| 151 | + return null; |
| 152 | + } |
| 153 | +} |
| 154 | + |
| 155 | +Modal.propTypes = propTypes; |
| 156 | +Modal.defaultProps = defaultProps; |
| 157 | + |
| 158 | +export default Modal; |
0 commit comments