-
Notifications
You must be signed in to change notification settings - Fork 4.2k
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 MarginVisualizer and PaddingVisualizer #59227
Conversation
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. |
Size Change: -8 B (0%) Total Size: 1.7 MB
ℹ️ View Unchanged
|
Thanks for the PR! Great PR description. These components always seem to be going wrong in one way or another. I tested the PR, there are some cases where it's not quite working for me. Probably easiest to show in a video: Kapture.2024-02-21.at.14.30.24.mp4Top/Bottom padding does seem to work well. Left/Right padding didn't seem to update for some steps, and also seemed to have outdated values (not sure why as it's the same code as top/bottom padding, but see when I drag back to zero). Margin seems to still have an issue with outdated values. Here I'm adjusting the size of the 'Team Member' pattern as it's what I had open. Not sure if there's something specific to that pattern that causes this issue. It would be interesting to see if you can also reproduce it or if it's just me! |
Aha, looks like you found a perfect example of a block that doesn't change size if the padding changes. Good catch. Looks like I can't use |
…gVisualizer showing the *last* value
ResizeObserver doesn't work in the case where an element with a fixed width has its padding changed.
5a67442
to
77ef06c
Compare
OK I've updated this to use the rAF approach for both. Since |
Flaky tests detected in 77ef06c. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/8014279096
|
Awesome refactor. Things are working as described for me. The visualizer is updating according the custom or step slider value. 🍺 2024-02-26.16.49.55.mp4Not related to this PR, as it's on trunk as well, but I'm just wondering if the custom slide controller should behave similarly. It doesn't update the visualizer. Or if it's even possible? 2024-02-26.16.56.06.mp4 |
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.
I consider this to be a good change. Works as described. Neat and tidy, like my desk isn't.
Thank you!
Consider my last comment to be an observation and note for possible investigation.
This line implies to me that it's supposed to do that. Must be bug. One thing at a time though 😀 gutenberg/packages/block-editor/src/components/global-styles/dimensions-panel.js Line 546 in 8f61b9b
|
What, why, how?
Fixes two bugs with
MarginVisualizer
andPaddingVisualizer
. These are the overlays that appear when you hover over the Margin or Padding controls in a block's settings.1. The
MarginVisualizer
doesn't appear at all.This one's easy to fix. This line needs to use the
value
prop instead ofattributes
. It looks like it was missed during a recent refactor: #56783.2. Both
MarginVisualizer
andPaddingVisualizer
show the previous margin and padding value.The easiest way to see this bug is to increment/decrement the margin or padding slider one by one using the keyboard arrow keys. You'll note that the visualiser displays the padding of the previous value instead of the current value.
Video of bug in
trunk
:Kapture.2024-02-21.at.16.10.04.mp4
This happens because the visualiser overlay uses the
margin
andpadding
value that comes back fromgetComputedStyle
which is not updated until right before the browser paints.MarginVisualizer
andPaddingVisualizer
callgetComputedStyle
in auseEffect
whenmargin
orpadding
changes. React does not guarantee thatuseEffect
will be called after the browser paints and, in fact,useEffect
can be called before a paint when React is handling a user event (e.g. a click) or if auseLayoutEffect
in the component tree updates state (e.g. callssetState
). See facebook/react#20863 (comment).To fixedit: This doesn't work, see @talldan's comment below, so we have to use the rAF approach described below for both padding and margin.PaddingVisualizer
we can use aResizeObserver
with{ box: 'border-box' }
since this will fire when the padding changes.To fix
MarginVisualizer
is more hacky. To my knowledge there's no API likeResizeObserver
that we can use that will trigger when the computed margin changes. Instead I've used auseLayoutEffect
with two calls to rAF to ensure that we callgetComputedStyle
after the first paint but before the second paint. (This video is great if you're like me and need a refresher on how the event loop works.)Video of this branch:
Kapture.2024-02-21.at.16.24.22.mp4
Testing Instructions
Insert a block and tweak its margin and padding in the block settings. The visualiser should appear on canvas and accurately illustrate the block's margin and padding.