The goal:
Create a React modal dialog box using the new html "dialog" element. Content provided as children. (Compare to: React modal using an ht...
For further actions, you may consider blocking this person and/or reporting abuse
BTW, if you don't want to have that inner div to block clicks, you could use something like this:
That's good as well, I've replaced the extra div with the rectangle check as a function, thanks.
If we have to write javascript to make an HTML element usable, I think that points to a design flaw. :o)
Thanks for this well detailed yet concise guide!
A few comments though:
If you use the keyboard you'll find out there's a bug: after closing the dialog with the
Esc
key, the dialog won't open again. That's because theisOpened
prop isn't being updated uponEsc
, hence clicking theOpen "dialog" modal
button doesn't change the state and theuseEffect
isn't being called. In order to fix this you need to listen to keyboard events on the window/document (not on the dialog itself, otherwise it won't work if theEsc
key is pressed after shifting the focus from the dialog element) and callonClose
afterEsc
was pressed.Just a minor tedious TypeScript suggestion: replace
const ref: any = useRef(null)
withconst ref = useRef<HTMLDialogElement>(null)
.Not really a comment but a question: why do we need the
preventAutoClose
stuff? I tried your code without it and I haven't encountered the situation in which clicking the dialog content closes it. What am I missing?Thanks!
Thanks.
Point 1. Just tested with Firefox (v112) and Edge (v111), and I'm afraid I haven't been able to reproduce that. (
isOpened
"is" being updated properly.)Point 2. If I do that, I get
Point 3. Again on Firefox & Edge, if I remove
preventAutoClose()
and click inside the dialog, it closes.Which perhaps goes to show, that the various implementations of the
<Dialog>
html element may have some bugs or inconsistencies as yet.Still got the issue here, as pressing Esc doesn't update the dialog state.
As I'm wondering to use
dialog
vs a full React solution, I still have to create a Esc key listener in order to make it works am I right?Does anyone succeeded by fixing this issue/totally normal behavior?
Is my code below right (made from scratch from my storybook)
A quick note. Personally, I would still prefer the React way: I find it simpler and more intuitive to implement and to read. The html dialog should have been simpler, but it feels a bit messy, and not well enough designed.
You have to add
type="button"
to the close button, otherwise you won't be able to open the dialog again after closing it with theEsc
key.Hi Bartłomiej,
Thanks. What you are saying: I have tested it on Firefox and Edge (both Windows and up-to-date), but I cannot reproduce it. The dialog can re-open without a problem after "Escape".
Actually, (if I understand it correctly) with React, we (or I :o) typically use the js action (ie. the onClick) to trigger the action and not the HTML action as such, so the html element doesn't matter much. (In React, typically:) We are just using the "onClick" for js, and we don't care much about the html element or its html structure. (Well, this is foremost a React and js application.)
That we are using a button is just for the show really. All of the following will work just fine:
(Actually, within the React context, I am "personally" a proponent of using div's instead of buttons and such where we use the onClick, and the html element name actually does not matter.)
AAMOF the html tag does matter. Using div's instead of buttons is really bad for accessibility.
True, there is that.
To be honest, most projects I work on, neither accessibility nor keyboard use is included in the requirements or functionality. I'm not saying it is good or bad. Just what it is.
And very coincidentally, I've just read this new article. It is somewhat relevant perhaps. And i sympathise with the point of view there. Simplicity is also a thing. thinkdobecreate.com/articles/a-cal...
There are not many examples of React modals using the HTMLDialogElement and even less with such a clear and clean implementation.
Thank you very much!
I made it into an npm package for a training project: npmjs.com/package/react-basic-moda...
Great. The npm page looks neat. I'll try it when I have time :)