react-ui-animate 4.3.1 → 5.0.0-alhpa.6

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.
Files changed (31) hide show
  1. package/README.md +128 -143
  2. package/dist/animation/Value.d.ts +12 -0
  3. package/dist/animation/controllers.d.ts +41 -0
  4. package/dist/animation/hooks/index.d.ts +1 -1
  5. package/dist/animation/hooks/useAnimatedList.d.ts +24 -0
  6. package/dist/animation/hooks/useMount.d.ts +10 -8
  7. package/dist/animation/hooks/useValue.d.ts +15 -7
  8. package/dist/animation/index.d.ts +3 -4
  9. package/dist/animation/interpolation/index.d.ts +1 -1
  10. package/dist/animation/types.d.ts +22 -11
  11. package/dist/index.d.ts +2 -6
  12. package/dist/index.js +1 -1
  13. package/dist/index.js.map +1 -1
  14. package/package.json +5 -3
  15. package/dist/animation/controllers/index.d.ts +0 -8
  16. package/dist/animation/controllers/withDecay.d.ts +0 -11
  17. package/dist/animation/controllers/withDelay.d.ts +0 -4
  18. package/dist/animation/controllers/withEase.d.ts +0 -8
  19. package/dist/animation/controllers/withLoop.d.ts +0 -8
  20. package/dist/animation/controllers/withNative.d.ts +0 -5
  21. package/dist/animation/controllers/withSequence.d.ts +0 -8
  22. package/dist/animation/controllers/withSpring.d.ts +0 -12
  23. package/dist/animation/controllers/withTiming.d.ts +0 -11
  24. package/dist/animation/helpers/getToValue.d.ts +0 -2
  25. package/dist/animation/helpers/index.d.ts +0 -2
  26. package/dist/animation/hooks/useValues.d.ts +0 -7
  27. package/dist/animation/interpolation/interpolate.d.ts +0 -11
  28. package/dist/animation/modules/MountedBlock.d.ts +0 -11
  29. package/dist/animation/modules/ScrollableBlock.d.ts +0 -11
  30. package/dist/animation/modules/index.d.ts +0 -2
  31. /package/dist/animation/{helpers/animationType.d.ts → AnimationConfig.d.ts} +0 -0
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 i react-ui-animate
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
- ### Getting Started
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
- The `react-ui-animate` library provides a straightforward way to add animations and gestures to your React components. Here’s how you can get started quickly:
25
+ ### 1. useValue
22
26
 
23
- ```javascript
24
- import { animate, useValue } from 'react-ui-animate';
27
+ Use `useValue` to initialize and update an animated value.
25
28
 
26
- export default function () {
27
- const opacity = useValue(0); // Initialize
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
- <animate.div
32
- style={{
33
- opacity: opacity.value, // Apply
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
- ANIMATED
40
- </animate.div>
41
-
42
- <button
49
+ SEQUENCE (100 → 0)
50
+ </button>
51
+ <button
43
52
  onClick={() => {
44
- opacity.value = 1 // Update
53
+ setWidth(withSpring(200));
45
54
  }}
46
55
  >
47
- Animate Me
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
- In this example, clicking the `Animate Me` button changes the opacity from 0 to 1.
55
-
56
- ---
80
+ ### 2. useMount
57
81
 
58
- ### Implementation Steps
82
+ Use `useMount` to animate component mount and unmount transitions.
59
83
 
60
- #### 1. Initialize
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
- The `useValue()` hook is central to creating animations. It initializes an animated value and allows you to seamlessly update it to create dynamic effects.
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
- ```javascript
65
- const opacity = useValue(0); // Initialize a animation value 0
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
- #### 2. Apply
121
+ ### 3. Interpolation
69
122
 
70
- `animate.div` is a special component designed to work with `useValue()`. It simplifies animating elements by directly using animated values.
123
+ Interpolate values for complex mappings like color transitions or movement.
71
124
 
