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

Fix list of base theme fonts when a theme variation is applied. #59959

Merged
merged 4 commits into from
Mar 21, 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
Original file line number Diff line number Diff line change
Expand Up @@ -105,18 +105,31 @@ function FontLibraryProvider( { children } ) {
const [ modalTabOpen, setModalTabOpen ] = useState( false );
const [ libraryFontSelected, setLibraryFontSelected ] = useState( null );

const baseThemeFonts = baseFontFamilies?.theme
? baseFontFamilies.theme
.map( ( f ) => setUIValuesNeeded( f, { source: 'theme' } ) )
.sort( ( a, b ) => a.name.localeCompare( b.name ) )
: [];

// Themes Fonts are the fonts defined in the global styles (database persisted theme.json data).
const themeFonts = fontFamilies?.theme
? fontFamilies.theme
.map( ( f ) => setUIValuesNeeded( f, { source: 'theme' } ) )
.sort( ( a, b ) => a.name.localeCompare( b.name ) )
: [];

const themeFontsSlugs = new Set( themeFonts.map( ( f ) => f.slug ) );

/*
* Base Theme Fonts are the fonts defined in the theme.json *file*.
*
* Uses the fonts from global styles + the ones from the theme.json file that hasn't repeated slugs.
* Avoids incosistencies with the fonts listed in the font library modal as base (unactivated).
* These inconsistencies can happen when the active theme fonts in global styles aren't defined in theme.json file as when a theme style variation is applied.
*/
const baseThemeFonts = baseFontFamilies?.theme
creativecoder marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Contributor

Choose a reason for hiding this comment

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

@matiasbenedetto: Since the value is now a union with themeFonts, are these still baseThemeFonts — or does the name need changing?

? themeFonts.concat(
baseFontFamilies.theme
.filter( ( f ) => ! themeFontsSlugs.has( f.slug ) )
.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' } ) )
Expand Down Expand Up @@ -144,8 +157,7 @@ function FontLibraryProvider( { children } ) {
return;
}

const fonts =
font.source === 'theme' ? baseThemeFonts : baseCustomFonts;
const fonts = font.source === 'theme' ? themeFonts : baseCustomFonts;

// Tries to find the font in the installed fonts
const fontSelected = fonts.find( ( f ) => f.slug === font.slug );
Expand Down
Loading