react-focus-lock
react-focus-lock copied to clipboard
Multiple modals
First of all thanks for a great library. I have a use case where I need to render nested modals side by side. The pseudo output looks like this:
<body>
<react-app-root />
<modal-root >
<modal1 >
<modal2 >
</modal-root >
</body>
Both modals are wrapped with FocusLock. When I open modal 2 within modal 1 then focus is correctly transferred to modal 2. Similarly when I close modal 2 and go back to modal 1. Focus is restored.
The problem happens when I close both modals at the same time. The focus is not returned back to the open button.
Steps to reproduce:
- Open sandbox
- Click show modal
- Within the modal click on show modal 2
- Within the modal 2 click on
close all - As you can see the focus is not returned to the initial button
This bug report is possibly related or it's a duplicate of https://github.com/theKashey/react-focus-lock/issues/68
In this example (using v2.4.1) you can get focus returned to the initial show modal button if you update the code to use a setTimeout like this:
<button
id="close-all-modals"
onClick={() => {
setModal2Active(false);
setTimeout(() => setModal1Active(false), 0);
}}
>
close all modals (return focus back to show modal button)
</button>
I logged a possibly related issue
completely missed this issue. My bad.
A very interesting case. At single point of time only one modal is active and only one modal can "return focus". And this logic "breaks the chain" is multiple modals are closed at one point of time.
Easy to write a test for this case. And if one have a test - then it's easy to create a patch.
This issue has been marked as "stale" because there has been no activity for 2 months. If you have any new information or would like to continue the discussion, please feel free to do so. If this issue got buried among other tasks, maybe this message will reignite the conversation. Otherwise, this issue will be closed in 7 days. Thank you for your contributions so far.