2
0

NewModal.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. Copyright (C) 2017 Cloudbase Solutions SRL
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. import React, { PropTypes } from 'react';
  15. import withStyles from 'isomorphic-style-loader/lib/withStyles';
  16. import s from './NewModal.scss';
  17. import Modal from 'react-modal';
  18. class NewModal extends React.Component {
  19. static defaultProps = {
  20. topBottomMargin: 8
  21. }
  22. static propTypes = {
  23. children: PropTypes.node.isRequired,
  24. isOpen: PropTypes.bool.isRequired,
  25. contentLabel: PropTypes.string,
  26. onRequestClose: PropTypes.func,
  27. contentStyle: PropTypes.object,
  28. topBottomMargin: PropTypes.number
  29. }
  30. componentDidMount() {
  31. window.addEventListener('resize', this.positionModal.bind(this), true)
  32. setTimeout(this.positionModal.bind(this), 100)
  33. }
  34. componentWillReceiveProps() {
  35. setTimeout(this.positionModal.bind(this), 100)
  36. }
  37. componentWillUnmount() {
  38. window.removeEventListener('resize', this.positionModal.bind(this), true)
  39. }
  40. handleChildUpdate() {
  41. setTimeout(this.positionModal.bind(this), 100)
  42. }
  43. positionModal() {
  44. let pageNode = this.modalDiv && this.modalDiv.node.firstChild
  45. let contentNode = pageNode && pageNode.firstChild;
  46. if (!contentNode) {
  47. return
  48. }
  49. contentNode.style.height = 'auto'
  50. let left = pageNode.offsetWidth / 2 - contentNode.offsetWidth / 2
  51. let top = pageNode.offsetHeight / 2 - contentNode.offsetHeight / 2
  52. let height = 'auto';
  53. if (top < this.props.topBottomMargin) {
  54. top = this.props.topBottomMargin
  55. height = pageNode.offsetHeight - this.props.topBottomMargin * 2 + 'px'
  56. }
  57. contentNode.style.left = left + 'px'
  58. contentNode.style.top = top + 'px'
  59. contentNode.style.height = height
  60. contentNode.style.opacity = 1;
  61. }
  62. render() {
  63. let modalStyle = {
  64. overlay: {
  65. position: 'fixed',
  66. zIndex: 10000,
  67. top: 0,
  68. left: 0,
  69. right: 0,
  70. bottom: 0,
  71. backgroundColor: 'rgba(164, 170, 181, 0.69)'
  72. },
  73. content: {
  74. padding: 0,
  75. overflowX: 'hidden',
  76. borderRadius: '4px',
  77. border: 'none',
  78. bottom: 'auto',
  79. top: 'auto',
  80. left: 'auto',
  81. right: 'auto',
  82. transition: 'all 0.2s',
  83. opacity: 0
  84. }
  85. }
  86. modalStyle.content = {
  87. ...modalStyle.content,
  88. ...this.props.contentStyle
  89. }
  90. let children = React.Children.map(this.props.children,
  91. child => React.cloneElement(child, {
  92. onResizeUpdate: this.handleChildUpdate.bind(this)
  93. })
  94. );
  95. return (
  96. <Modal
  97. ref={m => { this.modalDiv = m }}
  98. isOpen={this.props.isOpen}
  99. contentLabel={this.props.contentLabel}
  100. style={modalStyle}
  101. contentLabel={this.props.contentLabel}
  102. onRequestClose={this.props.onRequestClose}
  103. >
  104. {children}
  105. </Modal>
  106. )
  107. }
  108. }
  109. export default withStyles(NewModal, s);