Enhancing Your React Application with Modals Using Libraries
Written on
Introduction to Modals in React
Modals are essential components that we often need to implement in our React applications. To streamline this process, we can utilize pre-existing component libraries. In this guide, we will explore how to integrate a modal into our React application using the Rodal and Reactjs-popup libraries.
Adding the Rodal Library
To begin, you can incorporate the Rodal library by executing the following command:
npm i rodal --save
Next, you can use it in your application as follows:
import React, { useState } from "react";
import Rodal from "rodal";
import "rodal/lib/rodal.css";
export default function App() {
const [visible, setVisible] = useState(false);
return (
<div>
<div>
<button onClick={() => setVisible(true)}>Show</button>
<Rodal visible={visible} onClose={() => setVisible(false)}>
<div>Content</div></Rodal>
</div>
</div>
);
}
In this code, we import the Rodal library along with its corresponding styles. The visible property is linked to the state, allowing us to control the visibility of the modal. When the button is clicked, setVisible is called with true, displaying the modal. The onClose function is invoked when the close button is pressed, setting visible to false.
Rodal offers a variety of props to customize your modal experience, including:
- width to adjust the width
- height to set the height
- showMask to determine mask visibility
- animation for animation effects
- enterAnimation and leaveAnimation for entrance and exit animations
- duration for animation duration
- closeOnEsc to allow closing the modal with the Escape key
Here’s an example of using some of these props:
import React, { useState } from "react";
import Rodal from "rodal";
import "rodal/lib/rodal.css";
export default function App() {
const [visible, setVisible] = useState(false);
return (
<div>
<div>
<button onClick={() => setVisible(true)}>Show</button>
<Rodal
enterAnimation="zoom"
leaveAnimation="zoom"
closeOnEsc
visible={visible}
onClose={() => setVisible(false)}
>
<div>Content</div></Rodal>
</div>
</div>
);
}
Other animation types include: fade, flip, door, rotate, slideUp, slideDown, slideLeft, and slideRight.
Integrating Reactjs-popup for Popups
The Reactjs-popup library allows us to create popups that can be triggered by an element. To install this library, run:
npm i reactjs-popup
You can implement it in your app like this:
import React from "react";
import Popup from "reactjs-popup";
import "reactjs-popup/dist/index.css";
export default function App() {
return (
<div>
<div>
<Popup trigger={<button>Trigger</button>} position="right center">
<div>Popup content here!!</div></Popup>
</div>
</div>
);
}
Here, we import the Popup component and its styles. The trigger property is set to the element that activates the popup, while position specifies where the popup appears. You can include any content you wish inside the popup.
Additionally, you can create a modal by adding the modal prop, as shown below:
import React from "react";
import Popup from "reactjs-popup";
import "reactjs-popup/dist/index.css";
export default function App() {
return (
<div>
<div>
<Popup trigger={<button>Trigger</button>} modal nested>
{(close) => (
<div className="modal">
<button className="close" onClick={close}>
×</button>
<div className="header">Modal Title</div>
<div className="content">Lorem ipsum</div>
<div className="actions">
<Popup
trigger={<button className="button">Trigger</button>}
position="top center"
nested
>
<span>Lorem ipsum</span></Popup>
<button
className="button"
onClick={() => {
console.log("Modal closed");
close();
}}
>
Close Modal</button>
</div>
</div>
)}
</Popup>
</div>
</div>
);
}
In this example, the close function is utilized to close the modal, and the nested prop allows another modal to open within the first.
Conclusion
In summary, integrating modals into your React application can be achieved efficiently using the Rodal and Reactjs-popup libraries. This enhances user experience and provides a more interactive interface.
Learn more about implementing modals in React through these helpful videos:
The first video, "Create a Modal with React (Pop-up) - YouTube," provides a practical guide to building modals using React.
The second video, "How to Make Popup Modal in React JS - YouTube," offers further insights into creating popups in React applications.