From bdb4680057c2b9c1c819133862336eef7f0348bc Mon Sep 17 00:00:00 2001 From: George Lima Date: Wed, 19 Dec 2018 10:50:08 -0300 Subject: [PATCH] feature: add Modal component --- app/components/modal.js | 98 +++++++++++++++++++++++++++++++++++++++++ public/index.html | 3 +- 2 files changed, 99 insertions(+), 2 deletions(-) create mode 100644 app/components/modal.js diff --git a/app/components/modal.js b/app/components/modal.js new file mode 100644 index 0000000..63493ef --- /dev/null +++ b/app/components/modal.js @@ -0,0 +1,98 @@ +// @flow +import React, { PureComponent, Fragment, type Element } from 'react'; +import { createPortal } from 'react-dom'; +import styled from 'styled-components'; + +const ModalWrapper = styled.div` + width: 100vw; + height: 100vh; + position: fixed; + display: flex; + align-items: center; + justify-content: center; + top: 0; + left: 0; + z-index: 10; + background-color: rgba(0, 0, 0, 0.4); +`; + +const ChildrenWrapper = styled.div` + z-index: 90; +`; + +type Props = { + renderTrigger: (() => void) => Element<*>, + children: Element<*>, +}; + +type State = { + isVisible: boolean, +}; + +const modalRoot = document.getElementById('modal-root'); + +export class ModalComponent extends PureComponent { + element = document.createElement('div'); + + state = { + isVisible: false, + }; + + componentDidMount() { + window.addEventListener('keydown', this.handleEscPress); + } + + componentWillUnmount() { + window.removeEventListener('keydown', this.handleEscPress); + } + + handleEscPress = (event: Object) => { + const { isVisible } = this.state; + + if (event.key === 'Escape' && isVisible) { + this.close(); + } + }; + + open = () => { + this.setState( + () => ({ isVisible: true }), + () => { + if (modalRoot) modalRoot.appendChild(this.element); + }, + ); + }; + + close = () => { + this.setState( + () => ({ isVisible: false }), + () => { + if (modalRoot) modalRoot.removeChild(this.element); + }, + ); + }; + + render() { + const { renderTrigger, children } = this.props; + const { isVisible } = this.state; + + return ( + + {renderTrigger(isVisible ? this.close : this.open)} + {isVisible + ? createPortal( + { + if (event.target.id === 'modal-portal-wrapper') this.close(); + }} + > + {children} + , + this.element, + ) + : null} + + ); + } +} diff --git a/public/index.html b/public/index.html index 676487b..311e933 100644 --- a/public/index.html +++ b/public/index.html @@ -13,8 +13,7 @@ -
- +