Skip to content

Repository files navigation

@rc-component/motion

Ant Design Part of the Ant Design ecosystem.

🎞️ Lifecycle-driven motion primitives for React enter, leave, and list animations.

NPM versionnpm downloadsbuild statusCodecovbundle sizedumi

English | 简体中文

Highlights

  • Declarative CSSMotion component for appear, enter, and leave states.
  • CSSMotionList for keyed list transitions.
  • CSS class lifecycle hooks and inline style patching callbacks.
  • Optional deadline fallback when transition or animation events do not fire.
  • TypeScript definitions and React ref support.
  • Used by Ant Design components that need predictable motion lifecycles.

Install

npm install @rc-component/motion

Usage

importCSSMotionfrom'@rc-component/motion';exportdefault({ visible }: {visible: boolean})=>(<CSSMotionvisible={visible}motionName="fade">{({ className, style },ref)=>(<divref={ref}className={className}style={style}>
Content
</div>)}</CSSMotion>);
import{CSSMotionList}from'@rc-component/motion';exportdefault({ keys }: {keys: string[]})=>(<CSSMotionListkeys={keys}motionName="fade">{({ key, className, style },ref)=>(<divref={ref}key={key}className={className}style={style}>{key}</div>)}</CSSMotionList>);

Examples

Run the local dumi site:

npm install
npm start

Then open http://localhost:8000.

API

CSSMotion

PropertyTypeDefaultDescription
children(props, ref) => ReactElement-Render function that receives motion class, style, and ref.
forceRenderbooleanfalseKeep the element rendered even when invisible.
leavedClassNamestring-Class name applied after leave when the element remains.
motionAppearbooleantrueEnable appear motion.
motionDeadlinenumber-Fallback timeout in milliseconds for motion completion.
motionEnterbooleantrueEnable enter motion.
motionLeavebooleantrueEnable leave motion.
motionLeaveImmediatelyboolean-Trigger leave immediately after mount.
motionNamestring | MotionName-CSS class name prefix or per-phase class names.
removeOnLeavebooleantrueRemove the element after leave. Ignored when forceRender is set.
visiblebooleantrueControls whether the element is visible.
onAppearActiveMotionEventHandler-Triggered during appear active phase.
onAppearEndMotionEndEventHandler-Triggered when appear finishes. Return false to keep waiting.
onAppearPrepareMotionPrepareEventHandler-Prepare callback before appear starts.
onAppearStartMotionEventHandler-Triggered when appear starts.
onEnterActiveMotionEventHandler-Triggered during enter active phase.
onEnterEndMotionEndEventHandler-Triggered when enter finishes. Return false to keep waiting.
onEnterPrepareMotionPrepareEventHandler-Prepare callback before enter starts.
onEnterStartMotionEventHandler-Triggered when enter starts.
onLeaveActiveMotionEventHandler-Triggered during leave active phase.
onLeaveEndMotionEndEventHandler-Triggered when leave finishes. Return false to keep waiting.
onLeavePrepareMotionPrepareEventHandler-Prepare callback before leave starts.
onLeaveStartMotionEventHandler-Triggered when leave starts.
onVisibleChanged(visible: boolean) => void-Triggered after the final visible state changes.

CSSMotionList

CSSMotionList accepts the motion props above, except children is a list render function.

PropertyTypeDefaultDescription
children(props, ref) => ReactElement-Render function for each keyed item.
componentstring | ComponentType | falsedivWrapper component. Use false for no wrapper.
keys(React.Key | { key: React.Key })[]-Keys to animate.
onAllRemoved() => void-Triggered after every leaving item is removed.
onVisibleChanged(visible, info: { key: React.Key }) => void-Triggered after an item visibility changes.

Ref

Ref methodTypeDescription
enableMotion() => booleanWhether motion is currently enabled.
inMotion() => booleanWhether the element is in a motion lifecycle.
nativeElementHTMLElementCurrent DOM element.

Development

npm install
npm start
npm test
npm run tsc
npm run compile
npm run build

The dumi site runs at http://localhost:8000 by default.

Release

npm run prepublishOnly

The release flow is handled by @rc-component/np through the rc-np command after the package build.

License

@rc-component/motion is released under the MIT license.

About

🎞️ Lifecycle-driven motion primitives for React enter, leave, and list animations.

Topics

Resources

Stars

176 stars

Watchers

5 watching

Forks

Releases

Sponsor this project

Packages

Contributors

Languages

Generated from react-component/footer