-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Reset zoom level on component unmount #69087
base: trunk
Are you sure you want to change the base?
Reset zoom level on component unmount #69087
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. |
Do you know what part of the code disables zoomout mode for smaller screens? Controlling a global state-based component's mounted state is tricky and usually causes more bugs. This bug probably has a different root cause; I suggest finding it and fixing the issue there. |
Sure @Mamaduka, I’ll check what disables zoom-out on smaller screens and find the root cause. |
Hi @Mamaduka, The useLayoutEffect hook is triggered when the viewport width is at or above 782px. However, the condition: const isMediumOrBigger = window.matchMedia('(min-width: 782px)').matches; does not return false when the viewport width gradually decreases to exactly 782px. This is because the (min-width: 782px) media query does not include 782px itself it applies only to widths greater than 782px. As a result, when resizing the viewport slowly, the condition remains true even at 782px. However, when the viewport is resized quickly, bypassing the 782px threshold, the condition updates correctly and evaluates to false as expected. A fix to this would be to change code like: const isMediumOrBigger = window.matchMedia('(min-width: 783px)').matches; Or const isMediumOrBigger = window.width>=782; Screencast:Screen.Recording.2025-02-12.at.10.53.59.AM.movShould I open an issue about this also I am quite not sure which solution would be better. |
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 investigating this, @Infinite-Null!
I left a a couple of of notes, but unfortunately, I don't have time to test the proposed solution properly.
Pinging @WordPress/gutenberg-core in case someone has time to have a look.
@@ -17,6 +22,8 @@ export function useAdaptEditorToCanvas( canvas ) { | |||
} = useDispatch( editorStore ); | |||
const { get: getPreference } = useSelect( preferencesStore ); | |||
const registry = useRegistry(); | |||
const { resetZoomLevel } = unlock( useDispatch( blockEditorStore ) ); |
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 useDispatch( blockEditorStore )
is also defined above. Let's move and combine them in single call.
@@ -17,6 +22,8 @@ export function useAdaptEditorToCanvas( canvas ) { | |||
} = useDispatch( editorStore ); | |||
const { get: getPreference } = useSelect( preferencesStore ); | |||
const registry = useRegistry(); | |||
const { resetZoomLevel } = unlock( useDispatch( blockEditorStore ) ); | |||
const { isZoomOut } = unlock( select( blockEditorStore ) ); |
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 components shouldn't use global select
it doesn't react to store changes.
const { isZoomOut } = unlock( select( blockEditorStore ) ); | |
const { isZoomOut } = unlock( useSelect( blockEditorStore ) ); |
Hi @Mamaduka, |
What?
Closes #69086
Reset zoom level when editor component unmounts to prevent stale zoom states.
Why?
The zoom state and button remained active after responsive changes, causing inconsistency between the visual zoom effect and the button state. This led to a confusing user experience in which the zoom button appeared pressed, but no zoom effect was visible.
How?
Added a cleanup effect in the ZoomOutToggle component that calls resetZoomLevel when the component unmounts. This ensures the zoom state is properly reset between editor sessions and responsive changes.
Testing Instructions
Screencast
Screen.Recording.2025-02-07.at.12.27.40.PM.mov