Skip to content

Commit 90c7f5b

Browse files
authored
fix(useAnimate): respect immediate: false with conditionally rendered elements (#4947)
1 parent f6e8898 commit 90c7f5b

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

packages/core/useAnimate/index.browser.test.ts

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { mount } from '@vue/test-utils'
1+
import { flushPromises, mount } from '@vue/test-utils'
22
import { useAnimate } from '@vueuse/core'
33
import { describe, expect, it, vi } from 'vitest'
44
import { defineComponent, shallowRef } from 'vue'
@@ -73,4 +73,34 @@ describe('useAnimate', () => {
7373

7474
wrapper.unmount()
7575
})
76+
77+
it('should not automatically start the animation when shown if `immediate` is false', async () => {
78+
const wrapper = mount(defineComponent({
79+
template: '<p v-if="show" ref="el">test</p>',
80+
setup() {
81+
const show = shallowRef(false)
82+
const el = shallowRef<HTMLElement>()
83+
const animate = useAnimate(el, { transform: 'rotate(360deg)' }, {
84+
duration: 100,
85+
immediate: false,
86+
})
87+
88+
return { ...animate, el, show }
89+
},
90+
}))
91+
92+
const vm = wrapper.vm
93+
94+
// It is initially hidden
95+
expect(vm.animate).toBeUndefined()
96+
97+
// Toggle element into view
98+
vm.show = true
99+
await flushPromises()
100+
101+
// It should not have started automatically
102+
expect(vm.animate?.playState).toBe('paused')
103+
104+
wrapper.unmount()
105+
})
76106
})

packages/core/useAnimate/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ export function useAnimate(
225225

226226
watch(() => unrefElement(target), (el) => {
227227
if (el) {
228-
update()
228+
update(true)
229229
}
230230
else {
231231
animate.value = undefined

0 commit comments

Comments
 (0)