Skip to content

Commit 87f50ee

Browse files
authored
Merge pull request #170 from motiondivision/fix/use-transform
fix: useTransform not updating when reactive value changes
2 parents 794f856 + bf0448a commit 87f50ee

File tree

3 files changed

+33
-4
lines changed

3 files changed

+33
-4
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { describe, expect, it, vi } from 'vitest'
2+
import { mount } from '@vue/test-utils'
3+
import { Motion } from '@/components'
4+
import { defineComponent, nextTick, onMounted, ref, watchEffect } from 'vue'
5+
import { useTransform } from '@/value/use-transform'
6+
import { delay } from '@/shared/test'
7+
8+
describe('useTransform', () => {
9+
it('should update when reactive value changes', async () => {
10+
const Component = defineComponent({
11+
setup() {
12+
const x = ref(0)
13+
const transform = useTransform(() => {
14+
return x.value
15+
})
16+
onMounted(() => {
17+
x.value = 100
18+
})
19+
return () => {
20+
return <Motion style={{ x: transform }} />
21+
}
22+
},
23+
})
24+
const wrapper = mount(Component)
25+
await delay(100)
26+
expect(wrapper.html()).toContain('100')
27+
})
28+
})

packages/motion/src/value/use-combine-values.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,6 @@ export function useCombineMotionValues<T>(
3939
subscribe,
4040
unsubscribe,
4141
value,
42+
updateValue,
4243
}
4344
}

packages/motion/src/value/use-computed.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useCombineMotionValues } from '@/value/use-combine-values'
22
import { type MotionValue, collectMotionValues } from 'framer-motion/dom'
3-
import { onBeforeUpdate } from 'vue'
3+
import { watchEffect } from 'vue'
44

55
export function useComputed<T>(computed: () => T): MotionValue<T> {
66
/**
@@ -9,16 +9,16 @@ export function useComputed<T>(computed: () => T): MotionValue<T> {
99
*/
1010
collectMotionValues.current = []
1111

12-
const { value, subscribe, unsubscribe } = useCombineMotionValues<T>(computed)
12+
const { value, subscribe, unsubscribe, updateValue } = useCombineMotionValues<T>(computed)
1313

1414
subscribe(collectMotionValues.current)
1515

1616
collectMotionValues.current = undefined
1717

18-
onBeforeUpdate(() => {
18+
watchEffect(() => {
1919
unsubscribe()
2020
collectMotionValues.current = []
21-
computed()
21+
updateValue()
2222
subscribe(collectMotionValues.current)
2323
collectMotionValues.current = undefined
2424
})

0 commit comments

Comments
 (0)