Modal.tsx 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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 * as React from "react";
  15. import { observer } from "mobx-react";
  16. import styled from "styled-components";
  17. import Modal from "react-modal";
  18. import autobind from "autobind-decorator";
  19. import ReactTooltip from "react-tooltip";
  20. import KeyboardManager from "@src/utils/KeyboardManager";
  21. import { ThemeProps } from "@src/components/Theme";
  22. import backgroundImage from "@src/components/ui/Images/star-bg.jpg";
  23. const headerHeight = 48;
  24. const Title = styled.div`
  25. height: ${headerHeight}px;
  26. font-size: 24px;
  27. font-weight: ${ThemeProps.fontWeights.light};
  28. text-align: center;
  29. line-height: 48px;
  30. color: white;
  31. background: url("${backgroundImage}") center no-repeat;
  32. background-size: cover;
  33. `;
  34. type Props = {
  35. children: React.ReactNode;
  36. isOpen: boolean;
  37. contentLabel?: string;
  38. onRequestClose?: () => void;
  39. contentWidth?: React.CSSProperties["width"];
  40. topBottomMargin?: number;
  41. title?: string;
  42. componentRef?: (ref: any) => void;
  43. onScrollableRef?: () => HTMLElement | null | undefined;
  44. fixedHeight?: number;
  45. };
  46. @observer
  47. class NewModal extends React.Component<Props> {
  48. scrollableRef: HTMLDivElement | undefined | null;
  49. windowScrollY: number | undefined;
  50. contentRef: HTMLDivElement | undefined | null;
  51. overlayRef: HTMLDivElement | undefined | null;
  52. UNSAFE_componentWillMount() {
  53. if (this.props.componentRef) {
  54. this.props.componentRef(this);
  55. }
  56. }
  57. componentDidMount() {
  58. window.addEventListener("resize", this.handleResize, true);
  59. setTimeout(() => {
  60. this.positionModal(0);
  61. }, 100);
  62. }
  63. UNSAFE_componentWillReceiveProps(newProps: Props) {
  64. if (!this.props.isOpen && newProps.isOpen) {
  65. KeyboardManager.onKeyDown("modal", null, 1);
  66. } else if (this.props.isOpen && !newProps.isOpen) {
  67. KeyboardManager.removeKeyDown("modal");
  68. this.handleModalClose();
  69. }
  70. }
  71. UNSAFE_componentWillUpdate() {
  72. setTimeout(() => {
  73. this.positionModal(0);
  74. }, 100);
  75. }
  76. componentWillUnmount() {
  77. this.handleModalClose();
  78. window.removeEventListener("resize", this.handleResize, true);
  79. }
  80. @autobind
  81. handleResize() {
  82. this.positionModal(0);
  83. }
  84. handleChildUpdate(scrollableRef: HTMLDivElement, scrollOffset: number) {
  85. if (scrollableRef) {
  86. this.scrollableRef = scrollableRef;
  87. }
  88. setTimeout(() => {
  89. this.positionModal(scrollOffset);
  90. }, 100);
  91. }
  92. handleModalOpen() {
  93. if (!document.body) {
  94. return;
  95. }
  96. this.windowScrollY = window.scrollY;
  97. document.body.style.top = `${-this.windowScrollY}px`;
  98. document.body.style.position = "fixed";
  99. document.body.style.width = "100%";
  100. document.body.style.height = "100%";
  101. }
  102. handleModalClose() {
  103. if (!document.body) {
  104. return;
  105. }
  106. document.body.style.top = "";
  107. document.body.style.position = "";
  108. document.body.style.width = "";
  109. document.body.style.height = "";
  110. window.scroll(0, this.windowScrollY || 0);
  111. }
  112. handleRequestClose() {
  113. /** Remove existing tooltips. Tooltips are not automatically removed when a modal is closed
  114. ** (for example when pressing ESC key while hovering over a tooltip) **/
  115. ReactTooltip.hide();
  116. if (this.props.onRequestClose) {
  117. this.props.onRequestClose();
  118. }
  119. }
  120. @autobind
  121. positionModal(scrollOffset: number) {
  122. const overlay = this.overlayRef as HTMLDivElement;
  123. const content = this.contentRef as HTMLDivElement;
  124. if (!overlay || !content) {
  125. return;
  126. }
  127. const scrollableRef =
  128. this.props.onScrollableRef && this.props.onScrollableRef();
  129. const scrollableNode = (scrollableRef ||
  130. this.scrollableRef ||
  131. content) as HTMLDivElement;
  132. const scrollTop = scrollableNode.scrollTop;
  133. content.style.height = "auto";
  134. const contentDesiredHeight = this.props.fixedHeight
  135. ? this.props.fixedHeight + headerHeight
  136. : content.offsetHeight;
  137. const left = overlay.offsetWidth / 2 - content.offsetWidth / 2;
  138. let top = overlay.offsetHeight / 2 - contentDesiredHeight / 2;
  139. let height = this.props.fixedHeight
  140. ? `${this.props.fixedHeight + headerHeight}px`
  141. : "auto";
  142. const topBottomMargin = this.props.topBottomMargin || 8;
  143. if (top < topBottomMargin) {
  144. top = topBottomMargin;
  145. height = `${overlay.offsetHeight - topBottomMargin * 2}px`;
  146. }
  147. content.style.left = `${left}px`;
  148. content.style.top = `${top}px`;
  149. content.style.height = height;
  150. content.style.opacity = "1";
  151. scrollableNode.scrollTop = scrollTop + scrollOffset;
  152. }
  153. renderTitle() {
  154. if (!this.props.title) {
  155. return null;
  156. }
  157. return <Title>{this.props.title}</Title>;
  158. }
  159. render() {
  160. const modalStyle: Modal.Styles = {
  161. overlay: {
  162. position: "fixed",
  163. zIndex: 1000,
  164. top: 0,
  165. left: 0,
  166. right: 0,
  167. bottom: 0,
  168. backgroundColor: "rgba(73, 76, 81, 0.69)",
  169. },
  170. content: {
  171. padding: 0,
  172. width: "576px",
  173. overflowX: "hidden",
  174. borderRadius: "4px",
  175. border: "none",
  176. bottom: "auto",
  177. top: "auto",
  178. left: "auto",
  179. right: "auto",
  180. transition: "all 0.2s",
  181. opacity: 0,
  182. display: "flex",
  183. flexDirection: "column",
  184. },
  185. };
  186. if (this.props.contentWidth) {
  187. modalStyle.content!.width = this.props.contentWidth;
  188. }
  189. const children = React.Children.map(this.props.children, child =>
  190. React.cloneElement(child as React.ReactElement<any>, {
  191. onResizeUpdate: (
  192. scrollableRef: HTMLDivElement,
  193. scrollOffset: number
  194. ) => {
  195. this.handleChildUpdate(scrollableRef, scrollOffset);
  196. },
  197. })
  198. );
  199. return (
  200. <Modal
  201. contentRef={(m: HTMLDivElement) => {
  202. this.contentRef = m;
  203. }}
  204. overlayRef={(m: HTMLDivElement) => {
  205. this.overlayRef = m;
  206. }}
  207. isOpen={this.props.isOpen}
  208. contentLabel={this.props.contentLabel || this.props.title}
  209. style={modalStyle}
  210. onRequestClose={() => this.handleRequestClose()}
  211. onAfterOpen={() => {
  212. this.handleModalOpen();
  213. }}
  214. ariaHideApp={false}
  215. >
  216. {this.renderTitle()}
  217. {children}
  218. </Modal>
  219. );
  220. }
  221. }
  222. export default NewModal;