@@ -13,6 +13,7 @@ import type { VisualElement } from 'framer-motion'
1313import { animate , noop } from 'framer-motion/dom'
1414import { createVisualElement } from '@/state/create-visual-element'
1515import { prefersReducedMotion } from 'framer-motion/dist/es/utils/reduced-motion/state.mjs'
16+ import { calcChildStagger } from '@/features/animation/calc-child-stagger'
1617
1718const STATE_TYPES = [ 'initial' , 'animate' , 'whileInView' , 'whileHover' , 'whilePress' , 'whileDrag' , 'whileFocus' , 'exit' ] as const
1819export type StateType = typeof STATE_TYPES [ number ]
@@ -43,7 +44,7 @@ export class AnimationFeature extends Feature {
4344 } ,
4445 reducedMotionConfig : this . state . options . motionConfig . reducedMotion ,
4546 } )
46-
47+ this . state . visualElement . parent ?. addChild ( this . state . visualElement )
4748 this . state . animateUpdates = this . animateUpdates
4849 if ( this . state . isMounted ( ) )
4950 this . state . startAnimation ( )
@@ -61,7 +62,6 @@ export class AnimationFeature extends Feature {
6162 directAnimate,
6263 directTransition,
6364 controlDelay = 0 ,
64- isFallback,
6565 isExit,
6666 } = { } ) => {
6767 // check if the user has reduced motion
@@ -77,9 +77,11 @@ export class AnimationFeature extends Feature {
7777 directAnimate,
7878 directTransition,
7979 } )
80+ // The final transition to be applied to the state
81+ this . state . finalTransition = animationOptions
8082
8183 const factories = this . createAnimationFactories ( prevTarget , animationOptions , controlDelay )
82- const { getChildAnimations } = this . setupChildAnimations ( animationOptions , this . state . activeStates , isFallback )
84+ const { getChildAnimations } = this . setupChildAnimations ( animationOptions , this . state . activeStates )
8385 return this . executeAnimations ( {
8486 factories,
8587 getChildAnimations,
@@ -145,7 +147,6 @@ export class AnimationFeature extends Feature {
145147 setupChildAnimations (
146148 transition : $Transition | undefined ,
147149 controlActiveState : Partial < Record < string , boolean > > | undefined ,
148- isFallback : boolean ,
149150 ) {
150151 const visualElement = this . state . visualElement
151152 if ( ! visualElement . variantChildren ?. size || ! controlActiveState )
@@ -262,6 +263,21 @@ export class AnimationFeature extends Feature {
262263 // Add state reference to visual element
263264 ( this . state . visualElement as any ) . state = this . state
264265 this . updateAnimationControlsSubscription ( )
266+
267+ const visualElement = this . state . visualElement
268+ const parentVisualElement = visualElement . parent
269+ visualElement . enteringChildren = undefined
270+ /**
271+ * when current element is new entering child and it's controlled by parent,
272+ * animate it by delayChildren
273+ */
274+ if ( this . state . parent ?. isMounted ( ) && ! visualElement . isControllingVariants && parentVisualElement ?. enteringChildren ?. has ( visualElement ) ) {
275+ const { delayChildren } = this . state . parent . finalTransition || { } ;
276+ ( this . animateUpdates ( {
277+ controlActiveState : this . state . parent . activeStates ,
278+ controlDelay : calcChildStagger ( parentVisualElement . enteringChildren , visualElement , delayChildren ) ,
279+ } ) as Function ) ( )
280+ }
265281 }
266282
267283 update ( ) {
0 commit comments