diff --git a/packages/block-library/src/spacer/edit.js b/packages/block-library/src/spacer/edit.js index 6d773cac5c2812..5a02543cfaf5ba 100644 --- a/packages/block-library/src/spacer/edit.js +++ b/packages/block-library/src/spacer/edit.js @@ -6,7 +6,7 @@ import classnames from 'classnames'; /** * WordPress dependencies */ -import { Fragment } from '@wordpress/element'; +import { Fragment, useState } from '@wordpress/element'; import { __ } from '@wordpress/i18n'; import { InspectorControls } from '@wordpress/block-editor'; import { BaseControl, PanelBody, ResizableBox } from '@wordpress/components'; @@ -15,6 +15,7 @@ import { withInstanceId } from '@wordpress/compose'; const SpacerEdit = ( { attributes, isSelected, setAttributes, toggleSelection, instanceId } ) => { const { height } = attributes; const id = `block-spacer-height-input-${ instanceId }`; + const [ inputHeightValue, setInputHeightValue ] = useState( height ); return ( @@ -38,9 +39,11 @@ const SpacerEdit = ( { attributes, isSelected, setAttributes, toggleSelection, i topLeft: false, } } onResizeStop={ ( event, direction, elt, delta ) => { + const spacerHeight = parseInt( height + delta.height, 10 ); setAttributes( { - height: parseInt( height + delta.height, 10 ), + height: spacerHeight, } ); + setInputHeightValue( spacerHeight ); toggleSelection( true ); } } onResizeStart={ () => { @@ -54,11 +57,21 @@ const SpacerEdit = ( { attributes, isSelected, setAttributes, toggleSelection, i type="number" id={ id } onChange={ ( event ) => { + let spacerHeight = parseInt( event.target.value, 10 ); + setInputHeightValue( spacerHeight ); + if ( isNaN( spacerHeight ) ) { + // Set spacer height to default size and input box to empty string + setInputHeightValue( '' ); + spacerHeight = 100; + } else if ( spacerHeight < 20 ) { + // Set spacer height to minimum size + spacerHeight = 20; + } setAttributes( { - height: parseInt( event.target.value, 10 ), + height: spacerHeight, } ); } } - value={ height } + value={ inputHeightValue } min="20" step="10" />