LazyMotion component enables code-splitting for Motion. Use it with the m component (instead of motion) to load only essential features synchronously, then load animations and gestures on-demand.
Usage
Synchronous Loading
Asynchronous Loading
Props
React children. Use the
m component instead of motion within LazyMotion.Feature bundle to load. Can be provided synchronously or asynchronously.Available bundles:
domAnimation- All animation features (~30kb)domMax- All features including layout animations (~50kb)
If
true, throws an error if a motion component (instead of m) is used within LazyMotion.Useful for enforcing code-splitting across your team.Feature Bundles
domAnimation
Includes animation and gesture features:- Animations
- Gestures (hover, tap, drag, pan)
- Variants
- Exit animations
domMax
Includes all features:- Everything in
domAnimation - Layout animations
- Shared layout animations
Examples
Async Loading with Suspense
Feature Bundle in Separate File
Strict Mode Enforcement
Conditional Features
Progressive Enhancement
TypeScript
Bundle Size Comparison
| Approach | Initial Bundle | Features |
|---|---|---|
motion | ~50kb | All features always loaded |
m + LazyMotion (sync) | ~20kb + 30kb | Features loaded synchronously |
m + LazyMotion (async) | ~20kb | Features loaded on-demand (~30kb) |
How It Works
- The
mcomponent loads only essential rendering features LazyMotionprovides animation/gesture features via context- Features are loaded synchronously or asynchronously
- All
mcomponents withinLazyMotionshare the loaded features
Notes
- Use
minstead ofmotionwhen usingLazyMotion - Only one
LazyMotionshould wrap your app (typically at the root) - Async loading may cause a flash if animations start before features load
strictmode helps catch accidentalmotionusage- Features are loaded once and cached
Related
- motion - Full-featured motion component
- bundle size optimization guide - Learn more about code-splitting
- MotionConfig - Configure default settings