react-ui-animate 2.0.0-rc.2 → 2.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.
Files changed (73) hide show
  1. package/.vscode/settings.json +3 -3
  2. package/LICENSE +21 -21
  3. package/README.md +115 -115
  4. package/dist/animation/index.d.ts +1 -0
  5. package/dist/animation/modules/AnimatedBlock.d.ts +8 -0
  6. package/dist/animation/modules/AnimatedImage.d.ts +8 -0
  7. package/dist/animation/modules/AnimatedInline.d.ts +8 -0
  8. package/dist/animation/modules/MountedBlock.d.ts +18 -0
  9. package/dist/animation/modules/ScrollableBlock.d.ts +22 -0
  10. package/dist/animation/modules/TransitionBlock.d.ts +18 -0
  11. package/dist/animation/modules/index.d.ts +6 -0
  12. package/dist/animation/useAnimatedValue.d.ts +7 -3
  13. package/dist/animation/useMountedValue.d.ts +5 -4
  14. package/dist/gestures/controllers/MouseMoveGesture.d.ts +2 -2
  15. package/dist/gestures/controllers/ScrollGesture.d.ts +2 -2
  16. package/dist/gestures/controllers/WheelGesture.d.ts +2 -2
  17. package/dist/gestures/controllers/index.d.ts +4 -4
  18. package/dist/gestures/eventAttacher.d.ts +1 -1
  19. package/dist/gestures/hooks/index.d.ts +5 -5
  20. package/dist/gestures/hooks/useDrag.d.ts +1 -1
  21. package/dist/gestures/hooks/useGesture.d.ts +1 -1
  22. package/dist/gestures/hooks/useMouseMove.d.ts +1 -1
  23. package/dist/gestures/hooks/useRecognizer.d.ts +1 -1
  24. package/dist/gestures/hooks/useScroll.d.ts +1 -1
  25. package/dist/gestures/hooks/useWheel.d.ts +1 -1
  26. package/dist/gestures/index.d.ts +2 -2
  27. package/dist/index.d.ts +1 -1
  28. package/dist/index.js +181 -150
  29. package/dist/index.js.map +1 -1
  30. package/package.json +49 -49
  31. package/rollup.config.js +18 -18
  32. package/src/animation/animationType.ts +17 -17
  33. package/src/animation/getInitialConfig.ts +61 -61
  34. package/src/animation/index.ts +10 -9
  35. package/src/animation/interpolation.ts +24 -24
  36. package/src/animation/modules/AnimatedBlock.ts +8 -0
  37. package/src/animation/modules/AnimatedImage.ts +8 -0
  38. package/src/animation/modules/AnimatedInline.ts +8 -0
  39. package/src/animation/modules/MountedBlock.tsx +25 -0
  40. package/src/animation/modules/ScrollableBlock.tsx +62 -0
  41. package/src/animation/modules/TransitionBlock.tsx +26 -0
  42. package/src/animation/modules/index.ts +6 -0
  43. package/src/animation/useAnimatedValue.ts +71 -62
  44. package/src/animation/useMountedValue.ts +67 -66
  45. package/src/gestures/controllers/DragGesture.ts +177 -177
  46. package/src/gestures/controllers/Gesture.ts +54 -54
  47. package/src/gestures/controllers/MouseMoveGesture.ts +111 -111
  48. package/src/gestures/controllers/ScrollGesture.ts +107 -107
  49. package/src/gestures/controllers/WheelGesture.ts +123 -123
  50. package/src/gestures/controllers/index.ts +4 -4
  51. package/src/gestures/eventAttacher.ts +67 -67
  52. package/src/gestures/hooks/index.ts +5 -5
  53. package/src/gestures/hooks/useDrag.ts +14 -14
  54. package/src/gestures/hooks/useGesture.ts +38 -38
  55. package/src/gestures/hooks/useMouseMove.ts +11 -11
  56. package/src/gestures/hooks/useRecognizer.ts +59 -59
  57. package/src/gestures/hooks/useScroll.ts +11 -11
  58. package/src/gestures/hooks/useWheel.ts +11 -11
  59. package/src/gestures/index.ts +2 -2
  60. package/src/gestures/math.ts +120 -120
  61. package/src/gestures/types.ts +49 -49
  62. package/src/gestures/withDefault.ts +3 -3
  63. package/src/hooks/index.ts +3 -3
  64. package/src/hooks/useMeasure.ts +133 -133
  65. package/src/hooks/useOutsideClick.ts +36 -36
  66. package/src/hooks/useWindowDimension.ts +59 -59
  67. package/src/index.ts +5 -5
  68. package/src/utils/delay.ts +9 -9
  69. package/src/utils/index.ts +2 -2
  70. package/src/utils/isDefined.ts +4 -4
  71. package/tsconfig.json +25 -25
  72. package/dist/animation/modules.d.ts +0 -55
  73. package/src/animation/modules.tsx +0 -105
@@ -1,177 +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 = 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
+ 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
+ }