-
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
Remove smart border style setting behavior #49714
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,13 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
import type { BoxEdge, GenerateFunction, StyleDefinition } from '../../types'; | ||
import type { | ||
BoxEdge, | ||
GenerateFunction, | ||
StyleDefinition, | ||
Style, | ||
StyleOptions, | ||
} from '../../types'; | ||
import { generateRule, generateBoxRules, camelCaseJoin } from '../utils'; | ||
|
||
/** | ||
|
@@ -26,15 +32,56 @@ function createBorderGenerateFunction( path: string[] ): GenerateFunction { | |
function createBorderEdgeGenerateFunction( edge: BoxEdge ): GenerateFunction { | ||
return ( style, options ) => { | ||
return [ 'color', 'style', 'width' ].flatMap( ( key ) => { | ||
let styleWithFallback = style; | ||
if ( | ||
( style?.border?.[ edge ]?.color || | ||
style?.border?.[ edge ]?.width ) && | ||
! style?.border?.[ edge ]?.style | ||
) { | ||
styleWithFallback = { | ||
...style, | ||
border: { | ||
...style?.border, | ||
[ edge ]: { | ||
...style?.border?.[ edge ], | ||
style: 'solid', | ||
}, | ||
}, | ||
}; | ||
} | ||
const path = [ 'border', edge, key ]; | ||
return createBorderGenerateFunction( path )( style, options ); | ||
return createBorderGenerateFunction( path )( | ||
styleWithFallback, | ||
options | ||
); | ||
} ); | ||
}; | ||
} | ||
|
||
const color: StyleDefinition = { | ||
name: 'color', | ||
generate: createBorderGenerateFunction( [ 'border', 'color' ] ), | ||
generate: ( style: Style, options: StyleOptions ) => { | ||
const borderColorGenerate = createBorderGenerateFunction( [ | ||
'border', | ||
'color', | ||
] ); | ||
const borderStyleGenerate = createBorderGenerateFunction( [ | ||
'border', | ||
'style', | ||
] ); | ||
const output = borderColorGenerate( style, options ); | ||
if ( style?.border?.color && ! style?.border?.style ) { | ||
return [ | ||
...output, | ||
...borderStyleGenerate( | ||
{ border: { style: 'solid' } }, | ||
options | ||
), | ||
]; | ||
} | ||
|
||
return output; | ||
}, | ||
}; | ||
|
||
const radius: StyleDefinition = { | ||
|
@@ -60,7 +107,32 @@ const borderStyle: StyleDefinition = { | |
|
||
const width: StyleDefinition = { | ||
name: 'width', | ||
generate: createBorderGenerateFunction( [ 'border', 'width' ] ), | ||
generate: ( style: Style, options: StyleOptions ) => { | ||
const borderWidthGenerate = createBorderGenerateFunction( [ | ||
'border', | ||
'width', | ||
] ); | ||
const borderStyleGenerate = createBorderGenerateFunction( [ | ||
'border', | ||
'style', | ||
] ); | ||
const output = borderWidthGenerate( style, options ); | ||
if ( | ||
style?.border?.width && | ||
! style?.border.color && // this is only here to avoid duplication | ||
! style?.border?.style | ||
) { | ||
return [ | ||
...output, | ||
...borderStyleGenerate( | ||
{ border: { style: 'solid' } }, | ||
options | ||
), | ||
]; | ||
} | ||
|
||
return output; | ||
}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One interesting thing to note here is that the architecture of these rules is "too abstracted", Basically, I have to repeat the same fallback code because it's organized as if "one style property" = "one CSS rule output" and it shouldn't be because "style" is an abstraction and not just another way to write CSS. Anyway, I managed to make it work and the good thing is that for the frontend at least, I had to update only the style engine to make the fallback work for both global styles and block supports. |
||
}; | ||
|
||
const borderTop: StyleDefinition = { | ||
|
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 think this "declarative approach" of one style object property generates one CSS rule is showing its limit in the style generation. There's so much edge cases in this file and it keeps growing. The other point here is that block supports do the exact same thing, so the code for style generation is duplicated.
The frontend side is a bit better and I think this highlights that we should move to a similar approach in the backend. (Style generation engine applied for both block supports and theme.json style generation). Not sure how that work is going.