react-ui-animate 1.4.5 → 2.0.0-rc.2

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 (52) hide show
  1. package/.vscode/settings.json +3 -0
  2. package/LICENSE +21 -21
  3. package/README.md +115 -115
  4. package/dist/animation/animationType.d.ts +15 -0
  5. package/dist/animation/getInitialConfig.d.ts +3 -3
  6. package/dist/animation/index.d.ts +5 -4
  7. package/dist/animation/interpolation.d.ts +3 -11
  8. package/dist/animation/modules.d.ts +18 -10
  9. package/dist/animation/useAnimatedValue.d.ts +11 -23
  10. package/dist/animation/useMountedValue.d.ts +5 -14
  11. package/dist/gestures/controllers/DragGesture.d.ts +2 -2
  12. package/dist/index.d.ts +5 -4
  13. package/dist/index.js +102 -114
  14. package/dist/index.js.map +1 -1
  15. package/dist/utils/delay.d.ts +5 -0
  16. package/dist/utils/index.d.ts +2 -1
  17. package/package.json +49 -49
  18. package/rollup.config.js +18 -18
  19. package/src/animation/animationType.ts +17 -0
  20. package/src/animation/getInitialConfig.ts +61 -31
  21. package/src/animation/index.ts +9 -4
  22. package/src/animation/interpolation.ts +24 -57
  23. package/src/animation/modules.tsx +105 -105
  24. package/src/animation/useAnimatedValue.ts +62 -132
  25. package/src/animation/useMountedValue.ts +66 -82
  26. package/src/gestures/controllers/DragGesture.ts +177 -176
  27. package/src/gestures/controllers/Gesture.ts +54 -54
  28. package/src/gestures/controllers/MouseMoveGesture.ts +111 -111
  29. package/src/gestures/controllers/ScrollGesture.ts +107 -107
  30. package/src/gestures/controllers/WheelGesture.ts +123 -123
  31. package/src/gestures/controllers/index.ts +4 -4
  32. package/src/gestures/eventAttacher.ts +67 -67
  33. package/src/gestures/hooks/index.ts +5 -5
  34. package/src/gestures/hooks/useDrag.ts +14 -14
  35. package/src/gestures/hooks/useGesture.ts +38 -38
  36. package/src/gestures/hooks/useMouseMove.ts +11 -11
  37. package/src/gestures/hooks/useRecognizer.ts +59 -59
  38. package/src/gestures/hooks/useScroll.ts +11 -11
  39. package/src/gestures/hooks/useWheel.ts +11 -11
  40. package/src/gestures/index.ts +2 -2
  41. package/src/gestures/math.ts +120 -120
  42. package/src/gestures/types.ts +49 -49
  43. package/src/gestures/withDefault.ts +3 -3
  44. package/src/hooks/index.ts +3 -3
  45. package/src/hooks/useMeasure.ts +133 -133
  46. package/src/hooks/useOutsideClick.ts +36 -36
  47. package/src/hooks/useWindowDimension.ts +59 -59
  48. package/src/index.ts +5 -4
  49. package/src/utils/delay.ts +9 -0
  50. package/src/utils/index.ts +2 -1
  51. package/src/utils/isDefined.ts +4 -4
  52. package/tsconfig.json +25 -25
