Modal.tsx 807 B

123456789101112131415161718192021222324252627282930313233
  1. import React, { FunctionComponent, useEffect, useRef } from 'react'
  2. import { createPortal } from 'react-dom'
  3. import { modal } from '../styles'
  4. interface IModal {
  5. state: [boolean, Function]
  6. children?: any
  7. }
  8. const Modal: FunctionComponent<IModal> = ({ state, children }) => {
  9. const [visible, setVisible] = state
  10. const ref = useRef<undefined | HTMLElement>()
  11. useEffect(() => {
  12. ref.current = document.body
  13. }, [])
  14. const content = visible ? (
  15. <div className='modal'>
  16. <div className='container'>
  17. <button onClick={() => setVisible(false)} className='modal-close'>
  18. Close
  19. </button>
  20. {children}
  21. </div>
  22. <style jsx>{modal}</style>
  23. </div>
  24. ) : null
  25. return ref.current ? createPortal(content, ref.current) : null
  26. }
  27. export default Modal