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

SpacerControls: Set height to empty string if undefined and add deprecations for undefined height #68819

Open
wants to merge 6 commits into
base: trunk
Choose a base branch
from
2 changes: 1 addition & 1 deletion packages/block-library/src/spacer/controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ export default function SpacerControls( {
label={ __( 'Height' ) }
value={ height }
onChange={ ( nextHeight ) =>
setAttributes( { height: nextHeight } )
setAttributes( { height: nextHeight ?? '' } )
}
isResizing={ isResizing }
/>
Expand Down
83 changes: 52 additions & 31 deletions packages/block-library/src/spacer/deprecated.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,60 @@
*/
import { useBlockProps } from '@wordpress/block-editor';

const deprecated = [
{
attributes: {
height: {
type: 'number',
default: 100,
},
width: {
type: 'number',
},
const deprecatedSave = ( { attributes } ) => {
return (
<div
{ ...useBlockProps.save( {
style: {
height: attributes.height,
width: attributes.width,
},
'aria-hidden': true,
} ) }
/>
);
};

const v2 = {
attributes: {
height: {
type: 'string',
},
migrate( attributes ) {
const { height, width } = attributes;
return {
...attributes,
width: width !== undefined ? `${ width }px` : undefined,
height: height !== undefined ? `${ height }px` : undefined,
};
width: {
type: 'string',
},
save( { attributes } ) {
return (
<div
{ ...useBlockProps.save( {
style: {
height: attributes.height,
width: attributes.width,
},
'aria-hidden': true,
} ) }
/>
);
},
isEligible( { height, width } ) {
return height === undefined && width === undefined;
},
migrate( attributes ) {
return {
...attributes,
height: '',
};
},
save: deprecatedSave,
};

const v1 = {
attributes: {
height: {
type: 'number',
default: 100,
},
width: {
type: 'number',
},
},
];
migrate( attributes ) {
const { height, width } = attributes;
return {
...attributes,
width: width !== undefined ? `${ width }px` : undefined,
height: `${ height }px`,
Copy link
Contributor Author

@yogeshbhutkar yogeshbhutkar Jan 28, 2025

Choose a reason for hiding this comment

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

Thanks for the review!

The issue with the previously broken spacer blocks, which had missing heights, has been resolved using deprecation. The current implementation has been updated accordingly.

Additionally, the undefined check for the height attribute was redundant since a default value is always provided. Therefore, this check has been removed.

};
},
save: deprecatedSave,
};

export default deprecated;
export default [ v2, v1 ];
Loading