@@ -8,21 +8,38 @@ import {
8
8
Button ,
9
9
} from '@wordpress/components' ;
10
10
import { __ } from '@wordpress/i18n' ;
11
+ import TokenList from '@wordpress/token-list' ;
11
12
12
13
/**
13
14
* Renders the edit component for the button block.
14
15
*
15
16
* @param {Object } props - The properties passed to the component.
16
17
* @param {Object } props.attributes - The attributes of the button block.
17
18
* @param {string } props.attributes.buttonSize - The size of the button.
19
+ * @param {string } props.attributes.className - The current classname.
18
20
* @param {Function } props.setAttributes - The function to set the attributes of the button block.
19
21
* @return {JSX.Element } The rendered edit component.
20
22
*/
21
- function Edit ( { attributes : { buttonSize } , setAttributes } ) {
23
+ function Edit ( { attributes : { buttonSize, className } , setAttributes } ) {
24
+ // Replace the current size with a new size in the existing list of classes, but only if a value for them exists.
25
+ const replaceActiveSize = ( currentSize , newSize ) => {
26
+ const currentClasses = new TokenList ( className ) ;
27
+
28
+ if ( currentSize ) {
29
+ currentClasses . remove ( `has-size-${ currentSize } ` ) ;
30
+ }
31
+
32
+ if ( newSize ) {
33
+ currentClasses . add ( `has-size-${ newSize } ` ) ;
34
+ }
35
+
36
+ return currentClasses . value ;
37
+ } ;
38
+
22
39
const handleChange = ( newSize ) => {
23
40
// Check if we are toggling the size off
24
41
const size = buttonSize === newSize ? undefined : newSize ;
25
- const buttonClass = buttonSize !== newSize ? 'has- size-' + newSize : '' ;
42
+ const buttonClass = replaceActiveSize ( buttonSize , size ) ;
26
43
27
44
// Update attributes.
28
45
setAttributes ( {
0 commit comments