moai icon indicating copy to clipboard operation
moai copied to clipboard

Cannot press Escape to close Dialog

Open lqt93 opened this issue 4 years ago • 5 comments

Can not close the Dialog by pressing Escape. It is easy to see in the Basic example of Dialog in Moai docs: https://docs.moaijs.com/?path=/docs/components-dialog--primary

lqt93 avatar Jul 03 '21 03:07 lqt93

Yeah... the problem is we are binding the "onEscape" event at the wrapper div of the dialog:

https://github.com/moaijs/moai/blob/6dd5286444e2dc11e800031be85a84b4323a40f2/lib/core/src/dialog/main/main.tsx#L56-L58

This makes the code real simple, but it won't work if the user is not focusing on the dialog (technically: document.activeElement is inside the wrapper div).

At the moment I'm not really sure what should be done here:

  • We could bind the event to the document element, which will work but I don't feel right...,
  • Or, we should keep the listener at the wrapper, and "trap" the user's focus (technical) inside the dialog (e.g. they can't even "tab" outside of it")

Anyway, should look at other leading dialog to see how they are handling this.

thien-do avatar Jul 03 '21 03:07 thien-do

@nhducit @ZeroX-DG @monodyle We're more than happy to hear any opinion from you guys

thien-do avatar Jul 03 '21 03:07 thien-do

The problem is the dialog is not focused when the dialog shows up. So you can't close dialog via keydown event until you got focused on them. I have to quickly look on rc-dialog, which used by ant.design, and see that they focus on the dialog content, which inside the wrapper.

https://github.com/react-component/dialog/blob/cb6f8f0dbad9656b03d18c036ba4f7525b31d3d0/src/Dialog/index.tsx#L64-L70

Understandable because when you show up the dialog, which has a z-index over all others, then the tabindex focusing should be on this. See this example from w3 for better visualization.

monodyle avatar Jul 03 '21 09:07 monodyle

I totally agree with focusing on the content of dialog whenever it is opened.

lqt93 avatar Jul 03 '21 14:07 lqt93

oh thanks @monodyle ! Didn't know w3 has an example for dialog!

thien-do avatar Jul 05 '21 00:07 thien-do