-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement a block reordering animation (#16065)
- Loading branch information
1 parent
f8f8708
commit eba2fc8
Showing
8 changed files
with
127 additions
and
9 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
packages/block-editor/src/components/block-list/moving-animation.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { useSpring, interpolate } from 'react-spring/web.cjs'; | ||
|
||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useState, useLayoutEffect } from '@wordpress/element'; | ||
import { useReducedMotion } from '@wordpress/compose'; | ||
|
||
/** | ||
* Hook used to compute the styles required to move a div into a new position. | ||
* | ||
* The way this animation works is the following: | ||
* - It first renders the element as if there was no animation. | ||
* - It takes a snapshot of the position of the block to use it | ||
* as a destination point for the animation. | ||
* - It restores the element to the previous position using a CSS transform | ||
* - It uses the "resetAnimation" flag to reset the animation | ||
* from the beginning in order to animate to the new destination point. | ||
* | ||
* @param {Object} ref Reference to the element to animate. | ||
* @param {boolean} isSelected Whether it's the current block or not. | ||
* @param {boolean} enableAnimation Enable/Disable animation. | ||
* @param {*} triggerAnimationOnChange Variable used to trigger the animation if it changes. | ||
* | ||
* @return {Object} Style object. | ||
*/ | ||
function useMovingAnimation( ref, isSelected, enableAnimation, triggerAnimationOnChange ) { | ||
const prefersReducedMotion = useReducedMotion() || ! enableAnimation; | ||
const [ resetAnimation, setResetAnimation ] = useState( false ); | ||
const [ transform, setTransform ] = useState( { x: 0, y: 0 } ); | ||
|
||
const previous = ref.current ? ref.current.getBoundingClientRect() : null; | ||
useLayoutEffect( () => { | ||
if ( prefersReducedMotion ) { | ||
return; | ||
} | ||
ref.current.style.transform = 'none'; | ||
const destination = ref.current.getBoundingClientRect(); | ||
const newTransform = { | ||
x: previous ? previous.left - destination.left : 0, | ||
y: previous ? previous.top - destination.top : 0, | ||
}; | ||
ref.current.style.transform = `translate3d(${ newTransform.x }px,${ newTransform.y }px,0)`; | ||
setResetAnimation( true ); | ||
setTransform( newTransform ); | ||
}, [ triggerAnimationOnChange ] ); | ||
useLayoutEffect( () => { | ||
if ( resetAnimation ) { | ||
setResetAnimation( false ); | ||
} | ||
}, [ resetAnimation ] ); | ||
const animationProps = useSpring( { | ||
from: transform, | ||
to: { | ||
x: 0, | ||
y: 0, | ||
}, | ||
reset: resetAnimation, | ||
config: { mass: 5, tension: 2000, friction: 200 }, | ||
immediate: prefersReducedMotion, | ||
} ); | ||
|
||
return { | ||
transformOrigin: 'center', | ||
transform: interpolate( | ||
[ | ||
animationProps.x, | ||
animationProps.y, | ||
], | ||
( x, y ) => x === 0 && y === 0 ? undefined : `translate3d(${ x }px,${ y }px,0)` | ||
), | ||
zIndex: interpolate( | ||
[ | ||
animationProps.x, | ||
animationProps.y, | ||
], | ||
( x, y ) => ! isSelected || ( x === 0 && y === 0 ) ? undefined : `1` | ||
), | ||
}; | ||
} | ||
|
||
export default useMovingAnimation; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters