Replies: 6 comments 6 replies
-
I was thinking... What if we have an object for the rules declarations? So each selector can have its own object which can hold all the declarations and methods needed to compile the styles. In #42043 (proof of concept PR) I implemented that, and I think we can use an implementation like that to move all the parsing/optimizing etc logic for per-selector CSS in there 🤔 |
Beta Was this translation helpful? Give feedback.
-
cc @mtias and @draganescu for a bit of perspective in case we're getting ahead of ourselves. |
Beta Was this translation helpful? Give feedback.
-
If we're storing by selector we need a way to tie back pseudo selectors to their "parent" selector. Basically ensuring that I think that there's a chance the selector might be PS: not sure why this has to be an "answer". I'm just leaving a note. |
Beta Was this translation helpful? Give feedback.
-
This might be a "note to self" more than anything. I'm also thinking the wp_style_engine_generate function, which aims to the primary public interface to the style engine to generate styles (for now 😄) should return CSS (a string) only. At the moment, because the first incarnation was heavily influenced by block supports, This seemed neat and "optimal" at the time. The majority of cases however, at least for the foreseeable future and until we look at semantic classnames, will only need CSS. Maybe if we separate concerns now, and provide a method for getting a block's CSS and one for generating any necessary classnames, it'll be easier to optimize and tinker about under the hood later, especially when we start talking about generating semantic classnames. E.g., Would that make sense? I've been staring into the abyss for too long 😱 Edit: Furthermore, I wonder if we should have a facade class in Gutenberg under The reason is because I've been testing the style engine by plugging it into global styles and have come up against a few challenges due to idiosyncratic exceptions (Hello There'll probably be more highly specific rules coming too. Should the style engine care about these? Or just provide a toolbox to generate, process and enqueue? |
Beta Was this translation helpful? Give feedback.
-
I am thinking of another approach where the editor will have 2 artifacts, HTML and CSS, not just HTML as it is now, the generated CSS can be stored in the database and then used on Front End, with that approach we will have a single implementation on the editor only avoiding another implementation on backend. Just a fleeting thought What do you think? |
Beta Was this translation helpful? Give feedback.
-
Oh, it looks like I can't 🤣 https://github.com/orgs/community/discussions/3097 |
Beta Was this translation helpful? Give feedback.
-
Related:
This discussion thread is an attempt to break down the scope of functionality in order to help determine a suitable and flexible code structure and public interface.
The outcome should be general agreement about target quality, what lives where and how the world might interact with the style engine.
We can't tell the future, so we're not looking to sculpture Michelangelo's David, rather, something we're happy to ship now that has the most potential to align with our goals.
Given that the style engine is not yet in WordPress core, but is planned for 6.1, it's a great time to sort this out! Kudos to @aristath and @andrewserong for lighting the fires.
❗ To keep things focussed, this discussion does not cover:
New discussion threads are welcome.
The usual caveat: this is a brain dump. Nothing about what I write, ever, should be set in stone.
Context
The style engine has a single purpose at the moment: to bring together where we generate styles. For more information see: https://make.wordpress.org/core/2022/06/24/block-editor-styles-initiatives-and-goals/
Down the road, the intention is that the style engine will be responsible for not only generating CSS in Gutenberg but also outputting that CSS to the page with some stuff going on in between. :)
A general example flow might be the following:
'Something, somewhere' in Gutenberg needs to convert style information to valid CSS that needs to be available on the frontend. This 'something somewhere' passes style information to the style engine, which:
a. Processing/optimization (if any)
b. Generating/compiling output-ready CSS declarations
c. Using these CSS declarations, construct CSS rules
d. Enqueuing those CSS rules as a stylesheet for printing to the page.
It's likely that only some of the above steps are needed, for instance, where a block asks for CSS declarations for an element's style attribute, we'd only fire steps 1 and 4.b.
Below is a diagram of the flow. No, I didn't make the cut for Disney 🤣
The boxes are color-coded to indicate possible class groups.
Overview of required functionality
Parsing
The initial step of parsing raw input (see below) and creating styles ready for registration/storage and, eventually, conversion to CSS.
Parsing may involve:
var(--wp--preset--color--salami)
)Store
A centralized store of all styles accessible by unique keys (mostly likely a CSS selector). Stored in memory and rebuilt every page refresh.
For example:
To store additional metadata in the future, we might elect to turn the store value into a collection.
For example, if, later down the line, we want to support CSS nesting then the selector key could be the root selector. During processing, we'd group and nest rules using this information.
To output something like:
Further grouping may be required to represent individual layers in the style hierarchy, especially if we are to support cascade layers.
In order to output something like:
Registration
One or more functions that register WordPress hook callbacks that enqueue CSS stylesheets.
We'll need the flexibility to separate and group categories of styles, for example, block support styles may need to be enqueued in a separate stylesheet to global styles.
Processing
This is where the style engine accesses and processes stored styles immediately before generating CSS and printing it to the page.
This may involve:
Processing takes place before, or as part of generation, whereby we convert parsed and/or stored values to CSS.
Generating
Contains a set of functions that generates valid CSS from parsed and/or stored values.
The style engine should be at least able to build:
margin: 10px; background-color: orange; border-radius: 2rem;
.selector { margin: 10px; background-color: orange; border-radius: 2rem; }
These methods could probably be added to the public interface for use throughout Gutenberg.
Enqueuing and printing
Contains functionality to:
Input
The style engine is opinionated in that it will manage and create styles for content created with the block editor. Therefore it should ultimately accept current block style objects.
Block styles
These are styles that are valid for blocks and elements. The style engine supports these as a minimum. Every block or element might have one or a combination of these rules.
They are defined in
WP_Theme_JSON
withinVALID_STYLES
. See: https://github.com/WordPress/gutenberg/blob/trunk/lib/compat/wordpress-6.0/class-wp-theme-json-6-0.php#L256For every block or element that requires CSS, a call to the public style engine parser/generator/other cool name function will be required.
Architecture
What do folks think? Is the flow diagram a passable representation, and enough to guide how we build out the style engine?
We don't have to come up with a final blueprint here - it'll be at least enough to gain confidence about a short-term direction that won't lead us into too many dead ends despite the lack of clarity surrounding long-term tasks and goals.
Beta Was this translation helpful? Give feedback.
All reactions