Skip to content
Merged
Changes from 1 commit
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
Next Next commit
Memoize
  • Loading branch information
ciampo committed Mar 18, 2022
commit 32ff0d83b301d8cd8bd4b1fdce0be0538ef46fc0
17 changes: 12 additions & 5 deletions packages/block-library/src/spacer/controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ import {
PanelBody,
__experimentalUseCustomUnits as useCustomUnits,
__experimentalUnitControl as UnitControl,
__experimentalParseQuantityAndUnitFromRawValue as parseQuantityAndUnitFromRawValue,
} from '@wordpress/components';
import { useInstanceId } from '@wordpress/compose';
import { useState } from '@wordpress/element';
import { useState, useMemo } from '@wordpress/element';

/**
* Internal dependencies
Expand Down Expand Up @@ -51,7 +52,15 @@ function DimensionInput( { label, onChange, isResizing, value = '' } ) {
}
};

const inputValue = temporaryInput !== null ? temporaryInput : value;
const computedValue = useMemo( () => {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-blocking: Just a hunch, but to me parseQuantityAndUnitFromRawValue() does not seem computationally taxing enough to offset the overhead of a useMemo. Not unless we have to loop through hundreds or maybe thousands of allowed units. (Not saying this warrants a perf benchmark, but just throwing it out there in case you were on the fence 😄)

Copy link
Contributor Author

@ciampo ciampo Mar 18, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, it does feel like a bit of an earlier optimisation — I went ahead and removed it in 1c22f97

I'll wait for CI to ✅ and merge

const inputValue = temporaryInput !== null ? temporaryInput : value;
const [ parsedQuantity, parsedUnit ] = parseQuantityAndUnitFromRawValue(
inputValue
);

// Force the unit to update to `px` when the Spacer is being resized.
return [ parsedQuantity, isResizing ? 'px' : parsedUnit ].join( '' );
}, [ isResizing, temporaryInput, value ] );

return (
<BaseControl label={ label } id={ inputId }>
Expand All @@ -63,10 +72,8 @@ function DimensionInput( { label, onChange, isResizing, value = '' } ) {
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