Skip to content

Commit 63c103d

Browse files
committed
Draggable: Improve detection for when to blur the active element
Fixes #12472 Fixes #14905 Closes gh-1548
1 parent 8fc3ba2 commit 63c103d

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

tests/unit/draggable/core.js

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ test( "#8399: A draggable should become the active element after you are finishe
291291
strictEqual( document.activeElement, element.get( 0 ), "finishing moving a draggable anchor made it the active element" );
292292
} );
293293

294-
asyncTest( "blur behavior", function() {
294+
asyncTest( "blur behavior - handle is main element", function() {
295295
expect( 3 );
296296

297297
var element = $( "#draggable1" ).draggable(),
@@ -315,6 +315,26 @@ asyncTest( "blur behavior", function() {
315315
} );
316316
} );
317317

318+
asyncTest( "blur behavior - descendant of handle", function() {
319+
expect( 2 );
320+
321+
var element = $( "#draggable2" ).draggable( { handle: "span" } ),
322+
323+
// The handle is a descendant, but we also want to grab a descendant of the handle
324+
handle = element.find( "span em" ),
325+
focusElement = $( "<div tabindex='1'></div>" ).appendTo( element );
326+
327+
testHelper.onFocus( focusElement, function() {
328+
strictEqual( document.activeElement, focusElement.get( 0 ), "test element is focused before mousing down on a draggable" );
329+
330+
testHelper.move( handle, 50, 50 );
331+
332+
// Elements outside of the handle should blur (#12472, #14905)
333+
notStrictEqual( document.activeElement, focusElement.get( 0 ), "test element is no longer focused after mousing down on a draggable" );
334+
start();
335+
} );
336+
} );
337+
318338
test( "ui-draggable-handle assigned to appropriate element", function( assert ) {
319339
expect( 5 );
320340

ui/widgets/draggable.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,14 +143,19 @@ $.widget( "ui.draggable", $.ui.mouse, {
143143
},
144144

145145
_blurActiveElement: function( event ) {
146-
147-
// Only need to blur if the event occurred on the draggable itself, see #10527
148-
if ( !this.handleElement.is( event.target ) ) {
146+
var activeElement = $.ui.safeActiveElement( this.document[ 0 ] ),
147+
target = $( event.target );
148+
149+
// Only blur if the event occurred on an element that is:
150+
// 1) within the draggable handle
151+
// 2) but not within the currently focused element
152+
// See #10527, #12472
153+
if ( this._getHandle( event ) && target.closest( activeElement ).length ) {
149154
return;
150155
}
151156

152157
// Blur any element that currently has focus, see #4261
153-
$.ui.safeBlur( $.ui.safeActiveElement( this.document[ 0 ] ) );
158+
$.ui.safeBlur( activeElement );
154159
},
155160

156161
_mouseStart: function( event ) {

0 commit comments

Comments
 (0)