Skip to content

Commit 7b9e66c

Browse files
committed
document new api
1 parent b24cfde commit 7b9e66c

File tree

4 files changed

+69
-33
lines changed

4 files changed

+69
-33
lines changed

API.md

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,16 @@ class Spring extends React.PureComponent {
3333
to: PropTypes.oneOfType([PropTypes.object, PropTypes.func]),
3434
// Callback when the animation comes to a still-stand
3535
onRest: PropTypes.func,
36+
// Frame by frame callback, first argument passed is the animated value
37+
onFrame: PropTypes.func,
3638
// Takes a function that receives interpolated styles
3739
children: PropTypes.func,
3840
// Same as children, but takes precedence if present
3941
render: PropTypes.func,
4042
// Prevents animation if true, you can also pass individual keys
4143
immediate: PropTypes.oneOfType([PropTypes.bool, PropTypes.arrayOf(PropTypes.string)]),
44+
// When true it literally resets: from -> to
45+
reset: PropTypes.bool,
4246
}
4347
static defaultProps = { from: {}, to: {}, config: config.default, native: false, immediate: false }
4448
}
@@ -56,17 +60,26 @@ class Transition extends React.PureComponent {
5660
native: PropTypes.bool,
5761
config: PropTypes.object,
5862
// Base styles
59-
from: PropTypes.object,
63+
from: PropTypes.oneOfType([PropTypes.object, PropTypes.func]),
6064
// Animated styles when the component is mounted
61-
enter: PropTypes.object,
65+
enter: PropTypes.oneOfType([PropTypes.object, PropTypes.func]),
6266
// Unmpount styles
63-
leave: PropTypes.object,
67+
leave: PropTypes.oneOfType([PropTypes.object, PropTypes.func]),
68+
//
69+
update: PropTypes.oneOfType([PropTypes.object, PropTypes.func]),
6470
// A collectiomn of unique keys that must match with the childrens order
6571
// Can be omitted if children/render aren't an array
72+
// Can be a function, which then acts as a key-accessor which is useful when you use the items prop
6673
keys: PropTypes.oneOfType([
74+
PropTypes.func,
6775
PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.string, PropTypes.number])),
6876
PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
6977
]),
78+
// Optional. Let items refer to the actual data and from/enter/leaver/update can return per-object styles
79+
items: PropTypes.oneOfType([
80+
PropTypes.arrayOf(PropTypes.oneOfType([PropTypes.string, PropTypes.number, PropTypes.object])),
81+
PropTypes.oneOfType([PropTypes.string, PropTypes.number, PropTypes.object]),
82+
]),
7083
children: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.func), PropTypes.func]),
7184
render: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.func), PropTypes.func]),
7285
}

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-spring",
3-
"version": "4.0.0",
3+
"version": "4.0.1",
44
"description": "Animate React with ease",
55
"main": "dist/react-spring",
66
"module": "dist/react-spring.es",

readme.md

Lines changed: 52 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,27 @@
55
npm install react-spring
66

