Skip to content

Commit

Permalink
Skip animations (#2461)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattgperry authored Jan 2, 2024
1 parent b4f3003 commit 1651322
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 3 deletions.
15 changes: 14 additions & 1 deletion packages/framer-motion/src/animation/__tests__/animate.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { render } from "../../../jest.setup"
import * as React from "react"
import { useEffect } from "react"
import { motion } from "../.."
import { motion, MotionGlobalConfig } from "../.."
import { animate } from "../animate"
import { useMotionValue } from "../../value/use-motion-value"
import { motionValue, MotionValue } from "../../value"
Expand Down Expand Up @@ -228,6 +228,19 @@ describe("animate", () => {
})
})

test("Skips animations", async () => {
const div = document.createElement("div")
MotionGlobalConfig.skipAnimations = true
animate(div, { opacity: [0.2, 0.5] }, { duration: 1 })
await new Promise<void>((resolve) => {
setTimeout(() => {
MotionGlobalConfig.skipAnimations = false
expect(div).toHaveStyle("opacity: 0.5")
resolve()
}, 100)
})
})

test("time sets and gets time", async () => {
const div = document.createElement("div")
const animation = animate(div, { x: 100 }, { duration: 10 })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { getKeyframes } from "../utils/keyframes"
import { getValueTransition, isTransitionDefined } from "../utils/transitions"
import { animateValue } from "../animators/js"
import { AnimationPlaybackControls, ValueAnimationOptions } from "../types"
import { MotionGlobalConfig } from "../../utils/GlobalConfig"

export const animateMotionValue = (
valueName: string,
Expand Down Expand Up @@ -101,7 +102,8 @@ export const animateMotionValue = (
!isOriginAnimatable ||
!isTargetAnimatable ||
instantAnimationState.current ||
valueTransition.type === false
valueTransition.type === false ||
MotionGlobalConfig.skipAnimations
) {
/**
* If we can't animate this value, or the global instant animation flag is set,
Expand Down
6 changes: 5 additions & 1 deletion packages/framer-motion/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,10 @@ export { isMotionComponent } from "./motion/utils/is-motion-component"
export { unwrapMotionComponent } from "./motion/utils/unwrap-motion-component"
export { VisualElement } from "./render/VisualElement"
export { addScaleCorrector } from "./projection/styles/scale-correction"
export { useInstantTransition, disableInstantTransitions } from "./utils/use-instant-transition"
export {
useInstantTransition,
disableInstantTransitions,
} from "./utils/use-instant-transition"
export { useInstantLayoutTransition } from "./projection/use-instant-layout-transition"
export { useResetProjection } from "./projection/use-reset-projection"
export { buildTransform } from "./render/html/utils/build-transform"
Expand All @@ -88,6 +91,7 @@ export { color } from "./value/types/color"
export { complex } from "./value/types/complex"
export { px } from "./value/types/numbers/units"
export { ValueType } from "./value/types/types"
export { MotionGlobalConfig } from "./utils/GlobalConfig"

/**
* Appear animations
Expand Down
3 changes: 3 additions & 0 deletions packages/framer-motion/src/utils/GlobalConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export const MotionGlobalConfig = {
skipAnimations: false,
}

0 comments on commit 1651322

Please sign in to comment.