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: Allow variations to be filtered by multiple properties #62847

Merged
merged 10 commits into from
Jul 4, 2024
4 changes: 2 additions & 2 deletions packages/edit-site/src/components/global-styles/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ export function useSupportedStyles( name, element ) {

export function useColorVariations() {
const colorVariations = useCurrentMergeThemeStyleVariationsWithUserConfig( {
property: 'color',
properties: [ 'color' ],
} );
/*
* Filter out variations with no settings or styles.
Expand All @@ -134,7 +134,7 @@ export function useColorVariations() {
export function useTypographyVariations() {
const typographyVariations =
useCurrentMergeThemeStyleVariationsWithUserConfig( {
property: 'typography',
properties: [ 'typography' ],
} );
/*
* Filter out variations with no settings or styles.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { privateApis as blockEditorPrivateApis } from '@wordpress/block-editor';
*/
import PreviewStyles from './preview-styles';
import Variation from './variations/variation';
import { isVariationWithSingleProperty } from '../../hooks/use-theme-style-variations/use-theme-style-variations-by-property';
import { isVariationWithProperties } from '../../hooks/use-theme-style-variations/use-theme-style-variations-by-property';
import { unlock } from '../../lock-unlock';

const { GlobalStylesContext } = unlock( blockEditorPrivateApis );
Expand All @@ -33,11 +33,14 @@ export default function StyleVariationsContainer( { gap = 2 } ) {
).__experimentalGetCurrentThemeGlobalStylesVariations();
}, [] );

// Filter out variations that are of single property type, i.e. color or typography variations.
const multiplePropertyVariations = variations?.filter( ( variation ) => {
// Filter out variations that are color or typography variations.
const fullStyleVariations = variations?.filter( ( variation ) => {
return (
! isVariationWithSingleProperty( variation, 'color' ) &&
! isVariationWithSingleProperty( variation, 'typography' )
! isVariationWithProperties( variation, [ 'color' ] ) &&
! isVariationWithProperties( variation, [
'typography',
'spacing',
] )
);
} );

Expand All @@ -48,7 +51,7 @@ export default function StyleVariationsContainer( { gap = 2 } ) {
settings: {},
styles: {},
},
...( multiplePropertyVariations ?? [] ),
...( fullStyleVariations ?? [] ),
];
return [
...withEmptyVariation.map( ( variation ) => {
Expand Down Expand Up @@ -105,7 +108,7 @@ export default function StyleVariationsContainer( { gap = 2 } ) {
};
} ),
];
}, [ multiplePropertyVariations, userStyles?.blocks, userStyles?.css ] );
}, [ fullStyleVariations, userStyles?.blocks, userStyles?.css ] );

