Some Examples » 32. Animate Presence: Stack 3D
32. Animate Presence: Stack 3D
This is quite a complicated example. A lot is going on here.
Code component
A few details:
- There are always just two cards whose
key
s count up when the first card is removed (changing the key triggers the Animate Presence animation). - The cards have
scale
androtate
Motion values that are transformed by the card’sx
position (when the card is draggable, only the first card is). - The cards are wrapped in an
<AnimatePresence>
, and the first card will have anexit
animation that moves it to the left or right (starting from the point where you release it). - The animations are passed in variants, of which there are two sets:
variantsFrontCard
andvariantsBackCard
. Theexit
variant of the front card uses acustom
property set on the card: thex
position it should animate to. - That
custom
value is saved in anexitX
state and gets set just before theexit
animation happens in thehandleDragEnd()
handler that is called ononDragEnd()
. - The parent’s
setIndex()
is passed to the first card, and when it is called (in that samehandleDragEnd()
), the cards change position.
This is the Card()
component:
And this is the main component that contains the two cards.
Code override
This might be possible with three cards that get reused continuously (and no <AnimatePresence>
). But why bother? A component version will always be easier to make.