Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

react-modal Dynamic Sizing

Im using react-modal which is pretty great. Is it possible to dynamically size it (perhaps with css media tag). For example,

  1. For large screen, the modal only takes up a small potion of the screen (lets say max width 200px;
  2. For medium screen, the modal takes up most of the screen (Lets say 80% of the screen width and height
  3. For mobile device, it takes takes up 100% of the width and height.
like image 592
user172902 Avatar asked Oct 21 '18 09:10

user172902


People also ask

How do I resize react modal?

The Dialog supports resizing feature. To resize the dialog, we have to select and resize it by using its handle (grip) or hovering on any of the edges or borders of the dialog within the sample container.

How do you resize a modal?

Modal Size Change the size of the modal by adding the . modal-sm class for small modals, . modal-lg class for large modals, or . modal-xl for extra large modals.

How do you increase the width of a modal in Reactstrap?

On component set size="lg" or what you need, and it works. The docs reactstrap really bad to explained. Don't forget everyone, you can also do size="xl" . To achieve greater sizes than this, you have to use a stylesheet imported and on the Modal component itself, specifically a property called contentClassName .

How do you make a modal responsive in react JS?

You can just add class/ID to your Modal and/or its child DOM, then use a normal CSS file, with @medi a declaration, and style your component responsively as you wish! You can simply include that normal CSS file in your main index. html file.


1 Answers

Have a look at this code that prepare for you.

ReactModal.setAppElement('#main');

class ExampleApp extends React.Component {
  constructor () {
    super();
    this.state = {
      showModal: false
    };

    this.handleOpenModal = this.handleOpenModal.bind(this);
    this.handleCloseModal = this.handleCloseModal.bind(this);
  }

  handleOpenModal () {
    this.setState({ showModal: true });
  }

  handleCloseModal () {
    this.setState({ showModal: false });
  }

  render () {
    return (
      <div>
        <button onClick={this.handleOpenModal}>Trigger Modal</button>
        <ReactModal 
           isOpen={this.state.showModal}
           contentLabel="onRequestClose Example"
           onRequestClose={this.handleCloseModal}
           className="Modal"
           overlayClassName="Overlay"
        >
          <p>Modal text!</p>
          <button onClick={this.handleCloseModal}>Close Modal</button>
        </ReactModal>
      </div>
    );
  }
}

const props = {};

ReactDOM.render(<ExampleApp {...props} />, document.getElementById('main'))

Checkout in responsive view:

https://codepen.io/ene_salinas/full/yRGMpG/

Checkout full code:

https://codepen.io/ene_salinas/pen/yRGMpG

Yellow color = large screen
Green color = medium screen
Gray color = Mobile

Don't forget this meta:

"<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">"

Happy coding.

like image 157
ene_salinas Avatar answered Sep 23 '22 04:09

ene_salinas