New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Opening nested dialog when the parent dialog doesn't have focus closes parent dialog #3522
Comments
Yes, the dialog element must be focused at least once to trigger the check on nested dialogs: ariakit/packages/ariakit-react-core/src/dialog/utils/use-hide-on-interact-outside.ts Lines 90 to 94 in 6d29ca1
Note For anyone else working on this in the future (myself included): A potential solution might be Could you provide more details on why you need |
The main reason: depending on what opened the dialog, the first focusable element may have Update 1: I've never given it enough thought before, but it seems that the focus will only be visible if at the moment of opening a dialog, Update 2: there's another reason (although not applicable to this particular dialog). We have an element with |
You may want to use Ariakit's - <div autofocus />
+ <Focusable autoFocus render={<div />} /> |
This works! The issue related to Update: it's actually not related to |
Current behavior
This may seem like a contrived example, but it has arisen from a real-world application. In order for the bug to happen, the following criteria need to be fulfilled:
autoFocusOnShow
set tofalse
onMouseDown={(e) => e.preventDefault()}
. The reason for this specific code is that in our application this interactive element comes from @hello-pangea/dnd, and according to their docs, a draggable handle that also serves as a clickable element will have this behaviour (to prevent the element from becoming focused)Steps to reproduce the bug
Expected behavior
The outer dialog stays open, and the nested dialog appears.
Workaround
In our case, removing
autoFocusOnShow={false}
is probably the best solution. However, it's not a proper workaround as it changes the behaviour.Edit by @diegohaz: Alternatively, return
false
on the parent Dialog'shideOnInteractOutside
function prop whendocument.querySelector("#inner-dialog").contains(event.target)
.Possible solutions
According to my understanding, this happens when the outer dialog doesn't have focus at the moment the inner dialog opens. The focus is outside the dialog (e.g. on the
<body>
element), and so the focus travels from outside the dialogs directly to the nested dialog. There's likely code in Ariakit that incorrectly identifies such focus event as moving focus outside of the outer dialog. PlacinghideOnInteractOutside
with an event handler on the outer dialog demonstrates that it's this focus event (namely,focusin
) that closes the outer dialog.The text was updated successfully, but these errors were encountered: