Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement skipAnimations option in MotionConfig (#2671) #2675

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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