Skip to content

Commit

Permalink
Fix animation
Browse files Browse the repository at this point in the history
  • Loading branch information
youknowriad committed Apr 18, 2022
1 parent 704fe94 commit c9f96f8
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 34 deletions.
2 changes: 1 addition & 1 deletion packages/block-editor/src/components/block-list/style.scss
Expand Up @@ -427,7 +427,7 @@

.is-root-container.is-exploded-mode > .wp-block {
transform: scale(0.8);
transform-origin: top center;
transform-origin: center center;
position: relative;
overflow: hidden;

Expand Down
Expand Up @@ -71,6 +71,7 @@ export function useBlockProps( props = {}, { __unstableIsHtml } = {} ) {
adjustScrolling,
enableAnimation,
isExplodedMode,
isRootBlock,
} = useSelect(
( select ) => {
const {
Expand All @@ -84,6 +85,7 @@ export function useBlockProps( props = {}, { __unstableIsHtml } = {} ) {
isAncestorMultiSelected,
isFirstMultiSelectedBlock,
__unstableGetEditorMode,
getBlockRootClientId,
} = select( blockEditorStore );
const isSelected = isBlockSelected( clientId );
const isPartOfMultiSelection =
Expand All @@ -105,6 +107,7 @@ export function useBlockProps( props = {}, { __unstableIsHtml } = {} ) {
! isTyping() &&
getGlobalBlockCount() <= BLOCK_ANIMATION_THRESHOLD,
isExplodedMode: __unstableGetEditorMode() === 'exploded',
isRootBlock: ! getBlockRootClientId( clientId ),
};
},
[ clientId ]
Expand All @@ -127,7 +130,7 @@ export function useBlockProps( props = {}, { __unstableIsHtml } = {} ) {
adjustScrolling,
enableAnimation,
triggerAnimationOnChange: index,
scale: isExplodedMode ? 0.8 : 1,
scale: isExplodedMode && isRootBlock ? 0.8 : 1,
} ),
] );

Expand Down
Expand Up @@ -90,21 +90,18 @@ function BlockPopover( {
// Controls when the side inserter on empty lines should
// be shown, including writing and selection modes.
const showEmptyBlockSideInserter =
! isTyping &&
! editorMode === 'visual' &&
isEmptyDefaultBlock &&
isValid;
! isTyping && ! editorMode === 'edit' && isEmptyDefaultBlock && isValid;
const shouldShowBreadcrumb =
editorMode === 'navigation' || editorMode === 'exploded';
const shouldShowContextualToolbar =
editorMode === 'visual' &&
editorMode === 'edit' &&
! hasFixedToolbar &&
isLargeViewport &&
! showEmptyBlockSideInserter &&
! isMultiSelecting &&
( ! isTyping || isCaretWithinFormattedText );
const canFocusHiddenToolbar =
editorMode === 'visual' &&
editorMode === 'edit' &&
! shouldShowContextualToolbar &&
! hasFixedToolbar &&
! isEmptyDefaultBlock;
Expand Down
40 changes: 14 additions & 26 deletions packages/block-editor/src/components/use-moving-animation/index.js
Expand Up @@ -63,7 +63,7 @@ function useMovingAnimation( {
0
);
const [ finishedAnimation, endAnimation ] = useReducer( counterReducer, 0 );
const [ transform, setTransform ] = useState( { x: 0, y: 0 } );
const [ transform, setTransform ] = useState( { x: 0, y: 0, scale: 1 } );
const previous = useMemo(
() => ( ref.current ? getAbsolutePosition( ref.current ) : null ),
[ triggerAnimationOnChange ]
Expand Down Expand Up @@ -111,56 +111,44 @@ function useMovingAnimation( {
return;
}

ref.current.style.transform = '';
ref.current.style.transform = `scale( ${ scale } )`;
const destination = getAbsolutePosition( ref.current );

triggerAnimation();
setTransform( {
x: Math.round( previous.left - destination.left ),
y: Math.round( previous.top - destination.top ),
scale,
} );
}, [ triggerAnimationOnChange ] );

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

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

preserveScrollPosition();
}

// Called for every frame computed by useSpring.
function onChange( { value } ) {
let { x, y } = value;
let { x, y, scale: currentScale } = value;
x = Math.round( x );
y = Math.round( y );
const finishedMoving = x === 0 && y === 0 && currentScale === scale;
ref.current.style.transformOrigin = 'center center';
ref.current.style.transform = finishedMoving
? `scale( ${ scale } )`
: `translate3d(${ x }px,${ y }px,0) scale(${ currentScale })`;
ref.current.style.zIndex = isSelected ? '1' : '';

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

onChange.x = 0;
onChange.y = 0;

useSpring( {
from: {
x: transform.x,
y: transform.y,
scale: transform.scale,
},
to: {
x: 0,
y: 0,
scale,
},
reset: triggeredAnimation !== finishedAnimation,
config: { mass: 5, tension: 2000, friction: 200 },
Expand Down
5 changes: 5 additions & 0 deletions packages/dom/src/dom/get-scroll-container.js
Expand Up @@ -19,11 +19,16 @@ export default function getScrollContainer( node ) {
if ( node.scrollHeight > node.clientHeight ) {
// ...except when overflow is defined to be hidden or visible
const { overflowY } = getComputedStyle( node );

if ( /(auto|scroll)/.test( overflowY ) ) {
return node;
}
}

if ( node.ownerDocument === node.parentNode ) {
return node;
}

// Continue traversing.
return getScrollContainer( /** @type {Element} */ ( node.parentNode ) );
}

0 comments on commit c9f96f8

Please sign in to comment.