From 3007db759ee55a7f02114b0f0072c826ab1c164f Mon Sep 17 00:00:00 2001 From: Mitchell Austin Date: Wed, 10 Apr 2024 15:29:20 -0700 Subject: [PATCH] =?UTF-8?q?Tweak=20block-editor=E2=80=99s=20`DimensionTool?= =?UTF-8?q?`=20to=20avoid=20updating=20tests`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dimensions-tool/width-height-tool.js | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/packages/block-editor/src/components/dimensions-tool/width-height-tool.js b/packages/block-editor/src/components/dimensions-tool/width-height-tool.js index 6a4609f9a0bf2..8d8bba539f534 100644 --- a/packages/block-editor/src/components/dimensions-tool/width-height-tool.js +++ b/packages/block-editor/src/components/dimensions-tool/width-height-tool.js @@ -60,13 +60,15 @@ export default function WidthHeightTool( { const height = value.height === 'auto' ? '' : value.height ?? ''; const onDimensionChange = ( dimension ) => ( nextDimension ) => { - const nextValue = { ...value }; - // Empty strings or undefined may be passed and both represent removing the value. - if ( ! nextDimension ) { - delete nextValue[ dimension ]; - } else { - nextValue[ dimension ] = nextDimension; - } + // Readies the nextValue by omitting the dimension to be changed. + const { [ dimension ]: existingDimensionValue, ...nextValue } = value; + // Bails when both nextDimension and its existing value are falsy. This + // can occur when a `onDeselect` callback runs after the state has + // already updated. In such cases calling onChange would be redundant. + if ( ! nextDimension && ! existingDimensionValue ) return; + + // Adds dimension only if non-falsy ('' or undefined may be passed). + if ( !! nextDimension ) nextValue[ dimension ] = nextDimension; onChange( nextValue ); };