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

Spacer block: stop using UnitControl's deprecated unit prop #39513

Merged
merged 4 commits into from
Mar 18, 2022
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
ciampo marked this conversation as resolved.
Show resolved Hide resolved
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