Skip to content
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

Modal doesn't trap focus #770

Closed
ivanbrykov opened this issue Jan 30, 2022 · 5 comments
Closed

Modal doesn't trap focus #770

ivanbrykov opened this issue Jan 30, 2022 · 5 comments
Labels
Fixed patch Completed issues that will be published with next patch (1.0.X)

Comments

@ivanbrykov
Copy link

Hi. I stumbled upon a scenario when a modal can't trap focus. It goes like this:

  • Open a drawer
  • From the drawer open a modal
  • Hide the drawer immediately in the same callback

As a result, the modal is opened but it has no focus and doesn't close by pressing ESC.

The reason it happens is that the drawer is animated. If I wait for 300ms before opening the modal, everything works as expected.

I don't know what the fix here would be. Maybe I could tell the drawer to pass focus to the modal via ref in some callback?

@rtivital
Copy link
Member

Thanks for reporting!

@rtivital rtivital added the help wanted Contributions from community are welcome label Jan 30, 2022
@rtivital rtivital added the bug label Feb 7, 2022
@auronsan
Copy link

auronsan commented Apr 2, 2022

I tried fix this bug. seems need change like 3 files. Drawer passRefModal, Modal pass modalRef and useFocusReturn pass the ref to trigger last active focus.
but still not gonna work without setTimeout too.
on useFocusReturn there race condition between focus lastActive on drawer when close and modal focusing

so I think use setTimeout on 1 line code it's not gonna hurt

@rtivital rtivital removed the help wanted Contributions from community are welcome label Apr 4, 2022
@rtivital rtivital added Fixed patch Completed issues that will be published with next patch (1.0.X) and removed bug labels Aug 11, 2022
@rtivital
Copy link
Member

Fixed in 5.1.4, Modal and Drawer components now support withFocusReturn prop, set it to false to prevent the component from returning focus to previous active element after Modal/Drawer is closed.

@adri1wald
Copy link

adri1wald commented Aug 22, 2022

Hey @rtivital, would be awesome if withFocusReturn were dynamic.

My use case is under certain circumstances I do want the focus to return when I close the modal, but other time I don't.

In my modal there is an action that closes the modal and renders an input the main part of the app. This input should be focused after closing the modal. However, when the modal is closed without taking this action the focus should return to where it was before.

Could withFocusReturn accept a callback of signature () => boolean? Then I could set a ref to true / false in onClose.

@rtivital
Copy link
Member

I'll make it dynamic without function in next patch

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Fixed patch Completed issues that will be published with next patch (1.0.X)
Projects
None yet
Development

No branches or pull requests

4 participants