Skip to content

Commit e0b9dde

Browse files
Draft adoption of width block support for button block
1 parent 3406995 commit e0b9dde

File tree

6 files changed

+68
-66
lines changed

6 files changed

+68
-66
lines changed

lib/experimental-default-theme.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,10 @@
219219
"customPadding": false,
220220
"units": [ "px", "em", "rem", "vh", "vw", "%" ]
221221
},
222+
"dimensions": {
223+
"customHeight": false,
224+
"customWidth": false
225+
},
222226
"border": {
223227
"customColor": false,
224228
"customRadius": false,
@@ -229,6 +233,9 @@
229233
"core/button": {
230234
"border": {
231235
"customRadius": true
236+
},
237+
"dimensions": {
238+
"customWidth": true
232239
}
233240
},
234241
"core/pullquote": {

packages/block-library/src/button/block.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@
6565
"fontSize": true,
6666
"__experimentalFontFamily": true
6767
},
68+
"__experimentalDimensions": {
69+
"width": "segmented",
70+
"__experimentalSkipSerialization": true,
71+
"__experimentalDefaultControls": {
72+
"width": true
73+
}
74+
},
6875
"reusable": false,
6976
"__experimentalBorder": {
7077
"radius": true,

packages/block-library/src/button/edit.js

Lines changed: 17 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,13 @@ import classnames from 'classnames';
99
import { __ } from '@wordpress/i18n';
1010
import { useCallback, useState, useRef } from '@wordpress/element';
1111
import {
12-
Button,
13-
ButtonGroup,
1412
KeyboardShortcuts,
15-
PanelBody,
1613
TextControl,
1714
ToolbarButton,
1815
Popover,
1916
} from '@wordpress/components';
2017
import {
2118
BlockControls,
22-
InspectorControls,
2319
InspectorAdvancedControls,
2420
RichText,
2521
useBlockProps,
@@ -31,40 +27,12 @@ import { rawShortcut, displayShortcut } from '@wordpress/keycodes';
3127
import { link, linkOff } from '@wordpress/icons';
3228
import { createBlock } from '@wordpress/blocks';
3329

34-
const NEW_TAB_REL = 'noreferrer noopener';
35-
36-
function WidthPanel( { selectedWidth, setAttributes } ) {
37-
function handleChange( newWidth ) {
38-
// Check if we are toggling the width off
39-
const width = selectedWidth === newWidth ? undefined : newWidth;
40-
41-
// Update attributes
42-
setAttributes( { width } );
43-
}
30+
/**
31+
* Internal dependencies
32+
*/
33+
import getWidthClassesAndStyles from './get-width-classes-and-styles';
4434

45-
return (
46-
<PanelBody title={ __( 'Width settings' ) }>
47-
<ButtonGroup aria-label={ __( 'Button width' ) }>
48-
{ [ 25, 50, 75, 100 ].map( ( widthValue ) => {
49-
return (
50-
<Button
51-
key={ widthValue }
52-
isSmall
53-
variant={
54-
widthValue === selectedWidth
55-
? 'primary'
56-
: undefined
57-
}
58-
onClick={ () => handleChange( widthValue ) }
59-
>
60-
{ widthValue }%
61-
</Button>
62-
);
63-
} ) }
64-
</ButtonGroup>
65-
</PanelBody>
66-
);
67-
}
35+
const NEW_TAB_REL = 'noreferrer noopener';
6836

6937
function URLPicker( {
7038
isSelected,
@@ -158,14 +126,15 @@ function ButtonEdit( props ) {
158126
mergeBlocks,
159127
} = props;
160128
const {
129+
fontSize,
161130
linkTarget,
162131
placeholder,
163132
rel,
164133
style,
165134
text,
166135
url,
167-
width,
168136
} = attributes;
137+
169138
const onSetLinkRel = useCallback(
170139
( value ) => {
171140
setAttributes( { rel: value } );
@@ -199,18 +168,20 @@ function ButtonEdit( props ) {
199168

200169
const borderProps = useBorderProps( attributes );
201170
const colorProps = useColorProps( attributes );
171+
const widthProps = getWidthClassesAndStyles( attributes );
202172
const ref = useRef();
203-
const blockProps = useBlockProps( { ref } );
173+
174+
const blockProps = useBlockProps( {
175+
ref,
176+
className: classnames( widthProps.className, {
177+
[ `has-custom-font-size` ]: fontSize || style?.typography?.fontSize,
178+
} ),
179+
style: widthProps.style,
180+
} );
204181

205182
return (
206183
<>
207-
<div
208-
{ ...blockProps }
209-
className={ classnames( blockProps.className, {
210-
[ `has-custom-width wp-block-button__width-${ width }` ]: width,
211-
[ `has-custom-font-size` ]: blockProps.style.fontSize,
212-
} ) }
213-
>
184+
<div { ...blockProps }>
214185
<RichText
215186
aria-label={ __( 'Button text' ) }
216187
placeholder={ placeholder || __( 'Add text…' ) }
@@ -251,12 +222,6 @@ function ButtonEdit( props ) {
251222
onToggleOpenInNewTab={ onToggleOpenInNewTab }
252223
anchorRef={ ref }
253224
/>
254-
<InspectorControls>
255-
<WidthPanel
256-
selectedWidth={ width }
257-
setAttributes={ setAttributes }
258-
/>
259-
</InspectorControls>
260225
<InspectorAdvancedControls>
261226
<TextControl
262227
label={ __( 'Link rel' ) }
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/**
2+
* External dependencies
3+
*/
4+
import classnames from 'classnames';
5+
6+
export default function getWidthProps( { style } ) {
7+
const width = style?.dimensions?.width;
8+
const presetWidth = [ 25, 50, 75, 100 ].find(
9+
( preset ) => `${ preset }%` === width
10+
);
11+
const customWidth = width && ! presetWidth ? width : undefined;
12+
13+
const className = classnames( {
14+
[ `has-custom-width` ]: width,
15+
[ `wp-block-button__width-${ presetWidth }` ]: presetWidth,
16+
} );
17+
18+
return {
19+
className: className || undefined,
20+
style: { width: customWidth },
21+
};
22+
}

packages/block-library/src/button/save.js

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,13 @@ import {
1313
__experimentalGetColorClassesAndStyles as getColorClassesAndStyles,
1414
} from '@wordpress/block-editor';
1515

16+
/**
17+
* Internal dependencies
18+
*/
19+
import getWidthClassesAndStyles from './get-width-classes-and-styles';
20+
1621
export default function save( { attributes, className } ) {
17-
const {
18-
fontSize,
19-
linkTarget,
20-
rel,
21-
style,
22-
text,
23-
title,
24-
url,
25-
width,
26-
} = attributes;
22+
const { fontSize, linkTarget, rel, style, text, title, url } = attributes;
2723

2824
if ( ! text ) {
2925
return null;
@@ -50,13 +46,18 @@ export default function save( { attributes, className } ) {
5046
// if it had already been assigned, for the sake of backward-compatibility.
5147
// A title will no longer be assigned for new or updated button block links.
5248

53-
const wrapperClasses = classnames( className, {
54-
[ `has-custom-width wp-block-button__width-${ width }` ]: width,
49+
const widthProps = getWidthClassesAndStyles( attributes );
50+
const wrapperClasses = classnames( className, widthProps.className, {
5551
[ `has-custom-font-size` ]: fontSize || style?.typography?.fontSize,
5652
} );
5753

5854
return (
59-
<div { ...useBlockProps.save( { className: wrapperClasses } ) }>
55+
<div
56+
{ ...useBlockProps.save( {
57+
className: wrapperClasses,
58+
style: widthProps.style,
59+
} ) }
60+
>
6061
<RichText.Content
6162
tagName="a"
6263
className={ buttonClasses }

packages/block-library/src/button/style.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ $blocks-block__margin: 0.5em;
7676
.wp-block-buttons {
7777

7878
> .wp-block-button,
79-
&.is-content-justification-right > .wp-block-button, {
79+
&.is-content-justification-right > .wp-block-button {
8080
// Added (duplicate) specificity needed to override the default button margin.
8181
&.wp-block-button {
8282
margin-right: 0;

0 commit comments

Comments
 (0)