A spring that solves your animation problems.
npx @tessl/cli install tessl/npm-react-motion@0.5.0React Motion is a spring-based animation library for React applications that uses spring physics instead of traditional easing curves and durations to create natural, fluid animations. The library provides three core components and utility functions for creating sophisticated animations that feel more responsive and natural when interrupted or adapted to changing conditions.
npm install --save react-motionimport { Motion, StaggeredMotion, TransitionMotion, spring, presets, stripStyle, reorderKeys } from 'react-motion';For CommonJS:
const { Motion, StaggeredMotion, TransitionMotion, spring, presets, stripStyle, reorderKeys } = require('react-motion');import React from 'react';
import { Motion, spring } from 'react-motion';
function AnimatedComponent() {
return (
<Motion defaultStyle={{x: 0}} style={{x: spring(10)}}>
{value => <div style={{transform: `translateX(${value.x}px)`}}>Animated!</div>}
</Motion>
);
}React Motion is built around several key principles:
Single element animations with spring physics. Ideal for simple state transitions, hover effects, and basic UI animations.
class Motion extends React.Component {
static propTypes = {
defaultStyle: PropTypes.objectOf(PropTypes.number),
style: PropTypes.objectOf(PropTypes.oneOfType([
PropTypes.number,
PropTypes.object,
])).isRequired,
children: PropTypes.func.isRequired,
onRest: PropTypes.func,
};
}Cascading animations where each element's motion depends on the previous element. Perfect for list transitions and domino effects.
class StaggeredMotion extends React.Component {
static propTypes = {
defaultStyles: PropTypes.arrayOf(PropTypes.objectOf(PropTypes.number)),
styles: PropTypes.func.isRequired,
children: PropTypes.func.isRequired,
};
}Advanced component for animating mounting and unmounting elements with full lifecycle control. Essential for dynamic lists and complex UI transitions.
class TransitionMotion extends React.Component {
static propTypes = {
defaultStyles: PropTypes.arrayOf(PropTypes.shape({
key: PropTypes.string.isRequired,
data: PropTypes.any,
style: PropTypes.objectOf(PropTypes.number).isRequired,
})),
styles: PropTypes.oneOfType([
PropTypes.func,
PropTypes.arrayOf(PropTypes.shape({
key: PropTypes.string.isRequired,
data: PropTypes.any,
style: PropTypes.objectOf(PropTypes.oneOfType([
PropTypes.number,
PropTypes.object,
])).isRequired,
}))
]).isRequired,
children: PropTypes.func.isRequired,
willEnter: PropTypes.func,
willLeave: PropTypes.func,
didLeave: PropTypes.func,
};
}Spring physics configuration system for controlling animation behavior.
function spring(val/*: number */, config/*?: SpringHelperConfig */)/*: OpaqueConfig */;
// Type definitions (Flow syntax):
/*::
type SpringHelperConfig = {
stiffness?: number,
damping?: number,
precision?: number,
};
type OpaqueConfig = {
val: number,
stiffness: number,
damping: number,
precision: number,
};
*/
// Preset configurations for spring physics
const presets = {
noWobble: {stiffness: 170, damping: 26}, // default
gentle: {stiffness: 120, damping: 14},
wobbly: {stiffness: 180, damping: 12},
stiff: {stiffness: 210, damping: 20},
};Helper functions for working with spring configurations and style objects.
function stripStyle(style/*: Style */)/*: PlainStyle */;
function reorderKeys()/*: void */; // DEPRECATEDstripStyle: Extracts plain numeric values from spring-configured style objects. Useful for getting initial values or converting spring styles to plain styles.
reorderKeys: Deprecated function that shows warning message. No longer needed for TransitionMotion's new styles array API.
// Core type definitions (Flow syntax):
/*::
// Style object that can contain numbers or spring configurations
type Style = {[key: string]: number | OpaqueConfig};
// Plain numeric style object (interpolated values)
type PlainStyle = {[key: string]: number};
// Spring configuration object (internal use)
type OpaqueConfig = {
val: number,
stiffness: number,
damping: number,
precision: number,
};
// Additional types for TransitionMotion
type TransitionStyle = {
key: string,
data?: any,
style: Style,
};
type TransitionPlainStyle = {
key: string,
data?: any,
style: PlainStyle,
};
*/performance-now: ^0.2.0 - High-resolution timing for animation framesprop-types: ^15.5.8 - Runtime type checking for React propsraf: ^3.1.0 - RequestAnimationFrame polyfill for older browsers^0.14.9 || ^15.3.0 || ^16.0.0