Skip to content

Commit

Permalink
Avoid recomputing the scrollble container on selection change (#21231)
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad committed Apr 3, 2020
1 parent 63a2e77 commit 8c6916c
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions packages/block-editor/src/components/block-list/moving-animation.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
useState,
useLayoutEffect,
useReducer,
useMemo,
useRef,
} from '@wordpress/element';
import { useReducedMotion } from '@wordpress/compose';
import { getScrollContainer } from '@wordpress/dom';
Expand Down Expand Up @@ -69,27 +69,25 @@ function useMovingAnimation(
} );

const previous = ref.current ? getAbsolutePosition( ref.current ) : null;
const scrollContainer = useMemo( () => {
if ( ! adjustScrolling ) {
return false;
}
return getScrollContainer( ref.current );
}, [ adjustScrolling ] );
const scrollContainer = useRef();

useLayoutEffect( () => {
if ( triggeredAnimation ) {
endAnimation();
}
}, [ triggeredAnimation ] );
useLayoutEffect( () => {
scrollContainer.current = getScrollContainer( ref.current );
if ( prefersReducedMotion ) {
if ( adjustScrolling && scrollContainer ) {
if ( adjustScrolling && scrollContainer.current && previous ) {
// if the animation is disabled and the scroll needs to be adjusted,
// just move directly to the final scroll position
ref.current.style.transform = 'none';
const destination = getAbsolutePosition( ref.current );
scrollContainer.scrollTop =
scrollContainer.scrollTop - previous.top + destination.top;
scrollContainer.current.scrollTop =
scrollContainer.current.scrollTop -
previous.top +
destination.top;
}

return;
Expand All @@ -100,8 +98,10 @@ function useMovingAnimation(
x: previous ? previous.left - destination.left : 0,
y: previous ? previous.top - destination.top : 0,
scrollTop:
previous && scrollContainer
? scrollContainer.scrollTop - previous.top + destination.top
previous && scrollContainer.current
? scrollContainer.current.scrollTop -
previous.top +
destination.top
: 0,
};
ref.current.style.transform =
Expand All @@ -127,11 +127,12 @@ function useMovingAnimation(
onFrame: ( props ) => {
if (
adjustScrolling &&
scrollContainer &&
scrollContainer.current &&
! prefersReducedMotion &&
props.y
) {
scrollContainer.scrollTop = transform.scrollTop + props.y;
scrollContainer.current.scrollTop =
transform.scrollTop + props.y;
}
},
} );
Expand Down

0 comments on commit 8c6916c

Please sign in to comment.