Skip to content

Theme: Use valid DTCG color format for primitive values#73858

Merged
aduth merged 3 commits intotrunkfrom
fix/theme-dtcg-token-color-format
Dec 10, 2025
Merged

Theme: Use valid DTCG color format for primitive values#73858
aduth merged 3 commits intotrunkfrom
fix/theme-dtcg-token-color-format

Conversation

@aduth
Copy link
Member

@aduth aduth commented Dec 9, 2025

What?

Updates theme package to ensure that primitive design tokens are output in a valid format, per the DTCG Color Format specification.

Why?

  • So that our design tokens are valid DTCG design tokens
  • To fix issues with trying to import these tokens in Figma

How?

We previously had some code to handle this, but it looks like the if condition was causing all values not to be matched. The updates here align to expectations that the color value passed to the function is a hex value, generated by getColorString.

Testing Instructions

Verify that running npm run --workspace @wordpress/theme build produces no local changes (see git status).

Verify that you can import the resulting color.json into Figma Variables panel, if you have a Figma subscription.

Alternatively, try using an online validator to confirm validity. Note that this tool doesn't appear to expect hex values, but as referenced in the specification above, hex is a valid fallback property.

@aduth aduth requested a review from a team as a code owner December 9, 2025 21:50
@github-actions
Copy link

github-actions bot commented Dec 9, 2025

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: aduth <aduth@git.wordpress.org>
Co-authored-by: mirka <0mirka00@git.wordpress.org>

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

Copy link
Member

@mirka mirka left a comment

Choose a reason for hiding this comment

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

I can't check Figma, but the validation is passing 👍 All the color values also seems to be preserved.

Comment on lines 33 to 37
try {
parsed = to( parse( color ), sRGB );
} catch {
return color;
}
Copy link
Member

Choose a reason for hiding this comment

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

Not introduced in this PR, but it's not obvious to me what kinds of cases would error on the parse but is still safe to silently return with the original value.

Copy link
Member Author

Choose a reason for hiding this comment

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

That's a good point, maybe we should avoid catching this, so that it actually disrupts the build for the unexpected input.

See: #73858 (comment)

The `return` fallback will likely produce an invalid result. We should fail the build if the value can't be parsed.
Avoid function allocation for each transformed value, since this behavior is static
@github-actions
Copy link

Flaky tests detected in 147f8bb.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/20108053593
📝 Reported issues:

@aduth aduth merged commit ce4811c into trunk Dec 10, 2025
33 checks passed
@aduth aduth deleted the fix/theme-dtcg-token-color-format branch December 10, 2025 18:37
@github-actions github-actions bot added this to the Gutenberg 22.3 milestone Dec 10, 2025
peterwilsoncc pushed a commit to peterwilsoncc/gutenberg-build that referenced this pull request Dec 10, 2025
* Theme: Use valid DTCG color format for primitive values

* Avoid graceful catching of converted color value

See: WordPress/gutenberg#73858 (comment)

The `return` fallback will likely produce an invalid result. We should fail the build if the value can't be parsed.

* Extract function for rounding hex component

Avoid function allocation for each transformed value, since this behavior is static

Co-authored-by: aduth <aduth@git.wordpress.org>
Co-authored-by: mirka <0mirka00@git.wordpress.org>

Source: WordPress/gutenberg@ce4811c
return color;
return {
colorSpace: 'srgb',
components: parsed.coords.map( roundHexComponent ),
Copy link
Member

Choose a reason for hiding this comment

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

In version 0.6, when it's released (currently in beta) we'll be able to write getAll( parsed, { precision: 3 } ).

Maybe we could switch to 0.6.0-beta2 right away, the stable 0.5.2 version is 17 months old.

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for the pointer. That'll be nice to have. It sounds like the 0.6.0 release could be imminent, or at least was planned for "next week" as of about two months ago 😅

Copy link
Member Author

Choose a reason for hiding this comment

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

I think how I'll handle this is set myself a reminder for about a month from now to see if the stable version is released and update both this and your suggestion below. I don't think it's pressing enough to warrant adopting the beta version, unless I'm missing something.

Copy link
Member

Choose a reason for hiding this comment

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

Yes, it can be updated once a stable version is released, no hurry. I found this suggestion by looking at the roundHexComponent code and thinking: wait a minute, many colorjs.io functions already have a precision option that does exactly this kind of rounding, can we get the rounded coords this way? And the answer turns out to be: yes, in the next version 🙂

};
}
const transformColorStringToDTCGValue = ( color: string ) => {
const parsed = to( parse( color ), sRGB );
Copy link
Member

Choose a reason for hiding this comment

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

Instead of parse( color ), we can almost always pass just color. Every colorjs.io function calls getColor on all color parameters before doing anything else. That's where the parsing will happen.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Package] Theme /packages/theme [Type] Task Issues or PRs that have been broken down into an individual action to take

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants