-
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
Allow register/unregister block style variations from theme.json
#49827
Conversation
As per the plans at #49602 it sounds like this is something we need. I wanted to gather thoughts on API and challenges before any implementation. |
Variations as an array:
{
"settings": {
"blocks": {
"core/quote": {
"variations": [
{ "name": "plain", "unregister": true }, // UNREGISTER via new field?
{ "name": "large", "label": "Large", "inlineStyle": "", "styleHandle": "", "isDefault": true }
]
}
}
}
} Variations as an object:
{
"settings": {
"blocks": {
"core/quote": {
"variations": {
"plain": false, // UNREGISTER
"large": { // REGISTER
"label": "Large", // Needs to be translated, see theme-i18n.json
"inlineStyle": "",
"styleHandle": "",
"isDefault": true
}
}
}
}
}
} |
It looks like any of the two options above will be internationalized without requiring any code changes 🥳 It's a matter of deciding which one has better ergonomics. Tested internationalization by:
"variations": {
"*":{
"label": "Style variation name"
}
}
"core/quote": {
"variations": [
{
"name": "large",
"label": "Large using array",
"inlineStyle": "",
"styleHandle": "",
"isDefault": true
}
]
},
"core/paragraph": {
"variations": {
"large": {
"label": "Large using object",
"inlineStyle": "",
"styleHandle": "",
"isDefault": true
},
"variationToUnregister": false
}
}
add_filter( 'gettext_with_context', function ( $translation, $text, $context, $domain ) {
if ( 'default' === $domain && 'Style variation name' === $context ) {
error_log( $text );
}
return $translation;
}, 10, 4 ); the expected result is that the log contains the strings to translate added in
|
Given internationalization is not a blocker, I'm inclined towards the API that declares variations as an object: {
"settings": {
"blocks": {
"core/quote": {
"variations": {
"plain": false, // UNREGISTER
"large": { // REGISTER
"label": "Large", // Needs to be translated, see theme-i18n.json
"inlineStyle": "",
"styleHandle": "",
"isDefault": true
}
}
}
}
}
} Thoughts @tellthemachines @gziolo @carolinan ? |
I think the biggest challenge here would be that both proposed APIs try to solve a similar issue differently than people do it today for all types of settings that list options: duotone, gradients, fonts, colors, shadows, etc. As of today, you need to provide the full list of overrides for existing settings, whereas here we see a try to inject more style variations or remove some that exist. Taking that into account, if we were to create a common language to apply atomic changes to all existing similar settings I would prefer the object-based notation with even more expressiveness: {
"settings": {
"blocks": {
"core/quote": {
"variations": {
"plain": false, // UNREGISTER
"large": { // REGISTER
"label": "Large", // Needs to be translated, see theme-i18n.json
"inlineStyle": "",
"styleHandle": "",
"isDefault": true
}
}
}
}
}
} Some implementation notes:
I like the idea overall, this would drastically simplify managing block styles by themes. I would only try to keep the ways of managing systems consistent between different options so here I would also consider replacing all style variations with the list of options expressed as an array like in other places. |
What will be the difference between |
Good point. Registration doesn't need to provide the CSS, we should use What about |
I wonder whether Another thing is that |
I'm not sure I get this @gziolo . When do we need to provide full list of overrides? When specifying styles for individual blocks in theme.json it works to just set a text color, or a font size, without mentioning all the other color or typography settings. I'd expect the block style variations to work similarly. From the two options, I'm more inclined towards the object, as it reads better and doesn't require us to change the current API for editing existing block style variations from theme.json. (I don't think consistency with presets is required because presets have more of a site-wide design token function, whereas these are block-level settings.) Some questions to help think this through:
|
Example for font sizes in Twenty Twenty-Three theme: The same setting in WordPress core: I'm only saying that I'm not aware of existing mechanisms for modifying the default list by adding, overriding, or deleting existing values. So either I miss something obvious or you mentioned a different type of setting. |
I have just learned about #49826 where the new feature for injecting styles into variations got documented. It uses an object with keys represented as a style variation name: It's quite interesting that you want to split it between two sections: styles and settings. |
Got it, yes, you mean the preset lists! I was thinking of the design tools/block supports settings for individual blocks. In my mind style variations are closer to block styles than to presets. |
I have to re-read anything that includes custom, variation, or style multiple times to try to understand what is really meant. |
This particular feature is called 'block styles' in most other places (the current API for it is I think 'Style variations' is only an internal name, and one that has been avoided since block variations were introduced. |
The panel label under Site Editor > Styles > Blocks is Style variations |
I'm talking about the naming used in the code, which doesn't have to match what's in the UI (though it's a bonus if it does). It's good to have a consistent API, and considering a name (block styles) was already chosen, my thinking was to keep it consistent. All the developer documentation also refers to it as block styles. When I say 'internal', I mean that some gutenberg contributors call it that. |
So, naming. This ship already sailed: block style variations landed in 6.2 as
Right. It's somehow the same as naming: we could have added block styles variations in a single place, in settings. That ship already sailed: Taking a step back to visualize the whole proposal so far: {
"settings": {
"blocks": {
"core/quote": {
"variations": { /* PROPOSAL TO LAND IN 6.3 */
"plain": "false or null to unregister"
"newVariationToRegister": {
"label": "Needs to be translated, via theme-i18n.json"
"styleHandle": "what is this for? is it useful?",
"isDefault": true
}
}
}
}
}
"styles": {
"blocks": {
"core/quote":{
"variations": { /* THIS ALREADY LANDED IN 6.2 */
"color": {},
"typography": {},
"...": {}
}
}
}
}
} Grzegorz brought up that this differs from how presets work (they use an array instead of an object). One alternative to the above would be having separate keys for registering and unregistering style variations. For example: {
"settings": {
"blocks": {
"core/quote": {
"variations": [ /* PROPOSAL TO LAND IN 6.3 */
{ "name": "new-variation", "label": "New variation", "styleHandle": "...", "isDefault": true }
],
"variationsToUnregister": [ "plain" ]
}
}
}
"styles": {
"blocks": {
"core/quote":{
"variations": { /* THIS ALREADY LANDED IN 6.2 */
"color": {},
"typography": {},
"...": {}
}
}
}
}
} |
Excellent summary @oandregal. I'm happy with both approaches now that I see full picture 👍🏻 |
Same here 😅
I went with "variations" when making them editable in global styles because they're called that in #44417, and because they're analogous to theme style variations (in both cases we can select from a bunch of customisations which we can then further customise). It's a shame that block variations have the same name, as they behave differently from both these cases. On the other hand, block variations are invisible as such from a UI perspective; they're only known as variations in the block API. I also think using just "styles" would be super confusing in theme.json because we'd end up with a nested structure like:
I'm not sure how clear it would be to call them "styles" in the UI either, it's already confusing enough to have global styles referred to as just "styles", and now we have "styles" and "settings" inside the "settings" sidebar too 😄 |
Just a note that the existing API for editing variations has the same shape as the proposed object API:
as it's not clear from the example above. Which makes me wonder if the new API would be like:
will the styles sit at the same level as the metadata? |
I think the style handle should be kept. I don't find it so useful personally but I think developers will expect all the different registrations method to match. |
My current view is that styles should be provided via An alternative would be to allow providing structured styles in both places, though I'd recommend against that. What do you think @tellthemachines? |
So, I've reviewed the docs, the PR that first introduced block style variations and the PR that updated them to consider loading styles per-block. It seems to me that we don't need |
OK. I've updated the PR description with the current API proposal and other notes. I'll wait one day or two before starting implementation in case there is any other feedback to address. |
Ah, got it, I somehow missed that metadata was nested under settings 😅 |
Great to see this coming in. Just out of interest, how would one go about implementing this today without this work being done and merged in? |
Closing this one, because I won't have time to implement this myself. |
NO IMPLEMENTATION YET, GATHERING THOUGHTS ABOUT THE API.
Related #49602
What?
This PR explores the ability to register/unregister block style variations from
theme.json
.Why?
theme.json
.How?
Status
inline_style
andstyle_handle
: see conversation.Testing Instructions
This is not working.