diff --git a/src/wp-includes/js/media-editor.js b/src/wp-includes/js/media-editor.js index aa407466d0..e972f6b39c 100644 --- a/src/wp-includes/js/media-editor.js +++ b/src/wp-includes/js/media-editor.js @@ -1111,13 +1111,6 @@ event.preventDefault(); - // Remove focus from the `.insert-media` button. - // Prevents Opera from showing the outline of the button - // above the modal. - // - // See: https://core.trac.wordpress.org/ticket/22445 - elem.blur(); - if ( elem.hasClass( 'gallery' ) ) { options.state = 'gallery'; options.title = wp.media.view.l10n.createGalleryTitle; diff --git a/src/wp-includes/js/media-views.js b/src/wp-includes/js/media-views.js index e8a8202d59..91a8e4872c 100644 --- a/src/wp-includes/js/media-views.js +++ b/src/wp-includes/js/media-views.js @@ -6698,6 +6698,8 @@ Modal = wp.media.View.extend({ 'keydown': 'keydown' }, + clickedOpenerEl: null, + initialize: function() { _.defaults( this.options, { container: document.body, @@ -6765,6 +6767,8 @@ Modal = wp.media.View.extend({ return this; } + this.clickedOpenerEl = document.activeElement; + if ( ! this.views.attached ) { this.attach(); } @@ -6815,8 +6819,12 @@ Modal = wp.media.View.extend({ // Hide modal and remove restricted media modal tab focus once it's closed this.$el.hide().undelegate( 'keydown' ); - // Put focus back in useful location once modal is closed - $('#wpbody-content').focus(); + // Put focus back in useful location once modal is closed. + if ( null !== this.clickedOpenerEl ) { + this.clickedOpenerEl.focus(); + } else { + $( '#wpbody-content' ).focus(); + } this.propagate('close'); diff --git a/src/wp-includes/js/media/views/modal.js b/src/wp-includes/js/media/views/modal.js index 2efb5dad0f..42e0b241b2 100644 --- a/src/wp-includes/js/media/views/modal.js +++ b/src/wp-includes/js/media/views/modal.js @@ -24,6 +24,8 @@ Modal = wp.media.View.extend({ 'keydown': 'keydown' }, + clickedOpenerEl: null, + initialize: function() { _.defaults( this.options, { container: document.body, @@ -91,6 +93,8 @@ Modal = wp.media.View.extend({ return this; } + this.clickedOpenerEl = document.activeElement; + if ( ! this.views.attached ) { this.attach(); } @@ -141,8 +145,12 @@ Modal = wp.media.View.extend({ // Hide modal and remove restricted media modal tab focus once it's closed this.$el.hide().undelegate( 'keydown' ); - // Put focus back in useful location once modal is closed - $('#wpbody-content').focus(); + // Put focus back in useful location once modal is closed. + if ( null !== this.clickedOpenerEl ) { + this.clickedOpenerEl.focus(); + } else { + $( '#wpbody-content' ).focus(); + } this.propagate('close');