Skip to content

Commit 7ccb0e5

Browse files
committed
Use of _bind, maintain original tabindex value for popup
1 parent 6d01873 commit 7ccb0e5

File tree

1 file changed

+21
-18
lines changed

1 file changed

+21
-18
lines changed

ui/jquery.ui.popup.js

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -81,21 +81,20 @@ $.widget( "ui.popup", {
8181

8282
if ( !this.element.is( ":ui-menu" ) ) {
8383
//default use case, wrap tab order in popup
84-
this.element.bind( "keydown.ui-popup", function( event ) {
85-
if ( event.keyCode !== $.ui.keyCode.TAB ) {
86-
return;
87-
}
88-
89-
var tabbables = $( ":tabbable", this ),
90-
first = tabbables.first(),
91-
last = tabbables.last();
92-
93-
if ( event.target === last[ 0 ] && !event.shiftKey ) {
94-
first.focus( 1 );
95-
return false;
96-
} else if ( event.target === first[ 0 ] && event.shiftKey ) {
97-
last.focus( 1 );
98-
return false;
84+
this._bind({ keydown : function( event ) {
85+
if ( event.keyCode !== $.ui.keyCode.TAB ) {
86+
return;
87+
}
88+
var tabbables = $( ":tabbable", this.element ),
89+
first = tabbables.first(),
90+
last = tabbables.last();
91+
if ( event.target === last[ 0 ] && !event.shiftKey ) {
92+
first.focus( 1 );
93+
event.preventDefault();
94+
} else if ( event.target === first[ 0 ] && event.shiftKey ) {
95+
last.focus( 1 );
96+
event.preventDefault();
97+
}
9998
}
10099
});
101100
}
@@ -174,11 +173,13 @@ $.widget( "ui.popup", {
174173
// set focus to the first tabbable element in the popup container
175174
// if there are no tabbable elements, set focus on the popup itself
176175
var tabbables = this.element.find( ":tabbable" );
176+
this.removeTabIndex = false;
177177
if ( !tabbables.length ) {
178178
if ( !this.element.is(":tabbable") ) {
179179
this.element.attr("tabindex", "0");
180+
this.removeTabIndex = true;
180181
}
181-
tabbables.add(this.element);
182+
tabbables = tabbables.add( this.element[ 0 ] );
182183
}
183184
tabbables.first().focus( 1 );
184185
}
@@ -195,8 +196,10 @@ $.widget( "ui.popup", {
195196
.attr( "aria-hidden", true )
196197
.attr( "aria-expanded", false );
197198

198-
this.options.trigger.attr("tabindex", 0);
199-
199+
this.options.trigger.attr( "tabindex" , 0 );
200+
if ( this.removeTabIndex ) {
201+
this.element.removeAttr( "tabindex" );
202+
}
200203
this.isOpen = false;
201204
this._trigger( "close", event );
202205
}

0 commit comments

Comments
 (0)