77
# Table of Contents 👇
8-
* [Installation](#installation-)
9-
* [Why](#why-)
10-
* [Overview](#overview-)
11-
* [API Overview](#api-overview-)
12-
* [Springs and Interpolation](#springs-and-interpolation)
13-
* [Render Props](#render-props)
14-
* [Native rendering](#native-rendering-demo)
15-
* [Transitions](#transitions)
16-
* [Parallax and page transitions](#parallax-and-page-transitions)
17-
* [License](#license-)
8+
9+
* [Installation](#installation-)
10+
* [Why](#why-)
11+
* [Overview](#overview-)
12+
* [API Overview](#api-overview-)
13+
* [Springs and Interpolation](#springs-and-interpolation)
14+
* [Render Props](#render-props)
15+
* [Native rendering](#native-rendering-demo)
16+
* [Transitions](#transitions)
17+
* [Parallax and page transitions](#parallax-and-page-transitions)
18+
* [License](#license-)
1819

1920
# Why 🤔
2021

2122
react-spring is a cooked down fork of Christopher Chedeau's [animated](https://github.com/animatedjs/animated) (which is used in react-native by default). It is trying to bridge it with Cheng Lou's [react-motion](https://github.com/chenglou/react-motion). Although both are similar in that they are spring-physics based, they have their pros and cons and could definitively benefit from one another:
2223

23-
| | Declarative | Primitives | Interpolations | Performance |
24-
|----------------|-------------|----------------|----------------|-------------|
25-
| React-motion   | ✅ | ✅ | ❌ | ❌
26-
| Animated       | ❌ | ❌ | ✅ | ✅
27-
| React-spring   | ✅ | ✅ | ✅ | ✅
24+
| | Declarative | Primitives | Interpolations | Performance |
25+
| -------------- | ----------- | ---------- | -------------- | ----------- |
26+
| React-motion   | | | | |
27+
| Animated       | | | | |
28+
| React-spring   | | | | |
2829

2930
react-spring inherits react-motions api (and simplifies it), has lots of primitives (springs, trails, transitions, reveals, parallax), can interpolate mostly everything (colors, gradients, percentages, degrees, svg-paths, arrays, etc.) and last but not least, can animate by committing directly to the dom instead of re-rendering a component frame-by-frame.
3031

@@ -76,10 +77,7 @@ A `Spring` will move data from one state to another. It remembers the current st
7677
Given a single child instead of a list you can reveal components with it.
7778

7879
```jsx
79-
<Transition
80-
from={{ opacity: 0 }}
81-
enter={{ opacity: 1 }}
82-
leave={{ opacity: 0 }}>
80+
<Transition from={{ opacity: 0 }} enter={{ opacity: 1 }} leave={{ opacity: 0 }}>
8381
{toggle ? ComponentA : ComponentB}
8482
</Transition>
8583
```
@@ -100,8 +98,12 @@ Given a single child instead of a list you can reveal components with it.
10098

10199
```jsx
102100
<Parallax pages={2}>
103-
<Parallax.Layer offset={0} speed={0.2}>first Page</Parallax.Layer>
104-
<Parallax.Layer offset={1} speed={0.5}>second Page</Parallax.Layer>
101+
<Parallax.Layer offset={0} speed={0.2}>
102+
first Page
103+
</Parallax.Layer>
104+
<Parallax.Layer offset={1} speed={0.5}>
105+
second Page
106+
</Parallax.Layer>
105107
</Parallax>
106108
```
107109

@@ -127,6 +129,8 @@ You can interpolate almost everything, from numbers, colors, svg-paths, percenta
127129
}}>
128130
```
129131

132+
A couple of extra props you might be interested in are `onRest`, which fires once the animations stops, `onFrame`, which fires on every frame and gives you the animation value, `reset`, which literally resets the spring so that it goes through `from` to `to` again, `immediate` which can enforce values to spring to their to-values immediatelly (can be `true` for a zero-time spring or an array where you can pass the key-names individually).
133+
130134
### Render props
131135

132136
Don't like the way render props wrap your code?
@@ -142,7 +146,6 @@ const Header = ({ children, bold, ...styles }) => (
142146
<Spring render={Header} to={{ color: 'fuchsia' }} bold>
143147
hello there
144148
</Spring>
145-
146149
```
147150

148151
Et voilà! `Header` animates on prop changes! Props that `Spring` doesn't recognize will be spread over the receiving component, in this example `bold`, but it also includes `children` if you use `render` to refer to the render-child.
@@ -153,7 +156,7 @@ By default we'll render the receiving component every frame as it gives you more
153156

154157
Just be aware of the following conditions:
155158

156-
1. It only animates element styles and attributes, the values you receive *are opaque objects, not regular values*
159+
1. It only animates element styles and attributes, the values you receive _are opaque objects, not regular values_
157160
2. Receiving elements must be `animated.[elementName]`, for instance `div` becomes `animated.div`
158161
3. If you need to interpolate styles use the `template` string literal
159162

@@ -169,6 +172,16 @@ import { Spring, animated, template } from 'react-spring'
169172
</Spring>
170173
```
171174

175+
You have several interpolation options, not just `template`. `interpolate` can be called on the value itself or as a stand-alone function which can read multiple values. It accepts either a function which receives the animation value(/s), or a range.
176+
177+
```jsx
178+
import { Spring, animated, interpolate } from 'react-spring'
179+
180+
<animated.svg style={{ transform: interpolate([x, y], (x, y) => `translate(${x}px, ${y}px)`), color: time.interpolate({ inputRange: [0, 1], outputRange: ['red', rgba(1, 50, 210, 0.5)] }) }}>
181+
<g><animated.path d={time.interpolate(customSvgInterpolator)} /></g>
182+
</animated.svg>
183+
```
184+
172185
### Transitions
173186

174187
Animates children as they mount and unmount. `from` denotes base styles, `enter` styles are applied when objects appear, `leave` styles are applied when objects disappear. Keys and children have to match in their order! The keys are the same that you would provide in any other looping situation.
@@ -190,14 +203,25 @@ import { Transition } from 'react-spring'
190203
You can use this prototype for two-state reveals, simply render a single child that you can switch out for another. You don't have to pass keys for this one.
191204

192205
```jsx
193-
<Transition
194-
from={{ opacity: 0 }}
195-
enter={{ opacity: 1 }}
196-
leave={{ opacity: 0 }}>
206+
<Transition from={{ opacity: 0 }} enter={{ opacity: 1 }} leave={{ opacity: 0 }}>
197207
{toggle ? ComponentA : ComponentB}
198208
</Transition>
199209
```
200210

211+
For more complex animations you can return per-object styles individually. Let Transition know the actual data by passing it raw to `items`, either pass your keys like always or give it an accessor. And for more control, there's `update` which fires for nodes that are neither entering nor leaving.
212+
213+
```jsx
214+
<Transition
215+
items={items}
216+
keys={item => item.key}
217+
from={item => ({ opacity: 0 })}
218+
enter={item => ({ opacity: 1 })}
219+
update={item => ({ opacity: 0.5 })}
220+
leave={item => ({ opacity: 0 })}>
221+
{items.map(item => styles => <li style={styles}>{item.text}</li>)}
222+
</Transition>
223+
```
224+
201225
### Trails/Staggered transitions
202226

203227
`Trail` animates the first child of the list you pass, the others will follow in a trailing motion. The api is similar to `Transition` though it will assume your list is fixed.
@@ -230,8 +254,8 @@ import { Parallax } from 'react-spring'
230254

231255
# Links 🔗
232256

233-
234257
[API](https://github.com/drcmda/react-spring/blob/master/API.md)[Changelog](https://github.com/drcmda/react-spring/blob/master/CHANGELOG.md)
235258

236259
# License ⚖
260+
237261
### [MIT](https://github.com/drcmda/react-spring/blob/master/LICENSE)

src/animated/AnimatedTracking.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import Animated from './Animated'
2-
import AnimatedValue from './AnimatedValue'
32

43
export default class AnimatedTracking extends Animated {
54
constructor(value, parent, animationClass, animationConfig, callback) {

0 commit comments

Comments
 (0)