diff --git a/packages/block-editor/src/components/border-radius-control/index.js b/packages/block-editor/src/components/border-radius-control/index.js index c995c126422fd1..5db1fde499833c 100644 --- a/packages/block-editor/src/components/border-radius-control/index.js +++ b/packages/block-editor/src/components/border-radius-control/index.js @@ -28,9 +28,9 @@ const DEFAULT_VALUES = { const RANGE_CONTROL_MAX_SIZE = 8; const EMPTY_ARRAY = []; function useBorderRadiusSizes( presets ) { - const defaultSizes = presets?.radiusSizes?.default ?? EMPTY_ARRAY; - const customSizes = presets?.radiusSizes?.custom ?? EMPTY_ARRAY; - const themeSizes = presets?.radiusSizes?.theme ?? EMPTY_ARRAY; + const defaultSizes = presets?.default ?? EMPTY_ARRAY; + const customSizes = presets?.custom ?? EMPTY_ARRAY; + const themeSizes = presets?.theme ?? EMPTY_ARRAY; return useMemo( () => { const sizes = [ @@ -109,6 +109,7 @@ export default function BorderRadiusControl( { onChange, values, presets } ) { values={ values } units={ units } corner="all" + presets={ options } /> > ) : ( @@ -127,6 +128,7 @@ export default function BorderRadiusControl( { onChange, values, presets } ) { values={ values || DEFAULT_VALUES } units={ units } corner={ corner } + presets={ options } /> ) ) } diff --git a/packages/block-editor/src/components/border-radius-control/single-input-control.js b/packages/block-editor/src/components/border-radius-control/single-input-control.js index 224bee46feb696..9ae15957f05604 100644 --- a/packages/block-editor/src/components/border-radius-control/single-input-control.js +++ b/packages/block-editor/src/components/border-radius-control/single-input-control.js @@ -4,15 +4,24 @@ import { __experimentalParseQuantityAndUnitFromRawValue as parseQuantityAndUnitFromRawValue, __experimentalUnitControl as UnitControl, + __experimentalHStack as HStack, Tooltip, RangeControl, + Button, } from '@wordpress/components'; import { __ } from '@wordpress/i18n'; +import { useState } from '@wordpress/element'; +import { settings } from '@wordpress/icons'; /** * Internal dependencies */ -import { getAllValue } from './utils'; +import { + getAllValue, + getPresetValueFromControlValue, + getSliderValueFromPreset, + isValuePreset, +} from './utils'; const CORNERS = { all: __( 'Border radius' ), @@ -27,6 +36,7 @@ const MAX_BORDER_RADIUS_VALUES = { em: 20, rem: 20, }; +const RANGE_CONTROL_MAX_SIZE = 8; export default function SingleInputControl( { corner, @@ -35,6 +45,7 @@ export default function SingleInputControl( { setSelectedUnits, values: valuesProp, units, + presets, } ) { const onChangeValue = ( next ) => { if ( ! onChange ) { @@ -71,6 +82,23 @@ export default function SingleInputControl( { } setSelectedUnits( newUnits ); }; + const handleSliderChange = ( next ) => { + const val = + next !== undefined ? `${ next }${ computedUnit }` : undefined; + if ( corner === 'all' ) { + onChange( { + topLeft: val, + topRight: val, + bottomLeft: val, + bottomRight: val, + } ); + } else { + onChange( { + ...values, + [ corner ]: val, + } ); + } + }; // For shorthand style & backwards compatibility, handle flat string value. const values = @@ -92,57 +120,114 @@ export default function SingleInputControl( { const unitConfig = units && units.find( ( item ) => item.value === computedUnit ); const step = unitConfig?.step || 1; - const handleSliderChange = ( next ) => { - const val = - next !== undefined ? `${ next }${ computedUnit }` : undefined; - if ( corner === 'all' ) { - onChange( { - topLeft: val, - topRight: val, - bottomLeft: val, - bottomRight: val, - } ); - } else { - onChange( { - ...values, - [ corner ]: val, - } ); - } - }; + const [ showCustomValueControl, setShowCustomValueControl ] = useState( + value !== undefined && ! isValuePreset( value ) + ); + const showRangeControl = presets.length <= RANGE_CONTROL_MAX_SIZE; + const presetValue = getSliderValueFromPreset( value, presets ); + const rangeTooltip = ( newValue ) => + value === undefined ? undefined : presets[ newValue ]?.name; + const marks = presets + .slice( 1, presets.length - 1 ) + .map( ( _newValue, index ) => ( { + value: index + 1, + label: undefined, + } ) ); + const hasPresets = marks.length > 0; // Controls are wrapped in tooltips as visible labels aren't desired here. // Tooltip rendering also requires the UnitControl to be wrapped. See: // https://github.com/WordPress/gutenberg/pull/24966#issuecomment-685875026 return ( -