You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

607 rivejä
20 KiB

  1. 'use strict';
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. var _extends2 = require('babel-runtime/helpers/extends');
  6. var _extends3 = _interopRequireDefault(_extends2);
  7. var _objectWithoutProperties2 = require('babel-runtime/helpers/objectWithoutProperties');
  8. var _objectWithoutProperties3 = _interopRequireDefault(_objectWithoutProperties2);
  9. var _getPrototypeOf = require('babel-runtime/core-js/object/get-prototype-of');
  10. var _getPrototypeOf2 = _interopRequireDefault(_getPrototypeOf);
  11. var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
  12. var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
  13. var _createClass2 = require('babel-runtime/helpers/createClass');
  14. var _createClass3 = _interopRequireDefault(_createClass2);
  15. var _possibleConstructorReturn2 = require('babel-runtime/helpers/possibleConstructorReturn');
  16. var _possibleConstructorReturn3 = _interopRequireDefault(_possibleConstructorReturn2);
  17. var _inherits2 = require('babel-runtime/helpers/inherits');
  18. var _inherits3 = _interopRequireDefault(_inherits2);
  19. var _simpleAssign = require('simple-assign');
  20. var _simpleAssign2 = _interopRequireDefault(_simpleAssign);
  21. var _react = require('react');
  22. var _react2 = _interopRequireDefault(_react);
  23. var _propTypes = require('prop-types');
  24. var _propTypes2 = _interopRequireDefault(_propTypes);
  25. var _reactDom = require('react-dom');
  26. var _reactDom2 = _interopRequireDefault(_reactDom);
  27. var _reactEventListener = require('react-event-listener');
  28. var _reactEventListener2 = _interopRequireDefault(_reactEventListener);
  29. var _keycode = require('keycode');
  30. var _keycode2 = _interopRequireDefault(_keycode);
  31. var _transitions = require('../styles/transitions');
  32. var _transitions2 = _interopRequireDefault(_transitions);
  33. var _Overlay = require('../internal/Overlay');
  34. var _Overlay2 = _interopRequireDefault(_Overlay);
  35. var _RenderToLayer = require('../internal/RenderToLayer');
  36. var _RenderToLayer2 = _interopRequireDefault(_RenderToLayer);
  37. var _Paper = require('../Paper');
  38. var _Paper2 = _interopRequireDefault(_Paper);
  39. var _TransitionGroup = require('react-transition-group/TransitionGroup');
  40. var _TransitionGroup2 = _interopRequireDefault(_TransitionGroup);
  41. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  42. var TransitionItem = function (_Component) {
  43. (0, _inherits3.default)(TransitionItem, _Component);
  44. function TransitionItem() {
  45. var _ref;
  46. var _temp, _this, _ret;
  47. (0, _classCallCheck3.default)(this, TransitionItem);
  48. for (var _len = arguments.length, args = Array(_len), _key = 0; _key < _len; _key++) {
  49. args[_key] = arguments[_key];
  50. }
  51. return _ret = (_temp = (_this = (0, _possibleConstructorReturn3.default)(this, (_ref = TransitionItem.__proto__ || (0, _getPrototypeOf2.default)(TransitionItem)).call.apply(_ref, [this].concat(args))), _this), _this.state = {
  52. style: {}
  53. }, _temp), (0, _possibleConstructorReturn3.default)(_this, _ret);
  54. }
  55. (0, _createClass3.default)(TransitionItem, [{
  56. key: 'componentWillUnmount',
  57. value: function componentWillUnmount() {
  58. clearTimeout(this.enterTimeout);
  59. clearTimeout(this.leaveTimeout);
  60. }
  61. }, {
  62. key: 'componentWillEnter',
  63. value: function componentWillEnter(callback) {
  64. this.componentWillAppear(callback);
  65. }
  66. }, {
  67. key: 'componentWillAppear',
  68. value: function componentWillAppear(callback) {
  69. var spacing = this.context.muiTheme.baseTheme.spacing;
  70. this.setState({
  71. style: {
  72. opacity: 1,
  73. transform: 'translate(0, ' + spacing.desktopKeylineIncrement + 'px)'
  74. }
  75. });
  76. this.enterTimeout = setTimeout(callback, 450); // matches transition duration
  77. }
  78. }, {
  79. key: 'componentWillLeave',
  80. value: function componentWillLeave(callback) {
  81. this.setState({
  82. style: {
  83. opacity: 0,
  84. transform: 'translate(0, 0)'
  85. }
  86. });
  87. this.leaveTimeout = setTimeout(callback, 450); // matches transition duration
  88. }
  89. }, {
  90. key: 'render',
  91. value: function render() {
  92. var _props = this.props,
  93. style = _props.style,
  94. children = _props.children,
  95. other = (0, _objectWithoutProperties3.default)(_props, ['style', 'children']);
  96. var prepareStyles = this.context.muiTheme.prepareStyles;
  97. return _react2.default.createElement(
  98. 'div',
  99. (0, _extends3.default)({}, other, { style: prepareStyles((0, _simpleAssign2.default)({}, this.state.style, style)) }),
  100. children
  101. );
  102. }
  103. }]);
  104. return TransitionItem;
  105. }(_react.Component);
  106. TransitionItem.contextTypes = {
  107. muiTheme: _propTypes2.default.object.isRequired
  108. };
  109. TransitionItem.propTypes = process.env.NODE_ENV !== "production" ? {
  110. children: _propTypes2.default.node,
  111. style: _propTypes2.default.object
  112. } : {};
  113. function getStyles(props, context) {
  114. var autoScrollBodyContent = props.autoScrollBodyContent,
  115. open = props.open;
  116. var _context$muiTheme = context.muiTheme,
  117. _context$muiTheme$bas = _context$muiTheme.baseTheme,
  118. spacing = _context$muiTheme$bas.spacing,
  119. palette = _context$muiTheme$bas.palette,
  120. dialog = _context$muiTheme.dialog,
  121. zIndex = _context$muiTheme.zIndex;
  122. var gutter = spacing.desktopGutter;
  123. var borderScroll = '1px solid ' + palette.borderColor;
  124. return {
  125. root: {
  126. position: 'fixed',
  127. boxSizing: 'border-box',
  128. WebkitTapHighlightColor: 'rgba(0,0,0,0)', // Remove mobile color flashing (deprecated)
  129. zIndex: zIndex.dialog,
  130. top: 0,
  131. left: open ? 0 : -10000,
  132. width: '100%',
  133. height: '100%',
  134. transition: open ? _transitions2.default.easeOut('0ms', 'left', '0ms') : _transitions2.default.easeOut('0ms', 'left', '450ms')
  135. },
  136. content: {
  137. boxSizing: 'border-box',
  138. WebkitTapHighlightColor: 'rgba(0,0,0,0)', // Remove mobile color flashing (deprecated)
  139. transition: _transitions2.default.easeOut(),
  140. position: 'relative',
  141. width: '75%',
  142. maxWidth: spacing.desktopKeylineIncrement * 12,
  143. margin: '0 auto',
  144. zIndex: zIndex.dialog
  145. },
  146. actionsContainer: {
  147. boxSizing: 'border-box',
  148. WebkitTapHighlightColor: 'rgba(0,0,0,0)', // Remove mobile color flashing (deprecated)
  149. padding: 8,
  150. width: '100%',
  151. textAlign: 'right',
  152. marginTop: autoScrollBodyContent ? -1 : 0
  153. },
  154. overlay: {
  155. zIndex: zIndex.dialogOverlay
  156. },
  157. title: {
  158. margin: 0,
  159. padding: gutter + 'px ' + gutter + 'px 20px ' + gutter + 'px',
  160. color: palette.textColor,
  161. fontSize: dialog.titleFontSize,
  162. lineHeight: '32px',
  163. fontWeight: 400,
  164. marginBottom: autoScrollBodyContent ? -1 : 0
  165. },
  166. body: {
  167. fontSize: dialog.bodyFontSize,
  168. color: dialog.bodyColor,
  169. padding: (props.title ? 0 : gutter) + 'px ' + gutter + 'px ' + gutter + 'px',
  170. boxSizing: 'border-box',
  171. overflowY: autoScrollBodyContent ? 'auto' : 'hidden',
  172. borderTop: autoScrollBodyContent ? borderScroll : 'none',
  173. borderBottom: autoScrollBodyContent ? borderScroll : 'none'
  174. }
  175. };
  176. }
  177. var DialogInline = function (_Component2) {
  178. (0, _inherits3.default)(DialogInline, _Component2);
  179. function DialogInline() {
  180. var _ref2;
  181. var _temp2, _this2, _ret2;
  182. (0, _classCallCheck3.default)(this, DialogInline);
  183. for (var _len2 = arguments.length, args = Array(_len2), _key2 = 0; _key2 < _len2; _key2++) {
  184. args[_key2] = arguments[_key2];
  185. }
  186. return _ret2 = (_temp2 = (_this2 = (0, _possibleConstructorReturn3.default)(this, (_ref2 = DialogInline.__proto__ || (0, _getPrototypeOf2.default)(DialogInline)).call.apply(_ref2, [this].concat(args))), _this2), _this2.handleClickOverlay = function () {
  187. _this2.requestClose(false);
  188. }, _this2.handleKeyUp = function (event) {
  189. if ((0, _keycode2.default)(event) === 'esc') {
  190. _this2.requestClose(false);
  191. }
  192. }, _this2.handleResize = function () {
  193. _this2.positionDialog();
  194. }, _temp2), (0, _possibleConstructorReturn3.default)(_this2, _ret2);
  195. }
  196. (0, _createClass3.default)(DialogInline, [{
  197. key: 'componentDidMount',
  198. value: function componentDidMount() {
  199. this.positionDialog();
  200. }
  201. }, {
  202. key: 'componentDidUpdate',
  203. value: function componentDidUpdate() {
  204. this.positionDialog();
  205. }
  206. }, {
  207. key: 'positionDialog',
  208. value: function positionDialog() {
  209. var _props2 = this.props,
  210. actions = _props2.actions,
  211. autoDetectWindowHeight = _props2.autoDetectWindowHeight,
  212. autoScrollBodyContent = _props2.autoScrollBodyContent,
  213. bodyStyle = _props2.bodyStyle,
  214. open = _props2.open,
  215. repositionOnUpdate = _props2.repositionOnUpdate,
  216. title = _props2.title;
  217. if (!open) {
  218. return;
  219. }
  220. var clientHeight = window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
  221. var container = _reactDom2.default.findDOMNode(this);
  222. var dialogWindow = _reactDom2.default.findDOMNode(this.refs.dialogWindow);
  223. var dialogContent = _reactDom2.default.findDOMNode(this.refs.dialogContent);
  224. var minPaddingTop = 16;
  225. // Reset the height in case the window was resized.
  226. dialogWindow.style.height = '';
  227. dialogContent.style.height = '';
  228. var dialogWindowHeight = dialogWindow.offsetHeight;
  229. var paddingTop = (clientHeight - dialogWindowHeight) / 2 - 64;
  230. if (paddingTop < minPaddingTop) paddingTop = minPaddingTop;
  231. // Vertically center the dialog window, but make sure it doesn't
  232. // transition to that position.
  233. if (repositionOnUpdate || !container.style.paddingTop) {
  234. container.style.paddingTop = paddingTop + 'px';
  235. }
  236. // Force a height if the dialog is taller than clientHeight
  237. if (autoDetectWindowHeight || autoScrollBodyContent) {
  238. var styles = getStyles(this.props, this.context);
  239. styles.body = (0, _simpleAssign2.default)(styles.body, bodyStyle);
  240. var maxDialogContentHeight = clientHeight - 2 * 64;
  241. if (title) maxDialogContentHeight -= dialogContent.previousSibling.offsetHeight;
  242. if (_react2.default.Children.count(actions)) {
  243. maxDialogContentHeight -= dialogContent.nextSibling.offsetHeight;
  244. }
  245. dialogContent.style.maxHeight = maxDialogContentHeight + 'px';
  246. if (maxDialogContentHeight > dialogWindowHeight) {
  247. dialogContent.style.borderBottom = 'none';
  248. dialogContent.style.borderTop = 'none';
  249. }
  250. }
  251. }
  252. }, {
  253. key: 'requestClose',
  254. value: function requestClose(buttonClicked) {
  255. if (!buttonClicked && this.props.modal) {
  256. return;
  257. }
  258. if (this.props.onRequestClose) {
  259. this.props.onRequestClose(!!buttonClicked);
  260. }
  261. }
  262. }, {
  263. key: 'render',
  264. value: function render() {
  265. var _props3 = this.props,
  266. actions = _props3.actions,
  267. actionsContainerClassName = _props3.actionsContainerClassName,
  268. actionsContainerStyle = _props3.actionsContainerStyle,
  269. bodyClassName = _props3.bodyClassName,
  270. bodyStyle = _props3.bodyStyle,
  271. children = _props3.children,
  272. className = _props3.className,
  273. contentClassName = _props3.contentClassName,
  274. contentStyle = _props3.contentStyle,
  275. overlayClassName = _props3.overlayClassName,
  276. overlayStyle = _props3.overlayStyle,
  277. open = _props3.open,
  278. paperClassName = _props3.paperClassName,
  279. paperProps = _props3.paperProps,
  280. style = _props3.style,
  281. titleClassName = _props3.titleClassName,
  282. titleStyle = _props3.titleStyle,
  283. title = _props3.title;
  284. var prepareStyles = this.context.muiTheme.prepareStyles;
  285. var styles = getStyles(this.props, this.context);
  286. styles.root = (0, _simpleAssign2.default)(styles.root, style);
  287. styles.content = (0, _simpleAssign2.default)(styles.content, contentStyle);
  288. styles.body = (0, _simpleAssign2.default)(styles.body, bodyStyle);
  289. styles.actionsContainer = (0, _simpleAssign2.default)(styles.actionsContainer, actionsContainerStyle);
  290. styles.overlay = (0, _simpleAssign2.default)(styles.overlay, overlayStyle);
  291. styles.title = (0, _simpleAssign2.default)(styles.title, titleStyle);
  292. var actionsContainer = _react2.default.Children.count(actions) > 0 && _react2.default.createElement(
  293. 'div',
  294. { className: actionsContainerClassName, style: prepareStyles(styles.actionsContainer) },
  295. _react2.default.Children.toArray(actions)
  296. );
  297. var titleElement = title;
  298. if (_react2.default.isValidElement(title)) {
  299. titleElement = _react2.default.cloneElement(title, {
  300. className: title.props.className || titleClassName,
  301. style: prepareStyles((0, _simpleAssign2.default)(styles.title, title.props.style))
  302. });
  303. } else if (typeof title === 'string') {
  304. titleElement = _react2.default.createElement(
  305. 'h3',
  306. { className: titleClassName, style: prepareStyles(styles.title) },
  307. title
  308. );
  309. }
  310. return _react2.default.createElement(
  311. 'div',
  312. { className: className, style: prepareStyles(styles.root) },
  313. open && _react2.default.createElement(_reactEventListener2.default, {
  314. target: 'window',
  315. onKeyUp: this.handleKeyUp,
  316. onResize: this.handleResize
  317. }),
  318. _react2.default.createElement(
  319. _TransitionGroup2.default,
  320. {
  321. component: 'div',
  322. ref: 'dialogWindow',
  323. transitionAppear: true,
  324. transitionAppearTimeout: 450,
  325. transitionEnter: true,
  326. transitionEnterTimeout: 450
  327. },
  328. open && _react2.default.createElement(
  329. TransitionItem,
  330. {
  331. className: contentClassName,
  332. style: styles.content
  333. },
  334. _react2.default.createElement(
  335. _Paper2.default,
  336. (0, _extends3.default)({ className: paperClassName, zDepth: 4 }, paperProps),
  337. titleElement,
  338. _react2.default.createElement(
  339. 'div',
  340. {
  341. ref: 'dialogContent',
  342. className: bodyClassName,
  343. style: prepareStyles(styles.body)
  344. },
  345. children
  346. ),
  347. actionsContainer
  348. )
  349. )
  350. ),
  351. _react2.default.createElement(_Overlay2.default, {
  352. show: open,
  353. className: overlayClassName,
  354. style: styles.overlay,
  355. onClick: this.handleClickOverlay
  356. })
  357. );
  358. }
  359. }]);
  360. return DialogInline;
  361. }(_react.Component);
  362. DialogInline.contextTypes = {
  363. muiTheme: _propTypes2.default.object.isRequired
  364. };
  365. DialogInline.propTypes = process.env.NODE_ENV !== "production" ? {
  366. actions: _propTypes2.default.node,
  367. actionsContainerClassName: _propTypes2.default.string,
  368. actionsContainerStyle: _propTypes2.default.object,
  369. autoDetectWindowHeight: _propTypes2.default.bool,
  370. autoScrollBodyContent: _propTypes2.default.bool,
  371. bodyClassName: _propTypes2.default.string,
  372. bodyStyle: _propTypes2.default.object,
  373. children: _propTypes2.default.node,
  374. className: _propTypes2.default.string,
  375. contentClassName: _propTypes2.default.string,
  376. contentStyle: _propTypes2.default.object,
  377. modal: _propTypes2.default.bool,
  378. onRequestClose: _propTypes2.default.func,
  379. open: _propTypes2.default.bool.isRequired,
  380. overlayClassName: _propTypes2.default.string,
  381. overlayStyle: _propTypes2.default.object,
  382. paperClassName: _propTypes2.default.string,
  383. paperProps: _propTypes2.default.object,
  384. repositionOnUpdate: _propTypes2.default.bool,
  385. style: _propTypes2.default.object,
  386. title: _propTypes2.default.node,
  387. titleClassName: _propTypes2.default.string,
  388. titleStyle: _propTypes2.default.object
  389. } : {};
  390. var Dialog = function (_Component3) {
  391. (0, _inherits3.default)(Dialog, _Component3);
  392. function Dialog() {
  393. var _ref3;
  394. var _temp3, _this3, _ret3;
  395. (0, _classCallCheck3.default)(this, Dialog);
  396. for (var _len3 = arguments.length, args = Array(_len3), _key3 = 0; _key3 < _len3; _key3++) {
  397. args[_key3] = arguments[_key3];
  398. }
  399. return _ret3 = (_temp3 = (_this3 = (0, _possibleConstructorReturn3.default)(this, (_ref3 = Dialog.__proto__ || (0, _getPrototypeOf2.default)(Dialog)).call.apply(_ref3, [this].concat(args))), _this3), _this3.renderLayer = function () {
  400. return _react2.default.createElement(DialogInline, _this3.props);
  401. }, _temp3), (0, _possibleConstructorReturn3.default)(_this3, _ret3);
  402. }
  403. (0, _createClass3.default)(Dialog, [{
  404. key: 'render',
  405. value: function render() {
  406. return _react2.default.createElement(_RenderToLayer2.default, { render: this.renderLayer, open: true, useLayerForClickAway: false });
  407. }
  408. }]);
  409. return Dialog;
  410. }(_react.Component);
  411. Dialog.contextTypes = {
  412. muiTheme: _propTypes2.default.object.isRequired
  413. };
  414. Dialog.defaultProps = {
  415. autoDetectWindowHeight: true,
  416. autoScrollBodyContent: false,
  417. modal: false,
  418. repositionOnUpdate: true
  419. };
  420. Dialog.propTypes = process.env.NODE_ENV !== "production" ? {
  421. /**
  422. * Action buttons to display below the Dialog content (`children`).
  423. * This property accepts either a React element, or an array of React elements.
  424. */
  425. actions: _propTypes2.default.node,
  426. /**
  427. * The `className` to add to the actions container's root element.
  428. */
  429. actionsContainerClassName: _propTypes2.default.string,
  430. /**
  431. * Overrides the inline-styles of the actions container's root element.
  432. */
  433. actionsContainerStyle: _propTypes2.default.object,
  434. /**
  435. * If set to true, the height of the `Dialog` will be auto detected. A max height
  436. * will be enforced so that the content does not extend beyond the viewport.
  437. */
  438. autoDetectWindowHeight: _propTypes2.default.bool,
  439. /**
  440. * If set to true, the body content of the `Dialog` will be scrollable.
  441. */
  442. autoScrollBodyContent: _propTypes2.default.bool,
  443. /**
  444. * The `className` to add to the content's root element under the title.
  445. */
  446. bodyClassName: _propTypes2.default.string,
  447. /**
  448. * Overrides the inline-styles of the content's root element under the title.
  449. */
  450. bodyStyle: _propTypes2.default.object,
  451. /**
  452. * The contents of the `Dialog`.
  453. */
  454. children: _propTypes2.default.node,
  455. /**
  456. * @ignore
  457. */
  458. className: _propTypes2.default.string,
  459. /**
  460. * The `className` to add to the content container.
  461. */
  462. contentClassName: _propTypes2.default.string,
  463. /**
  464. * Overrides the inline-styles of the content container.
  465. */
  466. contentStyle: _propTypes2.default.object,
  467. /**
  468. * Force the user to use one of the actions in the `Dialog`.
  469. * Clicking outside the `Dialog` will not trigger the `onRequestClose`.
  470. */
  471. modal: _propTypes2.default.bool,
  472. /**
  473. * Fired when the `Dialog` is requested to be closed by a click outside the `Dialog` or on the buttons.
  474. *
  475. * @param {bool} buttonClicked Determines whether a button click triggered this request.
  476. */
  477. onRequestClose: _propTypes2.default.func,
  478. /**
  479. * Controls whether the Dialog is opened or not.
  480. */
  481. open: _propTypes2.default.bool.isRequired,
  482. /**
  483. * The `className` to add to the `Overlay` component that is rendered behind the `Dialog`.
  484. */
  485. overlayClassName: _propTypes2.default.string,
  486. /**
  487. * Overrides the inline-styles of the `Overlay` component that is rendered behind the `Dialog`.
  488. */
  489. overlayStyle: _propTypes2.default.object,
  490. /**
  491. * The CSS class name of the `Paper` element.
  492. */
  493. paperClassName: _propTypes2.default.string,
  494. /**
  495. * Properties applied to the `Paper` element.
  496. */
  497. paperProps: _propTypes2.default.object,
  498. /**
  499. * Determines whether the `Dialog` should be repositioned when it's contents are updated.
  500. */
  501. repositionOnUpdate: _propTypes2.default.bool,
  502. /**
  503. * Override the inline-styles of the root element.
  504. */
  505. style: _propTypes2.default.object,
  506. /**
  507. * The title to display on the `Dialog`. Could be number, string, element or an array containing these types.
  508. */
  509. title: _propTypes2.default.node,
  510. /**
  511. * The `className` to add to the title's root container element.
  512. */
  513. titleClassName: _propTypes2.default.string,
  514. /**
  515. * Overrides the inline-styles of the title's root container element.
  516. */
  517. titleStyle: _propTypes2.default.object
  518. } : {};
  519. exports.default = Dialog;