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

Escape key is totally supressed in modal when data-bs-keyboard is false #40801

Open
3 tasks done
raysuelzer opened this issue Sep 9, 2024 · 2 comments
Open
3 tasks done

Comments

@raysuelzer
Copy link

Prerequisites

Describe the issue

I have a typeahead which appears within a modal. Pressing the escape key should close the typeahead results. However, it appears bootstrap is completely supressing the escape key event so any bindings to the escape key used within a modal are never triggered.

You can see in the console.log that all keys except for escape key are correctly picked up by the event listener.

Reduced test cases

Minimal reproduction: https://codepen.io/raysuelzer/pen/mdZvzKm

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

Windows, macOS, 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.3

@raysuelzer raysuelzer changed the title Escape key is totally supressed in modal when data-bs-keyboard is falose Escape key is totally supressed in modal when data-bs-keyboard is false Sep 9, 2024
@raysuelzer
Copy link
Author

raysuelzer commented Sep 9, 2024

I believe this line of code is the problem, it appears to be preventing the escape key from doing anything. I feel like a better solution would be to not prevent default escape key behavior, or at least add an option to opt out of it preventing the use of the escape key within a modal.

https://github.com/twbs/bootstrap/blob/main/js/src/modal.js#L266

Edit:

Also important to note is if you change to listen for keydown event the escape key is not supressed on elements within the modal, but keyup on escape is completely intercepted.

@julien-deramond
Copy link
Member

Thanks for reporting an issue @raysuelzer

I quickly created an example replicating your modal setup, adding a basic Bootstrap dropdown example along with a custom typeahead, which you can check out here: CodePen Example.

  • When I open the dropdown using the keyboard, navigate through the elements, and press Esc, the dropdown closes as expected, and there's no modal animation triggered. It seems the event is properly intercepted by the dropdown.
  • Similarly, when typing in the input/typeahead (e.g., typing a), a list of countries appears. When navigating through the list and pressing Esc, the list closes without any modal animation. I've used stopPropagation() here to ensure the event doesn’t reach the modal, preventing any unintended animation. Additionally, pressing Esc while the input/typeahead has no suggestions does not attempt to close the modal either, though this could be something worth enhancing.

Based on your description:

I have a typeahead which appears within a modal. Pressing the escape key should close the typeahead results. However, it appears bootstrap is completely supressing the escape key event so any bindings to the escape key used within a modal are never triggered.

I'm not entirely sure I see the difference between our setups. Could you clarify the specific scenario where the escape key isn't behaving as expected in your case?

It's possible I may have misunderstood the issue, so any additional details would help :)

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

No branches or pull requests

2 participants