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 block & block toolbar vibration on block move #22640

Merged
merged 2 commits into from
Jun 23, 2020
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
Original file line number Diff line number Diff line change
Expand Up @@ -155,13 +155,8 @@ function BlockPopover( {
__unstableSticky={ ! showEmptyBlockSideInserter }
__unstableSlotName="block-toolbar"
__unstableBoundaryParent
// Allow subpixel positioning for the block movement animation.
__unstableAllowVerticalSubpixelPosition={
moverDirection !== 'horizontal' && node
}
__unstableAllowHorizontalSubpixelPosition={
moverDirection === 'horizontal' && node
}
// Observe movement for block animations (especially horizontal).
__unstableObserveElement={ node }
Copy link
Contributor

Choose a reason for hiding this comment

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

Seems like this is the same thing as anchorRef. I wonder if we should instead observe anchor ref when it's a DOM element. I also wonder whether this means the interval inside Popover is useless.

onBlur={ () => setIsToolbarForced( false ) }
shouldAnchorIncludePadding
// Popover calculates the width once. Trigger a reset by remounting
Expand Down
90 changes: 56 additions & 34 deletions packages/block-editor/src/components/use-moving-animation/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ function useMovingAnimation(
const [ transform, setTransform ] = useState( {
x: 0,
y: 0,
scrollTop: 0,
clientTop: 0,
} );

const previous = ref.current ? getAbsolutePosition( ref.current ) : null;
Expand All @@ -80,11 +80,12 @@ function useMovingAnimation(
}

scrollContainer.current = getScrollContainer( ref.current );

if ( prefersReducedMotion ) {
if ( adjustScrolling && scrollContainer.current ) {
// 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';
ref.current.style.transform = '';
const destination = getAbsolutePosition( ref.current );
scrollContainer.current.scrollTop =
scrollContainer.current.scrollTop -
Expand All @@ -94,25 +95,65 @@ function useMovingAnimation(

return;
}
ref.current.style.transform = 'none';

ref.current.style.transform = '';
const destination = getAbsolutePosition( ref.current );
const x = Math.round( previous.left - destination.left );
const y = Math.round( previous.top - destination.top );
ref.current.style.transform =
x === 0 && y === 0 ? '' : `translate3d(${ x }px,${ y }px,0)`;
const blockRect = ref.current.getBoundingClientRect();
const newTransform = {
x: previous.left - destination.left,
y: previous.top - destination.top,
scrollTop: scrollContainer.current
? scrollContainer.current.scrollTop -
previous.top +
destination.top
: 0,
x,
y,
clientTop: blockRect.top,
};
ref.current.style.transform =
newTransform.x === 0 && newTransform.y === 0
? undefined
: `translate3d(${ newTransform.x }px,${ newTransform.y }px,0)`;
triggerAnimation();
setTransform( newTransform );
}, [ triggerAnimationOnChange ] );

// Only called when either the x or y value changes.
function onFrameChange( { x, y } ) {
if ( ! ref.current ) {
return;
}

const isMoving = x === 0 && y === 0;
ref.current.style.transformOrigin = isMoving ? '' : 'center';
ref.current.style.transform = isMoving
? ''
: `translate3d(${ x }px,${ y }px,0)`;
ref.current.style.zIndex = ! isSelected || isMoving ? '' : '1';

if (
adjustScrolling &&
scrollContainer.current &&
! prefersReducedMotion
) {
const blockRect = ref.current.getBoundingClientRect();
const diff = blockRect.top - transform.clientTop;

if ( diff ) {
scrollContainer.current.scrollTop += diff;
}
}
}

// Called for every frame computed by useSpring.
function onFrame( { x, y } ) {
x = Math.round( x );
y = Math.round( y );

if ( x !== onFrame.x || y !== onFrame.y ) {
onFrameChange( { x, y } );
onFrame.x = x;
onFrame.y = y;
}
}

onFrame.x = 0;
onFrame.y = 0;

useSpring( {
from: {
x: transform.x,
Expand All @@ -125,26 +166,7 @@ function useMovingAnimation(
reset: triggeredAnimation !== finishedAnimation,
config: { mass: 5, tension: 2000, friction: 200 },
immediate: prefersReducedMotion,
onFrame( { x, y } ) {
if (
adjustScrolling &&
scrollContainer.current &&
! prefersReducedMotion &&
y
) {
scrollContainer.current.scrollTop = transform.scrollTop + y;
}

if ( ref.current ) {
ref.current.style.transformOrigin = 'center';
ref.current.style.transform =
x === 0 && y === 0
? null
: `translate3d(${ x }px,${ y }px,0)`;
ref.current.style.zIndex =
! isSelected || ( x === 0 && y === 0 ) ? null : '1';
}
},
onFrame,
} );
}

Expand Down
54 changes: 8 additions & 46 deletions packages/components/src/popover/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,7 @@ const Popover = ( {
onFocusOutside,
__unstableSticky,
__unstableSlotName = SLOT_NAME,
__unstableAllowVerticalSubpixelPosition,
__unstableAllowHorizontalSubpixelPosition,
__unstableObserveElement,
__unstableFixedPosition = true,
__unstableBoundaryParent,
/* eslint-enable no-unused-vars */
Expand Down Expand Up @@ -286,7 +285,7 @@ const Popover = ( {
return;
}

const refresh = ( { subpixels } = {} ) => {
const refresh = () => {
if ( ! containerRef.current || ! contentRef.current ) {
return;
}
Expand Down Expand Up @@ -359,38 +358,8 @@ const Popover = ( {
typeof popoverTop === 'number' &&
typeof popoverLeft === 'number'
) {
if ( subpixels && __unstableAllowVerticalSubpixelPosition ) {
setStyle(
containerRef.current,
'left',
popoverLeft + 'px'
);
setStyle( containerRef.current, 'top' );
setStyle(
containerRef.current,
'transform',
`translateY(${ popoverTop }px)`
);
} else if (
subpixels &&
__unstableAllowHorizontalSubpixelPosition
) {
setStyle( containerRef.current, 'top', popoverTop + 'px' );
setStyle( containerRef.current, 'left' );
setStyle(
containerRef.current,
'transform',
`translate(${ popoverLeft }px)`
);
} else {
setStyle( containerRef.current, 'top', popoverTop + 'px' );
setStyle(
containerRef.current,
'left',
popoverLeft + 'px'
);
setStyle( containerRef.current, 'transform' );
}
setStyle( containerRef.current, 'top', popoverTop + 'px' );
setStyle( containerRef.current, 'left', popoverLeft + 'px' );
}

setClass(
Expand Down Expand Up @@ -455,15 +424,9 @@ const Popover = ( {

let observer;

const observeElement =
__unstableAllowVerticalSubpixelPosition ||
__unstableAllowHorizontalSubpixelPosition;

if ( observeElement ) {
observer = new window.MutationObserver( () =>
refresh( { subpixels: true } )
);
observer.observe( observeElement, { attributes: true } );
if ( __unstableObserveElement ) {
observer = new window.MutationObserver( refresh );
observer.observe( __unstableObserveElement, { attributes: true } );
}

return () => {
Expand All @@ -487,8 +450,7 @@ const Popover = ( {
position,
contentSize,
__unstableSticky,
__unstableAllowVerticalSubpixelPosition,
__unstableAllowHorizontalSubpixelPosition,
__unstableObserveElement,
__unstableBoundaryParent,
] );

Expand Down