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

Global Styles: Fix block-level global styles color panels #34293

Merged
merged 5 commits into from
Aug 27, 2021
Merged
Show file tree
Hide file tree
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
6 changes: 4 additions & 2 deletions packages/blocks/src/api/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ export const __EXPERIMENTAL_STYLE_PROPERTY = {
},
backgroundColor: {
value: [ 'color', 'background' ],
support: [ 'color' ],
support: [ 'color', 'background' ],
requiresOptOut: true,
},
borderColor: {
value: [ 'border', 'color' ],
Expand All @@ -50,7 +51,8 @@ export const __EXPERIMENTAL_STYLE_PROPERTY = {
},
color: {
value: [ 'color', 'text' ],
support: [ 'color' ],
support: [ 'color', 'text' ],
requiresOptOut: true,
},
linkColor: {
value: [ 'elements', 'link', 'color', 'text' ],
Expand Down
20 changes: 18 additions & 2 deletions packages/edit-site/src/components/editor/global-styles-provider.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { set, get, mergeWith, mapValues, setWith, clone } from 'lodash';
import { set, get, has, mergeWith, mapValues, setWith, clone } from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -75,8 +75,24 @@ export const useGlobalStylesReset = () => {
const extractSupportKeys = ( supports ) => {
const supportKeys = [];
Object.keys( STYLE_PROPERTY ).forEach( ( name ) => {
if ( ! STYLE_PROPERTY[ name ].support ) {
return;
}

// Opting out means that, for certain support keys like background color,
// blocks have to explicitly set the support value false. If the key is
// unset, we still enable it.
if ( STYLE_PROPERTY[ name ].requiresOptOut ) {
if (
has( supports, STYLE_PROPERTY[ name ].support[ 0 ] ) &&
get( supports, STYLE_PROPERTY[ name ].support ) !== false
) {
return supportKeys.push( name );
}
}

if ( get( supports, STYLE_PROPERTY[ name ].support, false ) ) {
supportKeys.push( name );
return supportKeys.push( name );
}
} );
return supportKeys;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
/**
* WordPress dependencies
*/
import { dispatch } from '@wordpress/data';

/**
* External dependencies
*/
import { mount } from 'enzyme';
import { act } from 'react-dom/test-utils';

/**
* Internal dependencies
*/
import GlobalStylesProvider, {
useGlobalStylesContext,
} from '../global-styles-provider';

const settings = {
styles: [
{
css: 'body {\n\tmargin: 0;\n\tpadding: 0;\n}',
baseURL: 'http://localhost:4759/ponyfill.css',
},
],
__experimentalGlobalStylesBaseStyles: {},
};

const generateCoverBlockType = ( colorSupports ) => {
return {
name: 'core/cover',
supports: {
color: colorSupports,
},
};
};

const FakeCmp = () => {
const globalStylesContext = useGlobalStylesContext();
const coverBlockSupports =
globalStylesContext.blocks[ 'core/cover' ].supports;

return <div supports={ coverBlockSupports }></div>;
};

const generateWrapper = () => {
return mount(
<GlobalStylesProvider
baseStyles={ settings.__experimentalGlobalStylesBaseStyles }
>
<FakeCmp />
</GlobalStylesProvider>
);
};

describe( 'global styles provider', () => {
beforeAll( () => {
dispatch( 'core/edit-site' ).updateSettings( settings );
} );

describe( 'when a block enables color support', () => {
describe( 'and disables background color support', () => {
it( 'still enables text color support', () => {
act( () => {
dispatch( 'core/blocks' ).addBlockTypes(
generateCoverBlockType( {
link: true,
background: false,
} )
);
} );

const wrapper = generateWrapper();
const actual = wrapper
.findWhere( ( ele ) => Boolean( ele.prop( 'supports' ) ) )
.prop( 'supports' );
expect( actual ).not.toContain( 'backgroundColor' );
expect( actual ).toContain( 'color' );

act( () => {
dispatch( 'core/blocks' ).removeBlockTypes( 'core/cover' );
} );
} );
} );

describe( 'and both text color and background color support are disabled', () => {
it( 'disables text color and background color support', () => {
act( () => {
dispatch( 'core/blocks' ).addBlockTypes(
generateCoverBlockType( {
text: false,
background: false,
} )
);
} );

const wrapper = generateWrapper();
const actual = wrapper
.findWhere( ( ele ) => Boolean( ele.prop( 'supports' ) ) )
.prop( 'supports' );
expect( actual ).not.toContain( 'backgroundColor' );
expect( actual ).not.toContain( 'color' );

act( () => {
dispatch( 'core/blocks' ).removeBlockTypes( 'core/cover' );
} );
} );
} );

describe( 'and text color and background color supports are omitted', () => {
it( 'still enables both text color and background color supports', () => {
act( () => {
dispatch( 'core/blocks' ).addBlockTypes(
generateCoverBlockType( { link: true } )
);
} );

const wrapper = generateWrapper();
const actual = wrapper
.findWhere( ( ele ) => Boolean( ele.prop( 'supports' ) ) )
.prop( 'supports' );
expect( actual ).toContain( 'backgroundColor' );
expect( actual ).toContain( 'color' );

act( () => {
dispatch( 'core/blocks' ).removeBlockTypes( 'core/cover' );
} );
} );
} );
} );
} );