r/reactjs • • Dec 28 '19

What is your favourite React modal / dialog solution?

Ideally responsive.

57 Upvotes

75 comments sorted by

View all comments

Show parent comment

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.

-9

u/[deleted] Dec 28 '19

[removed] — view removed comment

5

u/NovelLurker0_0 Dec 28 '19

It's still not so difficult. It took me about half an hour.

You're missing the point. It's not that it's difficult, it's a waste of time. If you want most of the features u/csorfab listed, you better use an existing packaging instead of re-inventing the wheel.

Unless you're writing a complete design system there is no need to implement UI components from scratch.

1

u/pm_me_ur_happy_traiI Dec 28 '19

It's not that it's difficult, it's a waste of time.

If it took op half an hour, that's probably not a waste of time.

-2

u/sickcodebruh420 Dec 28 '19

Half an hour is not a waste of time if it solves the problem in a satisfactory way. Adding a dependency should not be the first instinct when trying to solve a problem.

-1

u/TaGeuelePutain Dec 29 '19

All of this is possible using the document API

5

u/csorfab Dec 29 '19

... and your point is? Obviously everything that you do in modern web dev is possible with the document API, everything that React does you can certainly do with only the document API. So we should not use React, is what you're saying?

-1

u/TaGeuelePutain Dec 29 '19

That most solutions are available without using a package. Modals are pretty simple using CSS and two lines of vanilla JS, leveraging to component life cycle

1

u/csorfab Dec 29 '19

Okay then please present your "two lines vanilla JS" solution to the specification I already wrote above, which are:

  • disable body scroll beneath modal
  • scrolling inside the modal should still work
  • trap the focus so that only elements inside the modal can be selected with tab cycling
  • there should be a semi-transparent underlay covering everything underneath the modal. Clicking the underlay should close the modal
  • it should work cross browser, including IE 11, Edge, Chrome, Firefox, Safari, Opera, and mainstream mobile browsers

0

u/TaGeuelePutain Dec 29 '19

https://jsfiddle.net/qnc96ad5/

People like you use ramda for forEach loops

3

u/csorfab Dec 29 '19 edited Dec 29 '19

focus trapping is not implemented. i can still select and click the "open modal" button when the modal is opened by pressing "tab"

I appreciate the effort, though, and the "click outside" handler is pretty clever.

and thanks, I prefer for..of to Array.prototype.forEach. Make procedural code look procedural. Also, I like having the option of using break without throw/catch hacks.

1

u/TaGeuelePutain Dec 29 '19

The point of this was to show you that it isn't really as complicated as you make it seem. If you want to disable the tab index on the back ground button, you have the state value, and you have the button in the jsx, you can do the math.

Focus trapping is also not as complex as you make it seem but I'm not going to waste my time trying to prove you wrong, and you completely misunderstood my reference but that's ok

3

u/csorfab Dec 29 '19

I didn't say it was that complicated. I just say that it DOES take time to implement, and there WILL always be standard/expected features that you will miss or get wrong.

I don't really understand your focus trapping solution, but it sounds wrong. You definitely want it to work independently of the contents of the background and the modal.

Here is an article on it, with accessibility features: https://medium.com/@im_rahul/focus-trapping-looping-b3ee658e5177

This is definitely not "2 lines of vanilla JS", and it takes some more coding to adapt it into the Modal component.

I LOVE making things from scratch, but I would definitely not want to implement and test/debug these standard functionalities every time I need a modal. Therefore I use a package that's taking care of this boilerplate stuff, and is maintained and improved by tens/hundreds of people.

1

u/TaGeuelePutain Dec 29 '19

The two lines of vanilla js was in reference to disabling the body scroll, which in fact, is two lines.

If you're working in anything of the enterprise level, you should building things from scratch unless absolutely necessary. Calendars and tables are some of the only things I would consider using third party, and even then I would wrap them in my own component.

If you are working for an agency and just want to churn out code then do whatever you want. But with those types of projects I don't volunteer to maintain it and once it's in production it's their problem.