Skip to content
Merged
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
25 changes: 10 additions & 15 deletions packages/block-library/src/spacer/controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,16 @@ import {
PanelBody,
__experimentalUseCustomUnits as useCustomUnits,
__experimentalUnitControl as UnitControl,
__experimentalParseQuantityAndUnitFromRawValue as parseQuantityAndUnitFromRawValue,
} from '@wordpress/components';
import { useInstanceId } from '@wordpress/compose';
import { useState } from '@wordpress/element';

/**
* Internal dependencies
*/
import { MAX_SPACER_SIZE } from './edit';

function DimensionInput( { label, onChange, isResizing, value = '' } ) {
const [ temporaryInput, setTemporaryInput ] = useState( null );

const inputId = useInstanceId( UnitControl, 'block-spacer-height-input' );

// In most contexts the spacer size cannot meaningfully be set to a
Expand All @@ -41,17 +39,17 @@ function DimensionInput( { label, onChange, isResizing, value = '' } ) {
} );

const handleOnChange = ( unprocessedValue ) => {
setTemporaryInput( null );
onChange( unprocessedValue );
};

const handleOnBlur = () => {
if ( temporaryInput !== null ) {
setTemporaryInput( null );
}
};

const inputValue = temporaryInput !== null ? temporaryInput : value;
// Force the unit to update to `px` when the Spacer is being resized.
const [ parsedQuantity, parsedUnit ] = parseQuantityAndUnitFromRawValue(
value
);
const computedValue = [
parsedQuantity,
isResizing ? 'px' : parsedUnit,
].join( '' );

return (
<BaseControl label={ label } id={ inputId }>
Expand All @@ -60,13 +58,10 @@ function DimensionInput( { label, onChange, isResizing, value = '' } ) {
isResetValueOnUnitChange
min={ 0 }
max={ MAX_SPACER_SIZE }
onBlur={ handleOnBlur }
onChange={ handleOnChange }
style={ { maxWidth: 80 } }
value={ inputValue }
value={ computedValue }
units={ units }
// Force the unit to update to `px` when the Spacer is being resized.
unit={ isResizing ? 'px' : undefined }
/>
</BaseControl>
);
Expand Down