-
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 themes to enqueue custom CSS variables via theme.json #25446
Changes from 6 commits
4915dc3
bc985b4
f8629a9
343864b
fdfcefa
a3c6b25
2390ca3
52a23fb
5262add
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 |
---|---|---|
|
@@ -100,6 +100,33 @@ export default ( blockData, baseTree, userTree ) => { | |
); | ||
}; | ||
|
||
const flattenTree = ( input, prefix, token ) => { | ||
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. Do we need to render the custom variables on the client? There is no UI to change the custom variables, so I guess we can just keep the ones set on the server? 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. 👍 I'll address in a follow-up PR. |
||
let result = []; | ||
Object.keys( input ).forEach( ( key ) => { | ||
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. Minor non-blocking suggestion: I guess using reduce instead of Object.keys + forEach would simplify a little bit the code here. |
||
const newKey = prefix + key.replace( '/', '-' ); | ||
const newLeaf = input[ key ]; | ||
|
||
if ( newLeaf instanceof Object ) { | ||
const newPrefix = newKey + token; | ||
result = [ | ||
...result, | ||
...flattenTree( newLeaf, newPrefix, token ), | ||
]; | ||
} else { | ||
result.push( `${ newKey }: ${ newLeaf }` ); | ||
} | ||
} ); | ||
return result; | ||
}; | ||
|
||
const getCustomDeclarations = ( blockCustom ) => { | ||
if ( Object.keys( blockCustom ).length === 0 ) { | ||
return []; | ||
} | ||
|
||
return flattenTree( blockCustom, '--wp--custom--', '--' ); | ||
}; | ||
|
||
const getBlockSelector = ( selector ) => { | ||
// Can we hook into the styles generation mechanism | ||
// so we can avoid having to increase the class specificity here | ||
|
@@ -118,6 +145,7 @@ export default ( blockData, baseTree, userTree ) => { | |
tree[ context ].styles | ||
), | ||
...getBlockPresetsDeclarations( tree[ context ].settings ), | ||
...getCustomDeclarations( tree[ context ].settings.custom ), | ||
]; | ||
if ( blockDeclarations.length > 0 ) { | ||
styles.push( | ||
|
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.
The CSS variables are like styles e.g: one can do:
Given that css variables are like styles wouldn't it make sense to set the variables together with the styles?
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.
Presets are also styles, though. I think it makes sense having presets & this custom thing together as they behave the same: name is generated based on the object shape, both generate CSS vars (so aren't attached to any style prop so won't do anything by themselves).