Skip to content
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

Fix styles declarations returning before all properties output #42954

Merged
merged 2 commits into from
Aug 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
getBlockSelectors,
toCustomProperties,
toStyles,
getStylesDeclarations,
} from '../use-global-styles-output';
import { ROOT_BLOCK_SELECTOR } from '../utils';

Expand Down Expand Up @@ -681,4 +682,66 @@ describe( 'global styles renderer', () => {
} );
} );
} );

describe( 'getStylesDeclarations', () => {
const blockStyles = {
spacing: {
padding: {
top: '33px',
right: '33px',
bottom: '33px',
left: '33px',
},
},
color: {
background: 'var:preset|color|light-green-cyan',
},
typography: {
fontFamily: 'sans-serif',
},
};

it( 'Should output padding variables and other properties if useRootPaddingAwareAlignments is enabled', () => {
expect(
getStylesDeclarations( blockStyles, 'body', true )
).toEqual( [
'font-family: sans-serif',
'--wp--style--root--padding-top: 33px',
'--wp--style--root--padding-right: 33px',
'--wp--style--root--padding-bottom: 33px',
'--wp--style--root--padding-left: 33px',
'background-color: var(--wp--preset--color--light-green-cyan)',
] );
} );

it( 'Should output padding and other properties if useRootPaddingAwareAlignments is disabled', () => {
expect(
getStylesDeclarations( blockStyles, 'body', false )
).toEqual( [
'font-family: sans-serif',
'background-color: var(--wp--preset--color--light-green-cyan)',
'padding-top: 33px',
'padding-right: 33px',
'padding-bottom: 33px',
'padding-left: 33px',
] );
} );

it( 'Should not output padding variables if selector is not root', () => {
expect(
getStylesDeclarations(
blockStyles,
'.wp-block-button__link',
true
)
).toEqual( [
'font-family: sans-serif',
'background-color: var(--wp--preset--color--light-green-cyan)',
'padding-top: 33px',
'padding-right: 33px',
'padding-bottom: 33px',
'padding-left: 33px',
] );
} );
} );
} );
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ function flattenTree( input = {}, prefix, token ) {
*
* @return {Array} An array of style declarations.
*/
function getStylesDeclarations(
export function getStylesDeclarations(
blockStyles = {},
selector = '',
useRootPaddingAlign
Expand All @@ -211,6 +211,15 @@ function getStylesDeclarations(

const styleValue = get( blockStyles, pathToValue );

// Root-level padding styles don't currently support strings with CSS shorthand values.
// This may change: https://github.com/WordPress/gutenberg/issues/40132.
if (
key === '--wp--style--root--padding' &&
( typeof styleValue === 'string' || ! useRootPaddingAlign )
) {
return declarations;
}

if ( !! properties && typeof styleValue !== 'string' ) {
Object.entries( properties ).forEach( ( entry ) => {
const [ name, prop ] = entry;
Expand All @@ -230,13 +239,6 @@ function getStylesDeclarations(
) }`
);
} );
} else if (
key === '--wp--style--root--padding' &&
typeof styleValue === 'string'
) {
// Root-level padding styles don't currently support strings with CSS shorthand values.
// This may change: https://github.com/WordPress/gutenberg/issues/40132.
return declarations;
} else if ( get( blockStyles, pathToValue, false ) ) {
const cssProperty = key.startsWith( '--' )
? key
Expand All @@ -253,14 +255,18 @@ function getStylesDeclarations(
[]
);

if ( isRoot && useRootPaddingAlign ) {
return output;
}

// The goal is to move everything to server side generated engine styles
// This is temporary as we absorb more and more styles into the engine.
const extraRules = getCSSRules( blockStyles );
extraRules.forEach( ( rule ) => {
// Don't output padding properties if padding variables are set.
if (
isRoot &&
useRootPaddingAlign &&
rule.key.startsWith( 'padding' )
) {
return;
}
const cssProperty = rule.key.startsWith( '--' )
? rule.key
: kebabCase( rule.key );
Expand Down