Skip to content

Commit

Permalink
Generate classes from preset slugs in the same way (server & client) (#…
Browse files Browse the repository at this point in the history
…32352)

Other than making sure that the class doesn't contain spaces we no longer do any processing
in the client (camelCase to kebab-case, separate numbers as a different kebab word, etc).
  • Loading branch information
nosolosw authored and youknowriad committed Jun 7, 2021
1 parent 1c7337e commit 39db7d4
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 12 deletions.
6 changes: 5 additions & 1 deletion lib/class-wp-theme-json-gutenberg.php
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,11 @@ private static function compute_preset_classes( $settings, $selector ) {
foreach ( $values as $value ) {
foreach ( $preset['classes'] as $class ) {
$stylesheet .= self::to_ruleset(
self::append_to_selector( $selector, '.has-' . $value['slug'] . '-' . $class['class_suffix'] ),
// We don't want to use kebabCase here,
// see https://github.com/WordPress/gutenberg/issues/32347
// However, we need to make sure the generated class
// doesn't contain spaces.
self::append_to_selector( $selector, '.has-' . preg_replace( '/\s+/', '-', $value['slug'] ) . '-' . $class['class_suffix'] ),
array(
array(
'name' => $class['property_name'],
Expand Down
2 changes: 1 addition & 1 deletion packages/block-editor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ _Parameters_

_Returns_

- `string`: String with the class corresponding to the fontSize passed. The class is generated by appending 'has-' followed by fontSizeSlug in kebabCase and ending with '-font-size'.
- `string`: String with the class corresponding to the fontSize passed. The class is generated by appending 'has-' followed by fontSizeSlug and ending with '-font-size'.

<a name="getFontSizeObjectByValue" href="#getFontSizeObjectByValue">#</a> **getFontSizeObjectByValue**

Expand Down
8 changes: 4 additions & 4 deletions packages/block-editor/src/components/colors/test/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,10 @@ describe( 'color utils', () => {
).toBeUndefined();
} );

it( 'should return a class name with the color slug in kebab case', () => {
expect( getColorClassName( 'background', 'Light Purple' ) ).toBe(
'has-light-purple-background'
);
it( 'should return a class name with the color slug without spaces', () => {
expect(
getColorClassName( 'background', 'Light Purple veryDark' )
).toBe( 'has-Light-Purple-veryDark-background' );
} );
} );
} );
11 changes: 9 additions & 2 deletions packages/block-editor/src/components/colors/utils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { find, kebabCase, map } from 'lodash';
import { find, map } from 'lodash';
import tinycolor from 'tinycolor2';

/**
Expand Down Expand Up @@ -60,7 +60,14 @@ export function getColorClassName( colorContextName, colorSlug ) {
return undefined;
}

return `has-${ kebabCase( colorSlug ) }-${ colorContextName }`;
// We don't want to use kebabCase from lodash here
// see https://github.com/WordPress/gutenberg/issues/32347
// However, we need to make sure the generated class
// doesn't contain spaces.
return `has-${ colorSlug.replace(
/\s+/g,
'-'
) }-${ colorContextName.replace( /\s+/g, '-' ) }`;
}

/**
Expand Down
10 changes: 7 additions & 3 deletions packages/block-editor/src/components/font-sizes/utils.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { find, kebabCase } from 'lodash';
import { find } from 'lodash';

/**
* Returns the font size object based on an array of named font sizes and the namedFontSize and customFontSize values.
Expand Down Expand Up @@ -55,12 +55,16 @@ export function getFontSizeObjectByValue( fontSizes, value ) {
* @param {string} fontSizeSlug Slug of the fontSize.
*
* @return {string} String with the class corresponding to the fontSize passed.
* The class is generated by appending 'has-' followed by fontSizeSlug in kebabCase and ending with '-font-size'.
* The class is generated by appending 'has-' followed by fontSizeSlug and ending with '-font-size'.
*/
export function getFontSizeClass( fontSizeSlug ) {
if ( ! fontSizeSlug ) {
return;
}

return `has-${ kebabCase( fontSizeSlug ) }-font-size`;
// We don't want to use kebabCase from lodash here
// see https://github.com/WordPress/gutenberg/issues/32347
// However, we need to make sure the generated class
// doesn't contain spaces.
return `has-${ fontSizeSlug.replace( /\s+/g, '-' ) }-font-size`;
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,14 @@ function getPresetsClasses( blockSelector, blockPresets = {} ) {
classes.forEach( ( { classSuffix, propertyName } ) => {
const slug = preset.slug;
const value = preset[ valueKey ];
const classSelectorToUse = `.has-${ slug }-${ classSuffix }`;
// We don't want to use kebabCase from lodash here
// see https://github.com/WordPress/gutenberg/issues/32347
// However, we need to make sure the generated class
// doesn't contain spaces.
const classSelectorToUse = `.has-${ slug.replace(
/\s+/g,
'-'
) }-${ classSuffix }`;
const selectorToUse = `${ blockSelector }${ classSelectorToUse }`;
declarations += `${ selectorToUse }{${ propertyName }: ${ value } !important;}`;
} );
Expand Down

0 comments on commit 39db7d4

Please sign in to comment.