React lifecycle controlled motion library for smooth enter/leave transitions and list animations
npx @tessl/cli install tessl/npm-rc-motion@2.9.0RC Motion is a React lifecycle controlled motion library that provides smooth, performant animations for enter/leave transitions, appearance animations, and list animations. It offers both CSS-based animations through dynamic class names and JavaScript-controlled animations through callback hooks, with automatic cleanup and cross-browser compatibility.
npm install rc-motionimport CSSMotion, { CSSMotionList, Provider } from "rc-motion";
import type { CSSMotionProps, CSSMotionListProps, MotionEventHandler } from "rc-motion";For CommonJS:
const CSSMotion = require("rc-motion");
const { CSSMotionList, Provider } = require("rc-motion");import CSSMotion from "rc-motion";
// Simple visibility toggle with CSS transitions
function AnimatedBox() {
const [visible, setVisible] = useState(true);
return (
<CSSMotion
visible={visible}
motionName="fade"
motionAppear
onAppearStart={(element) => {
console.log('Animation started');
}}
>
{({ style, className }) => (
<div style={style} className={className}>
Animated content
</div>
)}
</CSSMotion>
);
}RC Motion is built around several key components:
Core functionality for animating individual React elements with enter/leave/appear transitions. Supports both CSS-based animations and JavaScript motion callbacks.
export default function CSSMotion(props: CSSMotionProps): React.ReactElement;
interface CSSMotionProps {
motionName?: MotionName;
visible?: boolean;
motionAppear?: boolean;
motionEnter?: boolean;
motionLeave?: boolean;
motionDeadline?: number;
children?: (
props: {
visible?: boolean;
className?: string;
style?: React.CSSProperties;
[key: string]: any;
},
ref: (node: any) => void,
) => React.ReactElement;
}
type MotionName = string | {
appear?: string;
enter?: string;
leave?: string;
appearActive?: string;
enterActive?: string;
leaveActive?: string;
};Specialized component for animating arrays of keyed elements with smooth add/remove/reorder transitions. Automatically handles element lifecycle and position changes.
export function CSSMotionList(props: CSSMotionListProps): React.ReactElement;
interface CSSMotionListProps extends Omit<CSSMotionProps, 'onVisibleChanged' | 'children'> {
keys: (React.Key | { key: React.Key; [name: string]: any })[];
component?: string | React.ComponentType | false;
onVisibleChanged?: (visible: boolean, info: { key: React.Key }) => void;
onAllRemoved?: () => void;
children?: (
props: {
visible?: boolean;
className?: string;
style?: React.CSSProperties;
index?: number;
[key: string]: any;
},
ref: (node: any) => void,
) => React.ReactElement;
}Context provider for configuring motion behavior across your entire application. Enables performance optimizations and global motion controls.
export function Provider(props: MotionContextProps & { children?: React.ReactNode }): React.ReactElement;
interface MotionContextProps {
motion?: boolean;
}// Motion event handlers
type MotionEventHandler = (
element: HTMLElement,
event: MotionEvent,
) => React.CSSProperties | void;
type MotionEndEventHandler = (
element: HTMLElement,
event: MotionEvent,
) => boolean | void;
type MotionPrepareEventHandler = (
element: HTMLElement,
) => Promise<any> | void;
// Motion event with deadline support
type MotionEvent = (TransitionEvent | AnimationEvent) & {
deadline?: boolean;
};
// Motion and step status constants
type MotionStatus = 'none' | 'appear' | 'enter' | 'leave';
type StepStatus = 'none' | 'prepare' | 'start' | 'active' | 'end' | 'prepared';