@@ -17,6 +17,7 @@ import { useRef, useEffect } from '@wordpress/element';
1717 * Internal dependencies
1818 */
1919import {
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 = / v a r \( - - w p - - c o l o r s - - ( .* ) \) / . exec ( attributeValue ) ;
152+ const getColorFromAttributeValue = (
153+ colors ,
154+ namedAttributeValue ,
155+ styleAttributeValue
156+ ) => {
157+ const attributeParsed = / v a r : c o l o r s \| ( .+ ) / . 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 */
133171export 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,
0 commit comments