@@ -2,10 +2,11 @@ import type { Directive, DirectiveBinding, Ref, VNode } from 'vue'
2
2
import defu from 'defu'
3
3
import { ref , unref } from 'vue'
4
4
import { motionState } from '../features/state'
5
- import type { MotionVariants } from '../types'
5
+ import type { MotionInstance , MotionVariants } from '../types'
6
6
import { useMotion } from '../useMotion'
7
7
import { resolveVariants } from '../utils/directive'
8
8
import { variantToStyle } from '../utils/transform'
9
+ import { registerVisibilityHooks } from '../features/visibilityHooks'
9
10
10
11
export function directive < T extends string > ( variants : MotionVariants < T > = { } ) : Directive < HTMLElement | SVGElement > {
11
12
const register = ( el : HTMLElement | SVGElement , binding : DirectiveBinding , node : VNode < any , HTMLElement | SVGElement , Record < string , any > > ) => {
@@ -24,8 +25,11 @@ export function directive<T extends string>(variants: MotionVariants<T> = {}): D
24
25
// Resolve variants from node props
25
26
resolveVariants < T > ( node , variantsRef )
26
27
28
+ // Disable visibilityHooks, these will be registered in `mounted`
29
+ const motionOptions = { eventListeners : true , lifeCycleHooks : true , syncVariants : true , visibilityHooks : false }
30
+
27
31
// Create motion instance
28
- const motionInstance = useMotion ( el , variantsRef )
32
+ const motionInstance = useMotion ( el , variantsRef , motionOptions )
29
33
30
34
// Pass the motion instance via the local element
31
35
// @ts -expect-error - we know that the element is a HTMLElement
@@ -35,8 +39,18 @@ export function directive<T extends string>(variants: MotionVariants<T> = {}): D
35
39
if ( key ) motionState [ key ] = motionInstance
36
40
}
37
41
42
+ const mounted = (
43
+ el : ( HTMLElement | SVGElement ) & { motionInstance ?: MotionInstance < string , MotionVariants < T > > } ,
44
+ binding : DirectiveBinding ,
45
+ node : VNode < any , ( HTMLElement | SVGElement ) & { motionInstance ?: MotionInstance < string , MotionVariants < T > > } , Record < string , any > > ,
46
+ ) => {
47
+ // Visibility hooks
48
+ el . motionInstance && registerVisibilityHooks ( el . motionInstance )
49
+ }
50
+
38
51
return {
39
52
created : register ,
53
+ mounted,
40
54
getSSRProps ( binding , node ) {
41
55
// Get initial value from binding
42
56
let { initial : bindingInitial } = binding . value || ( node && node ?. props ) || { }
0 commit comments