From 418accfbd0c0bd43e7b9743d21be533c749f5155 Mon Sep 17 00:00:00 2001 From: Nicola Heald Date: Mon, 11 Feb 2019 12:01:39 +0000 Subject: [PATCH] Require an initial click on embed previews for interactivity (#12981) This fixes the block selection UX for video embeds. Previously the click on the video to select the block would start the video playing. Now that previous require a click to become interactive, the initial click selects the block without side effects. --- packages/block-library/src/embed/editor.scss | 9 ++ .../block-library/src/embed/embed-preview.js | 127 ++++++++++++------ packages/components/src/sandbox/index.js | 3 +- 3 files changed, 96 insertions(+), 43 deletions(-) diff --git a/packages/block-library/src/embed/editor.scss b/packages/block-library/src/embed/editor.scss index 29cbd9fb932cf..af0b050764ce7 100644 --- a/packages/block-library/src/embed/editor.scss +++ b/packages/block-library/src/embed/editor.scss @@ -31,3 +31,12 @@ word-break: break-word; } } + +.block-library-embed__interactive-overlay { + position: absolute; + top: 0; + left: 0; + right: 0; + bottom: 0; + opacity: 0; +} diff --git a/packages/block-library/src/embed/embed-preview.js b/packages/block-library/src/embed/embed-preview.js index efc11b7a5809b..cf8d35460ae3d 100644 --- a/packages/block-library/src/embed/embed-preview.js +++ b/packages/block-library/src/embed/embed-preview.js @@ -17,58 +17,101 @@ import classnames from 'classnames/dedupe'; import { __, sprintf } from '@wordpress/i18n'; import { Placeholder, SandBox } from '@wordpress/components'; import { RichText, BlockIcon } from '@wordpress/editor'; +import { Component } from '@wordpress/element'; /** * Internal dependencies */ import WpEmbedPreview from './wp-embed-preview'; -const EmbedPreview = ( props ) => { - const { preview, url, type, caption, onCaptionChange, isSelected, className, icon, label } = props; - const { scripts } = preview; +class EmbedPreview extends Component { + constructor() { + super( ...arguments ); + this.hideOverlay = this.hideOverlay.bind( this ); + this.state = { + interactive: false, + }; + } - const html = 'photo' === type ? getPhotoHtml( preview ) : preview.html; - const parsedHost = parse( url ).host.split( '.' ); - const parsedHostBaseUrl = parsedHost.splice( parsedHost.length - 2, parsedHost.length - 1 ).join( '.' ); - const cannotPreview = includes( HOSTS_NO_PREVIEWS, parsedHostBaseUrl ); - // translators: %s: host providing embed content e.g: www.youtube.com - const iframeTitle = sprintf( __( 'Embedded content from %s' ), parsedHostBaseUrl ); - const sandboxClassnames = classnames( type, className, 'wp-block-embed__wrapper' ); + static getDerivedStateFromProps( nextProps, state ) { + if ( ! nextProps.isSelected && state.interactive ) { + // We only want to change this when the block is not selected, because changing it when + // the block becomes selected makes the overlap disappear too early. Hiding the overlay + // happens on mouseup when the overlay is clicked. + return { interactive: false }; + } - const embedWrapper = 'wp-embed' === type ? ( - - ) : ( -
- -
- ); - - return ( -
- { ( cannotPreview ) ? ( - } label={ label }> -

{ url }

-

{ __( 'Sorry, this embedded content cannot be previewed in the editor.' ) }

-
- ) : embedWrapper } - { ( ! RichText.isEmpty( caption ) || isSelected ) && ( - + - ) } -
- ); -}; + { ! interactive &&
} +
+ ); + /* eslint-enable jsx-a11y/no-static-element-interactions */ + /* eslint-enable jsx-a11y/no-noninteractive-element-interactions */ + + return ( +
+ { ( cannotPreview ) ? ( + } label={ label }> +

{ url }

+

{ __( 'Sorry, this embedded content cannot be previewed in the editor.' ) }

+
+ ) : embedWrapper } + { ( ! RichText.isEmpty( caption ) || isSelected ) && ( + + ) } +
+ ); + } +} export default EmbedPreview; diff --git a/packages/components/src/sandbox/index.js b/packages/components/src/sandbox/index.js index 1862c00db9754..01f2e8f8e3b7f 100644 --- a/packages/components/src/sandbox/index.js +++ b/packages/components/src/sandbox/index.js @@ -198,7 +198,7 @@ class Sandbox extends Component { } render() { - const { title } = this.props; + const { title, onFocus } = this.props; return ( );