@@ -17,10 +17,12 @@ import { createHigherOrderComponent } from '@wordpress/compose';
1717 * Internal dependencies
1818 */
1919import {
20+ getColorClassName ,
2021 getColorObjectByColorValue ,
2122 getColorObjectByAttributeValues ,
2223} from '../components/colors' ;
2324import {
25+ __experimentalGetGradientClass ,
2426 getGradientValueBySlug ,
2527 getGradientSlugByValue ,
2628} from '../components/gradients' ;
@@ -32,7 +34,6 @@ import {
3234} from './utils' ;
3335import ColorPanel from './color-panel' ;
3436import useSetting from '../components/use-setting' ;
35- import { getClassnames } from '@wordpress/style-engine' ;
3637
3738export const COLOR_SUPPORT_KEY = 'color' ;
3839
@@ -251,53 +252,52 @@ export function addSaveProps( props, blockType, attributes ) {
251252 return props ;
252253 }
253254
255+ const hasGradient = hasGradientSupport ( blockType ) ;
256+
254257 // I'd have preferred to avoid the "style" attribute usage here
255258 const { backgroundColor, textColor, gradient, style } = attributes ;
256259
257260 const shouldSerialize = ( feature ) =>
258261 ! shouldSkipSerialization ( blockType , COLOR_SUPPORT_KEY , feature ) ;
259262
260- const colorStyles = { } ;
261-
262263 // Primary color classes must come before the `has-text-color`,
263264 // `has-background` and `has-link-color` classes to maintain backwards
264265 // compatibility and avoid block invalidations.
265- if ( shouldSerialize ( 'text' ) ) {
266- colorStyles . text = textColor
267- ? `var:preset|color|${ textColor } `
268- : style ?. color ?. text ;
269- }
266+ const textClass = shouldSerialize ( 'text' )
267+ ? getColorClassName ( 'color' , textColor )
268+ : undefined ;
270269
271- if ( shouldSerialize ( 'gradients' ) && hasGradientSupport ( blockType ) ) {
272- colorStyles . gradient = gradient
273- ? `var:preset|gradient|${ gradient } `
274- : style ?. color ?. gradient ;
275- }
270+ const gradientClass = shouldSerialize ( 'gradients' )
271+ ? __experimentalGetGradientClass ( gradient )
272+ : undefined ;
276273
277- if ( shouldSerialize ( 'background' ) ) {
278- colorStyles . background = backgroundColor
279- ? `var:preset|color|${ backgroundColor } `
280- : style ?. color ?. background ;
281- }
274+ const backgroundClass = shouldSerialize ( 'background' )
275+ ? getColorClassName ( 'background-color' , backgroundColor )
276+ : undefined ;
277+
278+ const serializeHasBackground =
279+ shouldSerialize ( 'background' ) || shouldSerialize ( 'gradients' ) ;
280+ const hasBackground =
281+ backgroundColor ||
282+ style ?. color ?. background ||
283+ ( hasGradient && ( gradient || style ?. color ?. gradient ) ) ;
282284
283285 const newClassName = classnames (
284286 props . className ,
285- ...getClassnames ( {
286- ...style ,
287- color : {
288- ...colorStyles ,
289- } ,
290- elements : {
291- ...style ?. elements ,
292- link : {
293- color : {
294- text : shouldSerialize ( 'link' )
295- ? style ?. elements ?. link ?. color
296- : undefined ,
297- } ,
298- } ,
299- } ,
300- } )
287+ textClass ,
288+ gradientClass ,
289+ {
290+ // Don't apply the background class if there's a custom gradient.
291+ [ backgroundClass ] :
292+ ( ! hasGradient || ! style ?. color ?. gradient ) &&
293+ ! ! backgroundClass ,
294+ 'has-text-color' :
295+ shouldSerialize ( 'text' ) &&
296+ ( textColor || style ?. color ?. text ) ,
297+ 'has-background' : serializeHasBackground && hasBackground ,
298+ 'has-link-color' :
299+ shouldSerialize ( 'link' ) && style ?. elements ?. link ?. color ,
300+ }
301301 ) ;
302302 props . className = newClassName ? newClassName : undefined ;
303303
0 commit comments