react-ui-animate 3.0.0-rc.2 → 3.0.0-rc.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 CHANGED
@@ -2,114 +2,152 @@
2
2
 
3
3
  [![npm version](https://badge.fury.io/js/react-ui-animate.svg)](https://badge.fury.io/js/react-ui-animate)
4
4
 
5
- > React library for gestures and animation
5
+ > Create smooth animations and interactive gestures in React applications effortlessly.
6
6
 
7
7
  ### Install
8
8
 
9
- Install with npm:
9
+ You can install react-ui-animate via npm or yarn:
10
10
 
11
11
  ```sh
12
12
  npm i react-ui-animate
13
13
  ```
14
14
 
15
- or
16
- Install with yarn:
17
-
18
15
  ```sh
19
16
  yarn add react-ui-animate
20
17
  ```
21
18
 
22
19
  ### Getting Started
23
20
 
24
- `react-ui-animate` provides lots of easy to use APIs to create smooth animations and gestures.
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
22
 
26
23
  ```javascript
27
- import { AnimatedBlock, useAnimatedValue } from "react-ui-animate";
24
+ import { AnimatedBlock, useAnimatedValue } from 'react-ui-animate';
28
25
 
29
26
  export default function () {
30
- const opacity = useAnimatedValue(0); // It initializes opacity object with value 0.
27
+ // Initialize an animated opacity value
28
+ const opacity = useAnimatedValue(0);
31
29
 
32
30
  return (
33
31
  <div>
34
- {/* AnimatedBlock component can read useAnimatedValue() */}
32
+ {/* AnimatedBlock component uses the animated opacity value */}
35
33
  <AnimatedBlock
36
34
  style={{
37
35
  opacity: opacity.value, // using opacity with value property
38
36
  width: 100,
39
37
  padding: 20,
40
- background: "#39F",
38
+ background: '#39F',
41
39
  }}
42
40
  >
43
41
  ANIMATED
44
42
  </AnimatedBlock>
45
43
 
46
- {/* Assigning value to 1 auto animates from initialized value 0 to 1 smoothly */}
44
+ {/* Clicking the button changes the opacity smoothly to 1 */}
47
45
  <button onClick={() => (opacity.value = 1)}>Animate Me</button>
48
46
  </div>
49
47
  );
50
48
  }
51
49
  ```
52
50
 
53
- Animates opacity from 0 to 1.
51
+ In this example, clicking the "Animate Me" button smoothly changes the opacity of the animated block from 0 to 1.
52
+
53
+ ### Key Features
54
54
 
55
55
  #### `useAnimatedValue()`
56
56
 
57
- `useAnimatedValue()` is very flexible and powerful hook that lets you define animated values. It accepts a value and returns a node with same value on `value` property. Whenever `value` property is assigned to another value, it auto animates from one value to another.
57
+ The `useAnimatedValue()` hook is central to creating animations. It initializes an animated value and allows you to seamlessly update it to create dynamic effects.
58
58
 
59
59
  ```javascript
60
- const opacity = useAnimatedValue(0); // initialize with 0 opacity
60
+ const opacity = useAnimatedValue(0); // Start with opacity set to 0
61
61
 
62
- ...
62
+ // Use in style
63
63
  style={{
64
- opacity: opacity.value // access with `.value`
64
+ opacity: opacity.value, // Access the animated opacity value
65
65
  }}
66
- ...
67
66
 
68
- ...
69
- onClick={() => opacity.value = 1} // Assignment
70
- ...
67
+ // Update the value on user interaction
68
+ onClick={() => (opacity.value = 1)} // Changes the opacity to 1
71
69
  ```
72
70
 
73
71
  #### `AnimatedBlock`
74
72
 
75
- `AnimatedBlock` is a `div` component which can accept the animation node from `useAnimatedValue()` hook.
73
+ `AnimatedBlock` is a special component designed to work with `useAnimatedValue()`. It simplifies animating elements by directly using animated values.
76
74
 
77
75
  ```javascript
78
- const width = useAnimatedValue(100);
76
+ const width = useAnimatedValue(100); // Start with a width of 100
79
77
 
80
78
  <AnimatedBlock
81
79
  style={{
82
80
  width: width.value,
83
81
  height: 100,
84
- backgroundColor: "#39f",
82
+ backgroundColor: '#39f',
85
83
  }}
86
84
  />;
87
85
  ```
88
86
 
89
87
  #### `interpolate`
90
88
 
91
- The `interpolate()` function allows animated node value to map from input ranges to different output ranges. By default, it will extrapolate the curve beyond the ranges given, but you can also have it clamp the output value.
89
+ The `interpolate()` function is useful for mapping values from one range to another, enabling more complex animations.
92
90
 
93
91
  ```javascript
94
- import { useAnimatedValue, AnimatedBlock, interpolate } from "react-ui-animate";
92
+ import { useAnimatedValue, AnimatedBlock, interpolate } from 'react-ui-animate';
95
93
 
96
- const width = useAnimatedValue(100);
94
+ const width = useAnimatedValue(100); // Start with a width of 100
97
95
 
98
96
  <AnimatedBlock
99
97
  style={{
100
98
  width: width.value,
101
99
  height: 100,
102
- backgroundColor: interpolate(width.value, [100, 200], ["red", "blue"]),
100
+ backgroundColor: interpolate(width.value, [100, 200], ['red', 'blue']),
103
101
  }}
104
102
  />;
105
103
  ```
106
104
 
107
- `backgroundColor` is interpolated from input range `[100, 200]` to output range `["red", "blue"]`. So, when the width changes from 100 to 200, `backgroundColor` will change from `red` to `blue`.
105
+ In this example, as the width changes from 100 to 200, the background color smoothly transitions from red to blue.
106
+
107
+ ### Gestures
108
+
109
+ The `react-ui-animate` library also provides several hooks for handling different types of gestures:
110
+
111
+ 1. `useDrag`: Handles drag gestures on elements.
112
+ 2. `useMouseMove`: Handles mouse movements.
113
+ 3. `useScroll`: Handles scrolling of the document.
114
+ 4. `useWheel`: Handles wheel rotation gestures.
115
+ 5. `useGesture`: Handles combinations of various gestures.
116
+
117
+ **Example**: `useDrag`
118
+
119
+ Here’s an example of using the useDrag hook to enable drag gestures:
120
+
121
+ ```jsx
122
+ import { useAnimatedValue, AnimatedBlock, useDrag } from 'react-ui-animate';
123
+
124
+ export const Draggable = () => {
125
+ const translateX = useAnimatedValue(0);
126
+
127
+ const bind = useDrag(function ({ down, movementX }) {
128
+ translateX.value = down ? movementX : 0;
129
+ });
130
+
131
+ return (
132
+ <AnimatedBlock
133
+ {...bind()}
134
+ style={{
135
+ width: 100,
136
+ height: 100,
137
+ backgroundColor: '#3399ff',
138
+ translateX: translateX.value, // Use translateX with animated value
139
+ }}
140
+ />
141
+ );
142
+ };
143
+ ```
144
+
145
+ In this example, the blue block can be dragged horizontally by clicking and dragging.
108
146
 
109
147
  ## Documentation
110
148
 
111
- The official documentation are now published at http://react-ui-animate.js.org/
149
+ For detailed documentation and examples, visit the official [react-ui-animate documentation](http://react-ui-animate.js.org/).
112
150
 
113
151
  ## License
114
152
 
115
- MIT
153
+ This library is licensed under the MIT License.
@@ -28,5 +28,5 @@ export declare class FluidValue {
28
28
  * @param config - Optional configuration for the animation.
29
29
  * @param callback - Optional callback to be called after the animation ends.
30
30
  */
31
- setValue(updatedValue: AssignValue, config?: FluidValueConfig, callback?: Fn<ResultType, void>): void;
31
+ setValue(updatedValue: AssignValue, callback?: Fn<ResultType, void>): void;
32
32
  }
@@ -6,6 +6,6 @@ export { isFluidValue } from './helpers/isFluidValue';
6
6
  export { Easing } from './easing/Easing';
7
7
  export { interpolate } from './interpolation/Interpolation';
8
8
  export { FluidValue } from './controllers/FluidValue';
9
- export type { FluidValueConfig } from './types/animation';
9
+ export type { FluidValueConfig, Length } from './types/animation';
10
10
  export type { UseMountConfig } from './react/useMount';
11
11
  export type { ExtrapolateConfig } from './interpolation/Interpolation';
@@ -1,9 +1,9 @@
1
1
  import { FluidValue } from '../controllers/FluidValue';
2
- import type { FluidValueConfig, Length, AssignValue, OnUpdateCallback } from '../types/animation';
2
+ import type { FluidValueConfig, Length, OnUpdateFn } from '../types/animation';
3
3
  /**
4
4
  * useFluidValue
5
5
  *
6
6
  * @param value - initial value
7
7
  * @param config - the config object for `FluidValue`
8
8
  */
9
- export declare const useFluidValue: (value: Length, config?: FluidValueConfig) => [FluidValue, (updateValue: AssignValue, config?: FluidValueConfig, callback?: OnUpdateCallback) => void];
9
+ export declare const useFluidValue: (value: Length, config?: FluidValueConfig) => [FluidValue, OnUpdateFn];
@@ -1,9 +1,9 @@
1
1
  import { FluidValue } from '../controllers/FluidValue';
2
- import type { AssignValue, FluidValueConfig } from '../types/animation';
2
+ import type { FluidValueConfig } from '../types/animation';
3
3
  export interface UseMountConfig {
4
4
  from: number;
5
- enter: number | AssignValue;
6
- exit: number | AssignValue;
5
+ enter: number;
6
+ exit: number;
7
7
  enterConfig?: FluidValueConfig;
8
8
  exitConfig?: FluidValueConfig;
9
9
  config?: FluidValueConfig;
@@ -1,15 +1,16 @@
1
+ import { FluidValueConfig } from '../core';
1
2
  export declare const AnimationConfigUtils: {
2
- ELASTIC: import("../core").FluidValueConfig;
3
- BOUNCE: import("../core").FluidValueConfig;
4
- EASE: import("../core").FluidValueConfig;
5
- STIFF: import("../core").FluidValueConfig;
6
- WOOBLE: import("../core").FluidValueConfig;
7
- EASE_IN: import("../core").FluidValueConfig;
8
- EASE_OUT: import("../core").FluidValueConfig;
9
- EASE_IN_OUT: import("../core").FluidValueConfig;
10
- POWER1: import("../core").FluidValueConfig;
11
- POWER2: import("../core").FluidValueConfig;
12
- POWER3: import("../core").FluidValueConfig;
13
- POWER4: import("../core").FluidValueConfig;
14
- LINEAR: import("../core").FluidValueConfig;
3
+ ELASTIC: FluidValueConfig;
4
+ BOUNCE: FluidValueConfig;
5
+ EASE: FluidValueConfig;
6
+ STIFF: FluidValueConfig;
7
+ WOOBLE: FluidValueConfig;
8
+ EASE_IN: FluidValueConfig;
9
+ EASE_OUT: FluidValueConfig;
10
+ EASE_IN_OUT: FluidValueConfig;
11
+ POWER1: FluidValueConfig;
12
+ POWER2: FluidValueConfig;
13
+ POWER3: FluidValueConfig;
14
+ POWER4: FluidValueConfig;
15
+ LINEAR: FluidValueConfig;
15
16
  };
@@ -1,3 +1,4 @@
1
1
  import { FluidValueConfig } from '../core';
2
- export type InitialConfigType = 'linear' | 'easein' | 'easeout' | 'easeinout' | 'ease' | 'power1' | 'power2' | 'power3' | 'power4' | 'elastic' | 'stiff' | 'wooble' | 'bounce';
2
+ type InitialConfigType = 'linear' | 'easein' | 'easeout' | 'easeinout' | 'ease' | 'power1' | 'power2' | 'power3' | 'power4' | 'elastic' | 'stiff' | 'wooble' | 'bounce';
3
3
  export declare const getInitialConfig: (animationType?: InitialConfigType) => FluidValueConfig;
4
+ export {};
@@ -3,3 +3,4 @@ export { AnimatedBlock, AnimatedImage, AnimatedInline, MountedBlock, ScrollableB
3
3
  export { useAnimatedValue, UseAnimatedValueConfig } from './useAnimatedValue';
4
4
  export { useMountedValue } from './useMountedValue';
5
5
  export { AnimationConfigUtils } from './animationType';
6
+ export { withSpring, withTiming, withSequence, withDelay, } from './withFunctions';
@@ -1,4 +1,5 @@
1
1
  import { ExtrapolateConfig, FluidValue } from '../core';
2
+ import { ValueType } from './useAnimatedValue';
2
3
  /**
3
4
  * Maps the input range to the given output range using the specified extrapolation configuration.
4
5
  * The function ensures that the value is either a number or a FluidValue.
@@ -10,7 +11,7 @@ import { ExtrapolateConfig, FluidValue } from '../core';
10
11
  * @returns - The interpolated value, which will be a number or FluidValue.
11
12
  * @throws - Will throw an error if the value is not a number or FluidValue.
12
13
  */
13
- export declare function interpolate(value: FluidValue | number | string | undefined, inputRange: Array<number>, outputRange: Array<number | string>, extrapolateConfig?: ExtrapolateConfig): any;
14
+ export declare function interpolate(value: FluidValue | ValueType | number, inputRange: Array<number>, outputRange: Array<number | string>, extrapolateConfig?: ExtrapolateConfig): any;
14
15
  /**
15
16
  * A shorthand function for interpolate that maps the input range [0, 1] to the given [minOutput, maxOutput].
16
17
  * The function ensures that the value is either a number or a FluidValue.
@@ -22,4 +23,4 @@ export declare function interpolate(value: FluidValue | number | string | undefi
22
23
  * @returns - The interpolated value, which will be a number or FluidValue.
23
24
  * @throws - Will throw an error if the value is not a number or FluidValue.
24
25
  */
25
- export declare function bInterpolate(value: FluidValue | number | string | undefined, minOutput: number | string, maxOutput: number | string, extrapolateConfig?: ExtrapolateConfig): any;
26
+ export declare function bInterpolate(value: FluidValue | ValueType | number, minOutput: number | string, maxOutput: number | string, extrapolateConfig?: ExtrapolateConfig): any;
@@ -1,11 +1,12 @@
1
1
  import * as React from 'react';
2
- import { FluidValue, FluidValueConfig } from '../../core';
2
+ import { FluidValueConfig } from '../../core';
3
+ import { ValueType } from '../useAnimatedValue';
3
4
  interface MountedValueConfig extends FluidValueConfig {
4
5
  }
5
6
  interface MountedBlockProps {
6
7
  state: boolean;
7
8
  children: (animation: {
8
- value: FluidValue;
9
+ value: ValueType;
9
10
  }) => React.ReactNode;
10
11
  from?: number;
11
12
  enter?: number;
@@ -1,9 +1,8 @@
1
1
  import * as React from 'react';
2
- import { UseAnimatedValueConfig } from '../useAnimatedValue';
3
- import { FluidValue } from '../../core';
2
+ import { UseAnimatedValueConfig, ValueType } from '../useAnimatedValue';
4
3
  interface ScrollableBlockProps {
5
4
  children?: (animation: {
6
- value?: FluidValue | string | number;
5
+ value: ValueType;
7
6
  }) => React.ReactNode;
8
7
  direction?: 'single' | 'both';
9
8
  threshold?: number;
@@ -1,10 +1,9 @@
1
1
  import * as React from 'react';
2
- import { UseAnimatedValueConfig } from '../useAnimatedValue';
3
- import { FluidValue } from '../../core';
2
+ import { UseAnimatedValueConfig, ValueType } from '../useAnimatedValue';
4
3
  interface TransitionBlockProps {
5
4
  state: boolean;
6
5
  children: (animation: {
7
- value?: FluidValue | string | number;
6
+ value: ValueType;
8
7
  }) => React.ReactNode;
9
8
  config?: UseAnimatedValueConfig;
10
9
  }
@@ -1,7 +1,12 @@
1
- import { FluidValueConfig, FluidValue } from '../core';
1
+ import { FluidValueConfig } from '../core';
2
2
  type Length = number | string;
3
3
  export interface UseAnimatedValueConfig extends FluidValueConfig {
4
4
  }
5
+ type AssignValue = {
6
+ toValue: Length;
7
+ config?: UseAnimatedValueConfig;
8
+ };
9
+ export type ValueType = Length | AssignValue | ((update: (next: AssignValue) => Promise<any>) => void);
5
10
  /**
6
11
  * `useAnimatedValue` returns an animation value with `.value` and `.currentValue` property which is
7
12
  * initialized when passed to argument (`initialValue`). The retured value persist until the lifetime of
@@ -11,7 +16,7 @@ export interface UseAnimatedValueConfig extends FluidValueConfig {
11
16
  * @param { UseAnimatedValueConfig } config - Animation configuration object.
12
17
  */
13
18
  export declare function useAnimatedValue(initialValue: Length, config?: UseAnimatedValueConfig): {
14
- value: FluidValue | string | number | undefined;
19
+ value: ValueType;
15
20
  currentValue: number | string;
16
21
  };
17
22
  export {};
@@ -0,0 +1,63 @@
1
+ import type { FluidValueConfig, Length } from '../core';
2
+ interface WithSpringConfig extends Pick<FluidValueConfig, 'mass' | 'friction' | 'tension'> {
3
+ }
4
+ /**
5
+ * Creates a spring animation configuration.
6
+ * @param {Length} toValue - The target value of the animation.
7
+ * @param {WithSpringConfig} [config=AnimationConfigUtils.ELASTIC] - Optional spring configuration.
8
+ * @returns {{ toValue: Length; config: WithSpringConfig }}
9
+ */
10
+ export declare const withSpring: (toValue: Length, config?: WithSpringConfig) => {
11
+ toValue: Length;
12
+ config: WithSpringConfig;
13
+ };
14
+ interface WithTimingConfig extends Pick<FluidValueConfig, 'duration' | 'easing'> {
15
+ }
16
+ /**
17
+ * Creates a timing animation configuration.
18
+ * @param {Length} toValue - The target value of the animation.
19
+ * @param {WithTimingConfig} [config={ duration: 250 }] - Optional timing configuration.
20
+ * @returns {{ toValue: Length; config: WithTimingConfig }}
21
+ */
22
+ export declare const withTiming: (toValue: Length, config?: WithTimingConfig) => {
23
+ toValue: Length;
24
+ config: WithTimingConfig;
25
+ };
26
+ /**
27
+ * Creates a sequence of animations that run one after another.
28
+ * @param {Array<{ toValue: Length; config?: FluidValueConfig }>} configs - An array of animation configurations.
29
+ * @returns {Function} An async function that runs the animations in sequence.
30
+ */
31
+ export declare const withSequence: ([...configs]: Array<{
32
+ toValue: Length;
33
+ config?: FluidValueConfig;
34
+ }>) => (next: (arg: {
35
+ toValue: Length;
36
+ config?: FluidValueConfig;
37
+ }) => void) => Promise<void>;
38
+ /**
39
+ * Adds a delay before the given animation.
40
+ * @param {number} delay - The delay in milliseconds.
41
+ * @param {{ toValue: Length; config?: FluidValueConfig }} animation - The animation configuration ( withTiming | withSpring )
42
+ * @returns {{ toValue: Length; config: FluidValueConfig }} The updated animation configuration with delay.
43
+ */
44
+ export declare const withDelay: (delay: number, animation: {
45
+ toValue: Length;
46
+ config?: FluidValueConfig;
47
+ }) => {
48
+ config: {
49
+ delay: number;
50
+ mass?: number;
51
+ tension?: number;
52
+ friction?: number;
53
+ duration?: number;
54
+ easing?: import("../core/types/common").Fn<number, number>;
55
+ immediate?: boolean;
56
+ restDistance?: number;
57
+ onChange?: import("../core/types/common").Fn<number, void>;
58
+ onRest?: import("../core/types/common").Fn<import("../core/types/animation").ResultType, void>;
59
+ onStart?: import("../core/types/common").Fn<number, void>;
60
+ };
61
+ toValue: Length;
62
+ };
63
+ export {};
package/dist/index.d.ts CHANGED
@@ -3,6 +3,7 @@ export { AnimatedBlock, AnimatedImage, AnimatedInline, AnimationConfigUtils, Mou
3
3
  export { bInterpolate, interpolate } from './animation/lib';
4
4
  export { useAnimatedValue, useMountedValue } from './animation/lib';
5
5
  export { delay } from './utils';
6
+ export { withSpring, withTiming, withSequence, withDelay, } from './animation/lib';
6
7
  export { useMeasure, useOutsideClick, useWindowDimension } from './hooks';
7
8
  export { useDrag, useGesture, useMouseMove, useScroll, useWheel, } from './gestures/hooks';
8
9
  export { bin, clamp, mix, rubberClamp, move, snapTo } from './gestures/helpers';