Modals
Add dialogs to your site for lightboxes, user notifications, or completely custom content.
Overview
- Modals are positioned over everything else in the document and remove scroll from the
<body>
so that modal content scrolls instead. - Modals are unmounted when closed.
- Bootstrap only supports one modal window at a time. Nested modals aren’t supported, but if you really need them the underlying
react-overlays
can support them if you're willing. - Modal's "trap" focus in them, ensuring the keyboard navigation cycles through the modal, and not the rest of the page.
- Unlike vanilla Bootstrap,
autoFocus
works in Modals because React handles the implementation.
Examples
Static Markup
Below is a static modal dialog (without the positioning) to demostrate the look and feel of the Modal.
<Modal.Dialog> <Modal.Header closeButton> <Modal.Title>Modal title</Modal.Title> </Modal.Header> <Modal.Body> <p>Modal body text goes here.</p> </Modal.Body> <Modal.Footer> <Button variant="secondary">Close</Button> <Button variant="primary">Save changes</Button> </Modal.Footer> </Modal.Dialog>;
Live demo
A modal with header, body, and set of actions in the footer. Use <Modal/>
in combination with other components to show or hide your Modal. The <Modal/>
Component comes with a few convenient "sub components": <Modal.Header/>
, <Modal.Title/>
, <Modal.Body/>
, and <Modal.Footer/>
, which you can use to build the Modal content.
class Example extends React.Component { constructor(props, context) { super(props, context); this.handleShow = this.handleShow.bind(this); this.handleClose = this.handleClose.bind(this); this.state = { show: false, }; } handleClose() { this.setState({ show: false }); } handleShow() { this.setState({ show: true }); } render() { return ( <> <Button variant="primary" onClick={this.handleShow}> Launch demo modal </Button> <Modal show={this.state.show} onHide={this.handleClose}> <Modal.Header closeButton> <Modal.Title>Modal heading</Modal.Title> </Modal.Header> <Modal.Body>Woohoo, you're reading this text in a modal!</Modal.Body> <Modal.Footer> <Button variant="secondary" onClick={this.handleClose}> Close </Button> <Button variant="primary" onClick={this.handleClose}> Save Changes </Button> </Modal.Footer> </Modal> </> ); } } render(<Example />);
The Modal Header, Title, Body, and Footer components are available as static properties the <Modal/>
component, but you can also, import them directly like: require("react-bootstrap/ModalHeader")
.
Vertically centered
You can vertically center a modal by passing the "verticallyCenter" prop.
class MyVerticallyCenteredModal extends React.Component { render() { return ( <Modal {...this.props} size="lg" aria-labelledby="contained-modal-title-vcenter" centered > <Modal.Header closeButton> <Modal.Title id="contained-modal-title-vcenter"> Modal heading </Modal.Title> </Modal.Header> <Modal.Body> <h4>Centered Modal</h4> <p> Cras mattis consectetur purus sit amet fermentum. Cras justo odio, dapibus ac facilisis in, egestas eget quam. Morbi leo risus, porta ac consectetur ac, vestibulum at eros. </p> </Modal.Body> <Modal.Footer> <Button onClick={this.props.onHide}>Close</Button> </Modal.Footer> </Modal> ); } } class App extends React.Component { constructor(...args) { super(...args); this.state = { modalShow: false }; } render() { let modalClose = () => this.setState({ modalShow: false }); return ( <ButtonToolbar> <Button variant="primary" onClick={() => this.setState({ modalShow: true })} > Launch vertically centered modal </Button> <MyVerticallyCenteredModal show={this.state.modalShow} onHide={modalClose} /> </ButtonToolbar> ); } } render(<App />);
Using the grid
You can use grid layouts within a model using regular grid components inside the modal content.
class MydModalWithGrid extends React.Component { render() { return ( <Modal {...this.props} aria-labelledby="contained-modal-title-vcenter"> <Modal.Header closeButton> <Modal.Title id="contained-modal-title-vcenter"> Using Grid in Modal </Modal.Title> </Modal.Header> <Modal.Body> <Container> <Row className="show-grid"> <Col xs={12} md={8}> <code>.col-xs-12 .col-md-8</code> </Col> <Col xs={6} md={4}> <code>.col-xs-6 .col-md-4</code> </Col> </Row> <Row className="show-grid"> <Col xs={6} md={4}> <code>.col-xs-6 .col-md-4</code> </Col> <Col xs={6} md={4}> <code>.col-xs-6 .col-md-4</code> </Col> <Col xs={6} md={4}> <code>.col-xs-6 .col-md-4</code> </Col> </Row> </Container> </Modal.Body> <Modal.Footer> <Button onClick={this.props.onHide}>Close</Button> </Modal.Footer> </Modal> ); } } class App extends React.Component { constructor(...args) { super(...args); this.state = { modalShow: false }; } render() { let modalClose = () => this.setState({ modalShow: false }); return ( <ButtonToolbar> <Button variant="primary" onClick={() => this.setState({ modalShow: true })} > Launch modal with grid </Button> <MydModalWithGrid show={this.state.modalShow} onHide={modalClose} /> </ButtonToolbar> ); } } render(<App />);
Optional Sizes
You can specify a bootstrap large or small modal by using the "size" prop.
class Example extends React.Component { constructor(props, context) { super(props, context); this.state = { smShow: false, lgShow: false, }; } render() { let smClose = () => this.setState({ smShow: false }); let lgClose = () => this.setState({ lgShow: false }); return ( <ButtonToolbar> <Button onClick={() => this.setState({ smShow: true })}> Small modal </Button> <Button onClick={() => this.setState({ lgShow: true })}> Large modal </Button> <Modal size="sm" show={this.state.smShow} onHide={smClose} aria-labelledby="example-modal-sizes-title-sm" > <Modal.Header closeButton> <Modal.Title id="example-modal-sizes-title-sm"> Small Modal </Modal.Title> </Modal.Header> <Modal.Body>...</Modal.Body> </Modal> <Modal size="lg" show={this.state.lgShow} onHide={lgClose} aria-labelledby="example-modal-sizes-title-lg" > <Modal.Header closeButton> <Modal.Title id="example-modal-sizes-title-lg"> Large Modal </Modal.Title> </Modal.Header> <Modal.Body>...</Modal.Body> </Modal> </ButtonToolbar> ); } } render(<Example />);
Sizing modals using custom CSS
You can apply custom css to the modal dialog div using the "dialogClassName" prop. Example is using a custom css class with width set to 90%.
class Example extends React.Component { constructor(props, context) { super(props, context); this.state = { show: false, }; this.handleShow = () => { this.setState({ show: true }); }; this.handleHide = () => { this.setState({ show: false }); }; } render() { return ( <> <Button variant="primary" onClick={this.handleShow}> Custom Width Modal </Button> <Modal show={this.state.show} onHide={this.handleHide} dialogClassName="modal-90w" aria-labelledby="example-custom-modal-styling-title" > <Modal.Header closeButton> <Modal.Title id="example-custom-modal-styling-title"> Custom Modal Styling </Modal.Title> </Modal.Header> <Modal.Body> <p> Ipsum molestiae natus adipisci modi eligendi? Debitis amet quae unde commodi aspernatur enim, consectetur. Cumque deleniti temporibus ipsam atque a dolores quisquam quisquam adipisci possimus laboriosam. Quibusdam facilis doloribus debitis! Sit quasi quod accusamus eos quod. Ab quos consequuntur eaque quo rem! Mollitia reiciendis porro quo magni incidunt dolore amet atque facilis ipsum deleniti rem! </p> </Modal.Body> </Modal> </> ); } } render(<Example />);
API
Modalview source file
import Modal from 'react-bootstrap/Modal'
Name | Type | Default | Description |
---|---|---|---|
animation | boolean | true | Open and close the Modal with a slide and fade animation. |
autoFocus | boolean | true | When |
backdrop | 'static' | true | false | true | Include a backdrop component. Specify 'static' for a backdrop that doesn't trigger an "onHide" when clicked. |
backdropClassName | string | Add an optional extra class name to .modal-backdrop It could end up looking like class="modal-backdrop foo-modal-backdrop in". | |
centered | boolean | vertically center the Dialog in the window | |
dialogAs | elementType | <ModalDialog> | A Component type that provides the modal content Markup. This is a useful prop when you want to use your own styles and markup to create a custom modal component. |
dialogClassName | string | A css class to apply to the Modal dialog DOM node. | |
enforceFocus | boolean | true | When |
keyboard | boolean | true | Close the modal when escape key is pressed |
onEnter | function | Callback fired before the Modal transitions in | |
onEntered | function | Callback fired after the Modal finishes transitioning in | |
onEntering | function | Callback fired as the Modal begins to transition in | |
onExit | function | Callback fired right before the Modal transitions out | |
onExited | function | Callback fired after the Modal finishes transitioning out | |
onExiting | function | Callback fired as the Modal begins to transition out | |
onHide | function | A callback fired when the header closeButton or non-static backdrop is clicked. Required if either are specified. | |
restoreFocus | boolean | true | When |
show | boolean | false | When |
size | 'sm' | 'lg' | Render a large or small modal. |
ModalDialogview source file
import ModalDialog from 'react-bootstrap/ModalDialog'
Name | Type | Default | Description |
---|---|---|---|
centered | boolean | Specify whether the Component should be vertically centered | |
size | 'sm' | 'lg' | Specifies a large or small modal. | |
bsPrefix | string | Change the underlying component CSS base class name and modifier class names prefix. This is an escape hatch for working with heavily customized bootstrap css. |
ModalHeaderview source file
import ModalHeader from 'react-bootstrap/ModalHeader'
Name | Type | Default | Description |
---|---|---|---|
closeButton | boolean | false | Specify whether the Component should contain a close button |
closeLabel | string | 'Close' | Provides an accessible label for the close button. It is used for Assistive Technology when the label text is not readable. |
onHide | function | A Callback fired when the close button is clicked. If used directly inside
a Modal component, the onHide will automatically be propagated up to the
parent Modal | |
bsPrefix | string | Change the underlying component CSS base class name and modifier class names prefix. This is an escape hatch for working with heavily customized bootstrap css. |
ModalTitleview source file
import ModalTitle from 'react-bootstrap/ModalTitle'
Name | Type | Default | Description |
---|---|---|---|
as | elementType | <divWithClassName(h4)> | You can use a custom element type for this component. |
bsPrefix | string | Change the underlying component CSS base class name and modifier class names prefix. This is an escape hatch for working with heavily customized bootstrap css. |
ModalBodyview source file
import ModalBody from 'react-bootstrap/ModalBody'
Name | Type | Default | Description |
---|---|---|---|
as | elementType | <div> | You can use a custom element type for this component. |
bsPrefix | string | Change the underlying component CSS base class name and modifier class names prefix. This is an escape hatch for working with heavily customized bootstrap css. |
ModalFooterview source file
import ModalFooter from 'react-bootstrap/ModalFooter'
Name | Type | Default | Description |
---|---|---|---|
as | elementType | <div> | You can use a custom element type for this component. |
bsPrefix | string | Change the underlying component CSS base class name and modifier class names prefix. This is an escape hatch for working with heavily customized bootstrap css. |