Motion provider
LazyMotion in strict mode with domMax loaded async. Wrap the app in it: every animated component imports m from motion/react-m.
Mount it once around the app, in the root layout. Under it, animate with m from motion/react-m and take springs from @khaeli/motion-tokens. It follows the reader's reduced-motion setting: movement and layout changes land at once, fades still play.
A child animating
"use client"
import { useState } from "react"
import * as m from "motion/react-m"
import { Button } from "@/components/ui/button"
import { MotionProvider } from "@/components/ui/motion-provider"
import { motionTokens } from "@/lib/motion-tokens"
export default function MotionProviderAChildAnimating() {
const [moved, setMoved] = useState(false)
return (
<MotionProvider>
<div className="flex w-full max-w-xs flex-col gap-4">
<div className={`flex rounded-control bg-muted p-1 ${moved ? "justify-end" : "justify-start"}`}>
<m.div
layout
animate={{ opacity: moved ? 1 : 0.5 }}
transition={motionTokens.spring.smooth}
className="size-8 rounded-small bg-primary"
/>
</div>
<Button variant="outline" className="self-start" onClick={() => setMoved(!moved)}>
{moved ? "Move back" : "Move"}
</Button>
</div>
</MotionProvider>
)
}
Installation
npx shadcn@latest add @khaeli/motion-providerbunx --bun shadcn@latest add @khaeli/motion-providerInstall @khaeli/foundation first: this item's styling lives in its tokens and [data-slot] rules.
Installs with
- npm packages
- motion ^12.23.12
Registry JSON
The item as the shadcn CLI reads it, with every file it ships: https://ui.khaeli.com/r/motion-provider.json