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

Block variations: Add block-supports flag to add variation specific classname #61864

Open
wants to merge 41 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
6fe71ec
Block Variations: Add block-support for variation-specific classnames
ockham Jul 4, 2024
d42c4d1
Add new helpers
ockham Jul 4, 2024
1081fe4
Use new helpers
ockham Jul 8, 2024
6cd7199
Change attribute from class to className
ockham Jul 8, 2024
608bfd6
Fix variation attributes in test
ockham Jul 8, 2024
d9036b6
Fix variation attributes in test
ockham Jul 8, 2024
4de031b
Fix variation attributes in test
ockham Jul 8, 2024
b1bd36c
Remove redundant assertion
ockham Jul 8, 2024
3cb6023
Undo whitespace change
ockham Jul 8, 2024
e384ec7
Fix test
ockham Jul 8, 2024
5dce388
Fix other test
ockham Jul 8, 2024
e99838e
Fix more variation attributes in test
ockham Jul 8, 2024
cb9430a
Remove greedy flag from Regex
ockham Jul 8, 2024
b47cc1f
Update comment
ockham Jul 8, 2024
2bde392
Change test to make it more easily extensible
ockham Jul 8, 2024
1568b02
Absorb variation logic into addGeneratedClassName
ockham Jul 8, 2024
818af29
Reuse addGeneratedClassName
ockham Jul 8, 2024
b34a14e
Use shorthand
ockham Jul 8, 2024
8515de7
Simplify test
ockham Jul 8, 2024
2df5db7
More verbose schema descriptions
ockham Jul 8, 2024
d625240
Revert "Reuse addGeneratedClassName"
ockham Jul 8, 2024
faac58c
Replace slash with hyphen
ockham Jul 8, 2024
c8b9a6a
Add getBlockVariationClassName selector
ockham Jul 10, 2024
2b215d7
Use new selector
ockham Jul 10, 2024
0af2def
Update docs
ockham Jul 10, 2024
962a5ae
Missing comma
ockham Jul 10, 2024
173bef2
Add docs
ockham Jul 10, 2024
5cccab7
Change default to true
ockham Jul 10, 2024
cff8fcc
Fix variation default value in docs
ockham Jul 10, 2024
50a0044
typeof classNameSupport === 'boolean'
ockham Jul 23, 2024
0d34163
Backticks
ockham Jul 23, 2024
aaaa855
Bail early
ockham Jul 23, 2024
e819f54
Remove unnecessary export
ockham Jul 23, 2024
7cf6f8e
Add note that the getBlockDefaultClassName filter also affects variat…
ockham Jul 23, 2024
6ad4153
Remove unnecessary check for blockName
ockham Jul 23, 2024
bc3fb00
Add more notes on block default classname filter
ockham Jul 23, 2024
a37f5d3
Move description before type in schema
ockham Jul 23, 2024
25f7a71
toEqual -> toBe
ockham Sep 11, 2024
6f016b3
toEqual -> toBe
ockham Sep 11, 2024
d369a97
Use __ instead of - to append variation name
ockham Sep 12, 2024
7350ced
Missed one test
ockham Sep 17, 2024
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
Prev Previous commit
Next Next commit
Absorb variation logic into addGeneratedClassName
  • Loading branch information
ockham committed Sep 12, 2024
commit 1568b021df9cf70fb7c3bf00cf17374e6978b9f2
61 changes: 41 additions & 20 deletions packages/block-editor/src/hooks/generated-class-name.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@
* WordPress dependencies
*/
import { addFilter } from '@wordpress/hooks';
import { getBlockDefaultClassName } from '@wordpress/blocks';
import {
getBlockDefaultClassName,
getActiveBlockVariation,
} from '@wordpress/blocks';

/**
* Internal dependencies
*/
import { hasBlockClassNameSupport } from '../hooks/supports';
import {
hasBlockClassNameSupport,
hasVariationClassNameSupport,
} from '../hooks/supports';