@@ -1,82 +1,66 @@
1
- import * as React from "react";
2
- import {
3
- useTransition,
4
- TransitionValue,
5
- UseTransitionConfig,
6
- } from "@raidipesh78/re-motion";
7
-
8
- export interface InnerUseMountedValueConfig extends UseTransitionConfig {
9
- enterDuration?: number;
10
- exitDuration?: number;
11
- }
12
-
13
- interface UseMountedValueConfig {
14
- from: number;
15
- enter: number;
16
- exit: number;
17
- config?: InnerUseMountedValueConfig;
18
- }
19
-
20
- /**
21
- * useMountedValue handles mounting and unmounting of a component
22
- * @param state - boolean
23
- * @param config - useTransitionConfig
24
- * @returns mountedValueFunction with a callback with argument ( animationNode, mounted )
25
- */
26
- export function useMountedValue(state: boolean, config: UseMountedValueConfig) {
27
- const [initial, setInitial] = React.useState(true);
28
- const [mounted, setMounted] = React.useState(state);
29
- const { from, enter, exit, config: _config } = React.useRef(config).current;
30
- const [animation, setAnimation] = useTransition(from, _config);
31
-
32
- const enterDuration = config.config?.enterDuration ?? config.config?.duration;
33
- const exitDuration =
34
- config.config?.exitDuration ?? config.config?.exitDuration;
35
-
36
- React.useEffect(() => {
37
- if (state) {
38
- setInitial(true);
39
- setMounted(true);
40
- } else {
41
- setInitial(false);
42
- setAnimation(
43
- {
44
- toValue: exit,
45
- config: {
46
- duration: exitDuration,
47
- },
48
- },
49
- function ({ finished }) {
50
- if (finished) {
51
- setMounted(false);
52
- }
53
- }
54
- );
55
- }
56
- }, [state]);
57
-
58
- React.useEffect(() => {
59
- if (mounted && initial) {
60
- setAnimation(
61
- {
62
- toValue: enter,
63
- config: {
64
- duration: enterDuration,
65
- },
66
- },
67
- function () {
68
- return;
69
- }
70
- );
71
- }
72
- }, [mounted, initial]);
73
-
74
- return function (
75
- callback: (
76
- { value }: { value: TransitionValue },
77
- mounted: boolean
78
- ) => React.ReactNode
79
- ) {
80
- return callback({ value: animation }, mounted);
81
- };
82
- }
1
+ import * as React from 'react';
2
+ import {
3
+ useTransition,
4
+ TransitionValue,
5
+ UseMountConfig,
6
+ } from '@raidipesh78/re-motion';
7
+
8
+ export interface UseMountedValueConfig extends UseMountConfig {}
9
+
10
+ /**
11
+ * useMountedValue handles mounting and unmounting of a component
12
+ * @param state - boolean
13
+ * @param config - useTransitionConfig
14
+ * @returns mountedValueFunction with a callback with argument ( { value: animationNode }, mounted )
15
+ */
16
+ export function useMountedValue(state: boolean, config: UseMountedValueConfig) {
17
+ const initial = React.useRef(true);
18
+ const [mounted, setMounted] = React.useState(state);
19
+ const {
20
+ from,
21
+ enter,
22
+ exit,
23
+ config: innerConfig,
24
+ enterConfig,
25
+ exitConfig,
26
+ } = React.useRef(config).current;
27
+ const [animation, setAnimation] = useTransition(from, innerConfig);
28
+
29
+ React.useEffect(() => {
30
+ if (state) {
31
+ initial.current = true;
32
+ setMounted(true);
33
+ } else {
34
+ initial.current = false;
35
+ setAnimation(
36
+ {
37
+ toValue: exit,
38
+ config: exitConfig,
39
+ },
40
+ function ({ finished }) {
41
+ if (finished) {
42
+ setMounted(false);
43
+ }
44
+ }
45
+ );
46
+ }
47
+ }, [state]);
48
+
49
+ React.useEffect(() => {
50
+ if (mounted && initial.current) {
51
+ setAnimation({
52
+ toValue: enter,
53
+ config: enterConfig,
54
+ });
55
+ }
56
+ }, [mounted, initial.current]);
57
+
58
+ return function (
59
+ callback: (
60
+ { value: animation }: { value: TransitionValue },
61
+ mounted: boolean
62
+ ) => React.ReactNode
63
+ ) {
64
+ return callback({ value: animation }, mounted);
65
+ };
66
+ }
@@ -1,176 +1,177 @@
1
- import { attachEvents } from "../eventAttacher";
2
- import { Vector2 } from "../types";
3
- import { clamp } from "../math";
4
- import { withDefault } from "../withDefault";
5
- import { Gesture } from "./Gesture";
6
-
7
- export class DragGesture extends Gesture {
8
- movementStart: Vector2 = withDefault(0, 0);
9
- initialMovement: Vector2 = withDefault(0, 0);
10
- movement: Vector2 = withDefault(0, 0);
11
- previousMovement: Vector2 = withDefault(0, 0);
12
- translation: Vector2 = withDefault(0, 0);
13
- offset: Vector2 = withDefault(0, 0);
14
- velocity: Vector2 = withDefault(0, 0);
15
-
16
- // @override
17
- // initialize the events
18
- _initEvents() {
19
- if (this.targetElement || this.targetElements.length > 0) {
20
- this._subscribe = attachEvents(
21
- [window],
22
- [
23
- ["mousedown", this.pointerDown.bind(this)],
24
- ["mousemove", this.pointerMove.bind(this)],
25
- ["mouseup", this.pointerUp.bind(this)],
26
- ["touchstart", this.pointerDown.bind(this), { passive: false }],
27
- ["touchmove", this.pointerMove.bind(this), { passive: false }],
28
- ["touchend", this.pointerUp.bind(this)],
29
- ]
30
- );
31
- }
32
- }
33
-
34
- // @override - cancel events
35
- // we only canceled down and move events because mouse up
36
- // will not be triggered
37
- _cancelEvents() {
38
- if (this._subscribe) {
39
- this._subscribe(["mousedown", "mousemove", "touchstart", "touchmove"]);
40
- }
41
- }
42
-
43
- _handleCallback() {
44
- if (this.callback) {
45
- this.callback({
46
- args: [this.currentIndex],
47
- down: this.isActive,
48
- movementX: this.movement.x,
49
- movementY: this.movement.y,
50
- offsetX: this.translation.x,
51
- offsetY: this.translation.y,
52
- velocityX: this.velocity.x,
53
- velocityY: this.velocity.y,
54
- distanceX: Math.abs(this.movement.x),
55
- distanceY: Math.abs(this.movement.y),
56
- directionX: Math.sign(this.movement.x),
57
- directionY: Math.sign(this.movement.y),
58
- cancel: () => {
59
- this._cancelEvents();
60
- },
61
- });
62
- }
63
- }
64
-
65
- pointerDown(e: any) {
66
- if (e.type === "touchstart") {
67
- this.movementStart = {
68
- x: e.touches[0].clientX,
69
- y: e.touches[0].clientY,
70
- };
71
- } else {
72
- this.movementStart = { x: e.clientX, y: e.clientY };
73
- }
74
-
75
- this.movement = { x: 0, y: 0 };
76
- this.offset = { x: this.translation.x, y: this.translation.y };
77
- this.previousMovement = { x: 0, y: 0 };
78
- this.velocity = { x: 0, y: 0 };
79
-
80
- // find current selected element
81
- const currElem = this.targetElements.find((elem: any) => elem === e.target);
82
-
83
- if (e.target === this.targetElement || currElem) {
84
- this.isActive = true;
85
- e.preventDefault();
86
-
87
- // set args
88
- if (currElem) {
89
- this.currentIndex = this.targetElements.indexOf(currElem);
90
- }
91
-
92
- // if initial function is defined then call it to get initial movementX and movementY
93
- // if only select to bounded draggable element
94
- const initial = this.config?.initial && this.config.initial();
95
- const initialMovementX = initial?.movementX;
96
- const initialMovementY = initial?.movementY;
97
-
98
- this.initialMovement = {
99
- x: initialMovementX ?? 0,
100
- y: initialMovementY ?? 0,
101
- };
102
-
103
- this.movement = {
104
- x: this.initialMovement.x,
105
- y: this.initialMovement.y,
106
- };
107
-
108
- this.previousMovement = {
109
- x: this.initialMovement.x,
110
- y: this.initialMovement.y,
111
- };
112
-
113
- this._handleCallback();
114
- }
115
- }
116
-
117
- pointerMove(e: any) {
118
- if (this.isActive) {
119
- e.preventDefault();
120
- const now = Date.now();
121
- const deltaTime = Math.min(now - this.lastTimeStamp, 64);
122
- this.lastTimeStamp = now;
123
-
124
- const t = deltaTime / 1000;
125
-
126
- if (e.type === "touchmove") {
127
- this.movement = {
128
- x:
129
- this.initialMovement.x +
130
- (e.touches[0].clientX - this.movementStart.x),
131
- y:
132
- this.initialMovement.y +
133
- (e.touches[0].clientY - this.movementStart.y),
134
- };
135
- } else {
136
- this.movement = {
137
- x: this.initialMovement.x + (e.clientX - this.movementStart.x),
138
- y: this.initialMovement.y + (e.clientY - this.movementStart.y),
139
- };
140
- }
141
-
142
- this.translation = {
143
- x: this.offset.x + this.movement.x,
144
- y: this.offset.y + this.movement.y,
145
- };
146
-
147
- this.velocity = {
148
- x: clamp(
149
- (this.movement.x - this.previousMovement.x) / t / 1000,
150
- -1 * Gesture._VELOCITY_LIMIT,
151
- Gesture._VELOCITY_LIMIT
152
- ),
153
- y: clamp(
154
- (this.movement.y - this.previousMovement.y) / t / 1000,
155
- -1 * Gesture._VELOCITY_LIMIT,
156
- Gesture._VELOCITY_LIMIT
157
- ),
158
- };
159
-
160
- this.previousMovement = {
161
- x: this.movement.x,
162
- y: this.movement.y,
163
- };
164
-
165
- this._handleCallback();
166
- }
167
- }
168
-
169
- pointerUp() {
170
- if (this.isActive) {
171
- this.isActive = false;
172
- this._handleCallback();
173
- this._initEvents();
174
- }
175
- }
176
- }
1
+ import { attachEvents } from '../eventAttacher';
2
+ import { Vector2 } from '../types';
3
+ import { clamp } from '../math';
4
+ import { withDefault } from '../withDefault';
5
+ import { Gesture } from './Gesture';
6
+
7
+ export class DragGesture extends Gesture {
8
+ movementStart: Vector2 = withDefault(0, 0);
9
+ initialMovement: Vector2 = withDefault(0, 0);
10
+ movement: Vector2 = withDefault(0, 0);
11
+ previousMovement: Vector2 = withDefault(0, 0);
12
+ translation: Vector2 = withDefault(0, 0);
13
+ offset: Vector2 = withDefault(0, 0);
14
+ velocity: Vector2 = withDefault(0, 0);
15
+
16
+ // @override
17
+ // initialize the events
18
+ _initEvents() {
19
+ if (this.targetElement || this.targetElements.length > 0) {
20
+ this._subscribe = attachEvents(
21
+ [window],
22
+ [
23
+ ['mousedown', this.pointerDown.bind(this)],
24
+ ['mousemove', this.pointerMove.bind(this)],
25
+ ['mouseup', this.pointerUp.bind(this)],
26
+ ['touchstart', this.pointerDown.bind(this), { passive: false }],
27
+ ['touchmove', this.pointerMove.bind(this), { passive: false }],
28
+ ['touchend', this.pointerUp.bind(this)],
29
+ ]
30
+ );
31
+ }
32
+ }
33
+
34
+ // @override - cancel events
35
+ // we only canceled down and move events because mouse up
36
+ // will not be triggered
37
+ _cancelEvents() {
38
+ if (this._subscribe) {
39
+ this._subscribe(['mousedown', 'mousemove', 'touchstart', 'touchmove']);
40
+ }
41
+ }
42
+
43
+ _handleCallback() {
44
+ if (this.callback) {
45
+ this.callback({
46
+ args: [this.currentIndex],
47
+ down: this.isActive,
48
+ movementX: this.movement.x,
49
+ movementY: this.movement.y,
50
+ offsetX: this.translation.x,
51
+ offsetY: this.translation.y,
52
+ velocityX: this.velocity.x,
53
+ velocityY: this.velocity.y,
54
+ distanceX: Math.abs(this.movement.x),
55
+ distanceY: Math.abs(this.movement.y),
56
+ directionX: Math.sign(this.movement.x),
57
+ directionY: Math.sign(this.movement.y),
58
+ cancel: () => {
59
+ this._cancelEvents();
60
+ },
61
+ });
62
+ }
63
+ }
64
+
65
+ pointerDown(e: any) {
66
+ if (e.type === 'touchstart') {
67
+ this.movementStart = {
68
+ x: e.touches[0].clientX,
69
+ y: e.touches[0].clientY,
70
+ };
71
+ } else {
72
+ this.movementStart = { x: e.clientX, y: e.clientY };
73
+ }
74
+
75
+ this.movement = { x: 0, y: 0 };
76
+ this.offset = { x: this.translation.x, y: this.translation.y };
77
+ this.previousMovement = { x: 0, y: 0 };
78
+ this.velocity = { x: 0, y: 0 };
79
+
80
+ // find current selected element
81
+ const currElem = this.targetElements.find((elem: any) => elem === e.target);
82
+
83
+ if (e.target === this.targetElement || currElem) {
84
+ this.isActive = true;
85
+ e.preventDefault();
86
+
87
+ // set args
88
+ if (currElem) {
89
+ this.currentIndex = this.targetElements.indexOf(currElem);
90
+ }
91
+
92
+ // if initial function is defined then call it to get initial movementX and movementY
93
+ // if only select to bounded draggable element
94
+ const initial = this.config?.initial && this.config.initial();
95
+ const initialMovementX = initial?.movementX;
96
+ const initialMovementY = initial?.movementY;
97
+
98
+ this.initialMovement = {
99
+ x: initialMovementX ?? 0,
100
+ y: initialMovementY ?? 0,
101
+ };
102
+
103
+ this.movement = {
104
+ x: this.initialMovement.x,
105
+ y: this.initialMovement.y,
106
+ };
107
+
108
+ this.previousMovement = {
109
+ x: this.initialMovement.x,
110
+ y: this.initialMovement.y,
111
+ };
112
+
113
+ this._handleCallback();
114
+ }
115
+ }
116
+
117
+ pointerMove(e: any) {
118
+ if (this.isActive) {
119
+ e.preventDefault();
120
+ const now = Date.now();
121
+ const deltaTime = clamp(now - this.lastTimeStamp, 0.1, 64);
122
+ this.lastTimeStamp = now;
123
+
124
+ const t = deltaTime / 1000;
125
+
126
+ if (e.type === 'touchmove') {
127
+ this.movement = {
128
+ x:
129
+ this.initialMovement.x +
130
+ (e.touches[0].clientX - this.movementStart.x),
131
+ y:
132
+ this.initialMovement.y +
133
+ (e.touches[0].clientY - this.movementStart.y),
134
+ };
135
+ } else {
136
+ this.movement = {
137
+ x: this.initialMovement.x + (e.clientX - this.movementStart.x),
138
+ y: this.initialMovement.y + (e.clientY - this.movementStart.y),
139
+ };
140
+ }
141
+
142
+ this.translation = {
143
+ x: this.offset.x + this.movement.x,
144
+ y: this.offset.y + this.movement.y,
145
+ };
146
+
147
+ this.velocity = {
148
+ x: clamp(
149
+ (this.movement.x - this.previousMovement.x) / t / 1000,
150
+ -1 * Gesture._VELOCITY_LIMIT,
151
+ Gesture._VELOCITY_LIMIT
152
+ ),
153
+ y: clamp(
154
+ (this.movement.y - this.previousMovement.y) / t / 1000,
155
+ -1 * Gesture._VELOCITY_LIMIT,
156
+ Gesture._VELOCITY_LIMIT
157
+ ),
158
+ };
159
+
160
+ this.previousMovement = {
161
+ x: this.movement.x,
162
+ y: this.movement.y,
163
+ };
164
+
165
+ this._handleCallback();
166
+ }
167
+ }
168
+
169
+ pointerUp() {
170
+ if (this.isActive) {
171
+ this.isActive = false;
172
+ this._handleCallback();
173
+ this._cancelEvents();
174
+ this._initEvents();
175
+ }
176
+ }
177
+ }
@@ -1,54 +1,54 @@
1
- export class Gesture {
2
- currentIndex?: number;
3
- lastTimeStamp: number = Date.now();
4
- isActive: boolean = false;
5
- targetElement?: HTMLElement; // represents the bounded element
6
- targetElements: Array<HTMLElement> = []; // represents the bounded elements
7
- config?: any;
8
- callback?: <T>(event: T) => void;
9
- _subscribe?: (eventKeys?: Array<string>) => void;
10
- static _VELOCITY_LIMIT: number = 20;
11
-
12
- // it must be overridden by other child classes
13
- _initEvents() {}
14
-
15
- // cancel events
16
- // we only canceled down and move events because mouse up
17
- // will not be triggered
18
- _cancelEvents() {
19
- if (this._subscribe) {
20
- this._subscribe();
21
- }
22
- }
23
-
24
- // re-apply new callback
25
- applyCallback(callback: <T>(event: T) => void) {
26
- this.callback = callback;
27
- }
28
-
29
- // apply gesture
30
- applyGesture({
31
- targetElement,
32
- targetElements,
33
- callback,
34
- config,
35
- }: {
36
- targetElement?: any;
37
- targetElements?: any;
38
- callback: <T>(event: T) => void;
39
- config?: any;
40
- }) {
41
- this.targetElement = targetElement;
42
- this.targetElements = targetElements.map(
43
- (element: { current: any }) => element.current
44
- );
45
- this.callback = callback;
46
- this.config = config;
47
-
48
- // initialize events
49
- this._initEvents();
50
-
51
- // unbind
52
- return () => this._subscribe && this._subscribe();
53
- }
54
- }
1
+ export class Gesture {
2
+ currentIndex?: number;
3
+ lastTimeStamp: number = Date.now();
4
+ isActive: boolean = false;
5
+ targetElement?: HTMLElement; // represents the bounded element
6
+ targetElements: Array<HTMLElement> = []; // represents the bounded elements
7
+ config?: any;
8
+ callback?: <T>(event: T) => void;
9
+ _subscribe?: (eventKeys?: Array<string>) => void;
10
+ static _VELOCITY_LIMIT: number = 20;
11
+
12
+ // it must be overridden by other child classes
13
+ _initEvents() {}
14
+
15
+ // cancel events
16
+ // we only canceled down and move events because mouse up
17
+ // will not be triggered
18
+ _cancelEvents() {
19
+ if (this._subscribe) {
20
+ this._subscribe();
21
+ }
22
+ }
23
+
24
+ // re-apply new callback
25
+ applyCallback(callback: <T>(event: T) => void) {
26
+ this.callback = callback;
27
+ }
28
+
29
+ // apply gesture
30
+ applyGesture({
31
+ targetElement,
32
+ targetElements,
33
+ callback,
34
+ config,
35
+ }: {
36
+ targetElement?: any;
37
+ targetElements?: any;
38
+ callback: <T>(event: T) => void;
39
+ config?: any;
40
+ }) {
41
+ this.targetElement = targetElement;
42
+ this.targetElements = targetElements.map(
43
+ (element: { current: any }) => element.current
44
+ );
45
+ this.callback = callback;
46
+ this.config = config;
47
+
48
+ // initialize events
49
+ this._initEvents();
50
+
51
+ // unbind
52
+ return () => this._subscribe && this._subscribe();
53
+ }
54
+ }