Skip to content

Commit 834ee5f

Browse files
authored
Dialog: Workaround broken focus re-triggering in jQuery 3.4/3.5
Focus re-triggering in jQuery 3.4/3.5 makes the original element have its focus event propagated last, breaking the re-targeting. Trigger focus in a delay in addition if needed to avoid the issue. This fixes the "interaction between overlay and other dialogs" core dialog test when tested against jQuery 3.4/3.5. Closes gh-1946 Ref jquery/jquery#4382
1 parent 5b5fda7 commit 834ee5f

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

ui/widgets/dialog.js

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -325,22 +325,23 @@ $.widget( "ui.dialog", {
325325
hasFocus.eq( 0 ).trigger( "focus" );
326326
},
327327

328-
_keepFocus: function( event ) {
329-
function checkFocus() {
330-
var activeElement = $.ui.safeActiveElement( this.document[ 0 ] ),
331-
isActive = this.uiDialog[ 0 ] === activeElement ||
332-
$.contains( this.uiDialog[ 0 ], activeElement );
333-
if ( !isActive ) {
334-
this._focusTabbable();
335-
}
328+
_restoreTabbableFocus: function() {
329+
var activeElement = $.ui.safeActiveElement( this.document[ 0 ] ),
330+
isActive = this.uiDialog[ 0 ] === activeElement ||
331+
$.contains( this.uiDialog[ 0 ], activeElement );
332+
if ( !isActive ) {
333+
this._focusTabbable();
336334
}
335+
},
336+
337+
_keepFocus: function( event ) {
337338
event.preventDefault();
338-
checkFocus.call( this );
339+
this._restoreTabbableFocus();
339340

340341
// support: IE
341342
// IE <= 8 doesn't prevent moving focus even with event.preventDefault()
342343
// so we check again later
343-
this._delay( checkFocus );
344+
this._delay( this._restoreTabbableFocus );
344345
},
345346

346347
_createWrapper: function() {
@@ -853,6 +854,8 @@ $.widget( "ui.dialog", {
853854
return;
854855
}
855856

857+
var jqMinor = $.fn.jquery.substring( 0, 4 );
858+
856859
// We use a delay in case the overlay is created from an
857860
// event that we're going to be cancelling (#2804)
858861
var isOpening = true;
@@ -874,6 +877,15 @@ $.widget( "ui.dialog", {
874877
if ( !instance._allowInteraction( event ) ) {
875878
event.preventDefault();
876879
instance._focusTabbable();
880+
881+
// Support: jQuery >=3.4 <3.6 only
882+
// Focus re-triggering in jQuery 3.4/3.5 makes the original element
883+
// have its focus event propagated last, breaking the re-targeting.
884+
// Trigger focus in a delay in addition if needed to avoid the issue
885+
// See https://github.com/jquery/jquery/issues/4382
886+
if ( jqMinor === "3.4." || jqMinor === "3.5." ) {
887+
instance._delay( instance._restoreTabbableFocus );
888+
}
877889
}
878890
}.bind( this ) );
879891
}

0 commit comments

Comments
 (0)