Skip to content

Commit

Permalink
Zoom out: maintain scroll position
Browse files Browse the repository at this point in the history
  • Loading branch information
ellatrix committed May 8, 2024
1 parent 4c28ed3 commit af73d49
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions packages/block-editor/src/components/iframe/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import {
useMergeRefs,
useRefEffect,
useDisabled,
usePrevious,
} from '@wordpress/compose';
import { __experimentalStyleProvider as StyleProvider } from '@wordpress/components';
import { useSelect } from '@wordpress/data';
Expand Down Expand Up @@ -320,6 +321,22 @@ function Iframe( {
// mode. They're only needed to capture focus in edit mode.
const shouldRenderFocusCaptureElements = tabIndex >= 0 && ! isPreviewMode;

const previousScale = usePrevious( scale );

// Scroll based on the new scale
useEffect( () => {
if ( ! iframeDocument ) {
return;
}

const { documentElement } = iframeDocument;
const { scrollTop, scrollLeft } = documentElement;
const delta = 1 + scale - previousScale;

documentElement.scrollTop = delta * scrollTop;
documentElement.scrollLeft = delta * scrollLeft;
}, [ scale, previousScale, iframeDocument ] );

return (
<>
{ shouldRenderFocusCaptureElements && before }
Expand Down

0 comments on commit af73d49

Please sign in to comment.