WordPress / gutenberg

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

enhancement: improve how extenders can add global CSS to editor and frontend #48454

Closed tresorama closed 1 year ago

tresorama commented 1 year ago

What problem does this address?

While working on a block theme for the first time, i was stuck at loading a global CSS with utilities classes inside. The Developer experience is a mess currently. This should be improved because extenders (theme dev /plugin dev) are crucial for Wordpress.

There are various bad "things" currently in place that a dev must fight against to complete a simple task.

I have made a tutorial post to illustrate the process

https://jacopomarrone.netlify.app/blog/load-global-css-in-block-theme-in-wordpress

Main Painponts

Should be easies to inject a simple CSS. With Javascript ??

We should know that iframe receive CSS assets only if some CSS selector are present. This alone is big ALARM that maybe a refactor is necessary.

Inject CSS to the editor iframe should be done with a dedicated hook or similar process, and now is not present.

Core css specificity is high for an extendible system:

It difficult to say to wordpress when to inject our custom css relative to the theme.json one. This lead us to increase even more the specificity of CSS selectors.

It impossible to add a medium amount of CSS without a CSS preprocessor that support nesting.

Imagine a child theme that want to overrides ?!?!?

Related

https://github.com/WordPress/gutenberg/issues/48437 https://github.com/WordPress/gutenberg/issues/41821 https://github.com/WordPress/gutenberg/issues/38673#issuecomment-1036142814 https://github.com/WordPress/gutenberg/issues/33212


What is your proposed solution?

Start discussing about how to improve these, and if necessary :

VladNov commented 1 year ago

There is no need for extra CSS and media queries, use build-in fluid font sizes:

https://make.wordpress.org/core/2022/10/03/fluid-font-sizes-in-wordpress-6-1/

tomdevisser commented 1 year ago

@tresorama Congratulations on working on your first block theme! Super exciting stuff.

I completely understand that the documentation which is work-in-progress might be overwhelming when there's so many new things to learn. That doesn't necessarily mean Gutenberg is a mess right now, just that it takes a bit more effort than what you're already familiar with.

I think this isn't a problem of what's possible, but of the approach you're taking. It's discouraged to overwrite the font size like this, cause it breaks the option to change the font size in the block's settings.

First you define font sizes in your theme.json. If it's an external theme and you're a plugin developer, you can define font sizes in your stylesheets in block.json, one for both front- and backend and one for just the backend.

If you want to alter an existing block, try block variations.

Good luck!

tresorama commented 1 year ago

@tresorama Congratulations on working on your first block theme! Super exciting stuff.

I completely understand that the documentation which is work-in-progress might be overwhelming when there's so many new things to learn. That doesn't necessarily mean Gutenberg is a mess right now, just that it takes a bit more effort than what you're already familiar with.

I think this isn't a problem of what's possible, but of the approach you're taking. It's discouraged to overwrite the font size like this, cause it breaks the option to change the font size in the block's settings.

First you define font sizes in your theme.json. If it's an external theme and you're a plugin developer, you can define font sizes in your stylesheets in block.json, one for both front- and backend and one for just the backend.

If you want to alter an existing block, try block variations.

Good luck!

I explain my block theme workflow to have more context.

I try to do almost everything on theme.json. So i defined my design tokens in theme.json, the block editor can now expose my tokens under blocks attribute controls (in the sidebar). So font size, colors, spacing.

This is the design system primitive.

Then i go in the editor and create template parts and templates, extracting these into .html file under theme directory. Then i slice my templates into block pattern and copy into .php file under theme directory.

While designing/developing templates in the block editor I use core blocks only and adjust font size, padding, color, using tokens defined in theme.json, no custom value. So i create all my design in desktop view. So far so good, it looks great.

Then i check my templates and page with tablet and mobile preview and some block need adjustments, (text too large, gap between items too large, and so on...). These responsive adjustment are not high in quantity but they exists.

So i started researching how to compensate for responsiveness, and tried different plugin also. This is what i found.

Other key aspect ,at least for me, that made me prefer CSS utilities classes:

So I tried to adopt this "way" to deal with responsiveness.
At least for current sperimentation.

This mens that i "design" the desktop version with the UI, and apply overrides with CSS utilities only for tablet and mobile, only if it's required.


Coming back to original question, that was "how hard is to add a global CSS file, that will be loaded both in frontend, both in editor iframe", there is a better solution than what i describe in the blogpost attached in this issue.

I was not aware that add_editor_style injects css in iframe of block editor at time of writing. More https://github.com/WordPress/gutenberg/issues/48437. I created a "documentation" issue for posterity https://github.com/WordPress/gutenberg/issues/48497.