Skip to content

Commit

Permalink
Global Styles: Filter out color and typography variations (#61327)
Browse files Browse the repository at this point in the history
Update packages/edit-site/src/components/global-styles/style-variations-container.js

Co-authored-by: Andrei Draganescu <me@andreidraganescu.info>

Update packages/edit-site/src/components/global-styles/style-variations-container.js

Co-authored-by: Andrei Draganescu <me@andreidraganescu.info>

update function name

remove variations which are empty once the property has been filtered out

move to a reduce function

remove duplicate values

Global Styles: Don't filter out variations where the heading and body fonts are the sane

Co-authored-by: scruffian <scruffian@git.wordpress.org>
Co-authored-by: richtabor <richtabor@git.wordpress.org>
  • Loading branch information
3 people committed May 22, 2024
1 parent f2d1937 commit 8075cb1
Showing 1 changed file with 10 additions and 37 deletions.
47 changes: 10 additions & 37 deletions packages/edit-site/src/components/global-styles/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,16 @@ import a11yPlugin from 'colord/plugins/a11y';
*/
import { store as blocksStore } from '@wordpress/blocks';
import { privateApis as blockEditorPrivateApis } from '@wordpress/block-editor';
import { privateApis as editorPrivateApis } from '@wordpress/editor';
import { useContext } from '@wordpress/element';
import { __ } from '@wordpress/i18n';

/**
* Internal dependencies
*/
import { useCurrentMergeThemeStyleVariationsWithUserConfig } from '../../hooks/use-theme-style-variations/use-theme-style-variations-by-property';
import { getFontFamilies } from './utils';
import { unlock } from '../../lock-unlock';
import { useSelect } from '@wordpress/data';

const { mergeBaseAndUserConfigs } = unlock( editorPrivateApis );
const { useGlobalSetting, useGlobalStyle, GlobalStylesContext } = unlock(
blockEditorPrivateApis
);
const { useGlobalSetting, useGlobalStyle } = unlock( blockEditorPrivateApis );

// Enable colord's a11y plugin.
extend( [ a11yPlugin ] );
Expand Down Expand Up @@ -142,38 +136,17 @@ export function useTypographyVariations() {
useCurrentMergeThemeStyleVariationsWithUserConfig( {
property: 'typography',
} );

const { base } = useContext( GlobalStylesContext );
/*
* Filter duplicate variations based on whether the variaitons
* have different heading and body font families.
* Filter out variations with no settings or styles.
*/
return typographyVariations?.length
? Object.values(
typographyVariations.reduce( ( acc, variation ) => {
const [ bodyFontFamily, headingFontFamily ] =
getFontFamilies(
mergeBaseAndUserConfigs( base, variation )
);

// Always preseve the default variation.
if ( variation?.title === 'Default' ) {
acc[
`${ headingFontFamily?.name }:${ bodyFontFamily?.name }`
] = variation;
} else if (
headingFontFamily?.name &&
bodyFontFamily?.name &&
! acc[
`${ headingFontFamily?.name }:${ bodyFontFamily?.name }`
]
) {
acc[
`${ headingFontFamily?.name }:${ bodyFontFamily?.name }`
] = variation;
}
return acc;
}, {} )
)
? typographyVariations.filter( ( variation ) => {
const { settings, styles, title } = variation;
return (
title === __( 'Default' ) || // Always preseve the default variation.
Object.keys( settings ).length > 0 ||
Object.keys( styles ).length > 0
);
} )
: [];
}

1 comment on commit 8075cb1

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in 8075cb1.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/9192112074
📝 Reported issues:

Please sign in to comment.