-
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
Add custom CSS support to elements and block style variations #49396
Closed
Closed
Changes from 12 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
d81c2ca
Add custom CSS support for theme.json elements and variations
carolinan 4a92a72
Add a not CSS selector to target the default block variation
carolinan ba23a86
Merge branch 'trunk' into add/elements-variations-custom-css
carolinan d76a4d2
Merge branch 'trunk' into add/elements-variations-custom-css
carolinan 1413f49
Update the name of the styles array
carolinan afb55a2
try a UI for block style variation CSS
carolinan 032b47c
Merge branch 'trunk' into add/elements-variations-custom-css
carolinan ea2f63a
Try to reduce merge conflicts
carolinan c605916
revert context-menu.js
carolinan bf7462d
Merge branch 'trunk' into add/elements-variations-custom-css
carolinan 0204608
Merge branch 'trunk' into add/elements-variations-custom-css
carolinan d47fff3
Merge branch 'trunk' into add/elements-variations-custom-css
carolinan 6ed3e70
Merge branch 'trunk' into add/elements-variations-custom-css
carolinan d97bbec
Merge branch 'trunk' into add/elements-variations-custom-css
carolinan 25db93f
Update class-wp-theme-json-gutenberg.php
carolinan 720ec7f
Don't include default variation styles added by theme.json
carolinan 313320a
Update class-wp-theme-json-gutenberg.php
carolinan f08f422
Update class-wp-theme-json-gutenberg.php
carolinan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1174,9 +1174,25 @@ export function useGlobalStylesOutputWithConfig( mergedConfig = {} ) { | |
}, | ||
]; | ||
|
||
// Loop through the elements to check if there are custom CSS values. | ||
// If there are, push the selector together with | ||
// the CSS value to the 'styles' array. | ||
Object.entries( ELEMENTS ).forEach( ( element ) => { | ||
const [ name, elementsSelector ] = element; | ||
if ( mergedConfig.styles.elements[ name ]?.css ) { | ||
styles.push( { | ||
css: processCSSNesting( | ||
mergedConfig.styles.elements[ name ]?.css, | ||
elementsSelector | ||
), | ||
isGlobalStyles: true, | ||
} ); | ||
} | ||
} ); | ||
|
||
// Loop through the blocks to check if there are custom CSS values. | ||
// If there are, get the block selector and push the selector together with | ||
// the CSS value to the 'stylesheets' array. | ||
// the CSS value to the 'styles' array. | ||
getBlockTypes().forEach( ( blockType ) => { | ||
if ( mergedConfig.styles.blocks[ blockType.name ]?.css ) { | ||
const selector = blockSelectors[ blockType.name ].selector; | ||
|
@@ -1188,6 +1204,26 @@ export function useGlobalStylesOutputWithConfig( mergedConfig = {} ) { | |
isGlobalStyles: true, | ||
} ); | ||
} | ||
|
||
/* CSS for block style variations */ | ||
if ( mergedConfig.styles.blocks[ blockType.name ]?.variations ) { | ||
Object.entries( | ||
mergedConfig.styles.blocks[ blockType.name ]?.variations | ||
).forEach( ( variation ) => { | ||
if ( variation[ 1 ].css ) { | ||
const variationSelector = | ||
blockSelectors[ blockType.name ] | ||
.styleVariationSelectors[ variation[ 0 ] ]; | ||
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. We should probably add a version of the "not" selector in here, otherwise styles applied to the default variation won't appear in the editor. |
||
styles.push( { | ||
css: processCSSNesting( | ||
variation[ 1 ].css, | ||
variationSelector | ||
), | ||
isGlobalStyles: true, | ||
} ); | ||
} | ||
} ); | ||
} | ||
} ); | ||
|
||
return [ styles, mergedConfig.settings ]; | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back 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.
We could simplify this and remove the loop altogether by making the rule
:not([class*="is-style-"])