DEV Community

Cover image for How to Create a Modal in React

How to Create a Modal in React

Francisco Mendes on August 21, 2021

One of the most used components in React is undoubtedly modals because they can be used in different contexts, from messages to user input. Like m...
Collapse
 
liquidvapour profile image
Ra-el Peters • Edited

Thanks for this tut it was supper useful.

One thing I noticed, at least on my setup, the .darkBG style leaves 1px line not darkened on the top and left side of the browser client area. This is dependent on the size of the window as resizing can make it go away.

I simplified the .darkBG style and this went away for me:

.darkBG {
  background-color: rgba(0, 0, 0, 0.2);
  width: 100vw;
  height: 100vh;
  z-index: 0;
  top: 0;
  position: absolute;
}
Enter fullscreen mode Exit fullscreen mode

hope this helps

Collapse
 
bikasv profile image
Bikas Vaibhav

You should consider portals (reactjs.org/docs/portals.html) for modal as it scopes it out of usual DOM structure including its events.

Collapse
 
franciscomendes10866 profile image
Francisco Mendes • Edited

I considered using it, but since I don't have other elements that use z-index like notifications and toasts, I decided to take a simpler approach. But if I had one of the ones mentioned before, I wouldn't hesitate to use Portals. 😎

Thanks for the comment, it is quite relevant. πŸ™Œ

Collapse
 
swagwik profile image
Sattwik Sahu • Edited

If you increase the padding a little bit and decrease the border-radius on the modal and buttons (about 6-7px) it will look even more sleek. Considering it being a React tutorial, mainly, you actually went ahead to discuss the styling; it won't hurt to beautify it a little bit more πŸ˜‰

Collapse
 
franciscomendes10866 profile image
Francisco Mendes

Thanks for the tip and feedback! πŸ‘Š

Collapse
 
johnsmall profile image
John Small

I'm puzzled by the nameless tag <>....</> introduced in step 7

I'm just starting React so it could be some special React syntax I've not learned about yet. It's not something I've ever encountered in HTML.

Can someone explain that one please.

Collapse
 
davidverhage profile image
DΞ›VΞ • Edited

Dear John, the <>...</> tag is a 'reference' tag. React needs a binding start and close tag so if you want to push < img src={background.image} alt='alternative info'. / > in a return this would cause an error. To overcome this an empty set of tags is used.

I hope this explains the tag.
kind regards,
David Verhage

Collapse
 
trostcodes profile image
Alex Trost

The term you're looking for is Fragment
reactjs.org/docs/fragments.html

Collapse
 
gdenn profile image
Dennis Groß (he/him)

I like the design of your modal. Looks very modern but simple.

Collapse
 
franciscomendes10866 profile image
Francisco Mendes

Thanks so much for the feedback! πŸ™ It's the small details that make the difference πŸ’ͺ

Collapse
 
gdenn profile image
Dennis Groß (he/him)

I also noticed that design is about the details like spacing, font, composition...

That was just not so obvious for me before because I have been more of a backend developer.

Collapse
 
tinapc profile image
Hung Nguyen

Did great job, man πŸ‘ŒThanks a lot.

Collapse
 
franciscomendes10866 profile image
Francisco Mendes

Glad to know! 😊 Thanks!

Collapse
 
thanhtutzaw profile image
ThanHtutZaw

when I click toggle it makes re rendering its parent. Is it okay ?

Collapse
 
darshand profile image
Darshan Dhabale

I am worried about all the rerendering that is going to happen in the parent component
You are storing the open/close modal state up in the parent, if you update it, the whole parent re-renders

Collapse
 
danielgould profile image
Gld

Thanks for this useful article.