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

List all active fonts in the typography section #65806

Merged
merged 3 commits into from
Oct 8, 2024
Merged
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
35 changes: 21 additions & 14 deletions packages/edit-site/src/components/global-styles/font-families.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand All @@ -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 ||
Expand All @@ -61,11 +68,11 @@ function FontFamilies() {
) }

<VStack spacing={ 4 }>
{ [ ...themeFonts, ...customFonts ].length > 0 && (
{ activeFonts.length > 0 && (
<>
<Subtitle level={ 3 }>{ __( 'Fonts' ) }</Subtitle>
<ItemGroup size="large" isBordered isSeparated>
{ themeFonts.map( ( font ) => (
{ activeFonts.map( ( font ) => (
<FontFamilyItem
key={ font.slug }
font={ font }
Expand Down
Loading