-
Notifications
You must be signed in to change notification settings - Fork 4.3k
/
generated-class-name.js
45 lines (42 loc) · 1.35 KB
/
generated-class-name.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/**
* WordPress dependencies
*/
import { addFilter } from '@wordpress/hooks';
import { hasBlockSupport, getBlockDefaultClassName } from '@wordpress/blocks';
/**
* Override props assigned to save component to inject generated className if
* block supports it. This is only applied if the block's save result is an
* element and not a markup string.
*
* @param {Object} extraProps Additional props applied to save element.
* @param {Object} blockType Block type.
*
* @return {Object} Filtered props applied to save element.
*/
export function addGeneratedClassName( extraProps, blockType ) {
// Adding the generated className.
if ( hasBlockSupport( blockType, 'className', true ) ) {
if ( typeof extraProps.className === 'string' ) {
// We have some extra classes and want to add the default classname
// We use uniq 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 );
}
}
return extraProps;
}
addFilter(
'blocks.getSaveContent.extraProps',
'core/generated-class-name/save-props',
addGeneratedClassName
);