Convert Figma logo to code with AI

pmndrs logoreact-spring

✌️ A spring physics based React animation library

28,112
1,189
28,112
113

Top Related Projects

23,338

Open source, production-ready animation and gesture library for React

🎊 A collection of animations for inline style libraries

A spring that solves your animation problems.

✌️ A spring physics based React animation library

49,575

JavaScript animation engine

Quick Overview

The react-spring library is a popular and powerful animation library for React that provides a simple and intuitive API for creating smooth and performant animations. It is part of the pmndrs (Poimandres) organization, which is a collection of open-source React libraries and tools.

Pros

  • Simplicity: react-spring offers a straightforward and easy-to-use API, making it accessible for developers of all skill levels.
  • Performance: The library is built on top of the Web Animations API, which provides efficient and hardware-accelerated animations.
  • Flexibility: react-spring supports a wide range of animation types, including spring-based, keyframe-based, and more, allowing developers to create complex and customized animations.
  • Declarative Approach: The library's declarative API allows developers to focus on describing the desired animation, rather than the implementation details.

Cons

  • Learning Curve: While the API is relatively simple, there is still a learning curve for developers who are new to the library.
  • Dependency on React: react-spring is tightly coupled with the React ecosystem, which may be a limitation for developers working with other JavaScript frameworks.
  • Performance Considerations: While the library is generally performant, complex animations with a large number of elements may still require careful optimization.
  • Limited Community Support: Compared to some other popular React libraries, the react-spring community is relatively smaller, which may limit the availability of resources and support.

Code Examples

Example 1: Basic Spring Animation

import { useSpring, animated } from 'react-spring';

const MyComponent = () => {
  const props = useSpring({
    from: { opacity: 0, transform: 'translate3d(0, -100%, 0)' },
    to: { opacity: 1, transform: 'translate3d(0, 0, 0)' },
    config: { duration: 1000 },
  });

  return (
    <animated.div style={props}>
      <h1>Hello, React Spring!</h1>
    </animated.div>
  );
};

This example demonstrates a basic spring-based animation that fades in and slides up a heading.

Example 2: Keyframe Animation

import { useKeyframes } from 'react-spring';

const MyComponent = () => {
  const props = useKeyframes(() => [
    { opacity: 0, transform: 'scale(0.5)' },
    { opacity: 1, transform: 'scale(1)' },
  ], []);

  return (
    <animated.div style={props}>
      <h1>Keyframe Animation</h1>
    </animated.div>
  );
};

This example shows how to use the useKeyframes hook to create a simple scale animation using keyframes.

Example 3: Parallax Effect

import { useParallax } from 'react-spring';

const MyComponent = () => {
  const { ref, style } = useParallax({
    from: { y: 0 },
    to: { y: -100 },
  });

  return (
    <div ref={ref} style={style}>
      <h1>Parallax Effect</h1>
      <p>This content will move at a different speed as the page scrolls.</p>
    </div>
  );
};

This example demonstrates the use of the useParallax hook to create a parallax effect, where the content moves at a different speed than the page scroll.

Getting Started

To get started with react-spring, you can follow these steps:

  1. Install the library using npm or yarn:
npm install react-spring
# or
yarn add react-spring
  1. Import the necessary hooks or components from the library:
import { useSpring, animated } from 'react-spring';
  1. Use the hooks or components in your React components to create animations:
