Skip to content

Commit 6abb107

Browse files
committed
Dialog: Only bind focus-trapping event once. Fixes #8551 - After repeated opening and closing of a modal dialog, focus navigation using tab becomes slow.
1 parent e242868 commit 6abb107

File tree

1 file changed

+19
-22
lines changed

1 file changed

+19
-22
lines changed

ui/jquery.ui.dialog.js

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,25 @@ $.widget("ui.dialog", {
170170
if ( $.fn.bgiframe ) {
171171
uiDialog.bgiframe();
172172
}
173+
174+
// prevent tabbing out of modal dialogs
175+
this._on( uiDialog, { keydown: function( event ) {
176+
if ( !options.modal || event.keyCode !== $.ui.keyCode.TAB ) {
177+
return;
178+
}
179+
180+
var tabbables = $( ":tabbable", uiDialog ),
181+
first = tabbables.filter( ":first" ),
182+
last = tabbables.filter( ":last" );
183+
184+
if ( event.target === last[0] && !event.shiftKey ) {
185+
first.focus( 1 );
186+
return false;
187+
} else if ( event.target === first[0] && event.shiftKey ) {
188+
last.focus( 1 );
189+
return false;
190+
}
191+
}});
173192
},
174193

175194
_init: function() {
@@ -225,7 +244,6 @@ $.widget("ui.dialog", {
225244
if ( this.overlay ) {
226245
this.overlay.destroy();
227246
}
228-
this._off( this.uiDialog, "keypress" );
229247

230248
if ( this.options.hide ) {
231249
this.uiDialog.hide( this.options.hide, function() {
@@ -309,27 +327,6 @@ $.widget("ui.dialog", {
309327
this.overlay = options.modal ? new $.ui.dialog.overlay( this ) : null;
310328
this.moveToTop( true );
311329

312-
// prevent tabbing out of modal dialogs
313-
if ( options.modal ) {
314-
this._on( uiDialog, { keydown: function( event ) {
315-
if ( event.keyCode !== $.ui.keyCode.TAB ) {
316-
return;
317-
}
318-
319-
var tabbables = $( ":tabbable", uiDialog ),
320-
first = tabbables.filter( ":first" ),
321-
last = tabbables.filter( ":last" );
322-
323-
if ( event.target === last[0] && !event.shiftKey ) {
324-
first.focus( 1 );
325-
return false;
326-
} else if ( event.target === first[0] && event.shiftKey ) {
327-
last.focus( 1 );
328-
return false;
329-
}
330-
}});
331-
}
332-
333330
// set focus to the first tabbable element in the content area or the first button
334331
// if there are no tabbable elements, set focus on the dialog itself
335332
hasFocus = this.element.find( ":tabbable" );

0 commit comments

Comments
 (0)