-
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.
Add some really dodgy animation code
- Loading branch information
Showing
3 changed files
with
149 additions
and
53 deletions.
There are no files selected for viewing
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
116 changes: 85 additions & 31 deletions
116
packages/block-editor/src/components/resizable-alignment-controls/snapped-content.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 |
---|---|---|
@@ -1,52 +1,106 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { Popover } from '@wordpress/components'; | ||
import { __unstableMotion as motion } from '@wordpress/components'; | ||
import { useLayoutEffect, useState } from '@wordpress/element'; | ||
|
||
export default function SnappedContent( { alignmentZone, children } ) { | ||
const [ snapStyle, setSnapStyle ] = useState( null ); | ||
function getOffsetRect( element, offsetElement ) { | ||
const rect = element.getBoundingClientRect(); | ||
const offsetRect = offsetElement.getBoundingClientRect(); | ||
// const frame = ownerDocument?.defaultView?.frameElement; | ||
// const frameRect = frame?.getBoundingClientRect(); | ||
|
||
return new window.DOMRect( | ||
rect.x - ( offsetRect?.x ?? 0 ), | ||
rect.y - ( offsetRect?.y ?? 0 ), | ||
rect.width, | ||
rect.height | ||
); | ||
} | ||
|
||
export default function SnappedContent( { | ||
alignmentVisualizerRef, | ||
alignmentZone, | ||
children, | ||
resizableBoxRef, | ||
} ) { | ||
const [ animationProps, setAnimationProps ] = useState( {} ); | ||
const [ anchor, setAnchor ] = useState( null ); | ||
|
||
useLayoutEffect( () => { | ||
if ( ! alignmentZone ) { | ||
return setSnapStyle( { visibility: 'hidden' } ); | ||
if ( ! resizableBoxRef.current || ! alignmentVisualizerRef.current ) { | ||
return; | ||
} | ||
|
||
const ownerDocument = alignmentZone.ownerDocument; | ||
const defaultView = ownerDocument.defaultView; | ||
const resizableBoxRect = getOffsetRect( | ||
resizableBoxRef.current, | ||
alignmentVisualizerRef.current | ||
); | ||
|
||
function update() { | ||
const rect = alignmentZone.getBoundingClientRect(); | ||
if ( ! alignmentZone ) { | ||
// When unsnapping, animate the image back to the resizable box's position, | ||
// and then set it as 'hidden'. | ||
setAnimationProps( { | ||
animate: { | ||
x: resizableBoxRect.x, | ||
y: 0, | ||
width: resizableBoxRect.width, | ||
height: resizableBoxRect.height, | ||
transitionEnd: { | ||
visibility: 'hidden', | ||
}, | ||
}, | ||
transition: { duration: 0.1 }, | ||
} ); | ||
return; | ||
} | ||
|
||
setSnapStyle( { | ||
position: 'absolute', | ||
width: rect.width, | ||
height: 'auto', | ||
if ( ! anchor ) { | ||
setAnchor( { | ||
getBoundingClientRect: () => | ||
alignmentZone.parentElement.getBoundingClientRect(), | ||
ownerDocument: alignmentZone.ownerDocument, | ||
} ); | ||
} | ||
|
||
const resizeObserver = defaultView.ResizeObserver | ||
? new defaultView.ResizeObserver( update ) | ||
: undefined; | ||
resizeObserver?.observe( alignmentZone ); | ||
const alignmentZoneRect = alignmentZone.getBoundingClientRect(); | ||
const aspectRatio = resizableBoxRect.width / resizableBoxRect.height; | ||
|
||
return () => { | ||
resizeObserver?.disconnect(); | ||
}; | ||
// When snapping, first move the image immediately to the resizable box's current position, | ||
// making it visible. | ||
setAnimationProps( { | ||
animate: { | ||
visibility: 'visible', | ||
x: resizableBoxRect.x, | ||
y: 0, | ||
width: resizableBoxRect.width, | ||
height: resizableBoxRect.height, | ||
}, | ||
transition: { duration: 0 }, | ||
} ); | ||
|
||
// Then using `requestAnimationFrame` to defer the animation, animate to the alignment zone's | ||
// position. | ||
window.requestAnimationFrame( () => { | ||
setAnimationProps( { | ||
animate: { | ||
visibility: 'visible', | ||
x: alignmentZoneRect.x, | ||
y: 0, | ||
width: alignmentZoneRect.width, | ||
height: alignmentZoneRect.width / aspectRatio, | ||
}, | ||
transition: { duration: 0.1 }, | ||
} ); | ||
} ); | ||
}, [ alignmentZone ] ); | ||
|
||
return ( | ||
<Popover | ||
anchor={ alignmentZone } | ||
placement="top-start" | ||
animate={ false } | ||
focusOnMount={ false } | ||
flip={ false } | ||
resize={ false } | ||
variant="unstyled" | ||
__unstableInline | ||
<motion.div | ||
style={ { position: 'absolute', visibility: 'hidden' } } | ||
{ ...animationProps } | ||
> | ||
<div style={ snapStyle }>{ children }</div> | ||
</Popover> | ||
{ children } | ||
</motion.div> | ||
); | ||
} |
a60d775
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Flaky tests detected.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.
🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/3738798924
📝 Reported issues:
specs/local/demo.test.js
Browse all
#45332 inspecs/editor/various/inserting-blocks.test.js
specs/editor/various/multi-block-selection.test.js