Skip to content

Commit 4b851fb

Browse files
Only use new references when global styles are used. Implement global styles reference mechanism.
1 parent ae2bf55 commit 4b851fb

20 files changed

Lines changed: 122 additions & 146 deletions

lib/global-styles.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -363,10 +363,10 @@ function gutenberg_experimental_global_styles_is_site_editor() {
363363
* @return array New block editor settings
364364
*/
365365
function gutenberg_experimental_global_styles_settings( $settings ) {
366-
if (
367-
gutenberg_experimental_global_styles_has_theme_support() &&
368-
gutenberg_experimental_global_styles_is_site_editor()
369-
) {
366+
if ( ! gutenberg_experimental_global_styles_has_theme_support() ) {
367+
return $settings;
368+
}
369+
if ( gutenberg_experimental_global_styles_is_site_editor() ) {
370370
$settings['__experimentalGlobalStylesUserEntityId'] = gutenberg_experimental_global_styles_get_user_cpt_id();
371371
}
372372

packages/block-editor/src/hooks/color-panel.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export default function ColorPanel( {
1616
settings,
1717
clientId,
1818
enableContrastChecking = true,
19+
...props
1920
} ) {
2021
const { getComputedStyle, Node } = window;
2122

@@ -55,6 +56,7 @@ export default function ColorPanel( {
5556
title={ __( 'Color settings' ) }
5657
initialOpen={ false }
5758
settings={ settings }
59+
{ ...props }
5860
>
5961
{ enableContrastChecking && (
6062
<ContrastChecker

packages/block-editor/src/hooks/color.js

Lines changed: 70 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { useRef, useEffect } from '@wordpress/element';
1717
* Internal dependencies
1818
*/
1919
import {
20+
getColorClassName,
2021
getColorObjectByColorValue,
2122
getColorObjectByAttributeValues,
2223
} from '../components/colors';
@@ -51,6 +52,24 @@ function addAttributes( settings ) {
5152
return settings;
5253
}
5354

55+
// Todo: Color attributes are added so we can support themes that use color classes.
56+
// If later we remove support for color classes the attributes should be removed.
57+
// allow blocks to specify their own attribute definition with default values if needed.
58+
if ( ! settings.attributes.backgroundColor ) {
59+
Object.assign( settings.attributes, {
60+
backgroundColor: {
61+
type: 'string',
62+
},
63+
} );
64+
}
65+
if ( ! settings.attributes.textColor ) {
66+
Object.assign( settings.attributes, {
67+
textColor: {
68+
type: 'string',
69+
},
70+
} );
71+
}
72+
5473
if ( hasGradientSupport( settings ) && ! settings.attributes.gradient ) {
5574
Object.assign( settings.attributes, {
5675
gradient: {
@@ -78,15 +97,30 @@ export function addSaveProps( props, blockType, attributes ) {
7897
const hasGradient = hasGradientSupport( blockType );
7998

8099
// I'd have prefered to avoid the "style" attribute usage here
81-
const { gradient, style } = attributes;
100+
const { backgroundColor, textColor, gradient, style } = attributes;
82101

102+
const backgroundClass = getColorClassName(
103+
'background-color',
104+
backgroundColor
105+
);
83106
const gradientClass = __experimentalGetGradientClass( gradient );
84-
const newClassName = classnames( props.className, gradientClass, {
85-
'has-text-color': style?.color?.text,
86-
'has-background':
87-
style?.color?.background ||
88-
( hasGradient && ( gradient || style?.color?.gradient ) ),
89-
} );
107+
const textClass = getColorClassName( 'color', textColor );
108+
const newClassName = classnames(
109+
props.className,
110+
textClass,
111+
gradientClass,
112+
{
113+
// Don't apply the background class if there's a custom gradient
114+
[ backgroundClass ]:
115+
( ! hasGradient || ! style?.color?.gradient ) &&
116+
!! backgroundClass,
117+
'has-text-color': textColor || style?.color?.text,
118+
'has-background':
119+
backgroundColor ||
120+
style?.color?.background ||
121+
( hasGradient && ( gradient || style?.color?.gradient ) ),
122+
}
123+
);
90124
props.className = newClassName ? newClassName : undefined;
91125

92126
return props;
@@ -115,12 +149,16 @@ export function addEditProps( settings ) {
115149
return settings;
116150
}
117151

118-
const getColorFromAttributeValue = ( colors, attributeValue ) => {
119-
const attributeParsed = /var\(--wp--colors--(.*)\)/.exec( attributeValue );
152+
const getColorFromAttributeValue = (
153+
colors,
154+
namedAttributeValue,
155+
styleAttributeValue
156+
) => {
157+
const attributeParsed = /var:colors\|(.+)/.exec( styleAttributeValue );
120158
return getColorObjectByAttributeValues(
121159
colors,
122-
attributeParsed && attributeParsed[ 1 ],
123-
attributeValue
160+
namedAttributeValue || ( attributeParsed && attributeParsed[ 1 ] ),
161+
styleAttributeValue
124162
).color;
125163
};
126164
/**
@@ -132,9 +170,19 @@ const getColorFromAttributeValue = ( colors, attributeValue ) => {
132170
*/
133171
export function ColorEdit( props ) {
134172
const { name: blockName, attributes } = props;
135-
const { colors, gradients } = useSelect( ( select ) => {
136-
return select( 'core/block-editor' ).getSettings();
137-
}, [] );
173+
const { colorsSetting, colorsGlobalStyles, gradients } = useSelect(
174+
( select ) => {
175+
const settings = select( 'core/block-editor' ).getSettings();
176+
return {
177+
colorsSetting: settings.colors,
178+
colorsGlobalStyles:
179+
settings.__experimentalGlobalStylesBase?.colors,
180+
gradients: settings.gradients,
181+
};
182+
},
183+
[]
184+
);
185+
const colors = colorsGlobalStyles || colorsSetting;
138186
// Shouldn't be needed but right now the ColorGradientsPanel
139187
// can trigger both onChangeColor and onChangeBackground
140188
// synchronously causing our two callbacks to override changes
@@ -150,7 +198,7 @@ export function ColorEdit( props ) {
150198

151199
const hasGradient = hasGradientSupport( blockName );
152200

153-
const { style, gradient } = attributes;
201+
const { style, textColor, backgroundColor, gradient } = attributes;
154202
let gradientValue;
155203
if ( hasGradient && gradient ) {
156204
gradientValue = getGradientValueBySlug( gradients, gradient );
@@ -160,18 +208,21 @@ export function ColorEdit( props ) {
160208

161209
const onChangeColor = ( name ) => ( value ) => {
162210
const colorObject = getColorObjectByColorValue( colors, value );
211+
const attributeName = name + 'Color';
163212
const newStyle = {
164213
...localAttributes.current.style,
165214
color: {
166215
...localAttributes.current?.style?.color,
167216
[ name ]: colorObject?.slug
168-
? `var(--wp--colors--${ colorObject?.slug })`
217+
? colorsGlobalStyles && `var:colors|${ colorObject?.slug }`
169218
: value,
170219
},
171220
};
172221

222+
const newNamedColor = colorObject?.slug ? colorObject.slug : undefined;
173223
const newAttributes = {
174224
style: cleanEmptyObject( newStyle ),
225+
[ attributeName ]: colorsGlobalStyles ? undefined : newNamedColor,
175226
};
176227

177228
props.setAttributes( newAttributes );
@@ -220,12 +271,14 @@ export function ColorEdit( props ) {
220271
<ColorPanel
221272
enableContrastChecking={ ! gradient && ! style?.color?.gradient }
222273
clientId={ props.clientId }
274+
colors={ colors }
223275
settings={ [
224276
{
225277
label: __( 'Text Color' ),
226278
onColorChange: onChangeColor( 'text' ),
227279
colorValue: getColorFromAttributeValue(
228280
colors,
281+
textColor,
229282
style?.color?.text
230283
),
231284
},
@@ -234,6 +287,7 @@ export function ColorEdit( props ) {
234287
onColorChange: onChangeColor( 'background' ),
235288
colorValue: getColorFromAttributeValue(
236289
colors,
290+
backgroundColor,
237291
style?.color?.background
238292
),
239293
gradientValue,

packages/block-editor/src/hooks/style.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* External dependencies
33
*/
4-
import { has, get } from 'lodash';
4+
import { has, get, startsWith } from 'lodash';
55

66
/**
77
* WordPress dependencies
@@ -34,6 +34,22 @@ const typographySupportKeys = [
3434
const hasStyleSupport = ( blockType ) =>
3535
styleSupportKeys.some( ( key ) => hasBlockSupport( blockType, key ) );
3636

37+
const VARIABLE_REFERENCE_PREFIX = 'var:';
38+
const VARIABLE_PATH_SEPARATOR_TOKEN_ATTRIBUTE = '|';
39+
const VARIABLE_PATH_SEPARATOR_TOKEN_STYLE = '--';
40+
function compileStyleValue( uncompiledValue ) {
41+
if ( startsWith( uncompiledValue, VARIABLE_REFERENCE_PREFIX ) ) {
42+
const variable = uncompiledValue
43+
.slice( VARIABLE_REFERENCE_PREFIX.length )
44+
.replace(
45+
VARIABLE_PATH_SEPARATOR_TOKEN_ATTRIBUTE,
46+
VARIABLE_PATH_SEPARATOR_TOKEN_STYLE
47+
);
48+
return `var(--wp--${ variable })`;
49+
}
50+
return uncompiledValue;
51+
}
52+
3753
/**
3854
* Returns the inline styles to add depending on the style object
3955
*
@@ -52,7 +68,7 @@ export function getInlineStyles( styles = {} ) {
5268
const output = {};
5369
Object.entries( mappings ).forEach( ( [ styleKey, objectKey ] ) => {
5470
if ( has( styles, objectKey ) ) {
55-
output[ styleKey ] = get( styles, objectKey );
71+
output[ styleKey ] = compileStyleValue( get( styles, objectKey ) );
5672
}
5773
} );
5874

packages/block-library/src/columns/deprecated.js

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -39,34 +39,18 @@ function getDeprecatedLayoutColumn( originalContent ) {
3939
}
4040

4141
const migrateCustomColors = ( attributes ) => {
42-
if (
43-
! attributes.textColor &&
44-
! attributes.backgroundColor &&
45-
! attributes.customTextColor &&
46-
! attributes.customBackgroundColor
47-
) {
42+
if ( ! attributes.customTextColor && ! attributes.customBackgroundColor ) {
4843
return attributes;
4944
}
5045
const style = { color: {} };
51-
if ( attributes.textColor ) {
52-
style.color.text = `var(--wp--colors--${ attributes.textColor })`;
53-
}
5446
if ( attributes.customTextColor ) {
5547
style.color.text = attributes.customTextColor;
5648
}
57-
if ( attributes.backgroundColor ) {
58-
style.color.background = `var(--wp--colors--${ attributes.backgroundColor })`;
59-
}
6049
if ( attributes.customBackgroundColor ) {
6150
style.color.background = attributes.customBackgroundColor;
6251
}
6352
return {
64-
...omit( attributes, [
65-
'textColor',
66-
'backgroundColor',
67-
'customTextColor',
68-
'customBackgroundColor',
69-
] ),
53+
...omit( attributes, [ 'customTextColor', 'customBackgroundColor' ] ),
7054
style,
7155
};
7256
};
@@ -91,14 +75,6 @@ export default [
9175
},
9276
},
9377
migrate: migrateCustomColors,
94-
isEligible( attribute ) {
95-
return (
96-
attribute.textColor ||
97-
attribute.customTextColor ||
98-
attribute.backgroundColor ||
99-
attribute.customBackgroundColor
100-
);
101-
},
10278
save( { attributes } ) {
10379
const {
10480
verticalAlignment,

packages/block-library/src/group/deprecated.js

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,34 +17,18 @@ const migrateAttributes = ( attributes ) => {
1717
};
1818
}
1919

20-
if (
21-
! attributes.textColor &&
22-
! attributes.backgroundColor &&
23-
! attributes.customTextColor &&
24-
! attributes.customBackgroundColor
25-
) {
20+
if ( ! attributes.customTextColor && ! attributes.customBackgroundColor ) {
2621
return attributes;
2722
}
2823
const style = { color: {} };
29-
if ( attributes.textColor ) {
30-
style.color.text = `var(--wp--colors--${ attributes.textColor })`;
31-
}
3224
if ( attributes.customTextColor ) {
3325
style.color.text = attributes.customTextColor;
3426
}
35-
if ( attributes.backgroundColor ) {
36-
style.color.background = `var(--wp--colors--${ attributes.backgroundColor })`;
37-
}
3827
if ( attributes.customBackgroundColor ) {
3928
style.color.background = attributes.customBackgroundColor;
4029
}
4130
return {
42-
...omit( attributes, [
43-
'textColor',
44-
'backgroundColor',
45-
'customTextColor',
46-
'customBackgroundColor',
47-
] ),
31+
...omit( attributes, [ 'customTextColor', 'customBackgroundColor' ] ),
4832
style,
4933
};
5034
};
@@ -71,14 +55,6 @@ const deprecated = [
7155
anchor: true,
7256
html: false,
7357
},
74-
isEligible( attribute ) {
75-
return (
76-
attribute.textColor ||
77-
attribute.customTextColor ||
78-
attribute.backgroundColor ||
79-
attribute.customBackgroundColor
80-
);
81-
},
8258
migrate: migrateAttributes,
8359
save( { attributes } ) {
8460
const {

packages/block-library/src/heading/deprecated.js

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,16 @@ const blockAttributes = {
3434
};
3535

3636
const migrateCustomColors = ( attributes ) => {
37-
if ( ! attributes.textColor && ! attributes.customTextColor ) {
37+
if ( ! attributes.customTextColor ) {
3838
return attributes;
3939
}
4040
const style = {
4141
color: {
42-
text: attributes.textColor
43-
? `var(--wp--colors--${ attributes.textColor })`
44-
: attributes.customTextColor,
42+
text: attributes.customTextColor,
4543
},
4644
};
47-
4845
return {
49-
...omit( attributes, [ 'textColor', 'customTextColor' ] ),
46+
...omit( attributes, [ 'customTextColor' ] ),
5047
style,
5148
};
5249
};
@@ -64,9 +61,6 @@ const deprecated = [
6461
},
6562
},
6663
migrate: migrateCustomColors,
67-
isEligible( attribute ) {
68-
return attribute.textColor || attribute.customTextColor;
69-
},
7064
save( { attributes } ) {
7165
const {
7266
align,

0 commit comments

Comments
 (0)