diff --git a/lib/theme.json b/lib/theme.json
index f13bd4a9e21c92..a9a339bc627d33 100644
--- a/lib/theme.json
+++ b/lib/theme.json
@@ -229,6 +229,9 @@
"core/button": {
"border": {
"customRadius": true
+ },
+ "dimensions": {
+ "customWidth": true
}
},
"core/pullquote": {
diff --git a/packages/block-library/src/button/block.json b/packages/block-library/src/button/block.json
index d954cc3c40e991..49a51d86b88659 100644
--- a/packages/block-library/src/button/block.json
+++ b/packages/block-library/src/button/block.json
@@ -65,6 +65,13 @@
"fontSize": true,
"__experimentalFontFamily": true
},
+ "__experimentalDimensions": {
+ "width": "segmented",
+ "__experimentalSkipSerialization": true,
+ "__experimentalDefaultControls": {
+ "width": true
+ }
+ },
"reusable": false,
"spacing": {
"__experimentalSkipSerialization": true,
diff --git a/packages/block-library/src/button/edit.js b/packages/block-library/src/button/edit.js
index c0f80bc40ae0d2..b46f5361fc765a 100644
--- a/packages/block-library/src/button/edit.js
+++ b/packages/block-library/src/button/edit.js
@@ -9,17 +9,13 @@ import classnames from 'classnames';
import { __ } from '@wordpress/i18n';
import { useCallback, useState, useRef } from '@wordpress/element';
import {
- Button,
- ButtonGroup,
KeyboardShortcuts,
- PanelBody,
TextControl,
ToolbarButton,
Popover,
} from '@wordpress/components';
import {
BlockControls,
- InspectorControls,
InspectorAdvancedControls,
RichText,
useBlockProps,
@@ -32,40 +28,12 @@ import { rawShortcut, displayShortcut } from '@wordpress/keycodes';
import { link, linkOff } from '@wordpress/icons';
import { createBlock } from '@wordpress/blocks';
-const NEW_TAB_REL = 'noreferrer noopener';
-
-function WidthPanel( { selectedWidth, setAttributes } ) {
- function handleChange( newWidth ) {
- // Check if we are toggling the width off
- const width = selectedWidth === newWidth ? undefined : newWidth;
-
- // Update attributes
- setAttributes( { width } );
- }
+/**
+ * Internal dependencies
+ */
+import getWidthClassesAndStyles from './get-width-classes-and-styles';
- return (
-
-
- { [ 25, 50, 75, 100 ].map( ( widthValue ) => {
- return (
-
- );
- } ) }
-
-
- );
-}
+const NEW_TAB_REL = 'noreferrer noopener';
function URLPicker( {
isSelected,
@@ -159,14 +127,15 @@ function ButtonEdit( props ) {
mergeBlocks,
} = props;
const {
+ fontSize,
linkTarget,
placeholder,
rel,
style,
text,
url,
- width,
} = attributes;
+
const onSetLinkRel = useCallback(
( value ) => {
setAttributes( { rel: value } );
@@ -201,18 +170,20 @@ function ButtonEdit( props ) {
const borderProps = useBorderProps( attributes );
const colorProps = useColorProps( attributes );
const spacingProps = useSpacingProps( attributes );
+ const widthProps = getWidthClassesAndStyles( attributes );
const ref = useRef();
- const blockProps = useBlockProps( { ref } );
+
+ const blockProps = useBlockProps( {
+ ref,
+ className: classnames( widthProps.className, {
+ [ `has-custom-font-size` ]: fontSize || style?.typography?.fontSize,
+ } ),
+ style: widthProps.style,
+ } );
return (
<>
-
+
-
-
-
`${ preset }%` === width
+ );
+ const customWidth = width && ! presetWidth ? width : undefined;
+
+ const className = classnames( {
+ [ `has-custom-width` ]: width,
+ [ `wp-block-button__width-${ presetWidth }` ]: presetWidth,
+ } );
+
+ return {
+ className: className || undefined,
+ style: { width: customWidth },
+ };
+}
diff --git a/packages/block-library/src/button/save.js b/packages/block-library/src/button/save.js
index 1910044f9529a5..582a3912b454e8 100644
--- a/packages/block-library/src/button/save.js
+++ b/packages/block-library/src/button/save.js
@@ -14,17 +14,13 @@ import {
__experimentalGetSpacingClassesAndStyles as getSpacingClassesAndStyles,
} from '@wordpress/block-editor';
+/**
+ * Internal dependencies
+ */
+import getWidthClassesAndStyles from './get-width-classes-and-styles';
+
export default function save( { attributes, className } ) {
- const {
- fontSize,
- linkTarget,
- rel,
- style,
- text,
- title,
- url,
- width,
- } = attributes;
+ const { fontSize, linkTarget, rel, style, text, title, url } = attributes;
if ( ! text ) {
return null;
@@ -53,13 +49,18 @@ export default function save( { attributes, className } ) {
// if it had already been assigned, for the sake of backward-compatibility.
// A title will no longer be assigned for new or updated button block links.
- const wrapperClasses = classnames( className, {
- [ `has-custom-width wp-block-button__width-${ width }` ]: width,
+ const widthProps = getWidthClassesAndStyles( attributes );
+ const wrapperClasses = classnames( className, widthProps.className, {
[ `has-custom-font-size` ]: fontSize || style?.typography?.fontSize,
} );
return (
-
+
.wp-block-button,
- &.is-content-justification-right > .wp-block-button, {
+ &.is-content-justification-right > .wp-block-button {
// Added (duplicate) specificity needed to override the default button margin.
&.wp-block-button {
margin-right: 0;