Skip to content

Commit c726f9b

Browse files
committed
Reverting getClassnames. It's a bigger change and should be looked at in another PR.
1 parent 1dbc3f2 commit c726f9b

File tree

12 files changed

+140
-184
lines changed

12 files changed

+140
-184
lines changed

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

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,12 @@ import { createHigherOrderComponent } from '@wordpress/compose';
1717
* Internal dependencies
1818
*/
1919
import {
20+
getColorClassName,
2021
getColorObjectByColorValue,
2122
getColorObjectByAttributeValues,
2223
} from '../components/colors';
2324
import {
25+
__experimentalGetGradientClass,
2426
getGradientValueBySlug,
2527
getGradientSlugByValue,
2628
} from '../components/gradients';
@@ -32,7 +34,6 @@ import {
3234
} from './utils';
3335
import ColorPanel from './color-panel';
3436
import useSetting from '../components/use-setting';
35-
import { getClassnames } from '@wordpress/style-engine';
3637

3738
export 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

packages/style-engine/README.md

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,18 +44,6 @@ _Returns_
4444

4545
- `string`: generated stylesheet.
4646

47-
### getClassnames
48-
49-
Returns an array of classnames.
50-
51-
_Parameters_
52-
53-
- _style_ `Style`: Style object.
54-
55-
_Returns_
56-
57-
- `string[]`: An array of classnames.
58-
5947
### getCSSRules
6048

6149
Returns a JSON representation of the generated CSS rules.

packages/style-engine/src/index.ts

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -74,21 +74,3 @@ export function getCSSRules(
7474

7575
return rules;
7676
}
77-
78-
/**
79-
* Returns an array of classnames.
80-
*
81-
* @param style Style object.
82-
*
83-
* @return An array of classnames.
84-
*/
85-
export function getClassnames( style: Style ): string[] {
86-
const classNames: string[] = [];
87-
styleDefinitions.forEach( ( definition: StyleDefinition ) => {
88-
if ( typeof definition.getClassNames === 'function' ) {
89-
classNames.push( ...definition.getClassNames( style ) );
90-
}
91-
} );
92-
93-
return [ ...new Set( classNames ) ];
94-
}

packages/style-engine/src/styles/color/background.ts

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Internal dependencies
33
*/
44
import type { Style, StyleOptions } from '../../types';
5-
import { generateRule, getSlugFromPreset } from '../utils';
5+
import { generateRule } from '../utils';
66

77
const background = {
88
name: 'background',
@@ -14,23 +14,6 @@ const background = {
1414
'backgroundColor'
1515
);
1616
},
17-
getClassNames: ( style: Style ) => {
18-
const classNames = [];
19-
const styleValue: string | undefined = style?.color?.background;
20-
21-
if ( styleValue ) {
22-
const slug = getSlugFromPreset( styleValue, 'color' );
23-
if ( slug ) {
24-
classNames.push( `has-${ slug }-background-color` );
25-
}
26-
// Primary color classes must come before the `has-text-color`,
27-
// `has-background` and `has-link-color` classes to maintain backwards
28-
// compatibility and avoid block invalidations.
29-
classNames.push( 'has-background' );
30-
}
31-
32-
return classNames;
33-
},
3417
};
3518

3619
export default background;

packages/style-engine/src/styles/color/gradient.ts

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Internal dependencies
33
*/
44
import type { Style, StyleOptions } from '../../types';
5-
import { generateRule, getSlugFromPreset } from '../utils';
5+
import { generateRule } from '../utils';
66

77
const gradient = {
88
name: 'gradient',
@@ -14,23 +14,6 @@ const gradient = {
1414
'background'
1515
);
1616
},
17-
getClassNames: ( style: Style ) => {
18-
const classNames = [];
19-
const styleValue: string | number | undefined = style?.color?.gradient;
20-
21-
if ( styleValue ) {
22-
const slug = getSlugFromPreset( styleValue, 'gradient' );
23-
if ( slug ) {
24-
classNames.push( `has-${ slug }-gradient-background` );
25-
}
26-
// Primary color classes must come before the `has-text-color`,
27-
// `has-background` and `has-link-color` classes to maintain backwards
28-
// compatibility and avoid block invalidations.
29-
classNames.push( 'has-background' );
30-
}
31-
32-
return classNames;
33-
},
3417
};
3518

3619
export default gradient;

packages/style-engine/src/styles/color/text.ts

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,13 @@
22
* Internal dependencies
33
*/
44
import type { Style, StyleOptions } from '../../types';
5-
import { generateRule, getSlugFromPreset } from '../utils';
5+
import { generateRule } from '../utils';
66

77
const text = {
88
name: 'text',
99
generate: ( style: Style, options: StyleOptions ) => {
1010
return generateRule( style, options, [ 'color', 'text' ], 'color' );
1111
},
12-
getClassNames: ( style: Style ) => {
13-
const classNames = [];
14-
const styleValue: string | undefined = style?.color?.text;
15-
if ( styleValue ) {
16-
const slug = getSlugFromPreset( styleValue, 'color' );
17-
if ( slug ) {
18-
classNames.push( `has-${ slug }-color` );
19-
}
20-
// Primary color classes must come before the `has-text-color`,
21-
// `has-background` and `has-link-color` classes to maintain backwards
22-
// compatibility and avoid block invalidations.
23-
classNames.push( 'has-text-color' );
24-
}
25-
26-
return classNames;
27-
},
2812
};
2913

3014
export default text;

packages/style-engine/src/styles/elements/index.ts

Lines changed: 0 additions & 20 deletions
This file was deleted.

packages/style-engine/src/styles/index.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,7 @@
22
* Internal dependencies
33
*/
44
import color from './color';
5-
import elements from './elements';
65
import spacing from './spacing';
76
import typography from './typography';
87

9-
export const styleDefinitions = [
10-
...color,
11-
...elements,
12-
...spacing,
13-
...typography,
14-
];
8+
export const styleDefinitions = [ ...color, ...spacing, ...typography ];

packages/style-engine/src/styles/typography/index.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,17 @@
44
import type { Style, StyleOptions } from '../../types';
55
import { generateRule } from '../utils';
66

7+
const fontFamily = {
8+
name: 'fontFamily',
9+
generate: ( style: Style, options: StyleOptions ) => {
10+
return generateRule(
11+
style,
12+
options,
13+
[ 'typography', 'fontFamily' ],
14+
'fontFamily'
15+
);
16+
},
17+
};
718
const fontSize = {
819
name: 'fontSize',
920
generate: ( style: Style, options: StyleOptions ) => {
@@ -16,8 +27,20 @@ const fontSize = {
1627
},
1728
};
1829

30+
const fontStyle = {
31+
name: 'fontStyle',
32+
generate: ( style: Style, options: StyleOptions ) => {
33+
return generateRule(
34+
style,
35+
options,
36+
[ 'typography', 'fontStyle' ],
37+
'fontStyle'
38+
);
39+
},
40+
};
41+
1942
const fontWeight = {
20-
name: 'fontSize',
43+
name: 'fontWeight',
2144
generate: ( style: Style, options: StyleOptions ) => {
2245
return generateRule(
2346
style,
@@ -77,7 +100,9 @@ const textTransform = {
77100
};
78101

79102
export default [
103+
fontFamily,
80104
fontSize,
105+
fontStyle,
81106
fontWeight,
82107
letterSpacing,
83108
lineHeight,

0 commit comments

Comments
 (0)