Skip to content

Commit 4866e14

Browse files
committed
Menu: Remove active class from top-level item when menu is blurred
This issue was introduced by 0bbd156, which reduced the use of ui-state-focus and ui-state-active to using only ui-state-focus. This introduced the issue addressed here. The fix is more of a workaround. With test test in place, we can investigate a better solution in the future. Fixes #14919
1 parent 54cd451 commit 4866e14

File tree

2 files changed

+38
-8
lines changed

2 files changed

+38
-8
lines changed

tests/unit/menu/core.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,4 +73,34 @@ asyncTest( "#9532: Need a way in Menu to keep ui-state-active class on selected
7373
} );
7474
} );
7575

76+
asyncTest( "active menu item styling", function( assert ) {
77+
expect( 5 );
78+
function isActive( item ) {
79+
assert.hasClasses( item.children( ".ui-menu-item-wrapper" ), "ui-state-active" );
80+
}
81+
function isInactive( item ) {
82+
assert.lacksClasses( item.children( ".ui-menu-item-wrapper" ), "ui-state-active" );
83+
}
84+
$.ui.menu.prototype.delay = 0;
85+
var element = $( "#menu4" ).menu();
86+
var parentItem = element.children( "li:eq(1)" );
87+
var childItem = parentItem.find( "li:eq(0)" );
88+
element.menu( "focus", null, parentItem );
89+
setTimeout( function() {
90+
isActive( parentItem );
91+
element.menu( "focus", null, childItem );
92+
setTimeout( function() {
93+
isActive( parentItem );
94+
isActive( childItem );
95+
element.blur();
96+
setTimeout( function() {
97+
isInactive( parentItem );
98+
isInactive( childItem );
99+
$.ui.menu.prototype.delay = 300;
100+
start();
101+
}, 50 );
102+
} );
103+
} );
104+
} );
105+
76106
} );

ui/widgets/menu.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,10 @@ return $.widget( "ui.menu", {
487487
this._close( currentMenu );
488488

489489
this.blur( event );
490+
491+
// Work around active item staying active after menu is blurred
492+
this._removeClass( currentMenu.find( ".ui-state-active" ), null, "ui-state-active" );
493+
490494
this.activeMenu = currentMenu;
491495
}, this.delay );
492496
},
@@ -498,14 +502,10 @@ return $.widget( "ui.menu", {
498502
startMenu = this.active ? this.active.parent() : this.element;
499503
}
500504

501-
var active = startMenu
502-
.find( ".ui-menu" )
503-
.hide()
504-
.attr( "aria-hidden", "true" )
505-
.attr( "aria-expanded", "false" )
506-
.end()
507-
.find( ".ui-state-active" ).not( ".ui-menu-item-wrapper" );
508-
this._removeClass( active, null, "ui-state-active" );
505+
startMenu.find( ".ui-menu" )
506+
.hide()
507+
.attr( "aria-hidden", "true" )
508+
.attr( "aria-expanded", "false" );
509509
},
510510

511511
_closeOnDocumentClick: function( event ) {

0 commit comments

Comments
 (0)