-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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
Global Styles: Expose body level settings as CSS variables #39432
Conversation
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 problem I see here is that these vars become a public CSS API which makes the form of theme.json
a fixed contract. Using these vars liberally makes it so that potential theme.json
updates become problematic for themes.
cc @youknowriad
return; | ||
} | ||
|
||
$theme_json_styles = _wp_array_get( $settings, array( 'styles' ) ); |
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.
A comment here explaining what "root" means to understand the unsets would be good.
I'd like to unwrap a bit the use case to make sure we explore the solution space. This is how a theme can create the above design (both the root text and the table border have the same color): {
"styles": {
"color": {
"text": "var(--wp--preset--color--YELLOW)"
},
"blocks": {
"core/table": {
"border": {
"color": "var(--wp--preset--color--YELLOW)"
}
}
}
}
} So, a theme can already do this. However, what happens when the user changes the value of the top-level text color to any other thing? This is the core issue, in my view, and we discussed this in a few other places, including this thread. If the user modifies the text color the values are no longer "connected": {
"styles": {
"color": {
"text": "var(--wp--preset--color--RED)"
},
"blocks": {
"core/table": {
"border": {
"color": "var(--wp--preset--color--YELLOW)"
}
}
}
}
} If this is the problem we want to solve, the root cause is that we don't have a way to maintain the "design links" between the different components. Note that there's other issues but we should discuss them separately (see). This has been "theme territory" so far and I've shared here a way for themes to address this: it involves the theme creating more CSS Custom Properties and wire them up itself. If we want the framework to absorb this issue, we can do it differently. What if a theme could declare "design links"? {
"styles": {
"color": {
"text": "var(--wp--preset--color--yellow)"
},
"blocks": {
"core/table": {
"border": {
"color": "styles.color.text"
}
}
}
}
} The CSS output: body {
color: var(--wp--preset--color--yellow);
}
.wp-block-table > table {
/* NOTE how this value has been resolved to the value of styles.color.text */
border-color: var(--wp--preset--color--yellow);
} When we generate the styles, the framework will resolve Thoughts on this approach:
|
@oandregal this is a great idea for themes, but I'm not sure how it would work for default block styles - do you have any thoughts about that? We could add these kind of rules to the default core theme.json, would that be appropriate? |
Do you mean how blocks can also take into account "user choices"? Don't they already have via the block supports? Do you have any use case in mind? I know there's still cases that are weird but, in general, my thinking is that we need to keep improving on that (moving the styles generation for all block supports to the server -aka "styles engine"-should help address these issues). There's a another angle that we left unexplored: the block's CSS should be another source of styles that is merged with the rest. We'd have something like "defaults (core theme.json) < blocks < theme (the theme's theme.json) < user (styles provided via GS sidebar)". I've got a PR for this at #34180 |
I'm not sure how. We have a case like this for the file block - we would like the file block to have a background color that uses the currentColor as defined by the theme. It looks like we could combine #34180 with your idea above to achieve that... |
Ah, I see what you mean. That would be feasible just with "design links", right? No need for absorving the block's CSS. We could add those to core's {
"core/file": {
"color": {
"background": "style.color.background"
}
}
} |
Yes that's correct, we just need to build that affordance! |
Closing in favour of #41696 |
What?
This exposes some of the styles from theme.json as CSS variables so that blocks and themes can build on them.
Why?
The source of truth for these styles should be the Global Styles, and in many cases these rules are able to cascade to other blocks using CSS inheritance, but this doesn't work if we want the style to apply to a different property of a block, e.g. we might want to apply the text color to the border of a table.
This takes the ideas from #29714 and spins out a new PR to deal with some of the issues raised in #38998.
How?
For all properties inside "styles", except those in the "blocks" and "elements" array, we create CSS variables.
Testing Instructions