Skip to content

Commit

Permalink
Global Styles: Filter out color and typography variations
Browse files Browse the repository at this point in the history
  • Loading branch information
scruffian committed Apr 12, 2024
1 parent e05558a commit 7ad5f93
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { __ } from '@wordpress/i18n';
*/
import PreviewStyles from './preview-styles';
import Variation from './variations/variation';
import { isVariationOfSingleProperty } from '../../hooks/use-theme-style-variations/use-theme-style-variations-by-property';

export default function StyleVariationsContainer( { gap = 2 } ) {
const variations = useSelect( ( select ) => {
Expand All @@ -20,20 +21,28 @@ export default function StyleVariationsContainer( { gap = 2 } ) {
).__experimentalGetCurrentThemeGlobalStylesVariations();
}, [] );

// Filter out variations that are of single property type, i.e. color and typography variations.
const filteredVariations = variations?.filter( ( variation ) => {
return (
! isVariationOfSingleProperty( variation, 'color' ) &&
! isVariationOfSingleProperty( variation, 'typography' )
);
} );

const withEmptyVariation = useMemo( () => {
return [
{
title: __( 'Default' ),
settings: {},
styles: {},
},
...( variations ?? [] ).map( ( variation ) => ( {
...( filteredVariations ?? [] ).map( ( variation ) => ( {
...variation,
settings: variation.settings ?? {},
styles: variation.styles ?? {},
} ) ),
];
}, [ variations ] );
}, [ filteredVariations ] );

return (
<Grid
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,25 @@ export default function useThemeStyleVariationsByProperty( {
return processedStyleVariations;
}, [ variations, property, baseVariation, filter ] );
}

/**
* Compares a style variation to the same variation filtered by a single property.
* Returns true if the variation contains only the property specified.
*
* @param {Object} variation The variation to compare.
* @param {string} property The property to compare.
* @return {boolean} Whether the variation contains only a single property.
*/
export function isVariationOfSingleProperty( variation, property ) {
const variationWithProperty = filterObjectByProperty(
cloneDeep( variation ),
property
);

return (
JSON.stringify( variationWithProperty?.styles ) ===
JSON.stringify( variation?.styles ) &&
JSON.stringify( variationWithProperty?.settings ) ===
JSON.stringify( variation?.settings )
);
}

0 comments on commit 7ad5f93

Please sign in to comment.