Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Global Styles on wpcom #40690

Merged
merged 2 commits into from
Apr 2, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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