Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 3 additions & 0 deletions lib/compat/wordpress-5.9/theme.json
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,9 @@
"core/button": {
"border": {
"radius": true
},
"dimensions": {
"width": true
}
},
"core/pullquote": {
Expand Down
7 changes: 7 additions & 0 deletions packages/block-library/src/button/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@
"fontSize": true
}
},
"__experimentalDimensions": {
"width": "segmented",
"__experimentalSkipSerialization": true,
"__experimentalDefaultControls": {
"width": true
}
},
"reusable": false,
"spacing": {
"__experimentalSkipSerialization": true,
Expand Down
75 changes: 19 additions & 56 deletions packages/block-library/src/button/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,7 @@ import classnames from 'classnames';
*/
import { __ } from '@wordpress/i18n';
import { useCallback, useEffect, useState, useRef } from '@wordpress/element';
import {
Button,
ButtonGroup,
PanelBody,
TextControl,
ToolbarButton,
Popover,
} from '@wordpress/components';
import { TextControl, ToolbarButton, Popover } from '@wordpress/components';
import {
BlockControls,
InspectorControls,
Expand All @@ -30,40 +23,12 @@ import { displayShortcut, isKeyboardEvent } 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 (
<PanelBody title={ __( 'Width settings' ) }>
<ButtonGroup aria-label={ __( 'Button width' ) }>
{ [ 25, 50, 75, 100 ].map( ( widthValue ) => {
return (
<Button
key={ widthValue }
isSmall
variant={
widthValue === selectedWidth
? 'primary'
: undefined
}
onClick={ () => handleChange( widthValue ) }
>
{ widthValue }%
</Button>
);
} ) }
</ButtonGroup>
</PanelBody>
);
}
const NEW_TAB_REL = 'noreferrer noopener';

function ButtonEdit( props ) {
const {
Expand All @@ -75,14 +40,15 @@ function ButtonEdit( props ) {
mergeBlocks,
} = props;
const {
fontSize,
linkTarget,
placeholder,
rel,
style,
text,
url,
width,
} = attributes;

const onSetLinkRel = useCallback(
( value ) => {
setAttributes( { rel: value } );
Expand Down Expand Up @@ -123,9 +89,9 @@ function ButtonEdit( props ) {
const borderProps = useBorderProps( attributes );
const colorProps = useColorProps( attributes );
const spacingProps = useSpacingProps( attributes );
const widthProps = getWidthClassesAndStyles( attributes );
const ref = useRef();
const richTextRef = useRef();
const blockProps = useBlockProps( { ref, onKeyDown } );

const [ isEditingURL, setIsEditingURL ] = useState( false );
const isURLSet = !! url;
Expand All @@ -151,15 +117,18 @@ function ButtonEdit( props ) {
}
}, [ isSelected ] );

const blockProps = useBlockProps( {
ref,
onKeyDown,
className: classnames( widthProps.className, {
[ `has-custom-font-size` ]: fontSize || style?.typography?.fontSize,
} ),
style: widthProps.style,
} );

return (
<>
<div
{ ...blockProps }
className={ classnames( blockProps.className, {
[ `has-custom-width wp-block-button__width-${ width }` ]: width,
[ `has-custom-font-size` ]: blockProps.style.fontSize,
} ) }
>
<div { ...blockProps }>
<RichText
ref={ richTextRef }
aria-label={ __( 'Button text' ) }
Expand Down Expand Up @@ -246,12 +215,6 @@ function ButtonEdit( props ) {
/>
</Popover>
) }
<InspectorControls>
<WidthPanel
selectedWidth={ width }
setAttributes={ setAttributes }
/>
</InspectorControls>
<InspectorControls __experimentalGroup="advanced">
<TextControl
label={ __( 'Link rel' ) }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/**
* External dependencies
*/
import classnames from 'classnames';

export default function getWidthProps( { style } ) {
const width = style?.dimensions?.width;
const presetWidth = [ 25, 50, 75, 100 ].find(
( preset ) => `${ 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 },
};
}
27 changes: 14 additions & 13 deletions packages/block-library/src/button/save.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 (
<div { ...useBlockProps.save( { className: wrapperClasses } ) }>
<div
{ ...useBlockProps.save( {
className: wrapperClasses,
style: widthProps.style,
} ) }
>
<RichText.Content
tagName="a"
className={ buttonClasses }
Expand Down