-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Theme JSON: add localized css variables to inline classes #59469
base: trunk
Are you sure you want to change the base?
Theme JSON: add localized css variables to inline classes #59469
Conversation
Warning: Type of PR label mismatch To merge this PR, it requires exactly 1 label indicating the type of PR. Other labels are optional and not being checked here.
Read more about Type labels in Gutenberg. Don't worry if you don't have the required permissions to add labels; the PR reviewer should be able to help with the task. |
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. |
👋 Thanks for your first Pull Request and for helping build the future of Gutenberg and WordPress, @jonathan-dejong! 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. |
Thanks for your efforts, to be really clear, it's very unclear to me how this fits in the global styles architecture. For the solution here my main concern is that it has a huge potential for conflicts with variables used in themes and it adds a lot of CSS to compensate for a missing CSS feature. |
What?
This adds a localized CSS variable for a given CSS variable under an automatically generated theme.json class name.
I don't expect the first commit of this PR to be final. I expect there to be a need to conditionally set if the variable should be created or not based on what the property is.
Why?
I was looking at ways of making the button styles more flexible, especially with things like psuedo classes (:hover, etc) and getting around the issue that there is no way to style these presently when also setting colors directly in the block control.
I am aware there is some open discussions on adding hover controls etc. to buttons and other elements but as far as I've seen they are not close to resolution.
So I came up with this change in the code which results in outputting a variable ready for use.
With this, I was able to use dynamic color manipulation to allow my buttons psuedo classes to modify the buttons look nicely regardless of what color combination I add from the color palette.
Here's a nice article on color manipulation: https://blog.jim-nielsen.com/2021/css-relative-colors/
Here's a sample of what the output CSS classes would look like:
And here's some sample use case with the
:hover
psuedo class:Using this I can actually achieve a dynamic standardised look for my hover effects in my theme with any colors!
This is also just one example use case based on my own experience, I am sure there are other examples where localized CSS variables would be very useful.
Another example that comes to mind would be adding content, usually icons, with the
:before
or:after
psuedo element.And since CSS variable scoping goes "element -> child elements" this would even work if we have an inline SVG with a fill color we want to change based on the text color of our button.
For example:
I'm using buttons as the example here because they're typical interactive elements that we may want to manipulate based on state, but the logic applies to any element.
How?
This PR adds the property with the css variable syntax by appending the
--
prefix and uses the same variable valuevar()
that we are already using to set the property itself.One thing that needs to be considered more deeply is probably variable scoping and how that could have unwanted effects.. but from my testing and thinking about it I don't think it should be an issue.
This implementation is always scoped to the generated classes and therefore the elements using them and their child elements.
The only way I can see a theoretical problem is if a developer have in their theme set a CSS Variable on a higher level parent element, then use the variable on some child elements in their CSS and we override their variables with these classes because our element is inserted inbetween their expected parent and the child.
My personal opinion is that this is a very edge case scenario unlikely to be an actual real world issue and therefore shouldn't be a dealbreaker for implementing something like this.
Testing Instructions
OR
simply check the rendered inline theme.json CSS on the frontend.
Testing Instructions for Keyboard
N/A
Screenshots or screencast
Result in browser for custom text and background color classes: