-
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
Style Engine: add border to backend #40531
Style Engine: add border to backend #40531
Conversation
} | ||
|
||
foreach ( $style_value as $key => $value ) { | ||
$side_style_definition_path = array( $style_definition['path'][0], $key ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've made some assumptions both here $style_definition['path'][0]
and below $style_definition['path'][1]
.
$style_definition['path'][0]
assumes the path array's first value is the style property, e.g., border
. It also implies that border
contains the necessary definitions (color, width, etc) for the sides.
The use of $style_definition['path'][1]
assumes the path array's final item corresponds to the side name.
Both of these are fine in my opinion because nothing will work if the paths are not right, but it does seems a bit "clever" and unreadable.
A possibility is to add explicit properties to the style's metadata such as inherits
or something. 🤷
The latter
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good question about the assumptions here — just to walk through how I understand it:
$style_definition['path'][0]
will map to the parent level property that we're looking up (in this caseborder
but could theoretically be any of the other root level props of the metadata).$style_definition['path'][1]
will map to the slug for the side.
It took me a little while to read through and work out what was going on here, but I think the assumptions are good! What I'm wondering is whether we can rename things slightly to improve readability.
In the function signature the $style_definition
prop will always be the definition for the side (at least based on the props that have opted in to use this callback). Would it improve clarity to rename this prop to $side_style_definition
?
Then, where we have the existing $side_style_definition
, what if we renamed this to $style_definition
.
We could also store the ['path'][0]
and ['path'][1]
values in variables, so they've got a concrete name for what they're referring to. Putting it all together, it might look like:
protected static function get_css_side_rules( $style_value, $side_style_definition ) {
$rules = array();
if ( ! is_array( $style_value ) || empty( $style_value ) || empty( $side_style_definition['path'] ) ) {
return $rules;
}
// The first item in the style definition path array tells us the style property, e.g., "border".
// We use this to get a corresponding CSS style definition such as "color" or "width" from the same group.
$definition_group_key = $side_style_definition['path'][0];
$side = $side_style_definition['path'][1];
foreach ( $style_value as $css_property => $value ) {
if ( empty( $value ) ) {
continue;
}
$style_definition_path = array( $definition_group_key, $css_property );
$style_definition = _wp_array_get( self::BLOCK_STYLE_DEFINITIONS_METADATA, $style_definition_path, null );
if ( $style_definition && isset( $style_definition['properties']['sides'] ) ) {
// Set a CSS var if there is a valid preset value.
$slug = isset( $side_style_definition['css_vars'][ $css_property ] ) ? static::get_slug_from_preset_value( $value, $css_property ) : null;
if ( $slug ) {
$css_var = strtr(
$side_style_definition['css_vars'][ $css_property ],
array( '$slug' => $slug )
);
$value = "var($css_var)";
}
// The second item in the style definition path array refers to the side property, e.g., "top".
$side_css_property = strtr( $style_definition['properties']['sides'], array( '$side' => $side ) );
$rules[ $side_css_property ] = $value;
}
}
return $rules;
}
What do you think? It's probably a fairly small change, so I'm not sure if that improves things very much, so feel free to skip it — but typing some of that out helped me understand what's going on a bit better 😀. At the very least, this is internal behaviour to the style engine, so I think it's reasonably safe for us to go with the general approach here, and we can always refine it in follow-ups if need be, without affecting the exposed API of the engine.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is much better thank you. If it helps folks who are reading it for the first time it's definitely worth it.
373de0c
to
18b8769
Compare
This is testing really well for me @ramonjd! I left a few comments, and I'll do a little more testing tomorrow, I'll take a closer look at your comments on |
fbb5b52
to
2883d54
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work on this @ramonjd 🙇
This has tested well for me.
✅ Works well for Site Title and other dynamic blocks with generic border support
✅ No impact on existing blocks given it's limited to dynamic blocks
✅ CSS classes and styles appear the same before/after applying this branch
✅ Unit tests are all passing
Despite my lack of familiarity with the style engine to date, the changes make sense and are easy to follow with only the exception being the "side" terminology.
The "side" functionality is being used for border-radius "corners" already. As @andrewserong points out there are other non-side use cases potentially on the horizon. Can we change this to something more accurate and versatile?
Thanks for testing this PR @aaronrobertshaw, and for the tip about corners. I'll take a look at it and try to come up with a way to accommodate a broader range of properties. |
f591b77
to
62ad3fe
Compare
I went with /*
"$style_property-$single_feature: $value;", which could represent the following:
"border-{top|right|bottom|left}-{color|width|style}: {value};" or,
"border-image-{outset|source|width|repeat|slice}: {value};"
*/ Very willing to use another term if folks think it's not clear enough, e.g., I tried a few alternative data models to store the properties, but couldn't come up with anything more flexible. I guess we'll find out once we have to support more of these kinds of styles. |
I think |
Thanks for the feedback @glendaviesnz Fair call about "single". I also considered "individual". What do you think about that? Maybe unnecessarily, I kept away from long- and shorthand because, while reading up on common terms, I found them to be interchangeable with longform, shortform and often longhand wasn't used at all with a preference for shorthand to imply grouping of individual properties. Who said naming things is hard again? 😄 |
|
9887cc4
to
06a4213
Compare
8c4fe65
to
778c04b
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for wrestling with the terminology used in this one @ramonjd 👍
This is functionally still testing well for me. I've left a few minor tweaks I think we should make but after that this should be good to go 🚢
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice work @ramonjd, this is still testing well for me, too, and nothing to add that Aaron hasn't already mentioned. Once those renamings are fixed up and this has been rebased, it looks good to go to me! 🎉
Allowing for border side styles, e.g., border.top.width et. al.
fbef1a2
to
2780374
Compare
Added the Needs Dev Note label in case this needs a dev note (either individual or as part of a "misc" dev note) for WP 6.1 release. |
Tracking Issue: #38167
What?
Adds border block supports.
Builds upon
Why?
Borders are gnarly.
How?
As well as generating styles and classnames for border properties, this PR adds support for
top
,bottom
,right
andleft
block style properties, e.g.,{ "border": { "top": { "color": "#cccccc", ... }, ... } ... }
Testing Instructions
Add a Site Title block and play around with border styles in the post editor.
To make life easier, give the dynamic Site Title block all the supports!
Example Site Title block.json
And some test block code:
Block code
<!-- wp:site-title {"style":{"color":{"background":"#cf1c1c"},"typography":{"fontStyle":"italic","fontWeight":"800","letterSpacing":"32px","lineHeight":3.9,"textTransform":"uppercase"},"border":{"top":{"color":"var:preset|color|vivid-cyan-blue","width":"9px"},"right":{"color":"var:preset|color|vivid-red","width":"18px"},"bottom":{"color":"var:preset|color|vivid-cyan-blue","width":"12px"},"left":{"color":"#b82b2b","width":"83px"},"radius":{"topLeft":"20px","topRight":"0px","bottomLeft":"20px","bottomRight":"100px"}},"spacing":{"padding":{"top":"82px","right":"82px","bottom":"82px","left":"82px"},"margin":{"top":"67px","right":"67px","bottom":"67px","left":"67px"}}},"textColor":"pale-pink","fontSize":"x-large"} /-->
Unit tests
More to come...