Nowadays, Modals are one of the most used components in React Application, having an easy way to create React Modals will help you sheep Modals related features fast in your upcoming React Applications.
In this tutorial, we will build A simple React Modal using only 22 lines of code.
The gif below will help you understand what we are trying to build
Letβs started
Create React Project
First, create a simple react project using create-react-app cli
In this tutorial, we will use the react-popup package which is a simple and Powerfull react component with a lot of benefits :
Modal, Tooltip, Menu: All in one ποΈ
Full style customization π
Function as children pattern to take control over your popup anywhere in your code. πͺ
IE Support. π
TypeScript Support π
All these clocks in at around 3 kB zipped. β‘οΈ
This package is available in npm repository as reactjs-popup. It will work correctly with all popular bundlers.
Create React Modal
Import Popup Component from reactjs-popup and start using it like the following.
Add trigger property as a simple React Button element and set modal property to true.
Customizing and Styling React Modal
we need to create a Content.js file for Modal Content component and add some style
Now itβs time for some magic
Because reactjs-popup provides a child as function pattern, you have full control on Popup state
we will update our example to use a function as a child instead of a react element to implement close button.