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

Fixed Input Unit Block: Unit jumps to px when set to 0 #68212

Open
wants to merge 5 commits into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ function FlexControls( {
flexSize: value,
} );
} }
shouldPreserveUnit
value={ flexSize }
label={ flexResetLabel }
hideLabelFromVision
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,7 @@ export default function SpacingInputControl( {
hideLabelFromVision
className="spacing-sizes-control__custom-value-input"
size="__unstable-large"
shouldPreserveUnit
onDragStart={ () => {
if ( value?.charAt( 0 ) === '-' ) {
setMinValue( 0 );
Expand Down
7 changes: 7 additions & 0 deletions packages/components/src/unit-control/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ function UnforwardedUnitControl(
value: valueProp,
onFocus: onFocusProp,
__shouldNotWarnDeprecated36pxSize,
shouldPreserveUnit = false,
...props
} = useDeprecated36pxDefaultSizeProp( unitControlProps );

Expand Down Expand Up @@ -136,6 +137,12 @@ function UnforwardedUnitControl(
typeof nextQuantityValue === 'undefined' ||
nextQuantityValue === null
) {
// Add a check if we should preserve the unit when the quantity is empty.
if ( shouldPreserveUnit ) {
onChangeProp?.( `0${ unit }`, changeProps );
return;
}

onChangeProp?.( '', changeProps );
return;
}
Expand Down
6 changes: 6 additions & 0 deletions packages/components/src/unit-control/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,10 @@ export type UnitControlProps = Pick< InputControlProps, 'size' > &
* @ignore
*/
__shouldNotWarnDeprecated36pxSize?: boolean;
/**
* Whether to preserve the unit when the value is empty.
*
* @default false
*/
shouldPreserveUnit?: boolean;
};
Loading