Skip to content

Commit

Permalink
Reinitialize the iframe after the parent block is moved around (#21916)
Browse files Browse the repository at this point in the history
* Reinitialize the iframe after the parent block is moved around

* Apply eslint
  • Loading branch information
adamziel authored Apr 30, 2020
1 parent e5dac46 commit ed06638
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion packages/components/src/sandbox/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,18 @@ class Sandbox extends Component {

componentDidMount() {
this.trySandbox();
this.trySandboxWithoutRerender = () => {
this.trySandbox( false );
};
// This used to be registered using <iframe onLoad={} />, but it made the iframe blank
// after reordering the containing block. See these two issues for more details:
// https://github.com/WordPress/gutenberg/issues/6146
// https://github.com/facebook/react/issues/18752
this.iframe.current.addEventListener(
'load',
this.trySandboxWithoutRerender,
false
);
}

componentDidUpdate( prevProps ) {
Expand All @@ -34,6 +46,13 @@ class Sandbox extends Component {
this.trySandbox( forceRerender );
}

componentWillUnmount() {
this.iframe.current.removeEventListener(
'load',
this.trySandboxWithoutRerender
);
}

isFrameAccessible() {
try {
return !! this.iframe.current.contentDocument.body;
Expand Down Expand Up @@ -233,7 +252,6 @@ class Sandbox extends Component {
title={ title }
className="components-sandbox"
sandbox="allow-scripts allow-same-origin allow-presentation"
onLoad={ () => this.trySandbox( false ) }
onFocus={ onFocus }
width={ Math.ceil( this.state.width ) }
height={ Math.ceil( this.state.height ) }
Expand Down

0 comments on commit ed06638

Please sign in to comment.