react-ui-animate 5.0.0-alpha.1 → 5.0.0-alpha.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +128 -143
- package/package.json +4 -2
package/README.md
CHANGED
|
@@ -6,194 +6,181 @@
|
|
|
6
6
|
|
|
7
7
|
### Install
|
|
8
8
|
|
|
9
|
-
You can install react-ui-animate via `npm` or `yarn`:
|
|
9
|
+
You can install `react-ui-animate` via `npm` or `yarn`:
|
|
10
10
|
|
|
11
11
|
```sh
|
|
12
|
-
npm
|
|
12
|
+
npm install react-ui-animate
|
|
13
13
|
```
|
|
14
14
|
|
|
15
15
|
```sh
|
|
16
16
|
yarn add react-ui-animate
|
|
17
17
|
```
|
|
18
18
|
|
|
19
|
-
|
|
19
|
+
---
|
|
20
|
+
|
|
21
|
+
## Getting Started
|
|
22
|
+
|
|
23
|
+
The `react-ui-animate` library provides a straightforward way to add animations and gestures to your React components. Below are some common use cases.
|
|
20
24
|
|
|
21
|
-
|
|
25
|
+
### 1. useValue
|
|
22
26
|
|
|
23
|
-
|
|
24
|
-
import { animate, useValue } from 'react-ui-animate';
|
|
27
|
+
Use `useValue` to initialize and update an animated value.
|
|
25
28
|
|
|
26
|
-
|
|
27
|
-
|
|
29
|
+
```tsx
|
|
30
|
+
import React from 'react';
|
|
31
|
+
import {
|
|
32
|
+
animate,
|
|
33
|
+
useValue,
|
|
34
|
+
withSpring,
|
|
35
|
+
withTiming,
|
|
36
|
+
withSequence,
|
|
37
|
+
} from 'react-ui-animate';
|
|
38
|
+
|
|
39
|
+
export const UseValue: React.FC = () => {
|
|
40
|
+
const [width, setWidth] = useValue(100);
|
|
28
41
|
|
|
29
42
|
return (
|
|
30
43
|
<>
|
|
31
|
-
<
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
width: 100,
|
|
35
|
-
padding: 20,
|
|
36
|
-
background: '#39F',
|
|
44
|
+
<button
|
|
45
|
+
onClick={() => {
|
|
46
|
+
setWidth(withSequence([withTiming(100), withSpring(0)]));
|
|
37
47
|
}}
|
|
38
48
|
>
|
|
39
|
-
|
|
40
|
-
</
|
|
41
|
-
|
|
42
|
-
<button
|
|
49
|
+
SEQUENCE (100 → 0)
|
|
50
|
+
</button>
|
|
51
|
+
<button
|
|
43
52
|
onClick={() => {
|
|
44
|
-
|
|
53
|
+
setWidth(withSpring(200));
|
|
45
54
|
}}
|
|
46
55
|
>
|
|
47
|
-
|
|
56
|
+
SPRING (→ 200)
|
|
48
57
|
</button>
|
|
58
|
+
<button
|
|
59
|
+
onClick={() => {
|
|
60
|
+
setWidth(400);
|
|
61
|
+
}}
|
|
62
|
+
>
|
|
63
|
+
IMMEDIATE (→ 400)
|
|
64
|
+
</button>
|
|
65
|
+
|
|
66
|
+
<animate.div
|
|
67
|
+
style={{
|
|
68
|
+
width,
|
|
69
|
+
height: 100,
|
|
70
|
+
backgroundColor: 'red',
|
|
71
|
+
left: 0,
|
|
72
|
+
top: 0,
|
|
73
|
+
}}
|
|
74
|
+
/>
|
|
49
75
|
</>
|
|
50
76
|
);
|
|
51
|
-
}
|
|
77
|
+
};
|
|
52
78
|
```
|
|
53
79
|
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
---
|
|
80
|
+
### 2. useMount
|
|
57
81
|
|
|
58
|
-
|
|
82
|
+
Use `useMount` to animate component mount and unmount transitions.
|
|
59
83
|
|
|
60
|
-
|
|
84
|
+
```tsx
|
|
85
|
+
import React from 'react';
|
|
86
|
+
import {
|
|
87
|
+
animate,
|
|
88
|
+
useMount,
|
|
89
|
+
withDecay,
|
|
90
|
+
withSequence,
|
|
91
|
+
withSpring,
|
|
92
|
+
withTiming,
|
|
93
|
+
} from 'react-ui-animate';
|
|
61
94
|
|
|
62
|
-
|
|
95
|
+
export const UseMount: React.FC = () => {
|
|
96
|
+
const [open, setOpen] = React.useState(true);
|
|
97
|
+
const mounted = useMount(open, { from: 0, enter: 1, exit: 0 });
|
|
63
98
|
|
|
64
|
-
|
|
65
|
-
|
|
99
|
+
return (
|
|
100
|
+
<>
|
|
101
|
+
{mounted(
|
|
102
|
+
(animation, isMounted) =>
|
|
103
|
+
isMounted && (
|
|
104
|
+
<animate.div
|
|
105
|
+
style={{
|
|
106
|
+
width: 100,
|
|
107
|
+
height: 100,
|
|
108
|
+
backgroundColor: 'teal',
|
|
109
|
+
opacity: animation,
|
|
110
|
+
}}
|
|
111
|
+
/>
|
|
112
|
+
)
|
|
113
|
+
)}
|
|
114
|
+
|
|
115
|
+
<button onClick={() => setOpen((prev) => !prev)}>ANIMATE ME</button>
|
|
116
|
+
</>
|
|
117
|
+
);
|
|
118
|
+
};
|
|
66
119
|
```
|
|
67
120
|
|
|
68
|
-
|
|
121
|
+
### 3. Interpolation
|
|
69
122
|
|
|
70
|
-
|
|
123
|
+
Interpolate values for complex mappings like color transitions or movement.
|
|
71
124
|
|
|
72
|
-
```
|
|
73
|
-
import {
|
|
125
|
+
```tsx
|
|
126
|
+
import React, { useLayoutEffect, useState } from 'react';
|
|
127
|
+
import { animate, useValue, withSpring } from 'react-ui-animate';
|
|
74
128
|
|
|
75
|
-
const
|
|
129
|
+
export const Interpolation: React.FC = () => {
|
|
130
|
+
const [open, setOpen] = useState(false);
|
|
131
|
+
const [x, setX] = useValue(0);
|
|
76
132
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
height: 100,
|
|
81
|
-
backgroundColor: '#39f',
|
|
82
|
-
}}
|
|
83
|
-
/>;
|
|
84
|
-
```
|
|
133
|
+
useLayoutEffect(() => {
|
|
134
|
+
setX(withSpring(open ? 500 : 0));
|
|
135
|
+
}, [open, setX]);
|
|
85
136
|
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
137
|
+
return (
|
|
138
|
+
<>
|
|
139
|
+
<animate.div
|
|
140
|
+
style={{
|
|
141
|
+
width: 100,
|
|
142
|
+
height: 100,
|
|
143
|
+
backgroundColor: x.to([0, 500], ['red', 'blue']),
|
|
144
|
+
translateX: x,
|
|
145
|
+
}}
|
|
146
|
+
/>
|
|
94
147
|
|
|
95
|
-
<button
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
}}
|
|
100
|
-
>
|
|
101
|
-
Update
|
|
102
|
-
</button>
|
|
148
|
+
<button onClick={() => setOpen((p) => !p)}>ANIMATE ME</button>
|
|
149
|
+
</>
|
|
150
|
+
);
|
|
151
|
+
};
|
|
103
152
|
```
|
|
104
153
|
|
|
105
|
-
In this example, `withSpring` runs spring animation when updating the value.
|
|
106
|
-
|
|
107
154
|
---
|
|
108
155
|
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
The `interpolate()` function is useful for mapping values from one range to another, enabling more complex animations.
|
|
112
|
-
|
|
113
|
-
```javascript
|
|
114
|
-
import { useValue, animate, interpolate } from 'react-ui-animate';
|
|
115
|
-
|
|
116
|
-
const width = useValue(100);
|
|
117
|
-
|
|
118
|
-
<animate.div
|
|
119
|
-
style={{
|
|
120
|
-
width: width.value,
|
|
121
|
-
height: 100,
|
|
122
|
-
backgroundColor: interpolate(width.value, [100, 200], ['red', 'blue']),
|
|
123
|
-
}}
|
|
124
|
-
/>;
|
|
125
|
-
```
|
|
126
|
-
|
|
127
|
-
In this example, as the width changes from 100 to 200, the background color smoothly transitions from red to blue.
|
|
128
|
-
|
|
129
|
-
#### Modifiers
|
|
130
|
-
|
|
131
|
-
You can dynamically modify animation configurations by assigning values to an animated value using various animation functions.
|
|
132
|
-
|
|
133
|
-
To apply a spring animation and update the value to `10`:
|
|
134
|
-
|
|
135
|
-
```jsx
|
|
136
|
-
x.value = withSpring(10);
|
|
137
|
-
```
|
|
138
|
-
|
|
139
|
-
To apply a timing animation with a duration of 5000 milliseconds:
|
|
156
|
+
## API Overview
|
|
140
157
|
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
To create sequential transitions using the `withSequence` function with dynamic modifiers like `withSpring` and `withTiming`:
|
|
146
|
-
|
|
147
|
-
```jsx
|
|
148
|
-
x.value = withSequence([withSpring(50), withTiming(100), withEase(200)]);
|
|
149
|
-
```
|
|
150
|
-
|
|
151
|
-
#### `useMount()`
|
|
158
|
+
- **`useValue(initial)`**: Initializes an animated value.
|
|
159
|
+
- **`animate`**: JSX wrapper for animatable elements (`animate.div`, `animate.span`, etc.).
|
|
160
|
+
- **Modifiers**: `withSpring`, `withTiming`, `withDecay`, `withSequence`, `withEase` — functions to define animation behavior.
|
|
161
|
+
- **`useMount(state, config)`**: Manages mount/unmount transitions. `config` includes `from`, `enter`, and `exit` values.
|
|
152
162
|
|
|
153
|
-
|
|
163
|
+
## Gestures
|
|
154
164
|
|
|
155
|
-
|
|
156
|
-
import { useMount } from 'react-ui-animate';
|
|
157
|
-
|
|
158
|
-
export default function App() {
|
|
159
|
-
const [visible, setVisible] = useState(false);
|
|
160
|
-
|
|
161
|
-
const open = useMount(visible);
|
|
162
|
-
|
|
163
|
-
return open((animation, mounted) => mounted && <animate.div />);
|
|
164
|
-
}
|
|
165
|
-
```
|
|
165
|
+
`react-ui-animate` also provides hooks for handling gestures:
|
|
166
166
|
|
|
167
|
-
|
|
167
|
+
- `useDrag`
|
|
168
|
+
- `useMouseMove`
|
|
169
|
+
- `useScroll`
|
|
170
|
+
- `useWheel`
|
|
171
|
+
- `useGesture`
|
|
168
172
|
|
|
169
|
-
|
|
170
|
-
2. The `useMount` hook takes `visible` as an argument and provides animation states for mounting and unmounting.
|
|
171
|
-
3. The `open` function, returned by `useMount`, is used to conditionally render `animate.div` based on the `mounted` boolean and apply the transition animation.
|
|
173
|
+
**Example: `useDrag`**
|
|
172
174
|
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
### Gestures
|
|
176
|
-
|
|
177
|
-
The `react-ui-animate` library also provides several hooks for handling different types of gestures:
|
|
178
|
-
|
|
179
|
-
1. `useDrag`: Handles drag gestures on elements.
|
|
180
|
-
2. `useMouseMove`: Handles mouse movements.
|
|
181
|
-
3. `useScroll`: Handles scrolling of the document.
|
|
182
|
-
4. `useWheel`: Handles wheel rotation gestures.
|
|
183
|
-
5. `useGesture`: Handles combinations of various gestures.
|
|
184
|
-
|
|
185
|
-
**Example**: `useDrag`
|
|
186
|
-
|
|
187
|
-
Here’s an example of using the useDrag hook to enable drag gestures:
|
|
188
|
-
|
|
189
|
-
```jsx
|
|
175
|
+
```tsx
|
|
176
|
+
import React from 'react';
|
|
190
177
|
import { useValue, animate, useDrag, withSpring } from 'react-ui-animate';
|
|
191
178
|
|
|
192
|
-
export const Draggable = () => {
|
|
193
|
-
const translateX = useValue(0);
|
|
179
|
+
export const Draggable: React.FC = () => {
|
|
180
|
+
const [translateX, setTranslateX] = useValue(0);
|
|
194
181
|
|
|
195
|
-
const bind = useDrag(
|
|
196
|
-
|
|
182
|
+
const bind = useDrag(({ down, movementX }) => {
|
|
183
|
+
setTranslateX(down ? movementX : withSpring(0));
|
|
197
184
|
});
|
|
198
185
|
|
|
199
186
|
return (
|
|
@@ -203,18 +190,16 @@ export const Draggable = () => {
|
|
|
203
190
|
width: 100,
|
|
204
191
|
height: 100,
|
|
205
192
|
backgroundColor: '#3399ff',
|
|
206
|
-
translateX
|
|
193
|
+
translateX,
|
|
207
194
|
}}
|
|
208
195
|
/>
|
|
209
196
|
);
|
|
210
197
|
};
|
|
211
198
|
```
|
|
212
199
|
|
|
213
|
-
In this example, the blue block can be dragged horizontally by clicking and dragging.
|
|
214
|
-
|
|
215
200
|
## Documentation
|
|
216
201
|
|
|
217
|
-
For detailed documentation and examples, visit the official [react-ui-animate documentation](
|
|
202
|
+
For detailed documentation and examples, visit the official [react-ui-animate documentation](https://react-ui-animate.js.org/).
|
|
218
203
|
|
|
219
204
|
## License
|
|
220
205
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-ui-animate",
|
|
3
|
-
"version": "5.0.0-alpha.
|
|
3
|
+
"version": "5.0.0-alpha.3",
|
|
4
4
|
"description": "React library for gestures and animation",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"peerDependencies": {
|
|
@@ -32,7 +32,9 @@
|
|
|
32
32
|
"test": "echo \"Error: no test specified\" && exit 1",
|
|
33
33
|
"version:minor": "npm version --no-git-tag-version minor",
|
|
34
34
|
"version:major": "npm version --no-git-tag-version major",
|
|
35
|
-
"version:patch": "npm version --no-git-tag-version patch"
|
|
35
|
+
"version:patch": "npm version --no-git-tag-version patch",
|
|
36
|
+
"publish:next": "npm publish --tag next",
|
|
37
|
+
"publish:latest": "npm publish --tag latest"
|
|
36
38
|
},
|
|
37
39
|
"repository": {
|
|
38
40
|
"type": "git",
|