Skip to content

Commit

Permalink
Fix Global Styles on wpcom (#40690)
Browse files Browse the repository at this point in the history
* Fix global styles

* Add comment linking to GH
  • Loading branch information
noahtallen authored Apr 2, 2020
1 parent a93a25e commit 86269c5
Showing 1 changed file with 22 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { subscribe } from '@wordpress/data';
import { subscribe, select } from '@wordpress/data';
import domReady from '@wordpress/dom-ready';
import { isEmpty, isEqual } from 'lodash';

Expand All @@ -13,10 +13,6 @@ import { isEmpty, isEqual } from 'lodash';
*/
export default ( options, getOptionValue ) => {
domReady( () => {
// Create style node.
const styleElement = document.createElement( 'style' );
document.body.appendChild( styleElement );

// Book-keeping.
const currentOptions = {};
let previousOptions = {};
Expand All @@ -25,7 +21,28 @@ export default ( options, getOptionValue ) => {
cssVariables[ option ] = `--${ option.replace( '_', '-' ) }`;
} );

let styleElement = null;
subscribe( () => {
/**
* Do nothing until the editor is ready. This is required when
* working in wpcom iframe environment to avoid running code before
* everything has loaded, which can cause bugs like the following.
*
* @see https://github.com/Automattic/wp-calypso/pull/40690
*/
const isEditorReady = select( 'core/editor' ).__unstableIsEditorReady;
if ( isEditorReady && isEditorReady() === false ) {
return;
}

// Create style element if it has not been created yet. Must happen
// after the editor is ready or the style element will be appended
// before the styles it needs to affect.
if ( ! styleElement ) {
styleElement = document.createElement( 'style' );
document.body.appendChild( styleElement );
}

// Maybe bail-out early.
options.forEach( option => {
currentOptions[ option ] = getOptionValue( option );
Expand Down

0 comments on commit 86269c5

Please sign in to comment.