72
- ```jsx
73
- import { useValue, animate } from 'react-ui-animate'
125
+ ```tsx
126
+ import React, { useLayoutEffect, useState } from 'react';
127
+ import { animate, useValue, withSpring } from 'react-ui-animate';
74
128
 
75
- const width = useValue(100); // Start with a width of 100
129
+ export const Interpolation: React.FC = () => {
130
+ const [open, setOpen] = useState(false);
131
+ const [x, setX] = useValue(0);
76
132
 
77
- <animate.div
78
- style={{
79
- width: width.value,
80
- height: 100,
81
- backgroundColor: '#39f',
82
- }}
83
- />;
84
- ```
133
+ useLayoutEffect(() => {
134
+ setX(withSpring(open ? 500 : 0));
135
+ }, [open, setX]);
85
136
 
86
- #### 3. Update
87
-
88
- To update the value simply assign the initialized animated node with a value.
89
-
90
- ```jsx
91
- import { useValue, withSpring } from 'react-ui-animate';
92
-
93
- const width = useValue(100);
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
- onClick={() => {
97
- // Update
98
- width.value = withSpring(400);
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
- #### `interpolate`
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
- ```jsx
142
- x.value = withTiming(10, { duration: 5000 });
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
- The `useMount()` hook facilitates managing the mounting and unmounting of a component with animations.
163
+ ## Gestures
154
164
 
155
- ```jsx
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
- In this example,
167
+ - `useDrag`
168
+ - `useMouseMove`
169
+ - `useScroll`
170
+ - `useWheel`
171
+ - `useGesture`
168
172
 
169
- 1. A state variable `visible` determines whether the component is visible.
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(function ({ down, movementX }) {
196
- translateX.value = down ? movementX : withSpring(0);
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: translateX.value,
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](http://react-ui-animate.js.org/).
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
 
@@ -0,0 +1,12 @@
1
+ import { MotionValue } from '@raidipesh78/re-motion';
2
+ import { ToValue } from './types';
3
+ export declare class Value<V extends number | string> {
4
+ private animation;
5
+ private unsubscribe?;
6
+ constructor(initial: V);
7
+ set(u: MotionValue<V> | ToValue<V>): void;
8
+ private buildDriver;
9
+ get value(): MotionValue<V>;
10
+ get current(): V;
11
+ destroy(): void;
12
+ }
@@ -0,0 +1,41 @@
1
+ import { DriverConfig } from './types';
2
+ interface WithSpringOptions {
3
+ mass?: number;
4
+ tension?: number;
5
+ friction?: number;
6
+ onStart?: () => void;
7
+ onChange?: (v: number | string) => void;
8
+ onRest?: () => void;
9
+ }
10
+ export declare const withSpring: (to: number, options?: WithSpringOptions) => DriverConfig;
11
+ export declare const withEase: (to: number, options?: WithSpringOptions) => DriverConfig;
12
+ interface WithTimingOptions {
13
+ duration?: number;
14
+ easing?: (t: number) => number;
15
+ onStart?: () => void;
16
+ onChange?: (v: number | string) => void;
17
+ onRest?: () => void;
18
+ }
19
+ export declare const withTiming: (to: number, options?: WithTimingOptions) => DriverConfig;
20
+ interface WithDecayOptions {
21
+ velocity: number;
22
+ onStart?: () => void;
23
+ onChange?: (v: number | string) => void;
24
+ onRest?: () => void;
25
+ clamp?: [number, number];
26
+ }
27
+ export declare const withDecay: (options: WithDecayOptions) => DriverConfig;
28
+ interface WithSequenceOptions {
29
+ onStart?: () => void;
30
+ onChange?: (v: number | string) => void;
31
+ onRest?: () => void;
32
+ }
33
+ export declare const withSequence: (steps: DriverConfig[], options?: WithSequenceOptions) => DriverConfig;
34
+ export declare const withDelay: (delay: number) => DriverConfig;
35
+ interface WithLoopOptions {
36
+ onStart?: () => void;
37
+ onChange?: (v: number | string) => void;
38
+ onRest?: () => void;
39
+ }
40
+ export declare const withLoop: (controller: DriverConfig, iterations: number, options?: WithLoopOptions) => DriverConfig;
41
+ export {};
@@ -1,3 +1,3 @@
1
1
  export * from './useValue';
2
2
  export * from './useMount';
3
- export * from './useValues';
3
+ export * from './useAnimatedList';
@@ -0,0 +1,24 @@
1
+ import { MotionValue } from '@raidipesh78/re-motion';
2
+ import type { ToValue } from '../types';
3
+ export declare function useAnimatedList<T>(items: T[], getKey: (item: T) => string, config?: {
4
+ from?: number;
5
+ enter?: ToValue<number>;
6
+ exit?: ToValue<number>;
7
+ }): Array<{
8
+ key: string;
9
+ item: T;
10
+ animation: MotionValue<number>;
11
+ }>;
12
+ export declare function useAnimatedList<T, I extends Record<string, number>>(items: T[], getKey: (item: T) => string, config: {
13
+ from: I;
14
+ enter?: Partial<{
15
+ [K in keyof I]: ToValue<I[K]>;
16
+ }>;
17
+ exit?: Partial<{
18
+ [K in keyof I]: ToValue<I[K]>;
19
+ }>;
20
+ }): Array<{
21
+ key: string;
22
+ item: T;
23
+ animation: Record<keyof I, MotionValue<number>>;
24
+ }>;
@@ -1,10 +1,12 @@
1
- import { FluidValue } from '@raidipesh78/re-motion';
1
+ import { MotionValue } from '@raidipesh78/re-motion';
2
2
  import type { ToValue } from '../types';
3
- export interface UseMountConfig {
3
+ export declare function useMount(isOpen: boolean, config?: {
4
4
  from?: number;
5
- enter?: ToValue;
6
- exit?: ToValue;
7
- }
8
- export declare const useMount: (state: boolean, config?: UseMountConfig) => (fn: (animation: {
9
- value: FluidValue;
10
- }, mounted: boolean) => React.ReactNode) => import("react").ReactNode;
5
+ enter?: ToValue<number>;
6
+ exit?: ToValue<number>;
7
+ }): (fn: (value: MotionValue<number>, mounted: boolean) => React.ReactNode) => React.ReactNode;
8
+ export declare function useMount<I extends Record<string, number>>(isOpen: boolean, config: {
9
+ from: I;
10
+ enter?: Partial<Record<keyof I, ToValue<number>>>;
11
+ exit?: Partial<Record<keyof I, ToValue<number>>>;
12
+ }): (fn: (values: Record<keyof I, MotionValue<number>>, mounted: boolean) => React.ReactNode) => React.ReactNode;
@@ -1,7 +1,15 @@
1
- import { FluidValue } from '@raidipesh78/re-motion';
2
- import type { ToValue } from '../types';
3
- export declare function useValue(initialValue: number | string): {
4
- get value(): FluidValue;
5
- set value(to: number | string | ToValue);
6
- readonly currentValue: string | number;
7
- };
1
+ import { MotionValue } from '@raidipesh78/re-motion';
2
+ import { Primitive, ToValue } from '../types';
3
+ type Input<V extends Primitive> = V | V[] | Record<string, V>;
4
+ type Output<V extends Primitive, I extends Input<V>> = I extends V ? MotionValue<V> : I extends V[] ? {
5
+ [K in keyof I]: MotionValue<V>;
6
+ } : I extends Record<string, V> ? {
7
+ [K in keyof I]: MotionValue<V>;
8
+ } : never;
9
+ type SetterParam<V extends Primitive, I extends Input<V>> = I extends V ? MotionValue<V> | ToValue<V> : I extends V[] ? Partial<{
10
+ [K in keyof I]: MotionValue<V> | ToValue<V>;
11
+ }> : I extends Record<string, V> ? Partial<{
12
+ [K in keyof I]: MotionValue<V> | ToValue<V>;
13
+ }> : never;
14
+ export declare function useValue<V extends Primitive, I extends Input<V>>(initial: I): [Output<V, I>, (to: SetterParam<V, I>) => void];
15
+ export {};
@@ -1,5 +1,4 @@
1
- export { interpolate, bInterpolate } from './interpolation';
2
- export { MountedBlock, ScrollableBlock } from './modules';
3
- export { useValue, useMount, useValues, type UseMountConfig } from './hooks';
1
+ export { useValue, useMount, useAnimatedList } from './hooks';
4
2
  export { withSpring, withTiming, withSequence, withDelay, withDecay, withLoop, withEase, } from './controllers';
5
- export { AnimationConfig } from './helpers';
3
+ export { AnimationConfig } from './AnimationConfig';
4
+ export { interpolateNumbers } from './interpolation';
@@ -1 +1 @@
1
- export { interpolate, bInterpolate } from './interpolate';
1
+ export { interpolateNumbers } from './interpolateNumbers';
@@ -1,16 +1,27 @@
1
- import { FluidValue, spring } from '@raidipesh78/re-motion';
2
- export type EndCallbackResult = {
3
- finished: boolean;
4
- value?: number | string;
5
- };
6
- export type EndCallback = (result: EndCallbackResult) => void;
7
- export type Controller = ReturnType<typeof spring>;
8
- export type ToValue = (animation: FluidValue) => {
9
- controller: Controller;
10
- callback?: EndCallback;
11
- };
1
+ export type Primitive = number | string;
12
2
  export interface WithCallbacks {
13
3
  onStart?: (value: number | string) => void;
14
4
  onChange?: (value: number | string) => void;
15
5
  onRest?: (value: number | string) => void;
16
6
  }
7
+ export type DriverConfig = {
8
+ type: 'spring' | 'timing' | 'decay' | 'sequence' | 'delay' | 'loop';
9
+ to?: number;
10
+ options?: {
11
+ controller?: DriverConfig;
12
+ iterations?: number;
13
+ delay?: number;
14
+ duration?: number;
15
+ easing?: (t: number) => number;
16
+ stiffness?: number;
17
+ damping?: number;
18
+ mass?: number;
19
+ velocity?: number;
20
+ clamp?: [number, number];
21
+ steps?: DriverConfig[];
22
+ onStart?: () => void;
23
+ onChange?: (v: string | number) => void;
24
+ onComplete?: () => void;
25
+ };
26
+ };
27
+ export type ToValue<V> = DriverConfig | V;
package/dist/index.d.ts CHANGED
@@ -1,9 +1,5 @@
1
- export { Easing, makeFluid as makeAnimated, fluid as animate, } from '@raidipesh78/re-motion';
2
- export { AnimationConfig, MountedBlock, ScrollableBlock } from './animation';
3
- export { interpolate, bInterpolate } from './animation';
4
- export { useValue, useMount, useValues } from './animation';
5
- export { withSpring, withTiming, withSequence, withDelay, withDecay, withLoop, withEase, } from './animation';
1
+ export { Easing, makeMotion as makeAnimated, motion as animate, } from '@raidipesh78/re-motion';
2
+ export { withSpring, withTiming, withSequence, withDelay, withDecay, withLoop, withEase, useValue, useMount, useAnimatedList, AnimationConfig, interpolateNumbers, } from './animation';
6
3
  export { useMeasure, useOutsideClick, useWindowDimension } from './hooks';
7
4
  export { useDrag, useGesture, useMouseMove, useScroll, useWheel, } from './gestures/hooks';
8
5
  export { bin, clamp, mix, rubberClamp, move, snapTo } from './gestures/helpers';
9
- export type { UseMountConfig } from './animation';