Skip to content

Commit

Permalink
Add event listener instead of timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
ajlende committed Nov 6, 2024
1 parent 42dd030 commit 5fff8f9
Showing 1 changed file with 26 additions and 16 deletions.
42 changes: 26 additions & 16 deletions packages/block-editor/src/components/iframe/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,16 @@ function Iframe( {
Math.min( Math.max( 0, scrollTopNext ), maxScrollTop )
);

const reduceMotion = iframeDocument.defaultView.matchMedia(
'(prefers-reduced-motion: reduce)'
).matches;

if ( reduceMotion ) {
// TODO: This seems to be broken somehow.
iframeDocument.documentElement.scrollTop = scrollTopNext;
return;
}

iframeDocument.documentElement.style.setProperty(
'--wp-block-editor-iframe-zoom-out-scroll-top',
`${ scrollTop }px`
Expand All @@ -402,24 +412,24 @@ function Iframe( {

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

// TODO: See if there's a way to wait for CSS transition to finish.
// 400ms should match the animation speed used in components/iframe/content.scss
// Ignore the delay when reduce motion is enabled.
const reduceMotion = iframeDocument.defaultView.matchMedia(
'(prefers-reduced-motion: reduce)'
).matches;
const delay = reduceMotion ? 0 : 400;
iframeDocument.documentElement.addEventListener(
'transitionend',
() => {
// Remove the position fixed for the animation.
iframeDocument.documentElement.classList.remove(
'zoom-out-animation'
);

zoomOutAnimationTimeoutRef.current = setTimeout( () => {
iframeDocument.documentElement.classList.remove(
'zoom-out-animation'
);
// Update previous values.
prevClientHeightRef.current = clientHeight;
prevFrameSizeRef.current = frameSizeValue;
prevScaleRef.current = scaleValue;

prevClientHeightRef.current = clientHeight;
prevFrameSizeRef.current = frameSizeValue;
prevScaleRef.current = scaleValue;
iframeDocument.documentElement.scrollTop = scrollTopNext;
}, delay );
// Set the final scroll position that was just animated to.
iframeDocument.documentElement.scrollTop = scrollTopNext;
},
{ once: true }
);
}, [ scaleValue, frameSizeValue, iframeDocument ] );

// Toggle zoom out CSS Classes only when zoom out mode changes. We could add these into the useEffect
Expand Down

0 comments on commit 5fff8f9

Please sign in to comment.