Mahmonir Bakhtiyari
Mahmonir Bakhtiyari

Reputation: 605

How to set responsive style to react modal in React js?

I am trying to set styles to my modal for be resposive in different media. In react js, I have used customStyle to

import Modal from 'react-modal';

const customStyles = {
  content : {
    top                   : '50%',
    left                  : '50%',
    right                 : 'auto',
    bottom                : 'auto',
    marginRight           : '-50%',
    transform             : 'translate(-50%, -50%)'
  }
};

class App extends React.Component {
  constructor() {
    super();

    this.state = {
      modalIsOpen: false
    };

    this.openModal = this.openModal.bind(this);
    this.afterOpenModal = this.afterOpenModal.bind(this);
    this.closeModal = this.closeModal.bind(this);
  }

  openModal() {
    this.setState({modalIsOpen: true});
  }

  afterOpenModal() {
    // references are now sync'd and can be accessed.
    this.subtitle.style.color = '#f00';
  }

  closeModal() {
    this.setState({modalIsOpen: false});
  }

  render() {
    return (
      <div>
        <button onClick={this.openModal}>Open Modal</button>
        <Modal
          isOpen={this.state.modalIsOpen}
          onAfterOpen={this.afterOpenModal}
          onRequestClose={this.closeModal}
          style={customStyles}
          contentLabel="Example Modal"
        >

          <h2 ref={subtitle => this.subtitle = subtitle}>Hello</h2>
          <button onClick={this.closeModal}>close</button>
          <div>I am a modal</div>
        </Modal>
      </div>
    );
  }
}

How i add more style feature to get resposive modal, thanks. How to add @media style to react-modal in my code. Please help me. Thanks alot.

Upvotes: 4

Views: 16574

Answers (2)

Hp Lam
Hp Lam

Reputation: 357

You can try use className and add the css for it.

<ReactModal 
       isOpen={this.state.showModal}
       contentLabel="onRequestClose Example"
       onRequestClose={this.handleCloseModal}
       className="YouClass"
       overlayClassName="Overlay"
    >
      <p>Modal text!</p>
      <button onClick={this.handleCloseModal}>Close Modal</button>
    </ReactModal>

Please read more: https://codepen.io/claydiffrient/pen/KNjVrG

Upvotes: 1

thinhvo0108
thinhvo0108

Reputation: 2232

You can just add class/ID to your Modal and/or its child DOM, then use a normal CSS file, with @media declaration, and style your component responsively as you wish!

You can simply include that normal CSS file in your main index.html file.

Upvotes: 0

Related Questions