/**
* Override props assigned to save component to inject generated className if
Expand All @@ -16,30 +22,45 @@ import { hasBlockClassNameSupport } from '../hooks/supports';
*
* @param {Object} extraProps Additional props applied to save element.
* @param {Object} blockType Block type.
* @param {Object} attributes Block attributes.
*
* @return {Object} Filtered props applied to save element.
*/
export function addGeneratedClassName( extraProps, blockType ) {
// Adding the generated className.
export function addGeneratedClassName( extraProps, blockType, attributes ) {
const generatedClassNames = [];
if ( hasBlockClassNameSupport( blockType ) ) {
if ( typeof extraProps.className === 'string' ) {
// We have some extra classes and want to add the default classname
// We use a Set to prevent duplicate classnames.

extraProps.className = [
...new Set( [
getBlockDefaultClassName( blockType.name ),
...extraProps.className.split( ' ' ),
] ),
]
.join( ' ' )
.trim();
} else {
// There is no string in the className variable,
// so we just dump the default name in there.
extraProps.className = getBlockDefaultClassName( blockType.name );
generatedClassNames.push( getBlockDefaultClassName( blockType.name ) );
}
if ( hasVariationClassNameSupport( blockType ) ) {
const activeVariation = getActiveBlockVariation(
blockType.name,
attributes
);
if ( activeVariation ) {
generatedClassNames.push(
getBlockDefaultClassName( blockType.name ) +
'-' +
activeVariation.name
);
}
}
if ( typeof extraProps.className === 'string' ) {
ockham marked this conversation as resolved.
Show resolved Hide resolved
// We have some extra classes and want to add the default classname
// We use a Set to prevent duplicate classnames.
extraProps.className = [
...new Set( [
...generatedClassNames,
...extraProps.className.split( ' ' ),
] ),
]
.join( ' ' )
.trim();
} else if ( generatedClassNames.length ) {
// There is no string in the className variable,
// so we just dump the default name(s) in there.
extraProps.className = generatedClassNames.join( ' ' );
}

return extraProps;
}

Expand Down
68 changes: 0 additions & 68 deletions packages/block-editor/src/hooks/generated-variation-class-name.js

This file was deleted.

1 change: 0 additions & 1 deletion packages/block-editor/src/hooks/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import anchor from './anchor';
import ariaLabel from './aria-label';
import customClassName from './custom-class-name';
import './generated-class-name';
import './generated-variation-class-name';
import style from './style';
import './settings';
import color from './color';
Expand Down
1 change: 0 additions & 1 deletion packages/block-editor/src/hooks/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import align from './align';
import anchor from './anchor';
import customClassName from './custom-class-name';
import './generated-class-name';
import './generated-variation-class-name';
import style from './style';
import color from './color';
import fontSize from './font-size';
Expand Down
96 changes: 96 additions & 0 deletions packages/block-editor/src/hooks/test/generated-class-name.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* WordPress dependencies
*/
import { applyFilters } from '@wordpress/hooks';
import { registerBlockType, unregisterBlockType } from '@wordpress/blocks';

/**
* Internal dependencies
Expand All @@ -16,8 +17,43 @@ describe( 'generated className', () => {
save: noop,
category: 'text',
title: 'block title',
attributes: {
fruit: {
type: 'string',
default: 'apple',
},
},
};

const variations = [
{
name: 'apple',
attributes: {
fruit: 'apple',
},
isActive: [ 'fruit' ],
isDefault: true,
},
{
name: 'banana',
attributes: {
fruit: 'banana',
},
isActive: [ 'fruit' ],
},
];

beforeAll( () => {
registerBlockType( 'produce/fruit', {
...blockSettings,
variations,
} );
} );

afterAll( () => {
unregisterBlockType( 'produce/fruit' );
} );

describe( 'addSaveProps', () => {
const addSaveProps = applyFilters.bind(
null,
Expand Down Expand Up @@ -61,5 +97,65 @@ describe( 'generated className', () => {

expect( extraProps.className ).toBe( 'wp-block-produce-fruit foo' );
} );

it( "should not inject the generated variation className if support isn't enabled", () => {
const attributes = { className: 'foo', fruit: 'banana' };
const extraProps = addSaveProps(
{},
{
...blockSettings,
variations,
supports: {
className: false,
},
},
attributes
);

expect( extraProps ).not.toHaveProperty( 'className' );
} );

it( 'should inject the generated variation className', () => {
const attributes = { className: 'bar', fruit: 'banana' };
const extraProps = addSaveProps(
{ className: 'foo' },
{
...blockSettings,
variations,
supports: {
className: {
variation: true,
},
},
},
attributes
);

expect( extraProps.className ).toBe(
'wp-block-produce-fruit-banana foo'
);
} );

it( 'should inject generated classNames for both block and variation', () => {
const attributes = { className: 'bar', fruit: 'banana' };
const extraProps = addSaveProps(
{ className: 'foo' },
{
...blockSettings,
variations,
supports: {
className: {
block: true,
variation: true,
},
},
},
attributes
);

expect( extraProps.className ).toBe(
'wp-block-produce-fruit wp-block-produce-fruit-banana foo'
);
} );
} );
} );
Loading