For ease of use, the motion component automatically loads its features on demand.
When using motion, animation and gesture features are loaded with the component. Additionally, drag and layout animations will be loaded on demand if requested. The first load in either case is around 33kb.
With LazyMotion and the m component, we can reduce this to about 5kb for the initial render and then sync or async load a subset of features.
import { LazyMotion, domAnimation } from "motion/react"
import * as m from "motion/react-m"
export const MyComponent = ({ isVisible }) => (
<LazyMotion features={domAnimation}>
<m.div animate={{ opacity: 1 }} />
</LazyMotion>
)Read the Reduce bundle size guide for full usage instructions.
Props
features
Define a feature bundle to load sync or async.
Sync loading
Synchronous loading is useful for defining a subset of functionality for a smaller bundlesize.
import { LazyMotion, domAnimation } from "motion/react"
import * as m from "motion/react-m"
export const MyComponent = ({ isVisible }) => (
<LazyMotion features={domAnimation}>
<m.div animate={{ opacity: 1 }} />
</LazyMotion>
)Async loading
Asynchronous loading can ensure your site is hydrated before loading in some or all animation functionality.
// features.js
import { domAnimation } from "motion/react"
export default domAnimation
// index.js
const loadFeatures = () => import("./features.js")
.then(res => res.default)
function Component() {
return (
<LazyMotion features={loadFeatures}>
<m.div animate={{ scale: 1.5 }} />
</LazyMotion>
)
}strict
Default: false
If true, will throw an error if a motion component renders within a LazyMotion component (thereby removing the bundlesize benefits of lazy-loading).
// This component will throw an error that explains using a motion component
// instead of the m component will break the benefits of code-splitting.
function Component() {
return (
<LazyMotion features={domAnimation} strict>
<motion.div />
</LazyMotion>
)
}