-
Notifications
You must be signed in to change notification settings - Fork 4.5k
SpacerControls: default the height to 100px
if left unset
#68819
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: default the height to 100px
if left unset
#68819
Conversation
0px
when no value is provided
The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.
To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Hi @Mamaduka, could you please review this PR and share your thoughts on the proposed approach? I'd appreciate your feedback! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The idea to avoid setting height to undefined
seems right to me. 0px
seems a fine fallback and fixes the issue. Though perhaps an empty string is a hair better because height
will not be output in the inline styles. That said, clearing the spacer height and saving it is rare usage so not much of a concern 🤷.
Hi @stokesman, thanks for the review. I've updated the PR to include the suggested changes. The empty string works better and the styles aren't reflected in the inline styles. Thanks for pointing that out. |
Hi @carolinan, can I please get a review on this PR? |
@yogeshbhutkar, my suggestion would be to avoid pinging people often. Everyone is monitoring the PRs, and they'll get reviewed when folks have time. |
c7b6d47
to
2a44603
Compare
This solves the problem for newly inserted spacer blocks. |
return { | ||
...attributes, | ||
width: width !== undefined ? `${ width }px` : undefined, | ||
height: `${ height }px`, |
There was a problem hiding this comment.
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.
I am confused, why was the change to controls.js reverted? |
That change would then become redundant. The idea is that if undefined gets stored, the deprecation would run and initialize height to be an empty string. This way, the block validation error never occurs, and we don't need to repeat the logic twice. Therefore, that change was not required. What do you think about the approach? Should we also keep the change in control.js? |
I don't think the deprecation script should run unless it is strictly necessary, when the block is invalid. It would be better to prevent it from being invalid in the first place. |
1d10e3d
to
378a1da
Compare
0px
when no value is provided
Thanks for highlighting that! After reviewing the context, I completely agree and have incorporated the changes in my latest commit ✨. |
Personally, I would like the default height of 100px to be restored when the value of the Height option is deleted, but what do you think? When a user deletes the value of the Height option, I suspect that they want to restore the default value. Currently, the only way to restore the default value is to enter a value directly or operate the slider. In other words, make the following changes: diffdiff --git a/packages/block-library/src/spacer/edit.js b/packages/block-library/src/spacer/edit.js
index b2de69ad9a..b3e9103914 100644
--- a/packages/block-library/src/spacer/edit.js
+++ b/packages/block-library/src/spacer/edit.js
@@ -171,7 +171,7 @@ const SpacerEdit = ( {
if ( isFlexLayout ) {
return undefined;
}
- return temporaryHeight || getSpacingPresetCssVar( height ) || undefined;
+ return temporaryHeight || getSpacingPresetCssVar( height ) || '100px';
};
const getWidthForHorizontalBlocks = () => {
@@ -186,6 +186,8 @@ const SpacerEdit = ( {
height:
inheritedOrientation === 'horizontal'
diff --git a/packages/block-library/src/spacer/save.js b/packages/block-library/src/spacer/save.js
index dd3a595a80..a1ab58c15b 100644
--- a/packages/block-library/src/spacer/save.js
+++ b/packages/block-library/src/spacer/save.js
@@ -3,6 +3,8 @@
*/
import { useBlockProps, getSpacingPresetCssVar } from '@wordpress/block-editor';
+const DEFAULT_HEIGHT = '100px';
+
export default function save( { attributes } ) {
const { height, width, style } = attributes;
const { layout: { selfStretch } = {} } = style || {};
@@ -13,7 +15,8 @@ export default function save( { attributes } ) {
<div
{ ...useBlockProps.save( {
style: {
- height: getSpacingPresetCssVar( finalHeight ),
+ height:
+ getSpacingPresetCssVar( finalHeight ) || DEFAULT_HEIGHT,
width: getSpacingPresetCssVar( width ),
},
'aria-hidden': true, 42a4f51f34bad196e2acec115ecd5508.mp4 |
This makes more sense. A |
100px
if left unset
844927f
to
679f2e0
Compare
Hi @t-hamano, Do we also account for cases where the Diffdiff --git a/packages/block-library/src/spacer/deprecated.js b/packages/block-library/src/spacer/deprecated.js
index 79ecc09fe1..91874413e4 100644
--- a/packages/block-library/src/spacer/deprecated.js
+++ b/packages/block-library/src/spacer/deprecated.js
@@ -8,7 +8,6 @@ const deprecated = [
attributes: {
height: {
type: 'number',
- default: 100,
},
width: {
type: 'number', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the update!
Do we also account for cases where the
Spacer Block
was previouslyundefined
but should default to100px
after migration? Removing thedefault: 100
statement fromdeprecations
might easily fix this, but I’m unsure if it’s even necessary in the first place, as this scenario is quite rare.
I think this is a rare case and we don't need to deal with it. Sure, we can solve it by deleting default: 100
, but then it will no longer match the attribute definition that this block had before.
My guess is that if the save function had been properly updated in the previous PR that added the update to save.js
, the issue reported in #68817 would not have occurred in the first place. Furthermore, I expect that most users have already removed the broken spacer blocks or performed a recovery.
219cde8
to
b115447
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! Finally, can we apply DEFAULT_HEIGHT
to the following places as well?
height: '100px', |
gutenberg/packages/block-library/src/spacer/controls.js
Lines 131 to 133 in 0d5c310
hasValue={ () => height !== '100px' } | |
onDeselect={ () => | |
setAttributes( { height: '100px' } ) |
Sorry, it seems that the approach I proposed caused some backwards compatibility issues in certain scenarios. See #69414 |
Apologies for introducing the above issue. @t-hamano, since we now default to a 100px height when a spacer block’s height isn’t explicitly defined, previously saved Spacer blocks without a height attribute will trigger a validation error. To maintain backward compatibility, I propose introducing a new deprecation: const v2 = {
attributes: {
height: {
type: 'string',
},
width: {
type: 'string',
},
},
isEligible( { height } ) {
return height === undefined;
},
migrate( attributes ) {
const { height } = attributes;
return {
...attributes,
height: height !== undefined ? height : DEFAULT_HEIGHT,
};
},
save,
}; What do you think? Edit: I believe the height on flex containers should have been properly applied using |
This comment was marked as resolved.
This comment was marked as resolved.
Maybe I was mistaken, but this PR is part of Gutenberg 20.5, so it hasn’t shipped as a plugin yet and won’t be bundled with WordPress 6.8, right? cc @Mamaduka If so, it might be better to revert this PR to avoid backwards compatibility issues, and then spend some more time finding the ideal solution. |
Correct. Gutenberg 20.4 is the last full release included in WP 6.8. |
@yogeshbhutkar Can you submit a PR to revert this PR at once? Then we can take our time to work on this issue. |
…s#68819) * SpacerControls: Set default height to '0px' when no value is provided * SpacerControls: Set height to an empty string when no value is provided * fix: deprecation logic to add height if height & width missing * refactor: minimize verbosity while exporting deprecations * fix: add width property to spacer deprecation schema for v2 * fix: set height to an empty string when nextHeight is null or undefined * fix: add `100px` as the default fallback * refactor: introduce `DEFAULT_HEIGHT` constant for consistent height usage * fix: add `DEFAULT_HEIGHT` fallback for vertical flex containers * refactor: apply `DEFAULT_HEIGHT` to missing places Co-authored-by: yogeshbhutkar <yogeshbhutkar@git.wordpress.org> Co-authored-by: stokesman <presstoke@git.wordpress.org> Co-authored-by: t-hamano <wildworks@git.wordpress.org> Co-authored-by: Mamaduka <mamaduka@git.wordpress.org> Co-authored-by: carolinan <poena@git.wordpress.org> Co-authored-by: himanshupathak95 <abcd95@git.wordpress.org> Co-authored-by: Mayank-Tripathi32 <mayanktripathi32@git.wordpress.org>
What, Why and How?
Fixes: #68817
When the height of the
spacer block
is set toundefined
, on reloading the editor, aRecovery Error
is shown for theSpacer Block
.The root cause of the problem lies in the fact that the default value of
height
attribute defined inblock.json
is100px
, therefore, whenever theheight
attribute is madeundefined
, on reload, the default value kicks in and causes a mismatch in the content generated by thesave
function as well as thepost body
leading to this warning:There are two potential solutions for this problem:
Remove the default value of height attribute (
100px
) fromblock.json
.gutenberg/packages/block-library/src/spacer/block.json
Line 12 in a60dd78
Save
0px
if height isundefined
.The later approach i.e. (2) makes more sense as:
i. This is how the height slider implements it.
ii. Width follows a similar approach.
iii. It doesn't quite make sense to save the height of the
spacer
asundefined
.Testing Instructions
undefined
by trying to save an empty input.Block Recovery
error occurs.Screencast
Screen.Recording.2025-01-22.at.11.23.20.AM.mov