diff --git a/lib/client-assets.php b/lib/client-assets.php index 609627dc1e3ed..389a3c5a11bb4 100644 --- a/lib/client-assets.php +++ b/lib/client-assets.php @@ -695,3 +695,15 @@ function gutenberg_extend_settings_custom_units( $settings ) { register_pattern( 'core/numbered-features', gutenberg_load_block_pattern( 'numbered-features' ) ); register_pattern( 'core/its-time', gutenberg_load_block_pattern( 'its-time' ) ); } + +/** + * Adds the global styles support flag to the frontend. + */ +add_action( 'init', function() { + // Add global styles support flag + wp_add_inline_script( + 'wp-blocks', + sprintf('__unstableSupportsGlobalStyles = %s;', gutenberg_experimental_global_styles_has_theme_support() ? 'true' : 'false' ), + 'before' + ); +} ); diff --git a/packages/block-editor/src/hooks/style.js b/packages/block-editor/src/hooks/style.js index 9ffd3fd34d0ca..2e10d828068ba 100644 --- a/packages/block-editor/src/hooks/style.js +++ b/packages/block-editor/src/hooks/style.js @@ -1,7 +1,7 @@ /** * External dependencies */ -import { mapKeys, kebabCase, isObject, entries } from 'lodash'; +import { mapKeys, kebabCase, isObject, entries, has, get } from 'lodash'; /** * WordPress dependencies @@ -20,6 +20,9 @@ const styleSupportKeys = [ COLOR_SUPPORT_KEY, LINE_HEIGHT_SUPPRT_KEY ]; const hasStyleSupport = ( blockType ) => styleSupportKeys.some( ( key ) => hasBlockSupport( blockType, key ) ); +const hasGlobalStylesSupport = () => + window && window.__unstableSupportsGlobalStyles; + /** * Flatten a nested Global styles config and generates the corresponding * flattened CSS variables. @@ -56,6 +59,29 @@ export function getCSSVariables( styles = {} ) { ); } +/** + * Returns the inline styles to add depending on the style object + * + * @param {Object} styles Styles configuration + * @return {Object} Flattened CSS variables declaration + */ +export function getInlineStyles( styles = {} ) { + const mappings = { + lineHeight: [ 'typography', 'lineHeight' ], + backgroundColor: [ 'color', 'background' ], + color: [ 'color', 'text' ], + }; + + const output = {}; + Object.entries( mappings ).forEach( ( [ styleKey, objectKey ] ) => { + if ( has( styles, objectKey ) ) { + output[ styleKey ] = get( styles, objectKey ); + } + } ); + + return output; +} + /** * Filters registered block settings, extending attributes to include `style` attribute. * @@ -94,10 +120,17 @@ export function addSaveProps( props, blockType, attributes ) { const { style } = attributes; - props.style = { - ...getCSSVariables( style ), - ...props.style, - }; + if ( hasGlobalStylesSupport() ) { + props.style = { + ...getCSSVariables( style ), + ...props.style, + }; + } else { + props.style = { + ...getInlineStyles( style ), + ...props.style, + }; + } return props; } diff --git a/packages/block-editor/src/hooks/test/style.js b/packages/block-editor/src/hooks/test/style.js index 886777ae8aa6c..bbe5ebb243a84 100644 --- a/packages/block-editor/src/hooks/test/style.js +++ b/packages/block-editor/src/hooks/test/style.js @@ -1,7 +1,7 @@ /** * Internal dependencies */ -import { getCSSVariables } from '../style'; +import { getCSSVariables, getInlineStyles } from '../style'; describe( 'getCSSVariables', () => { it( 'should return an empty object when called with undefined', () => { @@ -30,3 +30,26 @@ describe( 'getCSSVariables', () => { } ); } ); } ); + +describe( 'getInlineStyles', () => { + it( 'should return an empty object when called with undefined', () => { + expect( getInlineStyles() ).toEqual( {} ); + } ); + + it( 'should ignore unknown styles', () => { + expect( getInlineStyles( { color: 'red' } ) ).toEqual( {} ); + } ); + + it( 'should return the correct inline styles', () => { + expect( + getInlineStyles( { + color: { text: 'red', background: 'black' }, + typography: { lineHeight: 1.5 }, + } ) + ).toEqual( { + backgroundColor: 'black', + color: 'red', + lineHeight: 1.5, + } ); + } ); +} ); diff --git a/packages/block-library/src/columns/style.scss b/packages/block-library/src/columns/style.scss index f1ad5b4f2217c..11afb2f23330c 100644 --- a/packages/block-library/src/columns/style.scss +++ b/packages/block-library/src/columns/style.scss @@ -1,8 +1,11 @@ +.wp-gs .wp-block-columns { + background-color: var(--wp--color--background); + color: var(--wp--color--text); +} + .wp-block-columns { display: flex; margin-bottom: $default-block-margin; - background-color: var(--wp--color--background); - color: var(--wp--color--text); // Responsiveness: Allow wrapping on mobile. flex-wrap: wrap; diff --git a/packages/block-library/src/group/style.scss b/packages/block-library/src/group/style.scss index d71f534fd2d97..f039c55ccec02 100644 --- a/packages/block-library/src/group/style.scss +++ b/packages/block-library/src/group/style.scss @@ -1,4 +1,4 @@ -.wp-block-group { +.wp-gs .wp-block-group { background-color: var(--wp--color--background); color: var(--wp--color--text); } diff --git a/packages/block-library/src/heading/style.scss b/packages/block-library/src/heading/style.scss index b9a64de5655ec..4ab387579b75a 100644 --- a/packages/block-library/src/heading/style.scss +++ b/packages/block-library/src/heading/style.scss @@ -1,4 +1,4 @@ -:root { +.wp-gs { h1, h2, h3, diff --git a/packages/block-library/src/media-text/style.scss b/packages/block-library/src/media-text/style.scss index 74f791a974a65..f9ad1c3ccee43 100644 --- a/packages/block-library/src/media-text/style.scss +++ b/packages/block-library/src/media-text/style.scss @@ -1,7 +1,9 @@ -.wp-block-media-text { +.wp-gs .wp-block-media-text { background-color: var(--wp--color--background); color: var(--wp--color--text); +} +.wp-block-media-text { // This block's direction should not be manipulated by rtl, as the mediaPosition control does. /*!rtl:begin:ignore*/ direction: ltr; diff --git a/packages/block-library/src/paragraph/style.scss b/packages/block-library/src/paragraph/style.scss index 9c0f3d7edc761..8ae488fe26e9f 100644 --- a/packages/block-library/src/paragraph/style.scss +++ b/packages/block-library/src/paragraph/style.scss @@ -1,4 +1,4 @@ -:root p { +.wp-gs p { background-color: var(--wp--color--background); color: var(--wp--color--text); line-height: var(--wp--typography--line-height); diff --git a/test/integration/full-content/full-content.test.js b/test/integration/full-content/full-content.test.js index 77c237540def9..75debf1ea04a4 100644 --- a/test/integration/full-content/full-content.test.js +++ b/test/integration/full-content/full-content.test.js @@ -54,6 +54,7 @@ function normalizeParsedBlocks( blocks ) { describe( 'full post content fixture', () => { beforeAll( () => { + window.__unstableSupportsGlobalStyles = true; unstable__bootstrapServerSideBlockDefinitions( require( './server-registered.json' ) );