File size: 7,340 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 |
---
meta:
title: Spring Configs | React Spring
'og:title': Spring Configs | React Spring
'twitter:title': Spring Configs | React Spring
description: An advanced guide to the configs of Controllers, Components & SpringValues in React Spring.
'og:description': An advanced guide to the configs of Controllers, Components & SpringValues in React Spring.
'twitter:description': An advanced guide to the configs of Controllers, Components & SpringValues in React Spring.
'og:url': https://www.react-spring.dev/docs/advanced/config
'twitter:url': https://www.react-spring.dev/docs/advanced/config
sidebar_position: 1
---
import { formatFrontmatterToRemixMeta } from '../helpers/meta'
export const meta = formatFrontmatterToRemixMeta(frontmatter)
# Spring Configs
Part of a component's `configuration` argument, the `config` prop is an object
with many properties that can be used to customize & fine tune your animations.
It directly controls how your springs behave and can customized on a spring by
spring basis (e.g. `opacity` and `scale` could be different for one `useSpring` hook).
## Usage
### Basic Usage
Your most typical usage of the `config` prop is to edit the mass, friction and tension properties.
```jsx lines=7-11
import { useSpring, animated } from '@react-spring/web'
const MyComponent = () => {
const [springs, api] = useSpring(
() => ({
y: 0,
config: {
mass: 5,
friction: 120,
tension: 120,
},
}),
[]
)
return <animated.div style={springs} />
}
```
### Config per SpringValue
The `config` prop can also be a function where the argument is the key of the
spring value, this is great if you want one spring to control many elements or
have properties behave in different ways like a smooth opacity change but a
bouncy scale.
```jsx lines=8-18
import { useSpring, animated } from '@react-spring/web'
const MyComponent = () => {
const [springs, api] = useSpring(
() => ({
backgroundColor: '#00ff00',
y: 0,
config: key => {
if (key === 'y') {
return {
mass: 5,
friction: 120,
tension: 120,
}
}
return {}
},
}),
[]
)
return <animated.div style={springs} />
}
```
Returning an empty object will enforce the default config options:
`{ mass: 1, tension: 170, friction: 26 }`.
### Partial Updates
When using the spring `api` you can partially update your configs.
These partial configs are merged with the original config for each
call of the api.
```jsx lines=17-22
import { useSpring, animated } from '@react-spring/web'
const MyComponent = () => {
const [springs, api] = useSpring(
() => ({
y: 0,
config: {
mass: 5,
friction: 120,
tension: 120,
},
}),
[]
)
const handleClick = () => {
api.start({
y: 20,
config: {
friction: 10,
},
})
}
return <animated.div onClick={handleClick} style={springs} />
}
```
### Presets
Additionally to being able to configure your own spring, we export a
small selection of presets enclosed in the `config` named export.
```jsx
import { config } from '@react-spring/web'
```
The presets are:
import { DescriptiveList } from '../components/Text/List'
<DescriptiveList
data={[
['default', <code>{`{ tension: 170, friction: 26 }`}</code>],
['gentle', <code>{`{ tension: 120, friction: 14 }`}</code>],
['wobbly', <code>{`{ tension: 180, friction: 12 }`}</code>],
['stiff', <code>{`{ tension: 210, friction: 20 }`}</code>],
['slow', <code>{`{ tension: 280, friction: 60 }`}</code>],
['molasses', <code>{`{ tension: 280, friction: 120 }`}</code>],
]}
/>
## Config Visualizer
```jsx live=true template=configPlayground showCode=false
import { useRef, useState } from 'react'
import { useSpring, animated } from '@react-spring/web'
import { useControls } from 'leva'
export default function Card() {
const cardRef = useRef(null)
const config = useControls({
mass: 1,
tension: 170,
friction: 26,
clamp: false,
precision: 0.01,
velocity: 0,
})
const [{ xys }, api] = useSpring(() => ({ xys: [0, 0, 1], config }), [config])
const handleMouseLeave = () =>
api.start({
xys: [0, 0, 1],
})
const handleMouseMove = e => {
const rect = cardRef.current.getBoundingClientRect()
api.start({
xys: calc(e.clientX, e.clientY, rect),
})
}
return (
<div className="card-main" ref={cardRef}>
<animated.div
className="card"
style={{ transform: xys.to(trans) }}
onMouseLeave={handleMouseLeave}
onMouseMove={handleMouseMove}
/>
</div>
)
}
const calc = (x, y, rect) => [
-(y - rect.top - rect.height / 2) / 5,
(x - rect.left - rect.width / 2) / 5,
1.4,
]
const trans = (x, y, s) =>
`perspective(600px) rotateX(${x}deg) rotateY(${y}deg) scale(${s})`
```
## Easings
Easings can **only** be used in conjunction with the `duration` property.
### Why Springs?
We think of animation in terms of time and curves, but that causes most of the struggle we face
when trying to make elements on the screen move naturally, because nothing in the real world
moves like that. Springs don’t have a defined curve or a set duration.
As Andy Matuschak (ex Apple UI-Kit developer) expressed – “Animation APIs parameterized by duration
and curve are fundamentally opposed to continuous, fluid interactivity.”
### Available generic easings
All the following easings are part of the `easings` object imported like so:
```jsx
import { easings } from '@react-spring/web'
```
import { TableGeneric } from '../components/Tables/TablesConfig'
import { easingData } from '../data/fixtures'
<TableGeneric data={easingData} headData={[null, null, null]} />
### Steps easing
This unique easing function returns the standard `EasingFunction` a `config` object expects. Its still part
of the `easings` object but you need to set it up before passing.
```jsx
import { easings } from '@react-spring/web'
const MyComponent = () => {
const [springs, api] = useSpring(
() => ({
y: 0,
config: {
easing: easings.steps(5),
},
}),
[]
)
return <animated.div style={springs} />
}
```
It can also take a `Direction` as a second argument, which can be either `start` or `end`. The signature
for the function is shown below:
```ts
export function steps(
steps: number,
direction: Direction = 'end'
): EasingFunction
```
## Reference
import { TablesConfiguration } from '../components/Tables/TablesConfig'
import { configData } from '../data/fixtures'
<TablesConfiguration data={configData} />
## Pitfalls
### My animation jumps at the end.
In some instances such as animating `threejs` the items can feel as if they skip the
last couple of frames. This is because the precision prop has a default value of `0.01`.
Try editing this value to be `0.0001` to resolve this.
### My animation is bouncing and I just want it to stop.
Sometimes you want a snapping feel so you create a sharp spring. When you animate the spring
the animation bounces around, this is because of the physics. An easy solution to this is to
set `clamp` to true. Your spring will stop animating at the point it hit's it's goal value.
|