@@ -8,16 +8,19 @@ import { resolveVariants } from '../utils/directive'
8
8
import { variantToStyle } from '../utils/transform'
9
9
import { registerVisibilityHooks } from '../features/visibilityHooks'
10
10
11
- export function directive < T extends string > ( variants ?: MotionVariants < T > ) : Directive < HTMLElement | SVGElement > {
11
+ export function directive < T extends string > ( variants ?: MotionVariants < T > , isPreset = false ) : Directive < HTMLElement | SVGElement > {
12
12
const register = ( el : HTMLElement | SVGElement , binding : DirectiveBinding , node : VNode < any , HTMLElement | SVGElement , Record < string , any > > ) => {
13
13
// Get instance key if possible (binding value or element key in case of v-for's)
14
14
const key = ( binding . value && typeof binding . value === 'string' ? binding . value : node . key ) as string
15
15
16
16
// Cleanup previous motion instance if it exists
17
17
if ( key && motionState [ key ] ) motionState [ key ] . stop ( )
18
18
19
+ // We deep copy presets to prevent global mutation
20
+ const variantsObject = isPreset ? structuredClone ( variants || { } ) : variants || { }
21
+
19
22
// Initialize variants with argument
20
- const variantsRef = ref ( variants || { } ) as Ref < MotionVariants < T > >
23
+ const variantsRef = ref ( variantsObject ) as Ref < MotionVariants < T > >
21
24
22
25
// Set variants from v-motion binding
23
26
if ( typeof binding . value === 'object' ) variantsRef . value = binding . value
@@ -58,7 +61,7 @@ export function directive<T extends string>(variants?: MotionVariants<T>): Direc
58
61
bindingInitial = unref ( bindingInitial )
59
62
60
63
// Merge it with directive initial variants
61
- const initial = defu ( variants ?. initial || { } , bindingInitial || { } )
64
+ const initial = defu ( { } , variants ?. initial || { } , bindingInitial || { } )
62
65
63
66
// No initial
64
67
if ( ! initial || Object . keys ( initial ) . length === 0 ) return
0 commit comments