Fix: prevent multiple submenus from staying open during keyboard navigation in dropdown#41889
Fix: prevent multiple submenus from staying open during keyboard navigation in dropdown#41889VishalManole7 wants to merge 3 commits intotwbs:mainfrom
Conversation
|
Thanks for creating a PR, @VishalManole7. |
|
Hi, I’ve added the requested unit test for this fix. Updates included: The test simulates: Let me know if any adjustments are needed. |
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)