Fix: prevent multiple submenus from staying open during keyboard navigation in dropdown #41889
+250
−26
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This PR fixes an issue where nested dropdown submenus using
data-bs-auto-close="outside"
can remain open simultaneously when navigating with keyboard arrow keys.
When moving focus upward (ArrowUp), Bootstrap’s dataApiKeydownHandler triggers a call to show(), which reopens the submenu the user just left. This results in multiple overlapping open submenus.
This fix ensures that only the submenu associated with the currently focused item remains open.
Root Cause
In dropdown.js, the following block always calls instance.show() when navigating with ArrowUp/ArrowDown:
if (isUpOrDownEvent) {
event.stopPropagation();
instance.show();
instance._selectMenuItem(event);
return;
}
This causes previously opened submenus to be reopened during keyboard navigation.
Fix Implemented
Only call show() when navigating downward into a submenu.
Prevent calling show() when navigating upward, allowing Bootstrap’s normal auto-close behavior to work.
// FIX: Prevent reopening submenus while navigating with arrow keys
if (isUpOrDownEvent) {
event.stopPropagation();
// Only open submenu when moving DOWN into it
if (event.key === 'ArrowDown' && !instance._menu.classList.contains('show')) {
instance.show();
}
instance._selectMenuItem(event);
return;
}
This ensures that only one submenu stays open at a time and fixes the overlapping submenu issue.
Steps to Reproduce (Before Fix)
Open a dropdown with nested submenus (dropend items).
Click to open Submenu 3.
Press ArrowUp multiple times.
Submenu 3, 2, and 1 all remain open simultaneously.
Expected Behavior (After Fix)
✔ Only one submenu remains open at a time
✔ Upward navigation no longer reopens previous submenus
✔ Normal mouse behavior is unchanged
✔ Keyboard accessibility is improved and consistent
Tested On
Windows
Chrome
Microsoft Edge
Works consistently across browsers.
Additional Notes
This change preserves Bootstrap’s existing navigation patterns while fixing a regression specific to nested dropdown keyboard navigation.
Type of changes
Checklist
npm run lint)