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

Zoom out: keep original viewport width #61422

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 52 additions & 2 deletions packages/block-editor/src/components/iframe/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -320,7 +320,7 @@ function Iframe( {
const shouldRenderFocusCaptureElements = tabIndex >= 0 && ! isPreviewMode;

return (
<>
<ViewportScaling enabled={ ! isPreviewMode }>
{ shouldRenderFocusCaptureElements && before }
{ /* eslint-disable-next-line jsx-a11y/no-noninteractive-element-interactions */ }
<iframe
Expand Down Expand Up @@ -391,7 +391,57 @@ function Iframe( {
) }
</iframe>
{ shouldRenderFocusCaptureElements && after }
</>
</ViewportScaling>
);
}

function ViewportScaling( { children, enabled } ) {
const [ viewportWidth, setViewportWidth ] = useState();
const [ resizeElement, { width: divWidth } ] = useResizeObserver();
const ref = useRefEffect( ( node ) => {
const { ownerDocument } = node;
const { defaultView } = ownerDocument;

setViewportWidth( defaultView.innerWidth );
const onResize = () => {
setViewportWidth( defaultView.innerWidth );
};
defaultView.addEventListener( 'resize', onResize );
return () => {
defaultView.removeEventListener( 'resize', onResize );
};
}, [] );

if ( ! enabled ) {
return children;
}

return (
<div
ref={ ref }
style={ {
height: '100%',
overflow: 'hidden',
} }
>
{ resizeElement }
<div
style={ {
width: `${ viewportWidth }px`,
height: '100%',
} }
>
<div
style={ {
transform: `scale(${ divWidth / viewportWidth })`,
transformOrigin: 'top left',
height: `calc(100% / ${ divWidth / viewportWidth })`,
} }
>
{ children }
</div>
</div>
</div>
);
}

Expand Down
Loading