UNPKG

9.02 kBJavaScriptView Raw
1import _objectWithoutProperties from 'babel-runtime/helpers/objectWithoutProperties';
2import _classCallCheck from 'babel-runtime/helpers/classCallCheck';
3import _possibleConstructorReturn from 'babel-runtime/helpers/possibleConstructorReturn';
4import _inherits from 'babel-runtime/helpers/inherits';
5import _extends from 'babel-runtime/helpers/extends';
6import classNames from 'classnames';
7import events from 'dom-helpers/events';
8import ownerDocument from 'dom-helpers/ownerDocument';
9import canUseDOM from 'dom-helpers/util/inDOM';
10import getScrollbarSize from 'dom-helpers/util/scrollbarSize';
11import React from 'react';
12import PropTypes from 'prop-types';
13import ReactDOM from 'react-dom';
14import BaseModal from 'react-overlays/lib/Modal';
15import isOverflowing from 'react-overlays/lib/utils/isOverflowing';
16import elementType from 'prop-types-extra/lib/elementType';
17
18import Fade from './Fade';
19import Body from './ModalBody';
20import ModalDialog from './ModalDialog';
21import Footer from './ModalFooter';
22import Header from './ModalHeader';
23import Title from './ModalTitle';
24import { bsClass, bsSizes, prefix } from './utils/bootstrapUtils';
25import createChainedFunction from './utils/createChainedFunction';
26import splitComponentProps from './utils/splitComponentProps';
27import { Size } from './utils/StyleConfig';
28
29var propTypes = _extends({}, BaseModal.propTypes, ModalDialog.propTypes, {
30
31 /**
32 * Include a backdrop component. Specify 'static' for a backdrop that doesn't
33 * trigger an "onHide" when clicked.
34 */
35 backdrop: PropTypes.oneOf(['static', true, false]),
36
37 /**
38 * Add an optional extra class name to .modal-backdrop
39 * It could end up looking like class="modal-backdrop foo-modal-backdrop in".
40 */
41 backdropClassName: PropTypes.string,
42
43 /**
44 * Close the modal when escape key is pressed
45 */
46 keyboard: PropTypes.bool,
47
48 /**
49 * Open and close the Modal with a slide and fade animation.
50 */
51 animation: PropTypes.bool,
52
53 /**
54 * A Component type that provides the modal content Markup. This is a useful
55 * prop when you want to use your own styles and markup to create a custom
56 * modal component.
57 */
58 dialogComponentClass: elementType,
59
60 /**
61 * When `true` The modal will automatically shift focus to itself when it
62 * opens, and replace it to the last focused element when it closes.
63 * Generally this should never be set to false as it makes the Modal less
64 * accessible to assistive technologies, like screen-readers.
65 */
66 autoFocus: PropTypes.bool,
67
68 /**
69 * When `true` The modal will prevent focus from leaving the Modal while
70 * open. Consider leaving the default value here, as it is necessary to make
71 * the Modal work well with assistive technologies, such as screen readers.
72 */
73 enforceFocus: PropTypes.bool,
74
75 /**
76 * When `true` The modal will restore focus to previously focused element once
77 * modal is hidden
78 */
79 restoreFocus: PropTypes.bool,
80
81 /**
82 * When `true` The modal will show itself.
83 */
84 show: PropTypes.bool,
85
86 /**
87 * A callback fired when the header closeButton or non-static backdrop is
88 * clicked. Required if either are specified.
89 */
90 onHide: PropTypes.func,
91
92 /**
93 * Callback fired before the Modal transitions in
94 */
95 onEnter: PropTypes.func,
96
97 /**
98 * Callback fired as the Modal begins to transition in
99 */
100 onEntering: PropTypes.func,
101
102 /**
103 * Callback fired after the Modal finishes transitioning in
104 */
105 onEntered: PropTypes.func,
106
107 /**
108 * Callback fired right before the Modal transitions out
109 */
110 onExit: PropTypes.func,
111
112 /**
113 * Callback fired as the Modal begins to transition out
114 */
115 onExiting: PropTypes.func,
116
117 /**
118 * Callback fired after the Modal finishes transitioning out
119 */
120 onExited: PropTypes.func,
121
122 /**
123 * @private
124 */
125 container: BaseModal.propTypes.container
126});
127
128var defaultProps = _extends({}, BaseModal.defaultProps, {
129 animation: true,
130 dialogComponentClass: ModalDialog
131});
132
133var childContextTypes = {
134 $bs_modal: PropTypes.shape({
135 onHide: PropTypes.func
136 })
137};
138
139/* eslint-disable no-use-before-define, react/no-multi-comp */
140function DialogTransition(props) {
141 return React.createElement(Fade, _extends({}, props, { timeout: Modal.TRANSITION_DURATION }));
142}
143
144function BackdropTransition(props) {
145 return React.createElement(Fade, _extends({}, props, { timeout: Modal.BACKDROP_TRANSITION_DURATION }));
146}
147
148/* eslint-enable no-use-before-define */
149
150var Modal = function (_React$Component) {
151 _inherits(Modal, _React$Component);
152
153 function Modal(props, context) {
154 _classCallCheck(this, Modal);
155
156 var _this = _possibleConstructorReturn(this, _React$Component.call(this, props, context));
157
158 _this.handleEntering = _this.handleEntering.bind(_this);
159 _this.handleExited = _this.handleExited.bind(_this);
160 _this.handleWindowResize = _this.handleWindowResize.bind(_this);
161 _this.handleDialogClick = _this.handleDialogClick.bind(_this);
162 _this.setModalRef = _this.setModalRef.bind(_this);
163
164 _this.state = {
165 style: {}
166 };
167 return _this;
168 }
169
170 Modal.prototype.getChildContext = function getChildContext() {
171 return {
172 $bs_modal: {
173 onHide: this.props.onHide
174 }
175 };
176 };
177
178 Modal.prototype.componentWillUnmount = function componentWillUnmount() {
179 // Clean up the listener if we need to.
180 this.handleExited();
181 };
182
183 Modal.prototype.setModalRef = function setModalRef(ref) {
184 this._modal = ref;
185 };
186
187 Modal.prototype.handleDialogClick = function handleDialogClick(e) {
188 if (e.target !== e.currentTarget) {
189 return;
190 }
191
192 this.props.onHide();
193 };
194
195 Modal.prototype.handleEntering = function handleEntering() {
196 // FIXME: This should work even when animation is disabled.
197 events.on(window, 'resize', this.handleWindowResize);
198 this.updateStyle();
199 };
200
201 Modal.prototype.handleExited = function handleExited() {
202 // FIXME: This should work even when animation is disabled.
203 events.off(window, 'resize', this.handleWindowResize);
204 };
205
206 Modal.prototype.handleWindowResize = function handleWindowResize() {
207 this.updateStyle();
208 };
209
210 Modal.prototype.updateStyle = function updateStyle() {
211 if (!canUseDOM) {
212 return;
213 }
214
215 var dialogNode = this._modal.getDialogElement();
216 var dialogHeight = dialogNode.scrollHeight;
217
218 var document = ownerDocument(dialogNode);
219 var bodyIsOverflowing = isOverflowing(ReactDOM.findDOMNode(this.props.container || document.body));
220 var modalIsOverflowing = dialogHeight > document.documentElement.clientHeight;
221
222 this.setState({
223 style: {
224 paddingRight: bodyIsOverflowing && !modalIsOverflowing ? getScrollbarSize() : undefined,
225 paddingLeft: !bodyIsOverflowing && modalIsOverflowing ? getScrollbarSize() : undefined
226 }
227 });
228 };
229
230 Modal.prototype.render = function render() {
231 var _props = this.props,
232 backdrop = _props.backdrop,
233 backdropClassName = _props.backdropClassName,
234 animation = _props.animation,
235 show = _props.show,
236 Dialog = _props.dialogComponentClass,
237 className = _props.className,
238 style = _props.style,
239 children = _props.children,
240 onEntering = _props.onEntering,
241 onExited = _props.onExited,
242 props = _objectWithoutProperties(_props, ['backdrop', 'backdropClassName', 'animation', 'show', 'dialogComponentClass', 'className', 'style', 'children', 'onEntering', 'onExited']);
243
244 var _splitComponentProps = splitComponentProps(props, BaseModal),
245 baseModalProps = _splitComponentProps[0],
246 dialogProps = _splitComponentProps[1];
247
248 var inClassName = show && !animation && 'in';
249
250 return React.createElement(
251 BaseModal,
252 _extends({}, baseModalProps, {
253 ref: this.setModalRef,
254 show: show,
255 containerClassName: prefix(props, 'open'),
256 transition: animation ? DialogTransition : undefined,
257 backdrop: backdrop,
258 backdropTransition: animation ? BackdropTransition : undefined,
259 backdropClassName: classNames(prefix(props, 'backdrop'), backdropClassName, inClassName),
260 onEntering: createChainedFunction(onEntering, this.handleEntering),
261 onExited: createChainedFunction(onExited, this.handleExited)
262 }),
263 React.createElement(
264 Dialog,
265 _extends({}, dialogProps, {
266 style: _extends({}, this.state.style, style),
267 className: classNames(className, inClassName),
268 onClick: backdrop === true ? this.handleDialogClick : null
269 }),
270 children
271 )
272 );
273 };
274
275 return Modal;
276}(React.Component);
277
278Modal.propTypes = propTypes;
279Modal.defaultProps = defaultProps;
280Modal.childContextTypes = childContextTypes;
281
282Modal.Body = Body;
283Modal.Header = Header;
284Modal.Title = Title;
285Modal.Footer = Footer;
286
287Modal.Dialog = ModalDialog;
288
289Modal.TRANSITION_DURATION = 300;
290Modal.BACKDROP_TRANSITION_DURATION = 150;
291
292export default bsClass('modal', bsSizes([Size.LARGE, Size.SMALL], Modal));
\No newline at end of file