Font Library: Add font family name next to "Default" label
SUGGESTION When looking at a block’s Font Family, it would be very helpful to see the name of default font next to the word “Default”. It can be hard to remember what you set as default in your global styles.
Can you assign it to me?This is my first contribution,so where can i get this code?
Hi folks 👋
I'm working on this issue and I came across this working implementation in:
packages/edit-site/src/components/global-styles/typography-elements.js
In that file, default font families are accessed like this:
const [ fontFamily ] = useGlobalStyle( 'typography.fontFamily' );
So, I tried using the same approach in a different part of the codebase (in FontFamilyControl at packages/block-editor/src/components), by importing useGlobalStyle via:
import { privateApis as blockEditorPrivateApis } from '@wordpress/block-editor';
const { useGlobalStyle } = unlock( blockEditorPrivateApis );
However, doing this causes a fatal memory error during build:
Fatal error: Allowed memory size of 268435456 bytes exhausted (tried to allocate 262144 bytes) in wp-includes/class-wp-scripts.php on line 631
This happens on a local environment (Local by Flywheel) with default PHP memory settings (256MB).
❓ My questions:
- Why does this work fine inside
edit-site(Site Editor context), but not when used elsewhere likeblock-editororcomponents? - Is there a recommended way to safely fetch global typography values (especially default font families) outside the Site Editor?
- Should I avoid using
useGlobalStylefrom private APIs in shared packages altogether? - Any alternative direction or pattern to achieve this?
cc : @mikachan @creativecoder
Hi Team,
While investigating the issue and reviewing the related codebase, I identified that the Default value is currently hardcoded within the following section:
https://github.com/WordPress/gutenberg/blob/cebf65e50f7b7b2bc9eddc3c1f5d63dc771607b6/packages/block-editor/src/components/font-family/index.js#L38-L47
Key Observations
- The hardcoding of
Defaultmakes it challenging to customize or intercept the "default" font family at runtime. - Ideally, the
Defaultlabel should be sourced from the settings, specifically from:
so that modifications can be handled dynamically wherever required.TypographyPanel => settings.typography.fontFamily.default
Recommended Next Steps
-
Investigate Data Persistence
- Examine how Gutenberg currently saves the
defaultvalue forfontFamilywithin its settings structure. - Verify why, when the
TypographyPanelcomponent is rendered, the settings for the default font family appear asundefined.
- Examine how Gutenberg currently saves the
-
Implement Settings Retrieval
- If the
defaultvalue is not present in the settings, ensure it is correctly initialized insettings.typography.fontFamily.default.
- If the
-
Refactor FontFamilyControl
- Once the proper default is set in the configuration, update the
FontFamilyControlcomponent to reference the newly available value rather than rely on the hardcoded fallback. - As a safeguard, only use the hardcoded
Defaultif a proper value cannot be obtained from the settings.
- Once the proper default is set in the configuration, update the
Thank You,
Yes please, I would love this feature!
I tested this on trunk and it looks resolved by #77894. With Twenty Twenty-Five, a Paragraph block with no font of its own now shows the inherited font ("Manrope") in the Font control instead of "Default", and the dropdown marks Manrope as the current value.
The only remaining gap is that the "Default" option inside the dropdown doesn't name the font. That could be a small follow-up ("Default (Manrope)") if it's still wanted, otherwise I think this can be closed.
Indeed, thank you for testing. Closing as resolved!