Created
February 12, 2016 13:21
-
-
Save joshwcomeau/237f3e737237cc359512 to your computer and use it in GitHub Desktop.
Animating the Unanimatable
This file contains hidden or 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
componentDidUpdate(previousProps) { | |
previousProps.children.forEach( child => { | |
let domNode = ReactDOM.findDOMNode( this.refs[child.key] ); | |
const newBox = domNode.getBoundingClientRect(); | |
const oldBox = this.state[key]; | |
const deltaX = oldBox.left - newBox.left; | |
const deltaY = oldBox.top - newBox.top; | |
requestAnimationFrame( () => { | |
domNode.style.transform = `translate(${deltaX}px, ${deltaY}px)`; | |
domNode.style.transition = 'transform 0s'; | |
requestAnimationFrame( () => { | |
// In order to get the animation to play, we'll need to wait for | |
// the 'invert' animation frame to finish, so that its inverted | |
// position has propagated to the DOM. | |
// | |
// Then, we just remove the transform, reverting it to its natural | |
// state, and apply a transition so it does so smoothly. | |
domNode.style.transform = ''; | |
domNode.style.transition = 'transform 500ms'; | |
}); | |
}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You shouldn't need to use
requestAnimationFrame
to trigger transitions. You just need to trigger a style flush.e.g.
Will cause style to be flushed. So to trigger a transition, typically all you need is:
There's no "propagating to the DOM", it's just doing style resolution. The node is already in the DOM.