UNPKG

11.6 kBJavaScriptView Raw
1'use strict';
2
3exports.__esModule = true;
4
5var _objectWithoutProperties2 = require('babel-runtime/helpers/objectWithoutProperties');
6
7var _objectWithoutProperties3 = _interopRequireDefault(_objectWithoutProperties2);
8
9var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
10
11var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
12
13var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');
14
15var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);
16
17var _inherits2 = require('babel-runtime/helpers/inherits');
18
19var _inherits3 = _interopRequireDefault(_inherits2);
20
21var _extends2 = require('babel-runtime/helpers/extends');
22
23var _extends3 = _interopRequireDefault(_extends2);
24
25var _classnames = require('classnames');
26
27var _classnames2 = _interopRequireDefault(_classnames);
28
29var _events = require('dom-helpers/events');
30
31var _events2 = _interopRequireDefault(_events);
32
33var _ownerDocument = require('dom-helpers/ownerDocument');
34
35var _ownerDocument2 = _interopRequireDefault(_ownerDocument);
36
37var _inDOM = require('dom-helpers/util/inDOM');
38
39var _inDOM2 = _interopRequireDefault(_inDOM);
40
41var _scrollbarSize = require('dom-helpers/util/scrollbarSize');
42
43var _scrollbarSize2 = _interopRequireDefault(_scrollbarSize);
44
45var _react = require('react');
46
47var _react2 = _interopRequireDefault(_react);
48
49var _propTypes = require('prop-types');
50
51var _propTypes2 = _interopRequireDefault(_propTypes);
52
53var _reactDom = require('react-dom');
54
55var _reactDom2 = _interopRequireDefault(_reactDom);
56
57var _Modal = require('react-overlays/lib/Modal');
58
59var _Modal2 = _interopRequireDefault(_Modal);
60
61var _isOverflowing = require('react-overlays/lib/utils/isOverflowing');
62
63var _isOverflowing2 = _interopRequireDefault(_isOverflowing);
64
65var _elementType = require('prop-types-extra/lib/elementType');
66
67var _elementType2 = _interopRequireDefault(_elementType);
68
69var _Fade = require('./Fade');
70
71var _Fade2 = _interopRequireDefault(_Fade);
72
73var _ModalBody = require('./ModalBody');
74
75var _ModalBody2 = _interopRequireDefault(_ModalBody);
76
77var _ModalDialog = require('./ModalDialog');
78
79var _ModalDialog2 = _interopRequireDefault(_ModalDialog);
80
81var _ModalFooter = require('./ModalFooter');
82
83var _ModalFooter2 = _interopRequireDefault(_ModalFooter);
84
85var _ModalHeader = require('./ModalHeader');
86
87var _ModalHeader2 = _interopRequireDefault(_ModalHeader);
88
89var _ModalTitle = require('./ModalTitle');
90
91var _ModalTitle2 = _interopRequireDefault(_ModalTitle);
92
93var _bootstrapUtils = require('./utils/bootstrapUtils');
94
95var _createChainedFunction = require('./utils/createChainedFunction');
96
97var _createChainedFunction2 = _interopRequireDefault(_createChainedFunction);
98
99var _splitComponentProps2 = require('./utils/splitComponentProps');
100
101var _splitComponentProps3 = _interopRequireDefault(_splitComponentProps2);
102
103var _StyleConfig = require('./utils/StyleConfig');
104
105function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
106
107var propTypes = (0, _extends3.default)({}, _Modal2.default.propTypes, _ModalDialog2.default.propTypes, {
108
109 /**
110 * Include a backdrop component. Specify 'static' for a backdrop that doesn't
111 * trigger an "onHide" when clicked.
112 */
113 backdrop: _propTypes2.default.oneOf(['static', true, false]),
114
115 /**
116 * Add an optional extra class name to .modal-backdrop
117 * It could end up looking like class="modal-backdrop foo-modal-backdrop in".
118 */
119 backdropClassName: _propTypes2.default.string,
120
121 /**
122 * Close the modal when escape key is pressed
123 */
124 keyboard: _propTypes2.default.bool,
125
126 /**
127 * Open and close the Modal with a slide and fade animation.
128 */
129 animation: _propTypes2.default.bool,
130
131 /**
132 * A Component type that provides the modal content Markup. This is a useful
133 * prop when you want to use your own styles and markup to create a custom
134 * modal component.
135 */
136 dialogComponentClass: _elementType2.default,
137
138 /**
139 * When `true` The modal will automatically shift focus to itself when it
140 * opens, and replace it to the last focused element when it closes.
141 * Generally this should never be set to false as it makes the Modal less
142 * accessible to assistive technologies, like screen-readers.
143 */
144 autoFocus: _propTypes2.default.bool,
145
146 /**
147 * When `true` The modal will prevent focus from leaving the Modal while
148 * open. Consider leaving the default value here, as it is necessary to make
149 * the Modal work well with assistive technologies, such as screen readers.
150 */
151 enforceFocus: _propTypes2.default.bool,
152
153 /**
154 * When `true` The modal will restore focus to previously focused element once
155 * modal is hidden
156 */
157 restoreFocus: _propTypes2.default.bool,
158
159 /**
160 * When `true` The modal will show itself.
161 */
162 show: _propTypes2.default.bool,
163
164 /**
165 * A callback fired when the header closeButton or non-static backdrop is
166 * clicked. Required if either are specified.
167 */
168 onHide: _propTypes2.default.func,
169
170 /**
171 * Callback fired before the Modal transitions in
172 */
173 onEnter: _propTypes2.default.func,
174
175 /**
176 * Callback fired as the Modal begins to transition in
177 */
178 onEntering: _propTypes2.default.func,
179
180 /**
181 * Callback fired after the Modal finishes transitioning in
182 */
183 onEntered: _propTypes2.default.func,
184
185 /**
186 * Callback fired right before the Modal transitions out
187 */
188 onExit: _propTypes2.default.func,
189
190 /**
191 * Callback fired as the Modal begins to transition out
192 */
193 onExiting: _propTypes2.default.func,
194
195 /**
196 * Callback fired after the Modal finishes transitioning out
197 */
198 onExited: _propTypes2.default.func,
199
200 /**
201 * @private
202 */
203 container: _Modal2.default.propTypes.container
204});
205
206var defaultProps = (0, _extends3.default)({}, _Modal2.default.defaultProps, {
207 animation: true,
208 dialogComponentClass: _ModalDialog2.default
209});
210
211var childContextTypes = {
212 $bs_modal: _propTypes2.default.shape({
213 onHide: _propTypes2.default.func
214 })
215};
216
217/* eslint-disable no-use-before-define, react/no-multi-comp */
218function DialogTransition(props) {
219 return _react2.default.createElement(_Fade2.default, (0, _extends3.default)({}, props, { timeout: Modal.TRANSITION_DURATION }));
220}
221
222function BackdropTransition(props) {
223 return _react2.default.createElement(_Fade2.default, (0, _extends3.default)({}, props, { timeout: Modal.BACKDROP_TRANSITION_DURATION }));
224}
225
226/* eslint-enable no-use-before-define */
227
228var Modal = function (_React$Component) {
229 (0, _inherits3.default)(Modal, _React$Component);
230
231 function Modal(props, context) {
232 (0, _classCallCheck3.default)(this, Modal);
233
234 var _this = (0, _possibleConstructorReturn3.default)(this, _React$Component.call(this, props, context));
235
236 _this.handleEntering = _this.handleEntering.bind(_this);
237 _this.handleExited = _this.handleExited.bind(_this);
238 _this.handleWindowResize = _this.handleWindowResize.bind(_this);
239 _this.handleDialogClick = _this.handleDialogClick.bind(_this);
240 _this.setModalRef = _this.setModalRef.bind(_this);
241
242 _this.state = {
243 style: {}
244 };
245 return _this;
246 }
247
248 Modal.prototype.getChildContext = function getChildContext() {
249 return {
250 $bs_modal: {
251 onHide: this.props.onHide
252 }
253 };
254 };
255
256 Modal.prototype.componentWillUnmount = function componentWillUnmount() {
257 // Clean up the listener if we need to.
258 this.handleExited();
259 };
260
261 Modal.prototype.setModalRef = function setModalRef(ref) {
262 this._modal = ref;
263 };
264
265 Modal.prototype.handleDialogClick = function handleDialogClick(e) {
266 if (e.target !== e.currentTarget) {
267 return;
268 }
269
270 this.props.onHide();
271 };
272
273 Modal.prototype.handleEntering = function handleEntering() {
274 // FIXME: This should work even when animation is disabled.
275 _events2.default.on(window, 'resize', this.handleWindowResize);
276 this.updateStyle();
277 };
278
279 Modal.prototype.handleExited = function handleExited() {
280 // FIXME: This should work even when animation is disabled.
281 _events2.default.off(window, 'resize', this.handleWindowResize);
282 };
283
284 Modal.prototype.handleWindowResize = function handleWindowResize() {
285 this.updateStyle();
286 };
287
288 Modal.prototype.updateStyle = function updateStyle() {
289 if (!_inDOM2.default) {
290 return;
291 }
292
293 var dialogNode = this._modal.getDialogElement();
294 var dialogHeight = dialogNode.scrollHeight;
295
296 var document = (0, _ownerDocument2.default)(dialogNode);
297 var bodyIsOverflowing = (0, _isOverflowing2.default)(_reactDom2.default.findDOMNode(this.props.container || document.body));
298 var modalIsOverflowing = dialogHeight > document.documentElement.clientHeight;
299
300 this.setState({
301 style: {
302 paddingRight: bodyIsOverflowing && !modalIsOverflowing ? (0, _scrollbarSize2.default)() : undefined,
303 paddingLeft: !bodyIsOverflowing && modalIsOverflowing ? (0, _scrollbarSize2.default)() : undefined
304 }
305 });
306 };
307
308 Modal.prototype.render = function render() {
309 var _props = this.props,
310 backdrop = _props.backdrop,
311 backdropClassName = _props.backdropClassName,
312 animation = _props.animation,
313 show = _props.show,
314 Dialog = _props.dialogComponentClass,
315 className = _props.className,
316 style = _props.style,
317 children = _props.children,
318 onEntering = _props.onEntering,
319 onExited = _props.onExited,
320 props = (0, _objectWithoutProperties3.default)(_props, ['backdrop', 'backdropClassName', 'animation', 'show', 'dialogComponentClass', 'className', 'style', 'children', 'onEntering', 'onExited']);
321
322 var _splitComponentProps = (0, _splitComponentProps3.default)(props, _Modal2.default),
323 baseModalProps = _splitComponentProps[0],
324 dialogProps = _splitComponentProps[1];
325
326 var inClassName = show && !animation && 'in';
327
328 return _react2.default.createElement(
329 _Modal2.default,
330 (0, _extends3.default)({}, baseModalProps, {
331 ref: this.setModalRef,
332 show: show,
333 containerClassName: (0, _bootstrapUtils.prefix)(props, 'open'),
334 transition: animation ? DialogTransition : undefined,
335 backdrop: backdrop,
336 backdropTransition: animation ? BackdropTransition : undefined,
337 backdropClassName: (0, _classnames2.default)((0, _bootstrapUtils.prefix)(props, 'backdrop'), backdropClassName, inClassName),
338 onEntering: (0, _createChainedFunction2.default)(onEntering, this.handleEntering),
339 onExited: (0, _createChainedFunction2.default)(onExited, this.handleExited)
340 }),
341 _react2.default.createElement(
342 Dialog,
343 (0, _extends3.default)({}, dialogProps, {
344 style: (0, _extends3.default)({}, this.state.style, style),
345 className: (0, _classnames2.default)(className, inClassName),
346 onClick: backdrop === true ? this.handleDialogClick : null
347 }),
348 children
349 )
350 );
351 };
352
353 return Modal;
354}(_react2.default.Component);
355
356Modal.propTypes = propTypes;
357Modal.defaultProps = defaultProps;
358Modal.childContextTypes = childContextTypes;
359
360Modal.Body = _ModalBody2.default;
361Modal.Header = _ModalHeader2.default;
362Modal.Title = _ModalTitle2.default;
363Modal.Footer = _ModalFooter2.default;
364
365Modal.Dialog = _ModalDialog2.default;
366
367Modal.TRANSITION_DURATION = 300;
368Modal.BACKDROP_TRANSITION_DURATION = 150;
369
370exports.default = (0, _bootstrapUtils.bsClass)('modal', (0, _bootstrapUtils.bsSizes)([_StyleConfig.Size.LARGE, _StyleConfig.Size.SMALL], Modal));
371module.exports = exports['default'];
\No newline at end of file