return (
<Grid
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { privateApis as editorPrivateApis } from '@wordpress/editor';
/**
* Internal dependencies
*/
import { filterObjectByProperty } from '../../../hooks/use-theme-style-variations/use-theme-style-variations-by-property';
import { filterObjectByProperties } from '../../../hooks/use-theme-style-variations/use-theme-style-variations-by-property';
import { unlock } from '../../../lock-unlock';

const { mergeBaseAndUserConfigs } = unlock( editorPrivateApis );
Expand All @@ -28,24 +28,24 @@ export default function Variation( {
variation,
children,
isPill,
property,
properties,
showTooltip,
} ) {
const [ isFocused, setIsFocused ] = useState( false );
const { base, user, setUserConfig } = useContext( GlobalStylesContext );

const context = useMemo( () => {
let merged = mergeBaseAndUserConfigs( base, variation );
if ( property ) {
merged = filterObjectByProperty( merged, property );
if ( properties ) {
merged = filterObjectByProperties( merged, properties );
}
return {
user: variation,
base,
merged,
setUserConfig: () => {},
};
}, [ variation, base, property ] );
}, [ variation, base, properties ] );

const selectVariation = () => setUserConfig( variation );

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default function ColorVariations( { title, gap = 2 } ) {
key={ index }
variation={ variation }
isPill
property="color"
properties={ [ 'color' ] }
showTooltip
>
{ () => <StylesPreviewColors /> }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export default function TypographyVariations( { title, gap = 2 } ) {
<Variation
key={ index }
variation={ variation }
property="typography"
properties={ [ 'typography' ] }
showTooltip
>
{ ( isFocused ) => (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,33 @@
* Internal dependencies
*/
import {
filterObjectByProperty,
removePropertyFromObject,
filterObjectByProperties,
removePropertiesFromObject,
} from '../use-theme-style-variations-by-property';

describe( 'filterObjectByProperty', () => {
describe( 'filterObjectByProperties', () => {
const noop = () => {};
test.each( [
{
object: {
foo: 'bar',
array: [ 1, 3, 4 ],
},
property: 'array',
properties: [ 'array' ],
expected: { array: [ 1, 3, 4 ] },
},
{
object: {
foo: 'bar',
},
property: 'does-not-exist',
properties: [ 'does-not-exist' ],
expected: {},
},
{
object: {
foo: 'bar',
},
property: false,
properties: false,
expected: {},
},
{
Expand All @@ -39,7 +39,7 @@ describe( 'filterObjectByProperty', () => {
},
},
},
property: 'null',
properties: [ 'null' ],
expected: {
dig: {
deeper: {
Expand All @@ -52,19 +52,19 @@ describe( 'filterObjectByProperty', () => {
object: {
function: noop,
},
property: 'function',
properties: [ 'function' ],
expected: {
function: noop,
},
},
{
object: [],
property: 'something',
properties: [ 'something' ],
expected: {},
},
{
object: {},
property: undefined,
properties: undefined,
expected: {},
},
{
Expand All @@ -74,23 +74,23 @@ describe( 'filterObjectByProperty', () => {
array: [ 1, 3, 4 ],
},
},
property: 'nested-object-foo',
properties: [ 'nested-object-foo' ],
expected: {
'nested-object': {
'nested-object-foo': 'bar',
},
},
},
] )(
'should filter object by $property',
( { expected, object, property } ) => {
const result = filterObjectByProperty( object, property );
'should filter object by $properties',
( { expected, object, properties } ) => {
const result = filterObjectByProperties( object, properties );
expect( result ).toEqual( expected );
}
);
} );

describe( 'removePropertyFromObject', () => {
describe( 'removePropertiesFromObject', () => {
const mockBaseVariation = {
settings: {
typography: {
Expand Down Expand Up @@ -188,34 +188,38 @@ describe( 'removePropertyFromObject', () => {
},
};

it( 'should return with no property', () => {
it( 'should return with no properties', () => {
const object = { test: 'me' };
expect( removePropertyFromObject( object, undefined ) ).toEqual(
expect( removePropertiesFromObject( object, undefined ) ).toEqual(
object
);
} );

it( 'should return with non-string property', () => {
it( 'should return with non-string properties', () => {
const object = { test: 'you' };
expect( removePropertyFromObject( object, true ) ).toEqual( object );
expect( removePropertiesFromObject( object, true ) ).toEqual( object );
} );

it( 'should return with empty object', () => {
const object = {};
expect( removePropertyFromObject( object, 'color' ) ).toEqual( object );
expect( removePropertiesFromObject( object, [ 'color' ] ) ).toEqual(
object
);
} );

it( 'should return with null', () => {
expect( removePropertyFromObject( null, 'color' ) ).toEqual( null );
expect( removePropertiesFromObject( null, [ 'color' ] ) ).toEqual(
null
);
} );

it( 'should remove the specified property from the object', () => {
it( 'should remove the specified properties from the object', () => {
expect(
removePropertyFromObject(
removePropertiesFromObject(
{
...mockBaseVariation,
},
'typography'
[ 'typography' ]
)
).toEqual( {
settings: {
Expand Down
Loading
Loading