-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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 frontend #41803
Conversation
Size Change: +829 B (0%) Total Size: 1.24 MB
ℹ️ View Unchanged
|
* @param options Options object with settings to adjust how the styles are generated. | ||
* @param path An array of strings representing the path to the style value in the style object. | ||
* @param ruleKeys An array of CSS property keys and patterns. | ||
* @param individualProperties The "sides" or individual properties for which to generate rules. | ||
* | ||
* @return GeneratedCSSRule[] CSS rules. | ||
*/ | ||
export function generateBoxRules( |
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.
As a follow up, maybe this could assume the responsibility of generateRule
, similar to what we do in the backend.
@@ -11,7 +11,31 @@ export type Box< T extends BoxVariants = undefined > = { | |||
left?: CSSProperties[ T extends undefined ? 'left' : `${ T }Left` ]; | |||
}; | |||
|
|||
export type BorderIndividualProperty = 'top' | 'right' | 'bottom' | 'left'; | |||
export type BorderIndividualStyles< T extends BorderIndividualProperty > = { | |||
color?: CSSProperties[ `border${ Capitalize< string & T > }Color` ]; |
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.
My TS fu isn't great. Very glad to hear if there's a fancier way of doing this.
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.
My TS fu isn't very good, either, but this looks reasonable to me. I often find these rules a bit challenging to parse visually, but it given the sheer number of rules for each side, this reads better than the duplication of hard-coding each CSS property to me 👍
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.
Perhaps a simple inline comment with an example of the results might help people grok this quicker? Also, renaming T
to something more explanatory could be another option.
const borderLeft = { | ||
name: 'borderLeft', | ||
generate: createBorderGenerateFunction( 'left' ), | ||
}; |
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'm starting to think that we could refactor the JS style engine to use properties from a master "definitions" constant, which is basically what we do in hooks/style.js anyway using __EXPERIMENTAL_STYLE_PROPERTY
It's how we approached it in the backend.
And would allow us to store custom paths and CSS rules keys such as border-$s-color
without the need to build them on the fly.
Not sure yet. I'll test it later.
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 one again @ramonjd! This LGTM and each of the individual border properties are testing well for me in the editor, with the same markup as prior to this PR 👍
Good question about whether to bring a single canonical list of the properties into the style engine package — it could be useful to centralise that knowledge, but it's still challenging for us to attempt to reduce the duplication between JS and PHP (I'm not sure if there's all that much we can do there for now 🤔). I suppose with the JS implementation, because things are split out into a separate style properties, we've got a bit more flexibility to have each module responsible for itself, whereas in PHP, we're a bit more bound by trying to keep it all into the one class, where a single list is very useful. At any rate, I agree it's something we could explore in follow-ups.
This change looks good to me! 🎉
/** | ||
* External dependencies | ||
*/ | ||
import { camelCase, get } from 'lodash'; |
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.
There's some active work underway to remove lodash from the repo (#17025) — I'm not sure we can easily remove camelCase
from the repo just yet, but there might be a simpler alternative for get
(I've added a separate comment)
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'm not sure we can easily remove camelCase from the repo just yet
Maybe we could do it for the style engine though. We're also using _.upperFirst()
. We could kill 2 birds with one method:
const upperFirst = ( str ) => return ${ str.charAt(0).toUpperCase() + str.slice(1) };
const ruleKey = `border${ upperFirst( individualProperty ) }${ upperFirst( key ) }`;
I'll replace!
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.
Oh, I like it! And it makes sense for the style engine to have its own little utility methods like this 🙂
@@ -11,7 +11,31 @@ export type Box< T extends BoxVariants = undefined > = { | |||
left?: CSSProperties[ T extends undefined ? 'left' : `${ T }Left` ]; | |||
}; | |||
|
|||
export type BorderIndividualProperty = 'top' | 'right' | 'bottom' | 'left'; | |||
export type BorderIndividualStyles< T extends BorderIndividualProperty > = { | |||
color?: CSSProperties[ `border${ Capitalize< string & T > }Color` ]; |
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.
My TS fu isn't very good, either, but this looks reasonable to me. I often find these rules a bit challenging to parse visually, but it given the sheer number of rules for each side, this reads better than the duplication of hard-coding each CSS property to me 👍
f4a50ce
to
409a76b
Compare
Adding border style types and generator functions Refactoring generateBoxRules to accommodate various "individual" properties
Removing lodash camelCase and upperFirst in favour of own implementation + test Removing unnecessary lodash get()
409a76b
to
93abab8
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. |
What?
Adding border style generation to the style engine JS package.
Related backed PR: #40531
Why?
Carrying on with consolidating styles outlined in the tracking issue:
How?
Testing Instructions
I'm using Empty Theme to test.
Create a new post and add some blocks, such as Group or Column.
Update the border styles, and play around with individual border styles.
Here is some example block code:
Example HTML
Save the post.
Check that the editor and frontend match.
Run the tests!