const MyComponent = () => {
  const props = useSpring({
    from: { opacity: 0 },
    to: { opacity: 1 },

Competitor Comparisons

23,338

Open source, production-ready animation and gesture library for React

Pros of Motion

  • More intuitive API with a declarative approach
  • Built-in support for gestures and drag interactions
  • Comprehensive documentation and examples

Cons of Motion

  • Larger bundle size
  • Limited support for complex physics-based animations

Code Comparison

Motion:

<motion.div
  initial={{ opacity: 0 }}
  animate={{ opacity: 1 }}
  transition={{ duration: 0.5 }}
>
  Hello World
</motion.div>

React Spring:

import { useSpring, animated } from 'react-spring'

const props = useSpring({ opacity: 1, from: { opacity: 0 } })
return <animated.div style={props}>Hello World</animated.div>

Key Differences

  • Motion uses a more declarative approach, while React Spring relies on hooks
  • Motion offers built-in gesture support, whereas React Spring requires additional libraries
  • React Spring provides more fine-grained control over physics-based animations
  • Motion has a larger ecosystem and more comprehensive documentation
  • React Spring has a smaller bundle size and may be more suitable for performance-critical applications

Both libraries are powerful tools for creating animations in React applications, with Motion offering a more user-friendly approach and React Spring providing more flexibility for complex animations.

🎊 A collection of animations for inline style libraries

Pros of react-animations

  • Simpler API, easier to learn and implement for basic animations
  • Lightweight and focused solely on CSS animations
  • Compatible with any CSS-in-JS library

Cons of react-animations

  • Limited to predefined keyframe animations
  • Less flexible for complex, dynamic animations
  • No built-in support for spring physics or advanced timing functions

Code Comparison

react-animations:

import { fadeIn } from 'react-animations';
import styled, { keyframes } from 'styled-components';

const fadeInAnimation = keyframes`${fadeIn}`;
const FadeInDiv = styled.div`animation: 1s ${fadeInAnimation};`;

react-spring:

import { useSpring, animated } from 'react-spring';

const props = useSpring({ opacity: 1, from: { opacity: 0 } });
return <animated.div style={props}>I will fade in</animated.div>;

react-spring offers a more dynamic and flexible approach, allowing for complex animations with physics-based spring animations. It provides better performance for continuous animations and supports interruptions and changes mid-animation. react-animations, on the other hand, is simpler to use for basic CSS keyframe animations but lacks the advanced features and flexibility of react-spring. Choose react-animations for straightforward, predefined animations, and react-spring for more complex, interactive, and performance-critical animations.

A spring that solves your animation problems.

Pros of react-motion

  • Simpler API with fewer concepts to learn
  • More predictable and consistent animations
  • Better performance for complex animations due to its approach

Cons of react-motion

  • Less flexible than react-spring for certain types of animations
  • Steeper learning curve for beginners
  • Limited built-in animation types compared to react-spring

Code Comparison

react-motion:

<Motion defaultStyle={{x: 0}} style={{x: spring(10)}}>
  {({x}) => <div style={{transform: `translateX(${x}px)`}} />}
</Motion>

react-spring:

const props = useSpring({from: {x: 0}, to: {x: 10}})
return <animated.div style={{transform: props.x.interpolate(x => `translateX(${x}px)`)}} />

Both libraries provide ways to create smooth animations in React applications. react-motion focuses on physics-based animations with a more opinionated approach, while react-spring offers greater flexibility and a wider range of animation types. react-spring has gained more popularity in recent years due to its versatility and easier integration with modern React practices. However, react-motion may still be preferred for specific use cases where its approach to animations is more suitable.

✌️ A spring physics based React animation library

Pros of react-spring

  • Mature and widely adopted animation library for React
  • Extensive documentation and community support
  • Flexible API supporting various animation styles

Cons of react-spring

  • Larger bundle size compared to newer alternatives
  • Learning curve for complex animations
  • Performance overhead for simpler animations

Code Comparison

react-spring:

import { useSpring, animated } from 'react-spring'

function AnimatedComponent() {
  const props = useSpring({ opacity: 1, from: { opacity: 0 } })
  return <animated.div style={props}>I will fade in</animated.div>
}

react-spring:

import { useSpring, animated } from 'react-spring'

function AnimatedComponent() {
  const props = useSpring({ opacity: 1, from: { opacity: 0 } })
  return <animated.div style={props}>I will fade in</animated.div>
}

Note: The code comparison shows identical usage as both repositories refer to the same project. The react-spring library is maintained under the pmndrs organization on GitHub, and there is no separate repository with a different implementation.

49,575

JavaScript animation engine

Pros of Anime

  • Lightweight and fast, with a small file size
  • Works with any JavaScript environment, not limited to React
  • Supports a wide range of animation types, including SVG animations

Cons of Anime

  • Less integrated with React ecosystem
  • May require more manual setup for complex animations in React applications
  • Doesn't provide built-in hooks or components for React integration

Code Comparison

Anime:

anime({
  targets: '.element',
  translateX: 250,
  rotate: '1turn',
  duration: 800,
  easing: 'easeInOutQuad'
});

React Spring:

import { useSpring, animated } from 'react-spring';

const props = useSpring({ x: 250, rotation: 360 });
return <animated.div style={{ transform: props.x.interpolate(x => `translateX(${x}px) rotate(${props.rotation.value}deg)`) }} />;

React Spring offers a more declarative approach within React components, while Anime provides a simpler API that can be used in any JavaScript context. React Spring's integration with React's lifecycle and state management can be advantageous for complex React applications, whereas Anime's flexibility makes it suitable for a broader range of projects.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README


react-spring

A spring-physics first animation library
giving you flexible tools to confidently cast your ideas


Chat on Discord


react-spring is a cross-platform spring-physics first animation library.

It's as simple as:

const styles = useSpring({
  from: {
    opacity: 0
  },
  to: {
    opacity: 1
  }
})

<animated.div style={styles} />

Just a small bit about us:

  • Cross-Platform: We support react-dom, react-native, react-three-fiber, react-konva & react-zdog.
  • Versatile: Be declarative with your animations or if you prefer, imperative.
  • Spring-Physics First: By default animation use springs for fluid interactivity, but we support durations with easings as well.

There's a lot more to be had! Give it a try and find out.

Getting Started

⚡️ Jump Start

# Install the entire library
npm install react-spring
# or just install your specific target (recommended)
npm install @react-spring/web
import { animated, useSpring } from '@react-spring/web'

const FadeIn = ({ isVisible, children }) => {
  const styles = useSpring({
    opacity: isVisible ? 1 : 0,
    y: isVisible ? 0 : 24,
  })

  return <animated.div style={styles}>{children}</animated.div>
}

It's as simple as that to create scroll-in animations when value of isVisible is toggled.

📖 Documentation and Examples

More documentation on the project can be found here.

Pages contain their own examples which you can check out there, or open in codesandbox for a more in-depth view!


📣 What others say

Used by

And many others...

Backers

Thank you to all our backers! 🙏 If you want to join them here, then consider contributing to our Opencollective.

Contributors

This project exists thanks to all the people who contribute.

NPM DownloadsLast 30 Days