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

Fix/html scale code quality #66181

Merged
merged 4 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
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
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
Loading