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

Fix: Space visualizer showing previous value #69747

Open
wants to merge 2 commits into
base: trunk
Choose a base branch
from

Conversation

karthikeya-io
Copy link

@karthikeya-io karthikeya-io commented Mar 28, 2025

What?

Part of #63191

Why?

Fixes the issue that Margin and Padding visualizer shows the previous value. This issue doesn't occur in Safari but occurs in Chromium based browsers.

Related PR #59227

How?

Use MutationObserver to observe the changes in block.

Testing Instructions

  1. Open a post or page.
  2. Insert a block and tweak the margin or padding styles.
  3. The Space visualizer should appear accurately.

Testing Instructions for Keyboard

Same

Screenshots or screencast

Before After
space-visualizer-before.mov
space-visualizer-after.mov

Copy link

👋 Thanks for your first Pull Request and for helping build the future of Gutenberg and WordPress, @karthikeya-io! In case you missed it, we'd love to have you join us in our Slack community.

If you want to learn more about WordPress development in general, check out the Core Handbook full of helpful information.

@github-actions github-actions bot added the First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository label Mar 28, 2025
@karthikeya-io karthikeya-io marked this pull request as ready for review March 29, 2025 08:42
Copy link

github-actions bot commented Mar 29, 2025

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 props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: karthikeya-io <[email protected]>
Co-authored-by: stokesman <[email protected]>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@stokesman stokesman self-requested a review March 29, 2025 17:00
@t-hamano t-hamano added [Type] Bug An existing feature does not function as intended [Package] Block editor /packages/block-editor [Feature] Layout Layout block support, its UI controls, and style output. labels Mar 31, 2025
Copy link
Contributor

@stokesman stokesman left a comment

Choose a reason for hiding this comment

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

This is great! Thanks for working on it. I’ve experimented with some alternatives and it does seem like leveraging a MutationObserver is the best bet.

I’ve tested this and didn’t find any problems. I think it might not be necessary to observe the class attribute and just the style may suffice but I don’t think it’s likely to be critical for performance and may be helpful in some less common cases.

I’d be okay with this landing as is but I have a few suggestions.

} );
return () => {
observer.disconnect();
};
}, [ blockElement, value ] );

const previousValueRef = useRef( value );
Copy link
Contributor

Choose a reason for hiding this comment

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

The value no longer needs to be a dependency. Omitting it will prevent excessive turnover of mutation observer instances. Along with that this doesn’t need to be a layout effect. I can’t easily leave inline suggestions for what I’d like to so here’s a diff:

diff --git a/packages/block-editor/src/hooks/spacing-visualizer.js b/packages/block-editor/src/hooks/spacing-visualizer.js
index 6593a34fe8..0a058a70be 100644
--- a/packages/block-editor/src/hooks/spacing-visualizer.js
+++ b/packages/block-editor/src/hooks/spacing-visualizer.js
@@ -1,13 +1,7 @@
 /**
  * WordPress dependencies
  */
-import {
-	useState,
-	useRef,
-	useLayoutEffect,
-	useEffect,
-	useReducer,
-} from '@wordpress/element';
+import { useState, useRef, useEffect, useReducer } from '@wordpress/element';
 import isShallowEqual from '@wordpress/is-shallow-equal';
 
 /**
@@ -22,13 +16,13 @@ function SpacingVisualizer( { clientId, value, computeStyle, forceShow } ) {
 		computeStyle( blockElement )
 	);
 
-	useLayoutEffect( () => {
+	// It's not sufficient to read the block’s computed style when `value` changes because
+	// the effect would run before the block’s style has updated. Thus observing mutations
+	// to the block’s attributes is used to trigger updates to the visualizer’s styles.
+	useEffect( () => {
 		if ( ! blockElement ) {
 			return;
 		}
-		// It's not sufficient to read the computed spacing value when value.spacing changes as
-		// useEffect may run before the browser recomputes CSS. So we use a MutationObserver to track style/class changes.
-		// Fixes issue where visualizer applies previous margin/padding due to delayed style updates in Chromium based browsers.
 		const observer = new window.MutationObserver( updateStyle );
 		observer.observe( blockElement, {
 			attributes: true,
@@ -37,7 +31,7 @@ function SpacingVisualizer( { clientId, value, computeStyle, forceShow } ) {
 		return () => {
 			observer.disconnect();
 		};
-	}, [ blockElement, value ] );
+	}, [ blockElement ] );
 
 	const previousValueRef = useRef( value );
 	const [ isActive, setIsActive ] = useState( false );

Copy link
Author

Choose a reason for hiding this comment

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

Thanks for reviewing the PR. I have implemented the suggestions and updated the PR, please take a look.

@karthikeya-io karthikeya-io force-pushed the fix/spacing-visualizer branch from 9089c02 to a3b76b8 Compare April 1, 2025 07:09
@karthikeya-io karthikeya-io requested a review from stokesman April 1, 2025 07:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
[Feature] Layout Layout block support, its UI controls, and style output. First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository [Package] Block editor /packages/block-editor [Type] Bug An existing feature does not function as intended
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants