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

Can't focus input field inside Toast when a Modal is shown #38515

Open
3 tasks done
AgentSmith0 opened this issue Apr 26, 2023 · 16 comments · May be fixed by #41065
Open
3 tasks done

Can't focus input field inside Toast when a Modal is shown #38515

AgentSmith0 opened this issue Apr 26, 2023 · 16 comments · May be fixed by #41065
Labels

Comments

@AgentSmith0
Copy link

AgentSmith0 commented Apr 26, 2023

Prerequisites

Describe the issue

It is not possible to focus on input fields inside a Toast when a Modal is opened. Somehow, buttons are still clickable. Once the Modal is closed, the input field is clickable again.

Reduced test cases

Edit by maintainer: see https://codepen.io/julien-deramond/pen/gOBWBzX

<!-- Button trigger modal -->
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#exampleModal">
  Launch demo modal
</button>

<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h1 class="modal-title fs-5" id="exampleModalLabel">Modal title</h1>
        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
      </div>
      <div class="modal-body">
        ...
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div>
  </div>
</div>


<div class="toast-container position-fixed mt-5 top-0 start-0" style="z-index:20000">
  <div class="toast fade show" data-bs-autohide="false">
    <div class="toast-header">
      <strong class="me-auto">Title</strong>
      <button class="btn-close ms-2 mb-1" type="button" data-bs-dismiss="toast"></button>
    </div>
    <div class="toast-body">
      <input class="form-control" type="text">
      <button class="btn btn-outline-secondary">Btn</button>
    </div>
  </div>
</div>

Open the modal with the button and try to focus the input field inside the toast.

What operating system(s) are you seeing the problem on?

Windows, macOS, Android, iOS, Linux

What browser(s) are you seeing the problem on?

Chrome, Safari, Firefox, Microsoft Edge, Opera

What version of Bootstrap are you using?

v5.3.0-alpha3

@julien-deramond

This comment was marked as resolved.

@AgentSmith0

This comment was marked as resolved.

@coolBrown12
Copy link

It sounds like you are encountering an issue where you are unable to focus on an input field inside a Toast component when a Modal is displayed on the screen. This is a common problem that occurs when there are multiple layers of UI components on the screen, and the Modal is taking focus away from the Toast.

One solution to this problem is to use a higher z-index value for the Toast component, which will ensure that it is displayed on top of the Modal. Additionally, you can add a tabindex attribute to the input field inside the Toast, which will allow users to focus on the input field using the keyboard.

@AgentSmith0
Copy link
Author

No, I already tried setting a higher z-index for the Toast, but this doesn't work, you can try it yourself. Also tabindex has no effect. But thanks for trying to help!

@gpl4all
Copy link

gpl4all commented May 10, 2023

look at this:
https://codepen.io/gpl4all/pen/MWPVLQw
i moved toast inside modal, and input can get focus

your problem is caused by overlay layer of modal is on top of everything else on screen which is your toast.

another thought, i usually use toast to display msg only, not for taking any input, for taking input from user use another modal, look at the doc on multiple modal and how to switch back and forth between modals.

@AgentSmith0
Copy link
Author

Thank you for helping, but unfortunately this isn't the solution I am looking for. I need the toast with the input as a search box. This also makes it not really possible to put it into every modal that can possibly come up.

@AgentSmith0
Copy link
Author

Also, it doesn't seem that the modal has an overlay layer, since clicking buttons in the toast works fine.

@Firas-HadjKacem
Copy link

Hey there, one possible workaround could be manually giving the input field focus when the Toast is shown, like so:
var toastEl = document.querySelector('.toast');
var toast = new bootstrap.Toast(toastEl, { autohide: false });
toast.show();

toastEl.addEventListener('shown.bs.toast', function () {
var inputEl = toastEl.querySelector('input');
inputEl.focus();
});
This will ensure that the input field receives focus as soon as the Toast is displayed, even when a modal is active. However, once the input field loses focus (for example, if a user clicks elsewhere), it might not be possible to focus it again until the modal is closed. Any thoughts on this ?

@AgentSmith0
Copy link
Author

However, this won't work when the toast is shown before the modal is opened. Also, focusing the input inside the toast via JS doesn't even work when the modal is opened.

@AgentSmith0
Copy link
Author

Any thoughts on this issue from the maintainers? I think this is definitely a bug in bootstrap that could be fixed.

@julien-deramond
Copy link
Member

I'm trying a first quick analysis here.

IMO this behavior is linked to the focus trap system in place for modals. When a modal is displayed, when navigating with your keyboard, you can't focus on anything else on the page until you close this modal. We could argue that the focus trap could allow exceptions, but I'd say that if you would use a <dialog> instead of a modal from Bootstrap, you would have the same behavior here, and you probably wouldn't be able to change it because it's handled by browsers directly.

Based on the CodePen, I'd also say that we shouldn't even be able to click on the toast buttons while the modal is displayed.

Thoughts @twbs/js-review?
@patrickhlauke Do you have specific elements on the a11y side to add or precise here?

@AgentSmith0
Copy link
Author

How is it possible to make an exception to all modals for the toast? I think both cases should be possible. The default could be that the focus trap is enabled.

@ruiarii
Copy link

ruiarii commented May 13, 2023

Also, it doesn't seem that the modal has an overlay layer, since clicking buttons in the toast works fine.

Hi there !

Regarding this point, I think buttons on toasts should not be working when the modal is shown, IMO the current behaviour between toast input not working when the modal is shown and buttons that work, is somehow inconsistent.

@sanjogs
Copy link

sanjogs commented Mar 13, 2024

I think there should be a way to prevent modal's focus trap in certain scenarios. I am building a typeahead that goes full screen when in small devices (For better user experience). For small devices, i move the typeahead inside the body element and control it's positioning so as to make it full screen, i then programmatically put focus on the input element. This works in all cases except for the case when it's inside a modal. How can we possibly turn off focus trapping in a modal?

@AgentSmith0
Copy link
Author

As a workaround I call this before the modals are shown:
bootstrap.Modal.getInstance(event.target)._focustrap.deactivate();
Would be nice if there is some kind of global configuration for this...

@EstebanPina
Copy link

I work in this issue the last 3 weeks.
I found a solution by changing the style of modal dialog from position relative to position fixed

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