Skip to content

Commit 14691ae

Browse files
committed
Dialog: Save the active element that opened the dialog and restore focus to that. Fixes #8730 - Dialog: Restore focus to opener.
1 parent 2a2a2c0 commit 14691ae

File tree

2 files changed

+21
-6
lines changed

2 files changed

+21
-6
lines changed

tests/visual/dialog/complex-dialogs.html

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,13 @@
2222

2323
<script>
2424
$(function() {
25-
$( "#dialog" ).dialog({
26-
modal: true,
27-
height: 300,
28-
width: 500
29-
});
25+
var dialog = $( "#dialog" ).dialog({
26+
modal: true,
27+
height: 300,
28+
width: 500
29+
}),
3030

31-
var datepickerDialog = $( "#dialog-datepicker" ).dialog({
31+
datepickerDialog = $( "#dialog-datepicker" ).dialog({
3232
autoOpen: false,
3333
modal: true
3434
}),
@@ -39,6 +39,10 @@
3939
width: 600
4040
});
4141

42+
$( "#open-dialog" ).click(function() {
43+
dialog.dialog( "open" );
44+
});
45+
4246
$( "#open-datepicker" ).click(function() {
4347
datepickerDialog.dialog( "open" );
4448
});
@@ -70,6 +74,8 @@
7074

7175
<a href="#">Outside link</a>
7276

77+
<button id="open-dialog">Reopen dialog</button>
78+
7379
<div id="dialog" title="Basic dialog">
7480
<p>This is the default dialog which is useful for displaying information. The dialog window can be moved, resized and closed with the 'x' icon.</p>
7581
<p><button id="open-datepicker">Open another window with a datepicker.</button></p>

ui/jquery.ui.dialog.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,13 @@ $.widget("ui.dialog", {
248248
this.overlay.destroy();
249249
}
250250

251+
if ( !this.opener.filter( ":focusable" ).focus().length ) {
252+
// Hiding a focused element doesn't trigger blur in WebKit
253+
// so in case we have nothing to focus on, explicitly blur the active element
254+
// https://bugs.webkit.org/show_bug.cgi?id=47182
255+
$( this.document[ 0 ].activeElement ).blur();
256+
}
257+
251258
if ( this.options.hide ) {
252259
this._hide( this.uiDialog, this.options.hide, function() {
253260
that._trigger( "close", event );
@@ -278,6 +285,8 @@ $.widget("ui.dialog", {
278285
options = this.options,
279286
uiDialog = this.uiDialog;
280287

288+
this.opener = $( this.document[ 0 ].activeElement );
289+
281290
this._size();
282291
this._position( options.position );
283292
uiDialog.show( options.show );

0 commit comments

Comments
 (0)