SVG
@rootnative/inertia-svg adds animatable SVG primitives built on react-native-svg. It's an optional sibling package. Install it only when you need to morph a path or animate fill or stroke. The core library doesn't depend on react-native-svg.
MotionPath wraps <Path>. It accepts the same initial, animate, and transition shape as the core Motion.* primitives, with animatable keys for the path data (d) and for the color and numeric paint properties. The prebuilt MotionCircle, MotionRect, and MotionLine cover the other common shapes. The createMotionSvgComponent factory behind them wraps any react-native-svg element.
When to pick which
| You want | Reach for |
|---|---|
| Morph one shape into another (same command sequence) | MotionPath with animate.d — see structural compatibility |
| Switch between structurally different shapes | A remount via key={...}, not a morph |
| Animate a circle / rect / line's geometry or paint | MotionCircle / MotionRect / MotionLine |
| A "draw the stroke" effect (progress ring, signature) | Animate strokeDashoffset against a static strokeDasharray — see the progress-ring example |
Animate any other react-native-svg element (Ellipse, …) | createMotionSvgComponent |
| A gradient fill inside the SVG | <Defs> + <LinearGradient> from react-native-svg; the path references it by url(#id) — not this package |
| Animate a non-SVG view | The core Motion.* primitives — this package is for react-native-svg elements only |
Install
- Yarn
- npm
- pnpm
- Bun
yarn add @rootnative/inertia-svg react-native-svg
npm install @rootnative/inertia-svg react-native-svg
pnpm add @rootnative/inertia-svg react-native-svg
bun add @rootnative/inertia-svg react-native-svg
react-native-svg works in a bare React Native project and in Expo.
Usage
import Svg from 'react-native-svg'
import { MotionPath } from '@rootnative/inertia-svg'
function Toggle({ open }) {
return (
<Svg viewBox="0 0 100 100" width={120} height={120}>
<MotionPath
d="M 20 30 L 50 60 L 80 30 L 80 30 L 50 60 L 20 30"
animate={{
d: open
? 'M 20 30 L 50 60 L 80 30 L 80 30 L 50 60 L 20 30'
: 'M 20 50 L 50 20 L 80 50 L 80 50 L 50 80 L 20 50',
fill: open ? '#0ea5e9' : '#1f2937',
}}
transition={{ type: 'spring', tension: 140, friction: 12 }}
fill="#1f2937"
/>
</Svg>
)
}
The package also exports a MotionSvg namespace, which groups the components for autocomplete. MotionSvg.Path and MotionPath are the same component, so pick the import style that matches your codebase:
import { MotionSvg } from '@rootnative/inertia-svg'
;<MotionSvg.Path d={SOURCE_D} animate={{ d: TARGET_D }} />
The namespace also carries the prebuilt shapes: MotionSvg.Circle, MotionSvg.Rect, and MotionSvg.Line. See Shapes below.
The static d prop is required. Its command sequence is locked at the first render. Every target d that you pass in animate or initial must give the same command letters in the same order after implicit-repeat expansion. Element-wise scalar interpolation is the whole morphing model.
Structural compatibility
Two paths can morph only when their normalized templates (the command letters, in order, with the case kept) are equal. Examples:
✅ M 0 0 L 10 10 Z ↔ M 50 50 L 80 80 Z same: M L Z
✅ M 0 0 L 10 10 L 20 20 ↔ M 0 0 50 50 60 60 same: M L L
(implicit repeats after M expand to L)
❌ M 0 0 L 10 10 Z ↔ M 0 0 L 10 10 differs: count
❌ M 0 0 L 10 10 ↔ M 0 0 l 10 10 differs: absolute L vs relative l
❌ M 0 0 L 10 10 ↔ M 0 0 C 1 1 2 2 3 3 differs: L vs C
The component throws in dev when the templates differ: at mount (if initial.d doesn't match the static d), when animate.d changes its shape, or when the static d prop itself changes its shape between renders. In production, the component skips the bad target instead. The path keeps its current d, and a mismatched initial.d falls back to the static d, so one bad value doesn't crash the screen. Treat a dev error as a defect.
To switch between shapes with a different structure, remount with a new key:
<MotionPath
key={open ? 'hexagon' : 'circle'}
d={open ? HEXAGON_D : CIRCLE_AS_QUADS_D}
/>
Path normalization that resamples between arbitrary shapes (the approach of flubber) is out of scope by design.
Animatable props
| Key | Shape | Notes |
|---|---|---|
d | string | Path data. Source and target must share the same template — see above. Each scalar param springs / times independently. |
fill | string | Color, interpolated via Reanimated's color setter. Defaults to TRANSPARENT (rgba(0, 0, 0, 0)) if neither static nor initial is supplied. The SVG keyword none maps to TRANSPARENT, because Reanimated's color processor rejects it. |
stroke | string | Same as fill. |
strokeWidth | number | Numeric. Default seed 1. |
strokeOpacity | number | Numeric, 0–1. |
fillOpacity | number | Numeric, 0–1. |
opacity | number | Numeric, 0–1. |
strokeDashoffset | number | Useful for "draw" animations on a dashed stroke. |
Per-property transitions work the same as in the core primitives:
<MotionPath
d={SOURCE_D}
fill="#000"
animate={{ d: TARGET_D, fill: '#7c3aed' }}
transition={{
d: { type: 'spring', tension: 160, friction: 14 },
fill: { type: 'timing', duration: 300 },
}}
/>
initial
Pass initial to override the values of the mount frame, so the component starts somewhere other than the static props. Pass initial={false} to start at the animate target with no mount animation.
<MotionPath
d={STAR_D}
initial={{ d: TINY_STAR_D, opacity: 0 }} // hatch from a smaller star, fading in
animate={{ d: STAR_D, opacity: 1 }}
/>
<MotionPath
d={STAR_D}
initial={false} // skip the mount animation
animate={{ d: HEART_D }}
/>
If you pass initial.d, its template must match the static d, the same rule as for animate.d.
Reduced motion
MotionPath follows <MotionConfig reducedMotion> the same as the core primitives. When the reduce-motion setting of the OS is on, or you pass reducedMotion="always", a transition resolves as a direct assignment instead of withSpring or withTiming.
Shapes — MotionCircle, MotionRect, MotionLine
Prebuilt animatable wrappers for the common shapes that aren't paths, built with the factory below. Each one accepts the same initial, animate, and transition shape as MotionPath. transition also accepts a named transition from the nearest <MotionConfig transitions>, top-level and per property.
| Component | Numeric props | Color props | Array props |
|---|---|---|---|
MotionCircle | cx, cy, r, strokeWidth, strokeOpacity, fillOpacity, opacity, strokeDashoffset | fill, stroke | strokeDasharray |
MotionRect | x, y, width, height, rx, ry, strokeWidth, strokeOpacity, fillOpacity, opacity, strokeDashoffset | fill, stroke | strokeDasharray |
MotionLine | x1, y1, x2, y2, strokeWidth, strokeOpacity, opacity, strokeDashoffset | stroke | strokeDasharray |
The canonical use of MotionCircle is a circular progress ring: a static strokeDasharray of the circumference with an animated strokeDashoffset:
import Svg from 'react-native-svg'
import { MotionCircle } from '@rootnative/inertia-svg'
const CIRCUMFERENCE = 2 * Math.PI * 45
function ProgressRing({ progress }: { progress: number }) {
return (
<Svg viewBox="0 0 100 100" width={64} height={64}>
<MotionCircle
cx={50}
cy={50}
r={45}
stroke="#0ea5e9"
strokeWidth={8}
fill="none"
strokeDasharray={[CIRCUMFERENCE]}
strokeDashoffset={CIRCUMFERENCE}
animate={{ strokeDashoffset: CIRCUMFERENCE * (1 - progress) }}
transition={{ type: 'timing', duration: 300 }}
/>
</Svg>
)
}
The rules for the shapes mirror the command-sequence lock of MotionPath:
- An array prop locks its length at the first render.
strokeDasharrayinterpolates element-wise, so a target with a different length throws in dev, and production ignores it. Remount with a newkeyto change the length. - A key animates only when it's present at mount, in the static props, in
initial, or inanimate. A key that entersanimateafter mount warns in dev, and the component ignores it. A key that nothing names passes through as an ordinary static prop, so an attribute that you don't animate keeps its element default. fill="none"andstroke="none"are safe. A staticnonewith noinitialand noanimatefor that key stays a static prop. Anoneininitialor inanimatemaps toTRANSPARENT, which paints the same nothing. The color processor of Reanimated rejects the keyword, so the adapter never sends it throughanimatedProps.- A numeric key that only
animatenames seeds from0. A color seeds fromTRANSPARENT(rgba(0, 0, 0, 0), exported from@rootnative/inertia). Give the key a static prop or aninitialvalue when the mount animation must start elsewhere.
createMotionSvgComponent
The factory behind the prebuilt shapes. Point it at any react-native-svg element, and declare which props animate and how each one interpolates:
import { Ellipse } from 'react-native-svg'
import { createMotionSvgComponent } from '@rootnative/inertia-svg'
const MotionEllipse = createMotionSvgComponent(Ellipse, {
animatableProps: ['cx', 'cy', 'rx', 'ry', 'opacity'], // numeric
colorProps: ['fill', 'stroke'], // color strings
arrayProps: ['strokeDasharray'], // numeric arrays, length locked at mount
})
<MotionEllipse
cx={50} cy={50} rx={10} ry={20}
animate={{ rx: 30, fill: '#7c3aed' }}
transition={{ type: 'spring', tension: 180, friction: 14 }}
/>
TypeScript infers the prop keys from the wrapped component, so a typo in animatableProps is a compile error, and the animate and initial of the generated component accept only the declared keys. Everything above about the shapes (named transitions, per-property transitions, the initial rules, reduced motion, the mount locks) comes from the factory and applies to any component that it builds.
MotionPath stays hand-written, because path morphing needs the d template machinery that the generic factory doesn't model.
What this package doesn't do
- Path resampling between shapes with a different structure. Only a morph with the same template works. For the rest, remount with
key. - A gradient fill inside the SVG. For a gradient fill, use
<Defs>and<LinearGradient>fromreact-native-svg, and animate the stops with the patterns ofMotionLinearGradient. The path only references the gradient byurl(#id). - Path command interpolation (for example, a morph from an
Lto aC). Element-wise scalar interpolation is intentional. It's predictable, cheap, and matches what designers want 95% of the time.
Path utilities
The package exports the tokenizer and the template helpers for downstream tooling:
import {
parsePathD,
templateOf,
diffTemplate,
flattenParams,
serializePath,
} from '@rootnative/inertia-svg'
const segs = parsePathD('M 0 0 L 10 10 Z') // [{ cmd: 'M', args: [0, 0] }, …]
const t = templateOf(segs) // { cmds: ['M','L','Z'], widths: [2,2,0], size: 4 }
const params = flattenParams(segs) // [0, 0, 10, 10]
const out = serializePath(t, [5, 5, 20, 20]) // 'M 5 5L 20 20Z'
const err = diffTemplate(t, templateOf(parsePathD('M 0 0 L 1 1')))
// → 'command count differs: …'
serializePath is a worklet. Call it inside useAnimatedProps when you build your own SVG primitives on this layer.