diff --git a/packages/edit-site/src/components/global-styles/font-families.js b/packages/edit-site/src/components/global-styles/font-families.js index 5332478823c21..cd1697b7b79bd 100644 --- a/packages/edit-site/src/components/global-styles/font-families.js +++ b/packages/edit-site/src/components/global-styles/font-families.js @@ -25,6 +25,19 @@ import { unlock } from '../../lock-unlock'; const { useGlobalSetting } = unlock( blockEditorPrivateApis ); +/** + * Maps the fonts with the source, if available. + * + * @param {Array} fonts The fonts to map. + * @param {string} source The source of the fonts. + * @return {Array} The mapped fonts. + */ +function mapFontsWithSource( fonts, source ) { + return fonts + ? fonts.map( ( f ) => setUIValuesNeeded( f, { source } ) ) + : []; +} + function FontFamilies() { const { baseCustomFonts, modalTabOpen, setModalTabOpen } = useContext( FontLibraryContext ); @@ -34,18 +47,12 @@ function FontFamilies() { undefined, 'base' ); - const themeFonts = fontFamilies?.theme - ? fontFamilies.theme - .map( ( f ) => setUIValuesNeeded( f, { source: 'theme' } ) ) - .sort( ( a, b ) => a.name.localeCompare( b.name ) ) - : []; - const customFonts = fontFamilies?.custom - ? fontFamilies.custom - .map( ( f ) => setUIValuesNeeded( f, { source: 'custom' } ) ) - .sort( ( a, b ) => a.name.localeCompare( b.name ) ) - : []; - const hasFonts = 0 < customFonts.length || 0 < themeFonts.length; - + const themeFonts = mapFontsWithSource( fontFamilies?.theme, 'theme' ); + const customFonts = mapFontsWithSource( fontFamilies?.custom, 'custom' ); + const activeFonts = [ ...themeFonts, ...customFonts ].sort( ( a, b ) => + a.name.localeCompare( b.name ) + ); + const hasFonts = 0 < activeFonts.length; const hasInstalledFonts = hasFonts || baseFontFamilies?.theme?.length > 0 || @@ -61,11 +68,11 @@ function FontFamilies() { ) } - { [ ...themeFonts, ...customFonts ].length > 0 && ( + { activeFonts.length > 0 && ( <> { __( 'Fonts' ) } - { themeFonts.map( ( font ) => ( + { activeFonts.map( ( font ) => (