Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct border radius value in settings of Button block #29193

Closed
wants to merge 3 commits into from
Closed
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
58 changes: 41 additions & 17 deletions packages/block-library/src/button/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import classnames from 'classnames';
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { useCallback, useState } from '@wordpress/element';
import { useCallback, useState, useEffect, useRef } from '@wordpress/element';
import {
Button,
ButtonGroup,
Expand Down Expand Up @@ -41,30 +41,20 @@ import getColorAndStyleProps from './color-props';
const NEW_TAB_REL = 'noreferrer noopener';
const MIN_BORDER_RADIUS_VALUE = 0;
const MAX_BORDER_RADIUS_VALUE = 50;
const INITIAL_BORDER_RADIUS_POSITION = 5;

const EMPTY_ARRAY = [];

function BorderPanel( { borderRadius = '', setAttributes } ) {
const initialBorderRadius = borderRadius;
const setBorderRadius = useCallback(
( newBorderRadius ) => {
if ( newBorderRadius === undefined )
setAttributes( {
borderRadius: initialBorderRadius,
} );
else setAttributes( { borderRadius: newBorderRadius } );
},
[ setAttributes ]
);
function BorderPanel( { radius = '', revertRadius, setAttributes } ) {
const setBorderRadius = ( value ) =>
setAttributes( { borderRadius: value } );
return (
<PanelBody title={ __( 'Border settings' ) }>
<RangeControl
value={ borderRadius }
value={ radius }
label={ __( 'Border radius' ) }
min={ MIN_BORDER_RADIUS_VALUE }
max={ MAX_BORDER_RADIUS_VALUE }
initialPosition={ INITIAL_BORDER_RADIUS_POSITION }
initialPosition={ revertRadius }
allowReset
onChange={ setBorderRadius }
/>
Expand Down Expand Up @@ -233,6 +223,36 @@ function ButtonEdit( props ) {
const colorProps = getColorAndStyleProps( attributes, colors, true );
const blockProps = useBlockProps();

const [ computedBorderRadius, setComputedBorderRadius ] = useState();
const refBorderSettings = useRef( {
element: null,
variant: null,
} );
// Updates computedBorderRadius if the style variant has changed
useEffect( () => {
const variant = /\bis-style-[^\s]+/.exec( blockProps.className )?.[ 0 ];
const { current } = refBorderSettings;
if ( variant !== current.variant || ! computedBorderRadius ) {
const hasOwnValue = current.element.style.borderRadius !== '';
let ownValue;
if ( hasOwnValue ) {
ownValue = current.element.style.borderRadius;
current.element.style.borderRadius = '';
}
let value = current.element.ownerDocument.defaultView
.getComputedStyle( current.element )
.getPropertyValue( 'border-radius' );
// if it's a compound value (has a space) it's not suitable for the
// single input of the UI so an empty string is preferable
value = value.indexOf( ' ' ) !== -1 ? '' : parseFloat( value );
setComputedBorderRadius( value );
if ( hasOwnValue ) {
current.element.style.borderRadius = ownValue;
}
current.variant = variant;
}
}, [ blockProps.className ] );

return (
<>
<ColorEdit { ...props } />
Expand Down Expand Up @@ -271,6 +291,9 @@ function ButtonEdit( props ) {
onReplace={ onReplace }
onMerge={ mergeBlocks }
identifier="text"
ref={ ( ref ) =>
( refBorderSettings.current.element = ref )
}
/>
</div>
<URLPicker
Expand All @@ -283,7 +306,8 @@ function ButtonEdit( props ) {
/>
<InspectorControls>
<BorderPanel
borderRadius={ borderRadius }
radius={ borderRadius }
revertRadius={ computedBorderRadius }
setAttributes={ setAttributes }
/>
<WidthPanel
Expand Down