Skip to content

Commit eddd121

Browse files
authored
fix: visibility variants not triggered when visible on load (#171)
1 parent 4d0c622 commit eddd121

File tree

1 file changed

+16
-2
lines changed

1 file changed

+16
-2
lines changed

src/directive/index.ts

+16-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ import type { Directive, DirectiveBinding, Ref, VNode } from 'vue'
22
import defu from 'defu'
33
import { ref, unref } from 'vue'
44
import { motionState } from '../features/state'
5-
import type { MotionVariants } from '../types'
5+
import type { MotionInstance, MotionVariants } from '../types'
66
import { useMotion } from '../useMotion'
77
import { resolveVariants } from '../utils/directive'
88
import { variantToStyle } from '../utils/transform'
9+
import { registerVisibilityHooks } from '../features/visibilityHooks'
910

1011
export function directive<T extends string>(variants: MotionVariants<T> = {}): Directive<HTMLElement | SVGElement> {
1112
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
2425
// Resolve variants from node props
2526
resolveVariants<T>(node, variantsRef)
2627

28+
// Disable visibilityHooks, these will be registered in `mounted`
29+
const motionOptions = { eventListeners: true, lifeCycleHooks: true, syncVariants: true, visibilityHooks: false }
30+
2731
// Create motion instance
28-
const motionInstance = useMotion(el, variantsRef)
32+
const motionInstance = useMotion(el, variantsRef, motionOptions)
2933

3034
// Pass the motion instance via the local element
3135
// @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
3539
if (key) motionState[key] = motionInstance
3640
}
3741

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+
3851
return {
3952
created: register,
53+
mounted,
4054
getSSRProps(binding, node) {
4155
// Get initial value from binding
4256
let { initial: bindingInitial } = binding.value || (node && node?.props) || {}

0 commit comments

Comments
 (0)