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

Input field in a popover within a modal is not focusable #36692

Closed
lcsqlpete opened this issue Jul 6, 2022 · 17 comments
Closed

Input field in a popover within a modal is not focusable #36692

lcsqlpete opened this issue Jul 6, 2022 · 17 comments
Labels

Comments

@lcsqlpete
Copy link

lcsqlpete commented Jul 6, 2022

I have a popover with an input field that is within a modal. When the popover is displayed, it is not possible to type anything into the popover input field and the first field in the modal is highlighted instead of the popover input field.

I found a stack overflow post regarding this issue which fixed the problem by removing tabindex=-1 from the modal, which I did, and that appeared to fix the problem.

However, the stack overflow solution was using a beta version of Bootstrap 5, 5.0.0-beta1 and when I use my current version, 5.1.3, the problem returns.

I am on OSX 10.15.7 Bootstrap version 5.1.3 and using Chrome version 103.0.5060.53.

My code is in the attached zip file

modal_popover.html.zip

@julien-deramond
Copy link
Member

julien-deramond commented Jul 7, 2022

Thanks for reporting this issue @lcsqlpete.
Could you please try to embed your example in a CodePen or StackBlitz?
For security reasons, on my side, I won't download a zip file.

@lcsqlpete
Copy link
Author

OK, will try to do that today. I tried to paste the code into the issue report but for some reason, it didn't interpet all of it as html code and displayed it as a web page.

@lcsqlpete
Copy link
Author

The codepen is here

@julien-deramond
Copy link
Member

julien-deramond commented Jul 7, 2022

Your use case seems to be related to what's described in the 2nd and 3rd paragraphs of Making popovers work for keyboard and assistive technology users isn't it? Wouldn't it be better to consider using a modal dialog instead?

@lcsqlpete
Copy link
Author

lcsqlpete commented Jul 7, 2022 via email

@julien-deramond
Copy link
Member

Without talking about popover in a modal, even a simple popover containing form controls won't allow all your users to interact easily with those form controls because of what's mentioned in the paragraphs. I would suggest using a modal instead (or something else) but not a popover that doesn't seem appropriate here.

@lcsqlpete
Copy link
Author

lcsqlpete commented Jul 7, 2022 via email

@julien-deramond
Copy link
Member

I confirm that it is working in your CodePen when I change the reference to use the 5.0.0-beta1.
But it appears that you have removed the tabindex="-1" isn't it? If I put it back, it doesn't work anymore even with the 5.0.0-beta1. IMHO tabindex="-1" shouldn't be removed, otherwise the accessibility for the modal will be broken.

Keeping that in mind, I'll try to find what's changed between 5.0.0-beta1 and 5.1.3.

@lcsqlpete
Copy link
Author

Yes, I removed the tabindex since that was the recommended solution in the stack overflow post I referenced but I understand there are problems associated with that.

WHile I am interested in what changed, don't spend much time on that since I doubt it will be changed and I can probably use a hidden div instead of the popover that is displayed when the user clicks the Add button.

@patrickhlauke
Copy link
Member

modal dialogs need to contain their focus. once a modal is open, focus is not allowed to go out of it (e.g. into the underlying page behind the semi-transparent overlay) to conform with https://www.w3.org/WAI/ARIA/apg/patterns/dialogmodal/.

newer versions of bootstrap strictly enforce this. that can cause issues with other widgets that dynamically generate things like popovers etc if these live outside of the modal dialog.

try providing a container inside the modal, and explicitly pointing the popover to use that https://getbootstrap.com/docs/5.2/components/popovers/#custom-container - and in light of https://getbootstrap.com/docs/5.2/components/popovers/#making-popovers-work-for-keyboard-and-assistive-technology-users make sure that container follow immediately after the "Add" button.

as an aside, note that your "Add" button can't be activated using the keyboard, it seems.

and the tabindex="-1" on the modal is needed to make sure that it receives focus once it's opened, and for the correct focus trapping.

@lcsqlpete
Copy link
Author

Thanks for the suggestions, I will try them. I'm closing this now since if the suggestions don;t work, I will go with a hidden div as mentioned in an earlier post.

@patrickhlauke
Copy link
Member

@lcsqlpete i had a bit of spare time to see if i could get my suggestion above to work ... and indeed, forked your original codepen and just by changing the content: ... parameter for the popover in the JS to use .modal-body instead of body I think I got it to work just fine, without the need for any hacks like removing tabindex="-1" etc.

https://codepen.io/patrickhlauke/pen/GRxyjoG

as said, this is required because modals now enforce focus to stay inside the modal (as it should), and if the popover is generated/added to the end of the <body> element by default it's going to be outside of the modal. with this tweak, it's generated/appended to the content of .modal-body (still not ideal, you'd ideally want to provide an extra container that comes directly after the "Add" button ... but as a first stab this will do).

@patrickhlauke
Copy link
Member

patrickhlauke commented Jul 30, 2022

Slightly modified version, where I use an actual separate <div class="popover-container"></div> that comes straight after the "Add" control (now turned into a proper <button>) ... though this seems to mess with the placement a bit, but can likely be tweaked further. https://codepen.io/patrickhlauke/pen/poLpEbz

@lcsqlpete
Copy link
Author

Thank you Patrick, both of your solutions appear to work.

patrickhlauke added a commit that referenced this issue Jul 30, 2022
mention the scenario of popovers inside modals - see #36692 (comment)
mdo pushed a commit that referenced this issue Aug 3, 2022
mention the scenario of popovers inside modals - see #36692 (comment)
@patrickhlauke
Copy link
Member

got a small mention of this into the docs #36867

@lcsqlpete
Copy link
Author

lcsqlpete commented Aug 3, 2022 via email

@singhhardy
Copy link

I was stuck in same problem while upgrading my old theme along with plugins.
The solution is
when you initialize the popover that is inside a popover in a modal.
you have to specify the container of the popover

$('#my-popover').popover{ container: 'modal' // <== Just add this to make it focusable. }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants