Skip to content

Commit

Permalink
Fix clicking Gallery items buttons on Safari (#23586)
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad authored Jul 2, 2020
1 parent 4fe793a commit 2e80a87
Showing 1 changed file with 3 additions and 40 deletions.
43 changes: 3 additions & 40 deletions packages/block-library/src/gallery/gallery-image.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* External dependencies
*/
import classnames from 'classnames';
import { debounce, get, omit } from 'lodash';
import { get, omit } from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -34,8 +34,6 @@ class GalleryImage extends Component {
constructor() {
super( ...arguments );

this.onBlur = this.onBlur.bind( this );
this.onFocus = this.onFocus.bind( this );
this.onSelectImage = this.onSelectImage.bind( this );
this.onSelectCaption = this.onSelectCaption.bind( this );
this.onRemoveImage = this.onRemoveImage.bind( this );
Expand All @@ -45,20 +43,6 @@ class GalleryImage extends Component {
this
);
this.onSelectCustomURL = this.onSelectCustomURL.bind( this );

// The onDeselect prop is used to signal that the GalleryImage component
// has lost focus. We want to call it when focus has been lost
// by the figure element or any of its children but only if
// the element that gained focus isn't any of them.
//
// debouncedOnSelect is scheduled every time a figure's children
// is blurred and cancelled when any is focused. If none gain focus,
// the call to onDeselect will be executed.
//
// onBlur / onFocus events are quick operations (<5ms apart in my testing),
// so 50ms accounts for 10x lagging while feels responsive to the user.
this.debouncedOnDeselect = debounce( this.onDeselect.bind( this ), 50 );

this.state = {
captionSelected: false,
isEditing: false,
Expand Down Expand Up @@ -139,27 +123,10 @@ class GalleryImage extends Component {
}
}

onDeselect() {
this.setState( { isEditing: false } );
deselectOnBlur() {
this.props.onDeselect();
}

/**
* Note that, unlike the DOM, all React events bubble,
* so this will be called after the onBlur event of any figure's children.
*/
onBlur() {
this.debouncedOnDeselect();
}

/**
* Note that, unlike the DOM, all React events bubble,
* so this will be called after the onBlur event of any figure's children.
*/
onFocus() {
this.debouncedOnDeselect.cancel();
}

onSelectImageFromLibrary( media ) {
const { setAttributes, id, url, alt, caption, sizeSlug } = this.props;
if ( ! media || ! media.url ) {
Expand Down Expand Up @@ -258,11 +225,7 @@ class GalleryImage extends Component {
} );

return (
<figure
className={ className }
onBlur={ this.onBlur }
onFocus={ this.onFocus }
>
<figure className={ className }>
{ ! isEditing && ( href ? <a href={ href }>{ img }</a> : img ) }
{ isEditing && (
<MediaPlaceholder
Expand Down

0 comments on commit 2e80a87

Please sign in to comment.