Skip to content

Commit

Permalink
Merge pull request #4011 from WordPress/update/sandbox-focus
Browse files Browse the repository at this point in the history
Embeds: Detect focus on embed sandbox
  • Loading branch information
aduth authored Jan 4, 2018
2 parents 91f71ef + 2c0c13e commit ba4e71e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
7 changes: 6 additions & 1 deletion blocks/library/embed/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,12 @@ function getEmbedBlockSettings( { title, icon, category = 'embed', transforms, k
</Placeholder>
) : (
<div className="wp-block-embed__wrapper">
<SandBox html={ html } title={ iframeTitle } type={ type } />
<SandBox
html={ html }
title={ iframeTitle }
type={ type }
onFocus={ () => setFocus() }
/>
</div>
) }
{ ( caption && caption.length > 0 ) || !! focus ? (
Expand Down
15 changes: 13 additions & 2 deletions components/sandbox/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@ import { Component, renderToString } from '@wordpress/element';
export default class Sandbox extends Component {
constructor() {
super( ...arguments );

this.trySandbox = this.trySandbox.bind( this );
this.checkMessageForResize = this.checkMessageForResize.bind( this );
this.checkFocus = this.checkFocus.bind( this );

this.state = {
width: 0,
height: 0,
};
this.trySandbox = this.trySandbox.bind( this );
this.checkMessageForResize = this.checkMessageForResize.bind( this );
}

isFrameAccessible() {
Expand Down Expand Up @@ -50,6 +53,7 @@ export default class Sandbox extends Component {

componentDidMount() {
window.addEventListener( 'message', this.checkMessageForResize, false );
window.addEventListener( 'blur', this.checkFocus );
this.trySandbox();
}

Expand All @@ -59,6 +63,13 @@ export default class Sandbox extends Component {

componentWillUnmount() {
window.removeEventListener( 'message', this.checkMessageForResize );
window.removeEventListener( 'blur', this.checkFocus );
}

checkFocus() {
if ( this.props.onFocus && document.activeElement === this.iframe ) {
this.props.onFocus();
}
}

trySandbox() {
Expand Down

0 comments on commit ba4e71e

Please sign in to comment.