Skip to content

Commit

Permalink
Implement skipAnimations option in MotionConfig (#2671)
Browse files Browse the repository at this point in the history
  • Loading branch information
ccapndave committed Jun 11, 2024
1 parent 5425384 commit d0b5f56
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ export const animateMotionValue =

if (
instantAnimationState.current ||
element?.skipAnimations ||
MotionGlobalConfig.skipAnimations
) {
shouldSkip = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export function MotionConfig({
*/
const context = useMemo(
() => config,
[JSON.stringify(config.transition), config.transformPagePoint, config.reducedMotion]
[JSON.stringify(config.transition), config.transformPagePoint, config.reducedMotion, config.skipAnimations]
)

return (
Expand Down
6 changes: 6 additions & 0 deletions packages/framer-motion/src/context/MotionConfigContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ export interface MotionConfigContext {
*/
reducedMotion?: ReducedMotionConfig

/**
* If true, will disable all motion component animations within the `MotionContext`.
*/
skipAnimations?: boolean

/**
* A custom `nonce` attribute used when wanting to enforce a Content Security Policy (CSP).
* For more details see:
Expand All @@ -51,4 +56,5 @@ export const MotionConfigContext = createContext<MotionConfigContext>({
transformPagePoint: (p) => p,
isStatic: false,
reducedMotion: "never",
skipAnimations: false,
})
11 changes: 11 additions & 0 deletions packages/framer-motion/src/motion/utils/use-visual-element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export function useVisualElement<Instance, RenderState>(
const lazyContext = useContext(LazyContext)
const presenceContext = useContext(PresenceContext)
const reducedMotionConfig = useContext(MotionConfigContext).reducedMotion
const skipAnimations = useContext(MotionConfigContext).skipAnimations

const visualElementRef = useRef<VisualElement<Instance>>()

Expand All @@ -40,6 +41,7 @@ export function useVisualElement<Instance, RenderState>(
? presenceContext.initial === false
: false,
reducedMotionConfig,
skipAnimations,
})
}

Expand Down Expand Up @@ -96,5 +98,14 @@ export function useVisualElement<Instance, RenderState>(
}
})

/**
* Keep `skipAnimations` in sync with the context.
*/
useEffect(() => {
if (!visualElement) return

visualElement.skipAnimations = skipAnimations ?? false
}, [skipAnimations])

return visualElement
}
7 changes: 7 additions & 0 deletions packages/framer-motion/src/render/VisualElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,11 @@ export abstract class VisualElement<
*/
blockInitialAnimation: boolean

/**
* Decides whether this Visual element should skip its animations.
*/
skipAnimations: boolean = false

/**
* A reference to this VisualElement's projection node, used in layout animations.
*/
Expand Down Expand Up @@ -374,6 +379,7 @@ export abstract class VisualElement<
props,
presenceContext,
reducedMotionConfig,
skipAnimations,
blockInitialAnimation,
visualState,
}: VisualElementOptions<Instance, RenderState>,
Expand All @@ -389,6 +395,7 @@ export abstract class VisualElement<
this.presenceContext = presenceContext
this.depth = parent ? parent.depth + 1 : 0
this.reducedMotionConfig = reducedMotionConfig
this.skipAnimations = skipAnimations ?? false
this.options = options
this.blockInitialAnimation = Boolean(blockInitialAnimation)

Expand Down
1 change: 1 addition & 0 deletions packages/framer-motion/src/render/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export type VisualElementOptions<Instance, RenderState = any> = {
props: MotionProps
blockInitialAnimation?: boolean
reducedMotionConfig?: ReducedMotionConfig
skipAnimations?: boolean
}

/**
Expand Down

0 comments on commit d0b5f56

Please sign in to comment.