Skip to content

Commit adaca46

Browse files
authored
Merge pull request #51 from unovue/feat/animation-control
feat: improve pan interaction and animation state management
2 parents a0518e4 + 6f792ba commit adaca46

File tree

4 files changed

+16
-6
lines changed

4 files changed

+16
-6
lines changed

Diff for: docs/components/demo/pan/index.vue

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ function template({ rotateY, rotateX }: TransformProperties) {
88
99
const controls = useAnimationControls()
1010
function handlePan(_: PointerEvent, info: PanInfo) {
11-
_.preventDefault()
1211
controls.set({
1312
rotateY: info.offset.x / 2,
1413
rotateX: -info.offset.y / 2,
@@ -26,7 +25,7 @@ function handlePanEnd() {
2625
<template>
2726
<div class="flex flex-col items-center justify-center ">
2827
<Motion
29-
class="card"
28+
class="card touch-none"
3029
:transform-template="template"
3130
:animate="controls"
3231
@pan="handlePan"

Diff for: packages/motion/src/animation/hooks/animation-controls.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,21 @@ import type { Options } from '@/types'
44
import { invariant } from 'hey-listen'
55
import { setTarget } from 'framer-motion/dist/es/render/utils/setters.mjs'
66
import type { VisualElement } from 'framer-motion'
7+
import { resolveVariant } from '@/state/utils'
78

89
function stopAnimation(visualElement: VisualElement) {
910
visualElement.values.forEach(value => value.stop())
1011
}
1112

13+
function setStateTarget(state: MotionState, definition: Options['animate']) {
14+
const resolvedVariant = resolveVariant(definition, state.options.variants, state.options.custom)
15+
Object.entries(resolvedVariant).forEach(([key, value]) => {
16+
if (key === 'transition')
17+
return
18+
state.target[key] = value
19+
})
20+
}
21+
1222
/**
1323
* @public
1424
*/
@@ -85,6 +95,7 @@ export function setValues(
8595
return setVariants(state, [definition])
8696
}
8797
else {
98+
setStateTarget(state, definition)
8899
setTarget(state.visualElement, definition as any)
89100
}
90101
}
@@ -95,7 +106,7 @@ function setVariants(state: MotionState, variantLabels: string[]) {
95106
reversedLabels.forEach((key) => {
96107
const variant = visualElement.getVariant(key)
97108
variant && setTarget(visualElement, variant)
98-
109+
setStateTarget(state, variant)
99110
if (visualElement.variantChildren) {
100111
visualElement.variantChildren.forEach((child) => {
101112
setVariants(mountedStates.get(child.current as HTMLElement), variantLabels)

Diff for: packages/motion/src/state/animate-updates.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -117,8 +117,8 @@ function createAnimationFactories(
117117
): AnimationFactory[] {
118118
const factories: AnimationFactory[] = []
119119

120-
new Set(Object.keys(this.target)).forEach((key: any) => {
121-
if (!hasChanged(this.visualElement.getValue(key), this.target[key]))
120+
Object.keys(this.target).forEach((key: any) => {
121+
if (!hasChanged(prevTarget[key], this.target[key]))
122122
return
123123

124124
this.baseTarget[key] ??= style.get(this.element, key) as string

Diff for: playground/nuxt/pages/pan.vue

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ function handlePanEnd() {
2626
<template>
2727
<div class="flex flex-col items-center justify-center ">
2828
<Motion
29-
class="card"
29+
class="card touch-none"
3030
:transform-template="template"
3131
:animate="controls"
3232
@pan="handlePan"

0 commit comments

Comments
 (0)