Security News
RubyGems.org Adds New Maintainer Role
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
react-spring
Advanced tools
react-spring is a spring-physics-based animation library for React applications. It allows developers to create fluid and natural animations with ease, leveraging the power of physics to create realistic motion. The library is highly flexible and can be used for a variety of animation needs, from simple transitions to complex interactive animations.
Basic Animations
This feature allows you to create basic animations such as fading in and out. The `useSpring` hook is used to define the animation properties, and the `animated` component is used to apply these properties to a React element.
```jsx
import React from 'react';
import { useSpring, animated } from 'react-spring';
function BasicAnimation() {
const props = useSpring({
to: { opacity: 1 },
from: { opacity: 0 },
});
return <animated.div style={props}>I will fade in</animated.div>;
}
export default BasicAnimation;
```
Keyframe Animations
This feature allows you to create keyframe animations, where an element transitions through multiple states. The `to` property can be an async function that defines a sequence of animations.
```jsx
import React from 'react';
import { useSpring, animated } from 'react-spring';
function KeyframeAnimation() {
const props = useSpring({
from: { transform: 'translate3d(0,0,0)' },
to: async (next) => {
await next({ transform: 'translate3d(100px,0,0)' });
await next({ transform: 'translate3d(0,0,0)' });
},
});
return <animated.div style={props}>I will move</animated.div>;
}
export default KeyframeAnimation;
```
Gesture-based Animations
This feature allows you to create gesture-based animations, such as dragging. The `useDrag` hook from `react-use-gesture` is used in combination with `useSpring` to create a draggable element.
```jsx
import React from 'react';
import { useSpring, animated } from 'react-spring';
import { useDrag } from 'react-use-gesture';
function Draggable() {
const [props, set] = useSpring(() => ({ x: 0, y: 0 }));
const bind = useDrag(({ offset: [x, y] }) => set({ x, y }));
return <animated.div {...bind()} style={{ ...props, width: 100, height: 100, background: 'lightblue' }} />;
}
export default Draggable;
```
Framer Motion is a popular animation library for React that provides a simple API for creating animations and gestures. It is known for its ease of use and powerful features, such as layout animations and shared element transitions. Compared to react-spring, Framer Motion is more focused on providing a high-level API for common animation tasks, while react-spring offers more flexibility and control over the animation physics.
React Transition Group is a low-level animation library for React that provides more control over the transition states of components. It is often used for simple animations like fading, sliding, and collapsing. Compared to react-spring, React Transition Group is more focused on managing the lifecycle of animations and transitions, while react-spring provides more advanced physics-based animations.
React Move is an animation library for React that allows you to create complex animations using a declarative syntax. It is designed to work well with data-driven animations and provides a flexible API for creating custom animations. Compared to react-spring, React Move is more focused on data-driven animations and provides a different approach to defining animations using a declarative syntax.
npm install react-spring
Demo: https://codesandbox.io/embed/oln44nx8xq
React-spring is a wrapper around a cooked down fork of Facebooks animated. It is trying to bridge Chenglou's React-motion and animated as both have their pros and cons, but definitively could benefit from one another:
React-motion
Animated
This lib inherits React-motions api (Spring -> Motion, from -> defaultStyles, to -> styles) while you can feed it everything animated can take in as well as allowing for native animations.
import { Spring } from 'react-spring'
const TRIANGLE = 'M20,380 L380,380 L380,380 L200,20 L20,380 Z'
const RECTANGLE = 'M20,20 L20,380 L380,380 L380,20 L20,20 Z'
const RED = '#c23369'
const GREEN = '#28d79f'
const Content = ({ toggle, color, opacity, scale, path, start, stop, end }) => (
<div style={{ background: `linear-gradient(to bottom, ${start} ${stop}, ${end} 100%)` }}>
<svg
onClick={toggle}
style={{ opacity, transform: `scale3d(${scale}, ${scale}, ${scale})` }}
version="1.1"
viewBox="0 0 400 400">
<g fill={color} fillRule="evenodd">
<path id="path-1" d={path} />
</g>
</svg>
</div>
)
class App extends React.Component {
state = { toggle: true }
toggle = () => this.setState(state => ({ toggle: !state.toggle }))
render() {
const toggle = this.state.toggle
return (
<Spring
// Default values, optional ...
from={{ opacity: 0 }}
// Will animate to ...
to={{
// Can be numbers, colors, paths, degrees, percentages, ...
opacity: 1,
color: toggle ? RED : GREEN,
start: toggle ? RED : 'black',
end: toggle ? 'black' : GREEN,
stop: toggle ? '0%' : '50%',
scale: toggle ? 1 : 2,
path: toggle ? TRIANGLE : RECTANGLE,
}}
// You can finetune spring settings
config={{ friction: 1, tension: 10 }}
// All additional props will be spread over the child
toggle={this.toggle}
// Child as function/render-prop, receives interpolated values
children={Content}
/>
)
}
}
Demo: https://codesandbox.io/embed/882njxpz29
React-spring will re-render the receiving component on every frame. It is usually fine and gives you more freedom to animate whatever you like. If you need more performance supply the native
flag. Now your component will only render once and all updates will efficiently be applied to it outside of Reacts render loop.
This has a few gotchas:
import { Spring, animated } from 'react-spring'
const DIV = animated.div
const SVG = animated.createAnimatedComponent('svg')
const Content = ({ toggle, color, backgroundColor, transform }) => (
<animated.div style={{ backgroundColor }}>
<animated.svg style={{ transform }} version="1.1" viewBox="0 0 400 400">
<g style={{ color }} fillRule="evenodd" onClick={toggle}>
<path id="path-1" d="M20,380 L380,380 L380,380 L200,20 L20,380 Z" />
</g>
</animated.svg>
</animated.div>
)
class App extends React.Component {
state = { toggle: true }
toggle = () => this.setState(state => ({ toggle: !state.toggle }))
render() {
const toggle = this.state.toggle
return (
<Spring
native
from={{ color: 'black' }}
to={{
color: toggle ? '#247BA0' : '#70C1B3',
backgroundColor: toggle ? '#B2DBBF' : '#F3FFBD',
transform: [{ rotate: toggle ? '0deg' : '180deg' }, { scale: toggle ? 0.6 : 1.5 }],
}}
toggle={this.toggle}
children={Content}
/>
)
}
}
FAQs
Unknown package
The npm package react-spring receives a total of 135,330 weekly downloads. As such, react-spring popularity was classified as popular.
We found that react-spring demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.
Security News
Research
Socket's threat research team has detected five malicious npm packages targeting Roblox developers, deploying malware to steal credentials and personal data.