Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate classes from preset slugs in the same way (server & client) #32352

Merged
merged 3 commits into from
Jun 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -668,7 +668,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(
oandregal marked this conversation as resolved.
Show resolved Hide resolved
'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