Modals are very common in front-end applications. React-bootstrap has rebuilt the jQuery-based modal with React components that provide the same functionality as the jQuery counter-parts. In this guide I will show you how to install react-bootstrap, show and hide a modal, work with different modal events, and customize the modal to your needs using Bootstrap Modals in React. Let's get started.
To get started with using Bootstrap in your React application, you need to install the react-bootstrap
package from npm along with the bootstrap
v4 package. You will need to install regular Bootstrap for the CSS.
1yarn add react-bootstrap bootstrap
2# or
3npm i react-bootstrap bootstrap
After the installation is complete, you can import the modal component and the styling into your module.
1import Modal from "react-bootstrap/Modal";
2import "bootstrap/dist/css/bootstrap.min.css";
A modal has a few basic sections: the Header, the Title, the Body, and the Footer. These sections will hold the content that we need to display. Here's an example displaying a basic modal using these components.
1import Modal from "react-bootstrap/Modal";
2import "bootstrap/dist/css/bootstrap.min.css";
3
4const App = () => {
5 return (
6 <Modal show={true}>
7 <Modal.Header>Hi</Modal.Header>
8 <Modal.Body>asdfasdf</Modal.Body>
9 <Modal.Footer>This is the footer</Modal.Footer>
10 </Modal>
11 );
12};
Use Modal.<component>
syntax to display each component. If you would prefer not to use the object literal syntax when defining you modals, you can import each component separately as well.
1import React from "react";
2import ReactDOM from "react-dom";
3import Modal from "react-bootstrap/Modal";
4import ModalBody from "react-bootstrap/ModalBody";
5import ModalHeader from "react-bootstrap/ModalHeader";
6import ModalFooter from "react-bootstrap/ModalFooter";
7import ModalTitle from "react-bootstrap/ModalTitle";
8
9const App = () => {
10 return (
11 <Modal show={true}>
12 <ModalHeader>
13 <ModalTitle>Hi</ModalTitle>
14 </ModalHeader>
15 <ModalBody>asdfasdf</ModalBody>
16 <ModalFooter>This is the footer</ModalFooter>
17 </Modal>
18 );
19};
20const rootElement = document.getElementById("root");
21ReactDOM.render(<App />, rootElement);
Now that you have a functional modal in your application, let's make the modal interactive.
Our previous example shows how to display a modal using show
property on the modal component. I initially hard-coded the value to true
, but it is not very flexible. To improve this, let's set the value to a variable or a toggle-able expression. We will also create a button to toggle the visibility.
1const App = () => {
2 const [isOpen, setIsOpen] = React.useState(false);
3
4 const showModal = () => {
5 setIsOpen(true);
6 };
7
8 const hideModal = () => {
9 setIsOpen(false);
10 };
11
12 return (
13 <>
14 <button onClick={showModal}>Display Modal</button>
15 <Modal show={isOpen} onHide={hideModal}>
16 <Modal.Header>
17 <Modal.Title>Hi</Modal.Title>
18 </Modal.Header>
19 <Modal.Body>The body</Modal.Body>
20 <Modal.Footer>
21 <button onClick={hideModal}>Cancel</button>
22 <button>Save</button>
23 </Modal.Footer>
24 </Modal>
25 </>
26 );
27};
I've added a showModal
and hideModal
method to update a state property called isOpen
. Assigning the isOpen
variable as the value to the show
property means we now have control over whether the modal is showing or not. The new onHide
property is necessary if we want to hide the modal when clicking on the non-static backdrop or hitting the esc key. I've also added a couple of buttons to the footer to make this modal more realistic. In addition to hiding the Modal by hitting the "Esc" key or clicking the backdrop, I put the hideModal
method on the Cancel button to show a different way of hiding the modal.
The modal comes with some helpful events to assist us with the intermediate states between hiding and showing the modal. For example, if you want to show different content in your modal, on the main page as it's loading into view, or in the process of hiding, then you can utilize the onEnter
, onEntered
, onExit
or onExited
callbacks respectively. Let's look at these callbacks individually in greater depth.
The onEntered
event takes a callback that will fire once the modal has finished coming into view. This is useful if we want to asynchronously load data into the modal, or interact with elements once they are visible in the modal. In this example, let's update the title of our modal from Transitioning...
to Modal Ready
.
1const App = () => {
2 const [isOpen, setIsOpen] = React.useState(false);
3 const [title, setTitle] = React.useState("Transitioning...");
4
5 const showModal = () => {
6 setIsOpen(true);
7 };
8
9 const hideModal = () => {
10 setIsOpen(false);
11 setTitle("Transitioning...");
12 };
13
14 const modalLoaded = () => {
15 setTitle("Modal Ready");
16 };
17
18 return (
19 <>
20 <button onClick={showModal}>Display Modal</button>
21 <Modal show={isOpen} onHide={hideModal} onEntered={modalLoaded}>
22 <Modal.Header>
23 <Modal.Title>{title}</Modal.Title>
24 </Modal.Header>
25 <Modal.Body>The body</Modal.Body>
26 <Modal.Footer>
27 <button onClick={hideModal}>Cancel</button>
28 <button>Save</button>
29 </Modal.Footer>
30 </Modal>
31 </>
32 );
33};
In this example, we'll start a timer to count how long it takes to open the modal in milliseconds. We'll display in the modal's body how long it takes for the onEnter
callback to get the command to start opening the modal to when it finishes opening the modal.
1const App = () => {
2 const [isOpen, setIsOpen] = React.useState(false);
3 const [timer, setTimer] = React.useState(0);
4 const [startTime, setStartTime] = React.useState(0);
5 const [endTime, setEndTime] = React.useState(0);
6
7 const showModal = () => {
8 setIsOpen(true);
9 };
10
11 const hideModal = () => {
12 setIsOpen(false);
13 setTitle("Transitioning...");
14 };
15
16 const startTimer = () => {
17 setStartTime(Date.now());
18 };
19
20 const modalLoaded = () => {
21 setEndTime(Date.now());
22 };
23
24 return (
25 <>
26 <button onClick={showModal}>Display Modal</button>
27 <Modal
28 show={isOpen}
29 onHide={hideModal}
30 onEnter={startTimer}
31 onEntered={modalLoaded}
32 >
33 <Modal.Header>
34 <Modal.Title>{title}</Modal.Title>
35 </Modal.Header>
36 <Modal.Body>{endTime - startTime} ms</Modal.Body>
37 <Modal.Footer>
38 <button onClick={hideModal}>Cancel</button>
39 <button>Save</button>
40 </Modal.Footer>
41 </Modal>
42 </>
43 );
44};
We can similarly utilize the onExit
and onExited
callbacks to handle page interactions during the transition. Let's show a 'goodbye' message when the user closes the modal. We'll also change the page background when the modal is finished exiting.
1const App = () => {
2 const [isOpen, setIsOpen] = React.useState(false);
3 const [timer, setTimer] = React.useState(0);
4 const [startTime, setStartTime] = React.useState(0);
5 const [endTime, setEndTime] = React.useState(0);
6
7 const showModal = () => {
8 setIsOpen(true);
9 setTitle("Modal Ready");
10 document.body.style.backgroundColor = "white";
11 };
12
13 const hideModal = () => {
14 setIsOpen(false);
15 };
16
17 const startTimer = () => {
18 setStartTime(Date.now());
19 };
20
21 const modalLoaded = () => {
22 setEndTime(Date.now());
23 };
24
25 const onExit = () => {
26 setTitle("Goodbye 😀");
27 };
28
29 const onExited = () => {
30 document.body.style.backgroundColor = "green";
31 };
32
33 return (
34 <>
35 <button onClick={showModal}>Display Modal</button>
36 <Modal
37 show={isOpen}
38 onHide={hideModal}
39 onEnter={startTimer}
40 onEntered={modalLoaded}
41 onExit={onExit}
42 onExited={onExited}
43 >
44 <Modal.Header>
45 <Modal.Title>{title}</Modal.Title>
46 </Modal.Header>
47 <Modal.Body>{endTime - startTime} ms</Modal.Body>
48 <Modal.Footer>
49 <button onClick={hideModal}>Cancel</button>
50 <button>Save</button>
51 </Modal.Footer>
52 </Modal>
53 </>
54 );
55};
Making the modal match your brand and design is simple. Use the same methods available for any react component to style a modal, including any CSS-in-js solution, standard CSS, and CSS Modules. If you need to add a class to the modal dialog, use the dialogClassName
property.
1const App = () => {
2 return (
3 <Modal show={true} dialogClassName={"primaryModal"}>
4 <ModalHeader>
5 <ModalTitle>Hi</ModalTitle>
6 </ModalHeader>
7 <ModalBody>asdfasdf</ModalBody>
8 <ModalFooter>This is the footer</ModalFooter>
9 </Modal>
10 );
11};
In the above example, the .primaryModal
class will be added to the modal dialog div.
as
propertyThe default HTML element for a modal component container is the div
element. If you do not want to use the div
default, you can specify a different element with the as
property. This works for not just the modal dialog, but also for the header, title, body, and footer.
1const App = () => {
2 return (
3 <Modal show={true} as="section">
4 <ModalHeader as="span">
5 <ModalTitle as="h4">Hi</ModalTitle>
6 </ModalHeader>
7 <ModalBody as="section">asdfasdf</ModalBody>
8 <ModalFooter as="footer">This is the footer</ModalFooter>
9 </Modal>
10 );
11};
The size
property on the <Modal>
component can be used to set the width of the modal to the defaults defined in the bootstrap CSS. There are three options: sm, lg, xl. If you want to define your own custom class to set the width, you can do that as well.
1const App = () => {
2 return (
3 <Modal show={true} size="lg">
4 <ModalHeader>
5 <ModalTitle>Hi</ModalTitle>
6 </ModalHeader>
7 <ModalBody>asdfasdf</ModalBody>
8 <ModalFooter>This is the footer</ModalFooter>
9 </Modal>
10 );
11};
Working with the react-bootstrap component library allows you the benefits of Bootstrap and React in one tool. And, if you already know how to use React components and properties, then you immediately start working with Bootstrap. After reading this guide, you should be able to:
Please keep an eye out for more guides, where I will show you how to use bootstrap components within a react application. Thanks for reading 😃
For more details about styling react components, check out the course Styling React Components by Jake Trent. This guide will introduce you to react-bootstrap.