javascriptcssreactjsreact-modal

How can I place close button outside of modal window in react-modal?


I've imported Modal from react-modal (React Player is just for emdebbing video) and my code looks this way now:

<Modal
    isOpen={modalIsOpen}
    onRequestClose={closeModal}
    style={customStyles}
    contentLabel='Example Modal'
  >
    <button onClick={closeModal} className={css.button}>close</button>
    <ReactPlayer
      url='https://vimeo.com/49384334'
      playing='true'
      controls='true'
      volume={0}
    />
  </Modal>

So I tried to write some styles for button like that, but the button can't leave it's parent tag.

.button {
   display: block;
   float: right;
   position: relative;
   top: -10px;
   right: -10px;
   z-index: 1002;
 }

Are there any styles I should overwrite maybe in the modal div?


Solution

  • By default Modal component from react-modal has overflow: auto. That is why your button can't move outside the modal. To fix this you need change overflow: auto to overflow: visible (see code below)

    const modalStyles = {
        content : {
          top                   : '50%',
          left                  : '50%',
          right                 : 'auto',
          bottom                : 'auto',
          marginRight           : '-50%',
          transform             : 'translate(-50%, -50%)',
          overflow              : 'visibile'
        }
      };
    const buttonStyles = {
      position: "absolute",
      top: "10px",
      right: "10px"
    };
    
    <Modal
      isOpen={this.state.isOpen}
      onRequestClose={this.handleOpenModal}
      style={modalStyles}
      contentLabel="Example Modal"
    >
      <button onClick={this.handleOpenModal} style={buttonStyles}>
        close
      </button>
      modal content
    </Modal>
    

    Thank you.