Skip to content

Instantly share code, notes, and snippets.

Last active March 8, 2020 17:05
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?

Here's a rough draft proposal for an extension to the animated component that can handle both simple and advanced FLIP use cases.

Example usage

1. Simplest example

Initiate a FLIP animation on a component by updating the flipKey prop.



2. Two different components example

Match unconnected components across renders by making sure they share the same flipId prop, and when one unmounts and the other mounts, they will be connected with a FLIP animation.



3. Advanced custom spring example

Occasionally, like in the example below, users will want to be able to FLIP pre-existing springs, if for instance a FLIP could be interrupted by a drag. The api allows for this as well, by passing in a flipSet prop in addition to a flipKey or flipId.

(Rough) demo


Proposed extension to animated component

This is a first draft of the code wrapping the animated component to make the above three use cases possible. The AnimationHandler outer wrapper attempts to simplify the most common use cases. However, if you pass in the flipSet prop you can bypass this to get total control of the animation to facilitate interruptability and more complex effects.


To do:

  1. Think about improving or simplifying flipId/flipKey/flipSet api.
  2. Figure out chained staggering of FLIP components and/or whether it is necessary (react-flip-toolkit supports it).
  3. Figure out how to lazily import FLIP functionality into animated so as not to bloat the bundle for users who don't need FLIP. (or perhaps just have a FlippableAnimated component or similar?)
  4. Make the springs customizable for abstracted FLIP cases (like examples one and two). this would be easy with a flipSpring prop or something but that might start making the api too complex.
Copy link

aholachek commented Dec 24, 2019

Thank you both for your comments!

@aleclarson, I would defer to you on using hooks. It looks like the api you're proposing in your first example would be focused on list re-ordering, though, which is only one possible use case for flip.

In your second example of animating two separate components, i'm not sure I understand what the benefits using hooks over a wrapper component would be, or why

const Flip = useFlip({
  key: 'unique flip key',
return <Flip>{childA}</Flip>

would necessarily be superior to

return <Flip key='unique-flip-key'>{childA}</Flip>

@dbismut one issue with using flip to animate to height:auto is that FLIP uses transforms, which won't affect the layout of the rest of the page. So you will get a very different looking animation if you use scaleY versus height. Maybe that's an ok tradeoff, but I would just want to know what sort of effects people were hoping to achieve. I guess in my head I think of animating to height:auto typically is for something like this collapse, which wouldn't work as well as a flip animation. Was that sort of what you were thinking of or did you have other types of animation in mind?

Copy link

Actually @aleclarson, please disregard the second point. I reread your comment and I see that by using useFlip you would hope to keep the api more consistent with useSpring which makes sense.

Copy link

ShanonJackson commented Feb 9, 2020

Built a really simple example.

Works only with context parent "Flip"
And functional component "FlipItem" which just renders an animated div and registers itself in the context.
Runs useLayoutEffect every update and if their DOMRects normalized against where they would be with no transform (this is important) then they wont animate.
Uses matrix math (scale is useless just there as a placeholder).

There might be some drag-and-drop logic floating around because i had to rip my dnd stuff out of this implementation to just show the flip stuff but my real-world usecase for flip is more for drag-and-drop.

What iv'e learnt:
Must compute position logic as if transforms aren't applied.
Can't really use the inbuilt animated.div myself without forking react-spring but ideally it would just be animated.div id="xyz"
Must use context to provide seemless multi-parent transitions.
Can use either useLayoutEffect OR getSnapshotBeforeUpdate
Doesn't animate height/width to keep it simple however this would be very easy to add

EDIT: My drag-and-drop implementation utilizing flip is almost done but haven't had time to work on it but the only thing missing is multi-container drop. Implementing flip you can build drag-and-drop without ANY position math for moving elements around.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment