Skip to content

Commit

Permalink
Update global styles renderer for custom named css properties
Browse files Browse the repository at this point in the history
  • Loading branch information
aaronrobertshaw committed May 10, 2021
1 parent 51cd73e commit e416d7e
Showing 1 changed file with 45 additions and 17 deletions.
62 changes: 45 additions & 17 deletions packages/edit-site/src/components/editor/global-styles-renderer.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
/**
* External dependencies
*/
import { capitalize, get, kebabCase, reduce, startsWith } from 'lodash';
import {
capitalize,
get,
kebabCase,
reduce,
startsWith,
isString,
} from 'lodash';

/**
* WordPress dependencies
Expand Down Expand Up @@ -107,22 +114,43 @@ function getBlockStylesDeclarations( blockStyles = {} ) {
return reduce(
STYLE_PROPERTY,
( declarations, { value, properties }, key ) => {
if ( !! properties ) {
properties.forEach( ( prop ) => {
if ( ! get( blockStyles, [ ...value, prop ], false ) ) {
// Do not create a declaration
// for sub-properties that don't have any value.
return;
}
const cssProperty = key.startsWith( '--' )
? key
: kebabCase( `${ key }${ capitalize( prop ) }` );
declarations.push(
`${ cssProperty }: ${ compileStyleValue(
get( blockStyles, [ ...value, prop ] )
) }`
);
} );
const styleValue = get( blockStyles, value );

if ( !! properties && ! isString( styleValue ) ) {
if ( Array.isArray( properties ) ) {
properties.forEach( ( prop ) => {
if ( ! get( styleValue, [ prop ], false ) ) {
// Do not create a declaration
// for sub-properties that don't have any value.
return;
}
const cssProperty = key.startsWith( '--' )
? key
: kebabCase( `${ key }${ capitalize( prop ) }` );
declarations.push(
`${ cssProperty }: ${ compileStyleValue(
get( styleValue, [ prop ] )
) }`
);
} );
} else {
Object.entries( properties ).forEach( ( entry ) => {
const [ name, prop ] = entry;

if ( ! get( styleValue, [ prop ], false ) ) {
// Do not create a declaration
// for sub-properties that don't have any value.
return;
}

const cssProperty = kebabCase( name );
declarations.push(
`${ cssProperty }: ${ compileStyleValue(
get( styleValue, [ prop ] )
) }`
);
} );
}
} else if ( get( blockStyles, value, false ) ) {
const cssProperty = key.startsWith( '--' )
? key
Expand Down

0 comments on commit e416d7e

Please sign in to comment.