Skip to content

Commit c9161f9

Browse files
timpulverTim Pulver
and
Tim Pulver
authored
docs: fix useSpring example and rewrite docs (#181)
Co-authored-by: Tim Pulver <[email protected]>
1 parent 6695d58 commit c9161f9

File tree

1 file changed

+53
-22
lines changed

1 file changed

+53
-22
lines changed

docs/content/3.api/2.use-spring.md

+53-22
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,34 @@
11
# useSpring
22

3-
useSpring is a simpler hook than [useMotion](/api/use-motion).
3+
useSpring is a simpler hook than [useMotion](/api/use-motion) and makes it possible to animate HTML or SVG Elements in your Vue components using spring animations.
44

5-
It has been implemented in order for you to implement Spring animations in your apps, without the pain.
5+
Spring animations often feel more natural and fluid compared to linear animations, as they are based on the physics of a spring in the real world.
6+
7+
Springs maintain continuity for both static cases and cases with an initial velocity. This allows spring animations to adapt smoothly to user interactions like gestures.
68

79
useSpring can be bound to a HTML or SVG element, or to a simple object.
810

911
It skips the [Variants](/features/variants) system, allowing it to be as performant as using Popmotion natively, but with a nicer **API** to play with Vue refs.
1012

1113
## Parameters
1214

13-
### `target`
15+
### `values`
1416

15-
Target can be an element (**SVG** / **HTML**), or an object.
17+
The values argument expects a `motionProperties` object, which can be created using the [useMotionProperties](/api/use-motion-properties) function.
1618

1719
### `spring`
1820

19-
The spring argument takes a [Spring definition](https://popmotion.io/#quick-start-animation-animate-spring-options) from **Popmotion**.
21+
Spring animations can be configured in multiple ways, using spring options. The most intuitive way is using `duration` and `bounce`. Alternatively, you can use `stiffness`, `mass`, and `damping` to configure a spring animation.
2022

21-
It is optional as a default Spring will be applied if you do not specify it.
23+
Under the hood, `useSpring` uses **Popmotion**. See [Spring options in Popmotion](https://popmotion.io/#quick-start-animation-animate-spring-options) for a full list of spring options.
24+
25+
Passing a `string` argument is optional. A default spring will be applied if you do not specify it.
2226

2327
## Exposed
2428

2529
### `values`
2630

27-
Values are an object representing the current state from your Spring animations.
31+
Values are an object representing the current state from your spring animations.
2832

2933
### `set`
3034

@@ -36,27 +40,54 @@ Stop is a function allowing you to stop all the ongoing animations for the sprin
3640

3741
## Example
3842

39-
```typescript
40-
const target = ref<HTMLElement>()
43+
In the example below, click the green box to animate it, or press the escape key to stop the animation.
44+
45+
```html
46+
<template>
47+
<div class="container" tabindex="0" @keyup.esc="stop()">
48+
<div ref="box" class="box" @click="animate">Click me</div>
49+
</div>
50+
</template>
4151

42-
const { set, values, stop } = useSpring(target, {
43-
damping: 50,
44-
stiffness: 220,
52+
<script setup>
53+
import { ref } from 'vue'
54+
import { useSpring, useMotionProperties } from '@vueuse/motion'
55+
56+
const box = ref(null)
57+
58+
const { motionProperties } = useMotionProperties(box, {
59+
x: 0,
60+
y: 0,
4561
})
4662
47-
const onClick = () => {
48-
set({
49-
scale: 2,
50-
})
51-
}
63+
const { set, stop } = useSpring(motionProperties, {
64+
duration: 1000,
65+
bounce: 0.0,
66+
})
5267
53-
const onClickOut = () => {
68+
function animate() {
5469
set({
55-
scale: 1,
70+
x: Math.random() * 400,
71+
y: Math.random() * 400,
5672
})
5773
}
58-
59-
const stopTransitions = () => {
60-
stop()
74+
</script>
75+
76+
<style scoped>
77+
.container {
78+
width: 500px;
79+
height: 500px;
80+
outline: 2px solid #41B883;
81+
}
82+
.box {
83+
width: 100px;
84+
height: 100px;
85+
display: flex;
86+
justify-content: center;
87+
align-items: center;
88+
background-color: #41B883;
89+
color: white;
6190
}
91+
</style>
92+
6293
```

0 commit comments

Comments
 (0)