Skip to content

Commit

Permalink
feat(usemotion): resolve instance from directives from useMotion
Browse files Browse the repository at this point in the history
  • Loading branch information
Tahul committed Mar 24, 2022
1 parent 53ef762 commit f952cd8
Showing 1 changed file with 18 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/useMotion.ts
@@ -1,6 +1,6 @@
import { watch } from 'vue-demi'
import type { MaybeRef } from '@vueuse/core'
import { tryOnUnmounted } from '@vueuse/core'
import { tryOnUnmounted, unrefElement, watchOnce } from '@vueuse/core'
import type {
MotionInstance,
MotionVariants,
Expand Down Expand Up @@ -75,5 +75,22 @@ export function useMotion<T extends MotionVariants>(

tryOnUnmounted(() => instance.stop())

// Merge instances if binding detected from directive
watchOnce(
() => unrefElement(target),
(el) => {
if (!el) return

// @ts-expect-error - We are checking if motionInstance has be bound via directive
if (el?.motionInstance) {
// Stop previous instance
instance.stop()

// @ts-expect-error - We are checking if motionInstance has be bound via directive
Object.assign(instance, el.motionInstance)
}
},
)

return instance
}

0 comments on commit f952cd8

Please sign in to comment.