Skip to content

Commit

Permalink
Fix/html scale code quality (#66181)
Browse files Browse the repository at this point in the history
Co-authored-by: jeryj <[email protected]>
Co-authored-by: ajlende <[email protected]>
  • Loading branch information
3 people authored Oct 16, 2024
1 parent 2177aed commit 346fd4c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 34 deletions.
4 changes: 2 additions & 2 deletions packages/block-editor/src/components/iframe/content.scss
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
$frame-size: var(--wp-block-editor-iframe-zoom-out-frame-size);
$inner-height: var(--wp-block-editor-iframe-zoom-out-inner-height);
$content-height: var(--wp-block-editor-iframe-zoom-out-content-height);
$outer-container-width: var(--wp-block-editor-iframe-zoom-out-outer-container-width);
$scale-container-width: var(--wp-block-editor-iframe-zoom-out-scale-container-width);
$container-width: var(--wp-block-editor-iframe-zoom-out-container-width, 100vw);
// Apply an X translation to center the scaled content within the available space.
transform: translateX(calc((#{$outer-container-width} - #{$container-width}) / 2 / #{$scale}));
transform: translateX(calc((#{$scale-container-width} - #{$container-width}) / 2 / #{$scale}));
scale: #{$scale};
background-color: $gray-300;

Expand Down
57 changes: 27 additions & 30 deletions packages/block-editor/src/components/iframe/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ function Iframe( {
}, [] );
const { styles = '', scripts = '' } = resolvedAssets;
const [ iframeDocument, setIframeDocument ] = useState();
const initialContainerWidth = useRef();
const initialContainerWidth = useRef( 0 );
const [ bodyClasses, setBodyClasses ] = useState( [] );
const clearerRef = useBlockSelectionClearer();
const [ before, writingFlowRef, after ] = useWritingFlow();
Expand Down Expand Up @@ -247,6 +247,11 @@ function Iframe( {
}
}, [ containerWidth, isZoomedOut ] );

const scaleContainerWidth = Math.max(
initialContainerWidth.current,
containerWidth
);

const disabledRef = useDisabled( { isDisabled: ! readonly } );
const bodyRef = useMergeRefs( [
useBubbleEvents( iframeDocument ),
Expand Down Expand Up @@ -299,17 +304,6 @@ function Iframe( {
useEffect( () => cleanup, [ cleanup ] );

const zoomOutAnimationClassnameRef = useRef( null );
const handleZoomOutAnimationClassname = () => {
clearTimeout( zoomOutAnimationClassnameRef.current );

iframeDocument.documentElement.classList.add( 'zoom-out-animation' );

zoomOutAnimationClassnameRef.current = setTimeout( () => {
iframeDocument.documentElement.classList.remove(
'zoom-out-animation'
);
}, 400 ); // 400ms should match the animation speed used in components/iframe/content.scss
};

// Toggle zoom out CSS Classes only when zoom out mode changes. We could add these into the useEffect
// that controls settings the CSS variables, but then we would need to do more work to ensure we're
Expand All @@ -320,6 +314,20 @@ function Iframe( {
return;
}

const handleZoomOutAnimationClassname = () => {
clearTimeout( zoomOutAnimationClassnameRef.current );

iframeDocument.documentElement.classList.add(
'zoom-out-animation'
);

zoomOutAnimationClassnameRef.current = setTimeout( () => {
iframeDocument.documentElement.classList.remove(
'zoom-out-animation'
);
}, 400 ); // 400ms should match the animation speed used in components/iframe/content.scss
};

handleZoomOutAnimationClassname();
iframeDocument.documentElement.classList.add( 'is-zoomed-out' );

Expand Down Expand Up @@ -349,10 +357,7 @@ function Iframe( {
scale === 'default'
? ( Math.min( containerWidth, maxWidth ) -
parseInt( frameSize ) * 2 ) /
Math.max(
initialContainerWidth.current,
containerWidth
)
scaleContainerWidth
: scale
);

Expand All @@ -374,15 +379,10 @@ function Iframe( {
`${ containerWidth }px`
);
iframeDocument.documentElement.style.setProperty(
'--wp-block-editor-iframe-zoom-out-outer-container-width',
`${ Math.max( initialContainerWidth.current, containerWidth ) }px`
'--wp-block-editor-iframe-zoom-out-scale-container-width',
`${ scaleContainerWidth }px`
);

// iframeDocument.documentElement.style.setProperty(
// '--wp-block-editor-iframe-zoom-out-outer-container-width',
// `${ Math.max( initialContainerWidth.current, containerWidth ) }px`
// );

return () => {
iframeDocument.documentElement.style.removeProperty(
'--wp-block-editor-iframe-zoom-out-scale'
Expand All @@ -400,7 +400,7 @@ function Iframe( {
'--wp-block-editor-iframe-zoom-out-container-width'
);
iframeDocument.documentElement.style.removeProperty(
'--wp-block-editor-iframe-zoom-out-outer-container-width'
'--wp-block-editor-iframe-zoom-out-scale-container-width'
);
};
}, [
Expand All @@ -412,6 +412,7 @@ function Iframe( {
containerWidth,
windowInnerWidth,
isZoomedOut,
scaleContainerWidth,
] );

// Make sure to not render the before and after focusable div elements in view
Expand Down Expand Up @@ -501,12 +502,8 @@ function Iframe( {
isZoomedOut && 'is-zoomed-out'
) }
style={ {
'--wp-block-editor-iframe-zoom-out-outer-container-width':
isZoomedOut &&
`${ Math.max(
initialContainerWidth.current,
containerWidth
) }px`,
'--wp-block-editor-iframe-zoom-out-scale-container-width':
isZoomedOut && `${ scaleContainerWidth }px`,
} }
>
{ iframe }
Expand Down
4 changes: 2 additions & 2 deletions packages/block-editor/src/components/iframe/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
}

.block-editor-iframe__scale-container.is-zoomed-out {
$outer-container-width: var(--wp-block-editor-iframe-zoom-out-outer-container-width, 100vw);
width: $outer-container-width;
$scale-container-width: var(--wp-block-editor-iframe-zoom-out-scale-container-width, 100vw);
width: $scale-container-width;
// Position the iframe so that it is always aligned with the right side so that
// the scrollbar is always visible on the right side
position: absolute;
Expand Down

1 comment on commit 346fd4c

@github-actions
Copy link

Choose a reason for hiding this comment

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

Flaky tests detected in 346fd4c.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/11374404631
📝 Reported issues:

Please sign in to comment.