WordPress / gutenberg

The Block Editor project for WordPress and beyond. Plugin is available from the official repository.
https://wordpress.org/gutenberg/
Other
10.48k stars 4.18k forks source link

Custom CSS variables #64433

Open asafm7 opened 2 months ago

asafm7 commented 2 months ago

Consider introducing custom CSS variables to be used across the editor and in external CSS files using the var() function.

Similar to the global colors' functionality.

carolinan commented 2 months ago

Hi, Can you please add more context? Is this related to extending the editor, or for use in components in the project itself? CSS variables are used extensively already.

asafm7 commented 2 months ago

Thanks, @carolinan.

For example, in the editor, in the global styles section, being able to define a var called Global Border Radius with a value of 12px.

Then, everywhere in the editor where a border radius can be set, it will be possible to use the global variable. This way, if, for example, a decision is made to change the design from 12px to 8px it can be done from one place.

This will also allow external CSS files to use the variable, making the design easier to maintain in this regard as well.

Just like when a Primary color is changed, it is reflected on all places set to use it (including external CSS).

Maybe the variable's type is better defined by a unit (px, em etc.) rather than a property (border-radius, font-size etc.)

Please let me know if this is clearer.

Thanks again.

carolinan commented 2 months ago

How is this different from the existing global styles system? I personal don't think that having input fields for entering custom CSS variables would be useful for the majority of users. But I also believe that what you are describing is also already possible using the global styles. I don't think there is anything that is preventing using the preset css variables in third party CSS files.

asafm7 commented 2 months ago

@carolinan, using the current global styles it is possible to set global styles on a block level, but not on a cross-block level.

If, for example, my design uses "soft corners" (that is, with a specific border-radius where possible) and I want it to be applied to many elements: images, buttons, embeds etc. - how can it be done using the current options?

carolinan commented 2 months ago

By using the "custom" setting in theme.json then adding that custom variable to the border radius of each block and element. Or by creating style variations that you can apply to multiple blocks.

If this is specific to borders please see https://github.com/WordPress/gutenberg/issues/64041

asafm7 commented 2 months ago

Thanks, @carolinan.

I've tried adding a custom setting, but it isn't reflected in the UI.

So, if I understand correctly (correct me if I don't), what you mean is maintaining a custom code override for each relevant block.

This feels like complicating things, rather than simplifying them.

What I mean is making the custom variables available in the code.

Just like we have: image

Meaning, the ability to choose a custom color or a preset color.

We can have: image

I also went over the ticket you referred to and I'm now sure exactly what I mean. The tickets mention a scale (small, medium, large) rather than individual presets which aren't necessarily scale related.

carolinan commented 2 months ago

That it is not reflected in the UI is also already reported as a separate issue. For feature requests and bug reports to be actionable, they need to be limited in scope and specific.

asafm7 commented 2 months ago

Thanks, @carolinan.

That it is not reflected in the UI is also already reported as a separate issue.

Can you maybe refer to this ticket?

Thanks again.

carolinan commented 2 months ago

I found it here :) https://github.com/WordPress/gutenberg/issues/37752

asafm7 commented 2 months ago

Great. Thank you 🙏