Skip to content

Avoid quoting CSS variables in formatFontFamily - #69926

Closed
nextend wants to merge 2 commits into
WordPress:trunkfrom
nextend:formatFontFamily-patch
Closed

nextend wants to merge 2 commits into
WordPress:trunkfrom
nextend:formatFontFamily-patch

Conversation

@nextend

@nextend nextend commented Apr 17, 2025

Copy link
Copy Markdown

What?

Fixes an issue where font families defined as CSS variables in theme.json (e.g., "fontFamily": "var(--font-family-variable)") are incorrectly quoted when rendered in the Site Editor → Styles → Typography → Fonts panel.

Why?

Currently, when a font family is defined using a CSS variable in theme.json, it is rendered with quotes in the Site Editor UI. This causes the font not to be recognized or applied correctly. However, in other parts of the system (like frontend rendering), these variables are left unquoted and work as expected. This inconsistency leads to bugs and confusion for theme developers and users.

How?

Updated the formatFontFamily utility to detect and preserve valid CSS variable definitions using a regular expression (/^var(--[a-zA-Z0-9_-]+(,.+)?)$/). When such a value is found, it is returned as-is, without additional formatting or quoting. Other font family names continue to be trimmed and quoted when necessary.

Testing Instructions

  1. Testing Instructions
  2. Add a theme.json to your theme with a fontFamilies entry like this:
{
  "settings": {
    "typography": {
      "fontFamilies": [
        {
          "name": "Base font",
          "slug": "base-font",
          "fontFamily": "var(--font-family-variable)"
        }
      ]
    }
  }
}
  1. Open the Site Editor.
  2. Go to Styles → Typography → Fonts.
  3. Confirm that the font variable var(--font-family-variable) appears without surrounding quotes. (Inspect the element)

nextend added 2 commits April 16, 2025 16:37
- Preserve CSS variables like `var(--font-name)` without quoting
- Enhanced regex logic to handle mixed inputs properly
- Added inline and console examples for clarity and testing
@github-actions

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: nextend <nextendweb@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions github-actions Bot added the First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository label Apr 17, 2025
@github-actions

Copy link
Copy Markdown

👋 Thanks for your first Pull Request and for helping build the future of Gutenberg and WordPress, @nextend! In case you missed it, we'd love to have you join us in our Slack community.

If you want to learn more about WordPress development in general, check out the Core Handbook full of helpful information.

@nextend nextend changed the title Format font family patch Addign support for CSS variables to formatFontFamily Apr 17, 2025
@nextend nextend changed the title Addign support for CSS variables to formatFontFamily Addign support for CSS variables in formatFontFamily function Apr 17, 2025
@nextend nextend changed the title Addign support for CSS variables in formatFontFamily function Avoid quoting CSS variables in formatFontFamily Apr 17, 2025
@Mamaduka Mamaduka added [Type] Bug An existing feature does not function as intended Global Styles Anything related to the broader Global Styles efforts, including Styles Engine and theme.json labels Apr 17, 2025

const formatItem = ( item ) => {
item = item.trim();
if ( !cssVarRegex.test( item ) && item.match( regex ) ) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks @nextend for the fix.

A few things:

  • The var above should be cssVariableRegex.
  • The file has also moved to packages/global-styles-ui

If you're willing to update this PR, I can close the alternative over at #82010, or we can just go with the latter.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Also it'd be good to get some tests in.

@ramonjd

ramonjd commented Aug 25, 2026

Copy link
Copy Markdown
Member

I'll move it over to this PR since this one is a bit stale:

Thanks for getting it started.

@ramonjd ramonjd closed this Aug 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Feature] Font Library First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository Global Styles Anything related to the broader Global Styles efforts, including Styles Engine and theme.json [Type] Bug An existing feature does not function as intended

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants