diff --git a/packages/block-editor/src/components/child-layout-control/index.js b/packages/block-editor/src/components/child-layout-control/index.js index 20791d9751bcd..861560efe1e1f 100644 --- a/packages/block-editor/src/components/child-layout-control/index.js +++ b/packages/block-editor/src/components/child-layout-control/index.js @@ -187,6 +187,7 @@ function FlexControls( { flexSize: value, } ); } } + shouldPreserveUnit value={ flexSize } label={ flexResetLabel } hideLabelFromVision diff --git a/packages/block-editor/src/components/spacing-sizes-control/input-controls/spacing-input-control.js b/packages/block-editor/src/components/spacing-sizes-control/input-controls/spacing-input-control.js index f03af41bfbc00..b2970db16adc7 100644 --- a/packages/block-editor/src/components/spacing-sizes-control/input-controls/spacing-input-control.js +++ b/packages/block-editor/src/components/spacing-sizes-control/input-controls/spacing-input-control.js @@ -234,6 +234,7 @@ export default function SpacingInputControl( { hideLabelFromVision className="spacing-sizes-control__custom-value-input" size="__unstable-large" + shouldPreserveUnit onDragStart={ () => { if ( value?.charAt( 0 ) === '-' ) { setMinValue( 0 ); diff --git a/packages/components/src/unit-control/index.tsx b/packages/components/src/unit-control/index.tsx index 65e1e56cda3b3..5c976ea2b16f4 100644 --- a/packages/components/src/unit-control/index.tsx +++ b/packages/components/src/unit-control/index.tsx @@ -57,6 +57,7 @@ function UnforwardedUnitControl( value: valueProp, onFocus: onFocusProp, __shouldNotWarnDeprecated36pxSize, + shouldPreserveUnit = false, ...props } = useDeprecated36pxDefaultSizeProp( unitControlProps ); @@ -136,6 +137,18 @@ function UnforwardedUnitControl( typeof nextQuantityValue === 'undefined' || nextQuantityValue === null ) { + // Add a check if we should preserve the unit when the quantity is empty. + if ( shouldPreserveUnit ) { + const newValue = getValidParsedQuantityAndUnit( + 0, + units, + parsedQuantity, + unit + ).join( '' ); + onChangeProp?.( newValue, changeProps ); + return; + } + onChangeProp?.( '', changeProps ); return; } diff --git a/packages/components/src/unit-control/types.ts b/packages/components/src/unit-control/types.ts index 891945b422862..30752fd76bdda 100644 --- a/packages/components/src/unit-control/types.ts +++ b/packages/components/src/unit-control/types.ts @@ -114,4 +114,10 @@ export type UnitControlProps = Pick< InputControlProps, 'size' > & * @ignore */ __shouldNotWarnDeprecated36pxSize?: boolean; + /** + * Whether to preserve the unit when the value is empty. + * + * @default false + */ + shouldPreserveUnit?: boolean; };