-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Styles: Move global styles data logic to a dedicated package #72464
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
Conversation
|
Size Change: +9.58 kB (+0.44%) Total Size: 2.18 MB
ℹ️ View Unchanged
|
|
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 If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
|
Flaky tests detected in fa87ef6. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/18689711522
|
ramonjd
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for getting this started, and for the time taken to convert to TS.
I tested block/top-level styles. Style book/revisions also work as expected as far as I can see.
Stuff like addressing TS ignores and adding types for theme.json settings can always be done incrementally.
LGTM, though I think it'd be helpful to get a a confidence test from other folks.
| } from './core/render'; | ||
| export { getBlockSelector } from './core/selectors'; | ||
|
|
||
| // Utilities (Ideally these shouldn't be exposed) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What's the alternative?
I think a set of consistent functions to deal with theme.json's many special values is useful.
Just taking getValueFromVariable as an example, a presets control needs that to interpret var: strings and resolve them. Not saying the controls have to work this way.
I'm wondering, if a more controller API is desired, whether we could also move GlobalStylesContext to this package?
Then hooks like useGlobalStyle and others would follow.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The alternative for me is that this is all absorbed by getStyle setStyle, getSetting setSetting and things like that and UI elements shouldn't have to deal with these low level stuff.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm wondering, if a more controller API is desired, whether we could also move GlobalStylesContext to this package?
My idea is to remove the context entirely but I don't know how feasible is that yet. I think the context just adds confusion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At least some of these (getValueFromVariable, getPresetVariableFromValue for instance) could live in the style engine package, alongside things like getCSSValueFromRawStyle. They have similar generic converting-thing-to-other-thing functionality and can be useful for any styles, not just global ones.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the style engine package, alongside things like getCSSValueFromRawStyle. They have similar generic converting-thing-to-other-thing functionality and can be useful for any styles, not just global ones.
I think the style engine package should just merge with this new higher level package, there's no reason for it existing separately, it's the same purpose. Now, I don't want to do that merge work here, it can be done later, it's low impact.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
style-engine is used in core though, if we removed it we'd still have to support it for older WP versions.
global-style-engine is not and is a bundled package, so it can be removed at any moment.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@andrewserong I don't think we should move forward with this yet. The global-style-engine API is private and by moving it to style-engine it becomes public. It is very early to expose it as a public api (global variable) and I'm not sure if we should.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fair point @youknowriad! I've closed out #72559 — I found it a useful exercise at least to get a feel for what it might look like to combine them. It wasn't very much work to create that PR, so if and when we are ready to make things public, I don't think it'll be too much work to try it again 🙂
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's also the option of exporting anything we don't want public as a private API from the style engine package, that should ensure it doesn't end up in the global var, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that's possible too but I'm preferring more and more regular packages for a few reasons:
- privateApis usage have proven challenging in DataViews. Basically every time a "bundled" plugin uses a private API, that bundled plugin can't be used twice in two different plugins/scripts anymore. So this is forcing us to remove private APIs usage from DataViews. (not done yet but we'll have to do it soon).
- bundled packages are nice because they need to be stateless (no side effects) in general so they force folks to write the right APIs
- There's a DevX aspect to it: no ugly unlock and privateApis imports all around our code base.
- Bundled packages are tree shakable. You can bundle some of it in edit-post and another part in edit-site without necessarily needing to load the full package in both.
| getComputedFluidTypographyValue, | ||
| } from './fluid'; | ||
|
|
||
| function isFluidTypographyEnabled( typographySettings?: { fluid?: any } ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we'd already documented the typography type in JSDOC. Can be used here, or I'm happy to take care of it in a follow up
gutenberg/packages/block-editor/src/components/global-styles/typography-utils.js
Lines 10 to 49 in 98e9295
| import { | |
| getComputedFluidTypographyValue, | |
| getTypographyValueAndUnit, | |
| } from '../font-sizes/fluid-utils'; | |
| import { getFontStylesAndWeights } from '../../utils/get-font-styles-and-weights'; | |
| /** | |
| * @typedef {Object} FluidPreset | |
| * @property {string|undefined} max A maximum font size value. | |
| * @property {?string|undefined} min A minimum font size value. | |
| */ | |
| /** | |
| * @typedef {Object} Preset | |
| * @property {?string|?number} size A default font size. | |
| * @property {string} name A font size name, displayed in the UI. | |
| * @property {string} slug A font size slug | |
| * @property {boolean|FluidPreset|undefined} fluid Specifies the minimum and maximum font size value of a fluid font size. | |
| */ | |
| /** | |
| * @typedef {Object} TypographySettings | |
| * @property {?string} minViewportWidth Minimum viewport size from which type will have fluidity. Optional if size is specified. | |
| * @property {?string} maxViewportWidth Maximum size up to which type will have fluidity. Optional if size is specified. | |
| * @property {?number} scaleFactor A scale factor to determine how fast a font scales within boundaries. Optional. | |
| * @property {?number} minFontSizeFactor How much to scale defaultFontSize by to derive minimumFontSize. Optional. | |
| * @property {?string} minFontSize The smallest a calculated font size may be. Optional. | |
| */ | |
| /** | |
| * Returns a font-size value based on a given font-size preset. | |
| * Takes into account fluid typography parameters and attempts to return a css formula depending on available, valid values. | |
| * | |
| * The Core PHP equivalent is wp_get_typography_font_size_value(). | |
| * | |
| * @param {Preset} preset | |
| * @param {Object} settings | |
| * @param {boolean|TypographySettings} settings.typography.fluid Whether fluid typography is enabled, and, optionally, fluid font size options. | |
| * @param {?Object} settings.typography.layout Layout options. | |
| * |
tellthemachines
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did a quick round of testing on this by changing some global styles and checking them on site and post editors, and all seems to be in order!
I see the style book is erroring when opening a block style variation from the global styles sidebar, but that's also happening on trunk so not related to these changes 😅

| return getValueFromVariable( features, blockName, result as string ); | ||
| } | ||
|
|
||
| export function getValueFromVariable( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be good to copy across the JSDoc comments for these functions where they exist, makes it easier to tell at a glance what the function does.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good call on the comments, I added the ones that I found back.
| } from './core/render'; | ||
| export { getBlockSelector } from './core/selectors'; | ||
|
|
||
| // Utilities (Ideally these shouldn't be exposed) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At least some of these (getValueFromVariable, getPresetVariableFromValue for instance) could live in the style engine package, alongside things like getCSSValueFromRawStyle. They have similar generic converting-thing-to-other-thing functionality and can be useful for any styles, not just global ones.
| * | ||
| * @return {Array} Array of stylesheets and settings. | ||
| */ | ||
| export function useGlobalStylesOutputWithConfig( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Won't this ever be needed outside of edit-site? I'm thinking that strictly speaking things like the style book shouldn't really be a part of the site editor, but sit at the admin level instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Won't this ever be needed outside of edit-site? I'm thinking that strictly speaking things like the style book shouldn't really be a part of the site editor, but sit at the admin level instead.
It will in the post editor but I don't like how it works today (update settings that are not supposed to be edited), so let's leave it as is for now and see what makes sense when adding the global styles sidebar to the post editor.
4627fbb to
fa87ef6
Compare
| hasFallbackGapSupport, | ||
| disableLayoutStyles, | ||
| disableRootPadding, | ||
| getBlockStyles, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note to self: missed the object that tells the function to also generate block variation styles.
Fix here: #73535
First step to achieve #72317
Summary
The global styles code has long been neglected and lived within unrelated packages (block-editor, edit-site), this PR starts the work to organize it better. It moves all the logic to generate CSS out of a global styles object to a dedicated package, all the global styles object manipulation as well. It's the "global-styles-engine".
The code is also full typescript now and the PR also maintains same functionality without any change.
The following step is going to be a UI package that relies on this engine (extracted from edit-site) which would allow us to use the global styles UI in a more portable way (include in the post editor)
Testing instructions
Check global styles behavior / rendering in the site editor (it should work just like trunk)