Media: avoid page scrolling when opening the media modal.

Moves focus to a proper element within the media modal to avoid the underlying
page to scroll to the bottom. Removes the media modal `freeze` option.

Props andreiglingeanu, adamsilverstein.
Fixes 39859.

Built from https://develop.svn.wordpress.org/trunk@42624


git-svn-id: http://core.svn.wordpress.org/trunk@42453 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Andrea Fercia 2018-01-31 17:03:33 +00:00
parent 587da39a9e
commit d6a73ef110
4 changed files with 9 additions and 28 deletions

View File

@ -4248,10 +4248,6 @@ Modal = wp.media.View.extend(/** @lends wp.media.view.Modal.prototype */{
tagName: 'div', tagName: 'div',
template: wp.template('media-modal'), template: wp.template('media-modal'),
attributes: {
tabindex: 0
},
events: { events: {
'click .media-modal-backdrop, .media-modal-close': 'escapeHandler', 'click .media-modal-backdrop, .media-modal-close': 'escapeHandler',
'keydown': 'keydown' 'keydown': 'keydown'
@ -4263,8 +4259,7 @@ Modal = wp.media.View.extend(/** @lends wp.media.view.Modal.prototype */{
_.defaults( this.options, { _.defaults( this.options, {
container: document.body, container: document.body,
title: '', title: '',
propagate: true, propagate: true
freeze: true
}); });
this.focusManager = new wp.media.view.FocusManager({ this.focusManager = new wp.media.view.FocusManager({
@ -4319,7 +4314,6 @@ Modal = wp.media.View.extend(/** @lends wp.media.view.Modal.prototype */{
*/ */
open: function() { open: function() {
var $el = this.$el, var $el = this.$el,
options = this.options,
mceEditor; mceEditor;
if ( $el.is(':visible') ) { if ( $el.is(':visible') ) {
@ -4332,13 +4326,6 @@ Modal = wp.media.View.extend(/** @lends wp.media.view.Modal.prototype */{
this.attach(); this.attach();
} }
// If the `freeze` option is set, record the window's scroll position.
if ( options.freeze ) {
this._freeze = {
scrollTop: $( window ).scrollTop()
};
}
// Disable page scrolling. // Disable page scrolling.
$( 'body' ).addClass( 'modal-open' ); $( 'body' ).addClass( 'modal-open' );
@ -4346,7 +4333,7 @@ Modal = wp.media.View.extend(/** @lends wp.media.view.Modal.prototype */{
// Try to close the onscreen keyboard // Try to close the onscreen keyboard
if ( 'ontouchend' in document ) { if ( 'ontouchend' in document ) {
if ( ( mceEditor = window.tinymce && window.tinymce.activeEditor ) && ! mceEditor.isHidden() && mceEditor.iframeElement ) { if ( ( mceEditor = window.tinymce && window.tinymce.activeEditor ) && ! mceEditor.isHidden() && mceEditor.iframeElement ) {
mceEditor.iframeElement.focus(); mceEditor.iframeElement.focus();
mceEditor.iframeElement.blur(); mceEditor.iframeElement.blur();
@ -4356,7 +4343,8 @@ Modal = wp.media.View.extend(/** @lends wp.media.view.Modal.prototype */{
} }
} }
this.$el.focus(); // Set initial focus on the content instead of this view element, to avoid page scrolling.
this.$( '.media-modal' ).focus();
return this.propagate('open'); return this.propagate('open');
}, },
@ -4366,8 +4354,6 @@ Modal = wp.media.View.extend(/** @lends wp.media.view.Modal.prototype */{
* @returns {wp.media.view.Modal} Returns itself to allow chaining * @returns {wp.media.view.Modal} Returns itself to allow chaining
*/ */
close: function( options ) { close: function( options ) {
var freeze = this._freeze;
if ( ! this.views.attached || ! this.$el.is(':visible') ) { if ( ! this.views.attached || ! this.$el.is(':visible') ) {
return this; return this;
} }
@ -4387,11 +4373,6 @@ Modal = wp.media.View.extend(/** @lends wp.media.view.Modal.prototype */{
this.propagate('close'); this.propagate('close');
// If the `freeze` option is set, restore the container's scroll position.
if ( freeze ) {
$( window ).scrollTop( freeze.scrollTop );
}
if ( options && options.escape ) { if ( options && options.escape ) {
this.propagate('escape'); this.propagate('escape');
} }

File diff suppressed because one or more lines are too long

View File

@ -182,7 +182,7 @@ function wp_print_media_templates() {
</script> </script>
<script type="text/html" id="tmpl-media-modal"> <script type="text/html" id="tmpl-media-modal">
<div class="<?php echo $class; ?>"> <div tabindex="0" class="<?php echo $class; ?>">
<button type="button" class="media-modal-close"><span class="media-modal-icon"><span class="screen-reader-text"><?php _e( 'Close media panel' ); ?></span></span></button> <button type="button" class="media-modal-close"><span class="media-modal-icon"><span class="screen-reader-text"><?php _e( 'Close media panel' ); ?></span></span></button>
<div class="media-modal-content"></div> <div class="media-modal-content"></div>
</div> </div>

View File

@ -4,7 +4,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '5.0-alpha-42619'; $wp_version = '5.0-alpha-42624';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.