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

Block: remove animation component so it is just a hook #22936

Merged
merged 3 commits into from
Jun 9, 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
14 changes: 7 additions & 7 deletions packages/block-editor/src/components/block-list/block-wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
*/
import classnames from 'classnames';
import { first, last, omit } from 'lodash';
import { animated } from 'react-spring/web.cjs';

/**
* WordPress dependencies
Expand All @@ -24,7 +23,10 @@ import { BlockListBlockContext } from './block';
import ELEMENTS from './block-wrapper-elements';

const BlockComponent = forwardRef(
( { children, tagName = 'div', __unstableIsHtml, ...props }, wrapper ) => {
(
{ children, tagName: TagName = 'div', __unstableIsHtml, ...props },
wrapper
) => {
const onSelectionStart = useContext( Context );
const setBlockNodes = useContext( SetBlockNodes );
const {
Expand Down Expand Up @@ -130,7 +132,7 @@ const BlockComponent = forwardRef(
}, [ isSelected, isMultiSelecting, isNavigationMode ] );

// Block Reordering animation
const animationStyle = useMovingAnimation(
useMovingAnimation(
wrapper,
isSelected || isPartOfMultiSelection,
isSelected || isFirstMultiSelected,
Expand Down Expand Up @@ -188,10 +190,9 @@ const BlockComponent = forwardRef(
const htmlSuffix =
mode === 'html' && ! __unstableIsHtml ? '-visual' : '';
const blockElementId = `block-${ clientId }${ htmlSuffix }`;
const Animated = animated[ tagName ];

const blockWrapper = (
<Animated
<TagName
// Overrideable props.
aria-label={ blockLabel }
role="group"
Expand All @@ -216,11 +217,10 @@ const BlockComponent = forwardRef(
style={ {
...( wrapperProps ? wrapperProps.style : {} ),
...( props.style || {} ),
...animationStyle,
} }
>
{ children }
</Animated>
</TagName>
);

// For aligned blocks, provide a wrapper element so the block can be
Expand Down
38 changes: 11 additions & 27 deletions packages/block-editor/src/components/use-moving-animation/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* External dependencies
*/
import { useSpring, interpolate } from 'react-spring/web.cjs';
import { useSpring } from 'react-spring/web.cjs';

/**
* WordPress dependencies
Expand Down Expand Up @@ -46,8 +46,6 @@ const getAbsolutePosition = ( element ) => {
* @param {boolean} adjustScrolling Adjust the scroll position to the current block.
* @param {boolean} enableAnimation Enable/Disable animation.
* @param {*} triggerAnimationOnChange Variable used to trigger the animation if it changes.
*
* @return {Object} Style object.
*/
function useMovingAnimation(
ref,
Expand Down Expand Up @@ -115,7 +113,7 @@ function useMovingAnimation(
setTransform( newTransform );
}, [ triggerAnimationOnChange ] );

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

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';
Copy link
Contributor

Choose a reason for hiding this comment

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

hah :) so you're recreating react-spring. It would be interesting to see how this differs from React spring internally. I mean shouldn't we be able to pass a ref for react-spring to do that.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok, just read the use-spring issue. i wonder what's different between this and applyAnimatedValues calls? Also should we upgrade?

Copy link
Member Author

Choose a reason for hiding this comment

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

Not really as far as recreating. It still provides the values, we just take render it here instead of passing instructions on how to render it for us. Ideally it would be great if we could just pass the ref and instructions to useSpring, but there's no big difference.

Copy link
Contributor

Choose a reason for hiding this comment

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

yes, so it looks like this is the function that runs in the library https://github.com/react-spring/react-spring/blob/master/src/targets/web/globals.ts#L82-L127 which is pretty similar to what you did for the "styles" here.

Copy link
Contributor

Choose a reason for hiding this comment

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

While testing though, it seems the animation is broken for me (blocks overlap)

Copy link
Member

Choose a reason for hiding this comment

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

I tested on Arch Linux using a browser based on Chromium 80, and the block movement animations all seem fine to me. I also tested in Firefox Developer Edition 78.0b2 and everything worked there as well.

Copy link
Contributor

Choose a reason for hiding this comment

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

That's weird, they overlap for me regardless of the browser (Chrome and Safari)

Copy link
Member Author

Choose a reason for hiding this comment

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

@youknowriad Did you do a full refresh etc? It works fine for me. I also tried switching branches a few times, but I can't reproduce any overlap issues

Copy link
Contributor

Choose a reason for hiding this comment

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

So I just tested for the third time and this time it worked. Sorry for delaying that.

Copy link
Member Author

Choose a reason for hiding this comment

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

No worries :) I'm glad it works now

},
} );

// Dismiss animations if disabled.
return prefersReducedMotion
? {}
: {
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;