最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to set responsive style to react modal in React js? - Stack Overflow

programmeradmin4浏览0评论

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.

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.

Share Improve this question asked Apr 6, 2018 at 0:42 Mahmonir BakhtiyariMahmonir Bakhtiyari 6051 gold badge9 silver badges22 bronze badges 3
  • So one of the big things about react is the abstraction. These media queries would be applied via a stylesheet outside of the ponent – Sterling Archer Commented Apr 6, 2018 at 0:48
  • Possible duplicate of Inline CSS styles in React: how to implement media queries? – Mitch Lillie Commented Apr 6, 2018 at 0:48
  • Thank alot, I'm looking for this but did'nt find it. – Mahmonir Bakhtiyari Commented Apr 6, 2018 at 8:19
Add a ment  | 

2 Answers 2

Reset to default 1

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

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 ponent responsively as you wish!

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

发布评论

评论列表(0)

  1. 暂无评论