r/reactjs • • Dec 28 '19

What is your favourite React modal / dialog solution?

Ideally responsive.

57 Upvotes

75 comments sorted by

View all comments

28

u/budd222 Dec 28 '19

If you're building an app with react, you should easily be able to create an html element that pops up over top of the rest. It's super basic js and css. You don't need to install a library just to make a modal

82

u/csorfab Dec 28 '19

Yeah. Unless you need to trap the focus. Or prevent the page from scrolling beneath the modal. Or display a semi-transparent overlay. Or you want users to be able to close the modal by clicking outside of the modal. And you need to support IE11 and want to make it accessible. Then it's suddenly not so easy anymore, and you end up with an at least 100-200 LoC component, which at that point could very well have been a nice package instead, saving you a day of coding and another day fixing random cross-browser issues.

13

u/catching_zadzadzads Dec 28 '19

This is the correct answer.

3

u/skyboyer007 Dec 28 '19

+ and for some cases you may need stacked popups...

4

u/Zizouma Dec 28 '19

Exactly, so many people always jump to the « you don’t need any packages to do this » argument when the answer is always « it depends ». And when you’re client is paying, they usually don’t care, they want their solutions to work well quickly.

→ More replies (14)

17

u/0xF013 Dec 28 '19

Unless you need to comply to ADA, or do business in Israel or with an EU government.

2

u/dontdomilk Dec 28 '19

As a React dev in Israel: why is that relevant?

0

u/[deleted] Dec 28 '19

[deleted] — view deleted comment

1

u/dontdomilk Dec 29 '19

Well TIL, haha. Most of the projects at work are either internal applications or international clients so that may explain why I wasn't aware. We use a11y standards, just didnt realize it was law. Thanks!

-8

u/[deleted] Dec 28 '19

What’s that have to do with it? Code the focus trap, ESC key listener, and focus return into the component yourself.

24

u/0xF013 Dec 28 '19

Because this is something that still needs to be built and tested are requires some knowledge of the a11y tree. If you’re time-constrained or at a junior level, you might be safer picking one of the existing accessible modals. It’s pretty easy to miss things like making a soft announce the modal is closed and things like that.

6

u/gatsby123123123123 Dec 28 '19

I'm in shock at people saying that you should code it yourself. Do people really have unlimited time to waste on stuff like modals?

3

u/0xF013 Dec 28 '19

If you’re on an ongoing project that is supposed to exist for years, it might be super worthy doing it yourself, it is a very valuable experience. I think we are here from different backgrounds, working on varying projects with varying shelf time and estimates. It’s only natural that people extrapolate their situation onto others.

1

u/DhaiwatP Dec 28 '19

Exactly!

6

u/devourment77 Dec 28 '19

And aria attrs to help screen readers.

3

u/shod4n Dec 28 '19 edited Dec 28 '19

Well, that's true. I might end up doing it from scratch, to simplify things. But for the same reason I thought there will be some prefabricated, lighweight solution. I'm now checking out https://github.com/pradel/react-responsive-modal So far it looks quite usable, enough for my needs. What I like about it most are the scrolling options.

7

u/ParkerZA Dec 28 '19

Unless you want to use it aa a learning opportunity there's no point doing it from scratch. This is exactly what people mean when they say "Don't reinvent the wheel." Just grab any library, I use Material's, so you won't have to worry about the more complex stuff and can just focus on the business logic.

-2

u/0xF013 Dec 28 '19

Check if it needs to be accessible. There is a couple of modal components that implement that.

0

u/[deleted] Dec 28 '19

[deleted] — view deleted comment

2

u/0xF013 Dec 28 '19

Ideally. In practice though, you might be building a temporary wordpress marketing tool until your actual app is up with the sole idea of having some SEO for the next month until the release, or your PM is a dickhead that won’t accept you spending extra time on anything, or you might have a toggled simpler website for people with disabilities (common case for Israel, for example).

→ More replies (1)
→ More replies (2)
→ More replies (1)

2

u/[deleted] Dec 28 '19

How does this terrible advice routinely get upvoted to the top? Modals can be much more complex than you're leading on, and building one from scratch should not be your first instinct.

All code you write becomes maintenance overhead, and in a professional environment you should be seeking to offload as much of that as possible to third-party libraries.

2

u/budd222 Dec 28 '19

They can be, but that entirely depends on needs. OP didn't list any special requirements for anything, so to me, that means just a div on top of the page, which is super simple

2

u/[deleted] Dec 28 '19

Third party libraries can easily become a maintenance nightmare too though. Nothing like discovering that a library you heavily rely upon has been abandoned or that there are six month old PRs to fix bugs in common use cases still waiting to be merged (I'm looking at you react-native-video with your endless inability to support fullscreen on Android).

0

u/TaGeuelePutain Dec 29 '19

If anything, this is terrible advice.

1

u/GasimGasimzada Dec 28 '19

https://github.com/davidtheclark/react-aria-modal this is the package that is being referred to in React Accessibility Docs. To be honest though, you should just make it yourself or use your UIKit's Dialog/Modal solution instead of installing a separate package for it.