react-ui-animate 2.0.0-rc.1 → 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 (44) hide show
  1. package/.vscode/settings.json +3 -3
  2. package/LICENSE +21 -21
  3. package/README.md +115 -115
  4. package/dist/animation/animationType.d.ts +8 -0
  5. package/dist/animation/getInitialConfig.d.ts +2 -2
  6. package/dist/index.d.ts +5 -4
  7. package/dist/index.js +35 -0
  8. package/dist/index.js.map +1 -1
  9. package/package.json +49 -49
  10. package/rollup.config.js +18 -18
  11. package/src/animation/animationType.ts +17 -9
  12. package/src/animation/getInitialConfig.ts +61 -30
  13. package/src/animation/index.ts +9 -9
  14. package/src/animation/interpolation.ts +24 -24
  15. package/src/animation/modules.tsx +105 -105
  16. package/src/animation/useAnimatedValue.ts +62 -62
  17. package/src/animation/useMountedValue.ts +66 -66
  18. package/src/gestures/controllers/DragGesture.ts +177 -177
  19. package/src/gestures/controllers/Gesture.ts +54 -54
  20. package/src/gestures/controllers/MouseMoveGesture.ts +111 -111
  21. package/src/gestures/controllers/ScrollGesture.ts +107 -107
  22. package/src/gestures/controllers/WheelGesture.ts +123 -123
  23. package/src/gestures/controllers/index.ts +4 -4
  24. package/src/gestures/eventAttacher.ts +67 -67
  25. package/src/gestures/hooks/index.ts +5 -5
  26. package/src/gestures/hooks/useDrag.ts +14 -14
  27. package/src/gestures/hooks/useGesture.ts +38 -38
  28. package/src/gestures/hooks/useMouseMove.ts +11 -11
  29. package/src/gestures/hooks/useRecognizer.ts +59 -59
  30. package/src/gestures/hooks/useScroll.ts +11 -11
  31. package/src/gestures/hooks/useWheel.ts +11 -11
  32. package/src/gestures/index.ts +2 -2
  33. package/src/gestures/math.ts +120 -120
  34. package/src/gestures/types.ts +49 -49
  35. package/src/gestures/withDefault.ts +3 -3
  36. package/src/hooks/index.ts +3 -3
  37. package/src/hooks/useMeasure.ts +133 -133
  38. package/src/hooks/useOutsideClick.ts +36 -36
  39. package/src/hooks/useWindowDimension.ts +59 -59
  40. package/src/index.ts +5 -4
  41. package/src/utils/delay.ts +9 -9
  42. package/src/utils/index.ts +2 -2
  43. package/src/utils/isDefined.ts +4 -4
  44. package/tsconfig.json +25 -25
@@ -1,107 +1,107 @@
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 ScrollGesture extends Gesture {
8
- isActiveID?: any;
9
- movement: Vector2 = withDefault(0, 0);
10
- previousMovement: Vector2 = withDefault(0, 0);
11
- direction: Vector2 = withDefault(0, 0);
12
- velocity: Vector2 = withDefault(0, 0);
13
-
14
- // @override
15
- // initialize the events
16
- _initEvents() {
17
- if (this.targetElement) {
18
- this._subscribe = attachEvents(
19
- [this.targetElement],
20
- [["scroll", this.scrollElementListener.bind(this)]]
21
- );
22
- } else {
23
- this._subscribe = attachEvents(
24
- [window],
25
- [["scroll", this.scrollListener.bind(this)]]
26
- );
27
- }
28
- }
29
-
30
- _handleCallback() {
31
- if (this.callback) {
32
- this.callback({
33
- isScrolling: this.isActive,
34
- scrollX: this.movement.x,
35
- scrollY: this.movement.y,
36
- velocityX: this.velocity.x,
37
- velocityY: this.velocity.y,
38
- directionX: this.direction.x,
39
- directionY: this.direction.y,
40
- });
41
- }
42
- }
43
-
44
- onScroll({ x, y }: Vector2) {
45
- const now: number = Date.now();
46
- const deltaTime = Math.min(now - this.lastTimeStamp, 64);
47
- this.lastTimeStamp = now;
48
- const t = deltaTime / 1000; // seconds
49
-
50
- this.movement = { x, y };
51
-
52
- // Clear if scrolling
53
- if (this.isActiveID !== -1) {
54
- this.isActive = true;
55
- clearTimeout(this.isActiveID);
56
- }
57
-
58
- this.isActiveID = setTimeout(() => {
59
- this.isActive = false;
60
- this.direction = { x: 0, y: 0 };
61
-
62
- // Reset Velocity
63
- this.velocity = { x: 0, y: 0 };
64
-
65
- this._handleCallback(); // Debounce 250milliseconds
66
- }, 250);
67
-
68
- const diffX = this.movement.x - this.previousMovement.x;
69
- const diffY = this.movement.y - this.previousMovement.y;
70
-
71
- this.direction = {
72
- x: Math.sign(diffX),
73
- y: Math.sign(diffY),
74
- };
75
-
76
- this.velocity = {
77
- x: clamp(
78
- diffX / t / 1000,
79
- -1 * Gesture._VELOCITY_LIMIT,
80
- Gesture._VELOCITY_LIMIT
81
- ),
82
- y: clamp(
83
- diffY / t / 1000,
84
- -1 * Gesture._VELOCITY_LIMIT,
85
- Gesture._VELOCITY_LIMIT
86
- ),
87
- };
88
-
89
- this.previousMovement = {
90
- x: this.movement.x,
91
- y: this.movement.y,
92
- };
93
-
94
- this._handleCallback();
95
- }
96
-
97
- scrollListener() {
98
- const { pageYOffset: y, pageXOffset: x } = window;
99
- this.onScroll({ x, y });
100
- }
101
-
102
- scrollElementListener() {
103
- const x = this.targetElement?.scrollLeft || 0;
104
- const y = this.targetElement?.scrollTop || 0;
105
- this.onScroll({ x, y });
106
- }
107
- }
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 ScrollGesture extends Gesture {
8
+ isActiveID?: any;
9
+ movement: Vector2 = withDefault(0, 0);
10
+ previousMovement: Vector2 = withDefault(0, 0);
11
+ direction: Vector2 = withDefault(0, 0);
12
+ velocity: Vector2 = withDefault(0, 0);
13
+
14
+ // @override
15
+ // initialize the events
16
+ _initEvents() {
17
+ if (this.targetElement) {
18
+ this._subscribe = attachEvents(
19
+ [this.targetElement],
20
+ [["scroll", this.scrollElementListener.bind(this)]]
21
+ );
22
+ } else {
23
+ this._subscribe = attachEvents(
24
+ [window],
25
+ [["scroll", this.scrollListener.bind(this)]]
26
+ );
27
+ }
28
+ }
29
+
30
+ _handleCallback() {
31
+ if (this.callback) {
32
+ this.callback({
33
+ isScrolling: this.isActive,
34
+ scrollX: this.movement.x,
35
+ scrollY: this.movement.y,
36
+ velocityX: this.velocity.x,
37
+ velocityY: this.velocity.y,
38
+ directionX: this.direction.x,
39
+ directionY: this.direction.y,
40
+ });
41
+ }
42
+ }
43
+
44
+ onScroll({ x, y }: Vector2) {
45
+ const now: number = Date.now();
46
+ const deltaTime = Math.min(now - this.lastTimeStamp, 64);
47
+ this.lastTimeStamp = now;
48
+ const t = deltaTime / 1000; // seconds
49
+
50
+ this.movement = { x, y };
51
+
52
+ // Clear if scrolling
53
+ if (this.isActiveID !== -1) {
54
+ this.isActive = true;
55
+ clearTimeout(this.isActiveID);
56
+ }
57
+
58
+ this.isActiveID = setTimeout(() => {
59
+ this.isActive = false;
60
+ this.direction = { x: 0, y: 0 };
61
+
62
+ // Reset Velocity
63
+ this.velocity = { x: 0, y: 0 };
64
+
65
+ this._handleCallback(); // Debounce 250milliseconds
66
+ }, 250);
67
+
68
+ const diffX = this.movement.x - this.previousMovement.x;
69
+ const diffY = this.movement.y - this.previousMovement.y;
70
+
71
+ this.direction = {
72
+ x: Math.sign(diffX),
73
+ y: Math.sign(diffY),
74
+ };
75
+
76
+ this.velocity = {
77
+ x: clamp(
78
+ diffX / t / 1000,
79
+ -1 * Gesture._VELOCITY_LIMIT,
80
+ Gesture._VELOCITY_LIMIT
81
+ ),
82
+ y: clamp(
83
+ diffY / t / 1000,
84
+ -1 * Gesture._VELOCITY_LIMIT,
85
+ Gesture._VELOCITY_LIMIT
86
+ ),
87
+ };
88
+
89
+ this.previousMovement = {
90
+ x: this.movement.x,
91
+ y: this.movement.y,
92
+ };
93
+
94
+ this._handleCallback();
95
+ }
96
+
97
+ scrollListener() {
98
+ const { pageYOffset: y, pageXOffset: x } = window;
99
+ this.onScroll({ x, y });
100
+ }
101
+
102
+ scrollElementListener() {
103
+ const x = this.targetElement?.scrollLeft || 0;
104
+ const y = this.targetElement?.scrollTop || 0;
105
+ this.onScroll({ x, y });
106
+ }
107
+ }
@@ -1,123 +1,123 @@
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
- const LINE_HEIGHT = 40;
8
- const PAGE_HEIGHT = 800;
9
-
10
- export class WheelGesture extends Gesture {
11
- isActiveID?: any;
12
- movement: Vector2 = withDefault(0, 0);
13
- previousMovement: Vector2 = withDefault(0, 0);
14
- direction: Vector2 = withDefault(0, 0);
15
- velocity: Vector2 = withDefault(0, 0);
16
- delta: Vector2 = withDefault(0, 0);
17
-
18
- // Holds offsets
19
- offset: Vector2 = withDefault(0, 0);
20
- translation: Vector2 = withDefault(0, 0);
21
-
22
- // @override
23
- // initialize the events
24
- _initEvents() {
25
- if (this.targetElement) {
26
- this._subscribe = attachEvents(
27
- [this.targetElement],
28
- [["wheel", this.onWheel.bind(this)]]
29
- );
30
- }
31
- }
32
-
33
- _handleCallback() {
34
- if (this.callback) {
35
- this.callback({
36
- target: this.targetElement,
37
- isWheeling: this.isActive,
38
- deltaX: this.delta.x,
39
- deltaY: this.delta.y,
40
- directionX: this.direction.x,
41
- directionY: this.direction.y,
42
- movementX: this.movement.x,
43
- movementY: this.movement.y,
44
- offsetX: this.offset.x,
45
- offsetY: this.offset.y,
46
- velocityX: this.velocity.x,
47
- velocityY: this.velocity.y,
48
- });
49
- }
50
- }
51
-
52
- onWheel(event: WheelEvent) {
53
- let { deltaX, deltaY, deltaMode } = event;
54
-
55
- const now: number = Date.now();
56
- const deltaTime = Math.min(now - this.lastTimeStamp, 64);
57
- this.lastTimeStamp = now;
58
- const t = deltaTime / 1000; // seconds
59
-
60
- this.isActive = true;
61
-
62
- if (this.isActiveID !== -1) {
63
- this.isActive = true;
64
- clearTimeout(this.isActiveID);
65
- }
66
-
67
- this.isActiveID = setTimeout(() => {
68
- this.isActive = false;
69
- this.translation = { x: this.offset.x, y: this.offset.y };
70
- this._handleCallback();
71
-
72
- this.velocity = { x: 0, y: 0 }; // Reset Velocity
73
- this.movement = { x: 0, y: 0 };
74
- }, 200);
75
-
76
- // normalize wheel values, especially for Firefox
77
- if (deltaMode === 1) {
78
- deltaX *= LINE_HEIGHT;
79
- deltaY *= LINE_HEIGHT;
80
- } else if (deltaMode === 2) {
81
- deltaX *= PAGE_HEIGHT;
82
- deltaY *= PAGE_HEIGHT;
83
- }
84
-
85
- this.delta = { x: deltaX, y: deltaY };
86
- this.movement = {
87
- x: this.movement.x + deltaX,
88
- y: this.movement.y + deltaY,
89
- };
90
- this.offset = {
91
- x: this.translation.x + this.movement.x,
92
- y: this.translation.y + this.movement.y,
93
- };
94
-
95
- const diffX = this.movement.x - this.previousMovement.x;
96
- const diffY = this.movement.y - this.previousMovement.y;
97
-
98
- this.direction = {
99
- x: Math.sign(diffX),
100
- y: Math.sign(diffY),
101
- };
102
-
103
- this.velocity = {
104
- x: clamp(
105
- diffX / t / 1000,
106
- -1 * Gesture._VELOCITY_LIMIT,
107
- Gesture._VELOCITY_LIMIT
108
- ),
109
- y: clamp(
110
- diffY / t / 1000,
111
- -1 * Gesture._VELOCITY_LIMIT,
112
- Gesture._VELOCITY_LIMIT
113
- ),
114
- };
115
-
116
- this.previousMovement = {
117
- x: this.movement.x,
118
- y: this.movement.y,
119
- };
120
-
121
- this._handleCallback();
122
- }
123
- }
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
+ const LINE_HEIGHT = 40;
8
+ const PAGE_HEIGHT = 800;
9
+
10
+ export class WheelGesture extends Gesture {
11
+ isActiveID?: any;
12
+ movement: Vector2 = withDefault(0, 0);
13
+ previousMovement: Vector2 = withDefault(0, 0);
14
+ direction: Vector2 = withDefault(0, 0);
15
+ velocity: Vector2 = withDefault(0, 0);
16
+ delta: Vector2 = withDefault(0, 0);
17
+
18
+ // Holds offsets
19
+ offset: Vector2 = withDefault(0, 0);
20
+ translation: Vector2 = withDefault(0, 0);
21
+
22
+ // @override
23
+ // initialize the events
24
+ _initEvents() {
25
+ if (this.targetElement) {
26
+ this._subscribe = attachEvents(
27
+ [this.targetElement],
28
+ [["wheel", this.onWheel.bind(this)]]
29
+ );
30
+ }
31
+ }
32
+
33
+ _handleCallback() {
34
+ if (this.callback) {
35
+ this.callback({
36
+ target: this.targetElement,
37
+ isWheeling: this.isActive,
38
+ deltaX: this.delta.x,
39
+ deltaY: this.delta.y,
40
+ directionX: this.direction.x,
41
+ directionY: this.direction.y,
42
+ movementX: this.movement.x,
43
+ movementY: this.movement.y,
44
+ offsetX: this.offset.x,
45
+ offsetY: this.offset.y,
46
+ velocityX: this.velocity.x,
47
+ velocityY: this.velocity.y,
48
+ });
49
+ }
50
+ }
51
+
52
+ onWheel(event: WheelEvent) {
53
+ let { deltaX, deltaY, deltaMode } = event;
54
+
55
+ const now: number = Date.now();
56
+ const deltaTime = Math.min(now - this.lastTimeStamp, 64);
57
+ this.lastTimeStamp = now;
58
+ const t = deltaTime / 1000; // seconds
59
+
60
+ this.isActive = true;
61
+
62
+ if (this.isActiveID !== -1) {
63
+ this.isActive = true;
64
+ clearTimeout(this.isActiveID);
65
+ }
66
+
67
+ this.isActiveID = setTimeout(() => {
68
+ this.isActive = false;
69
+ this.translation = { x: this.offset.x, y: this.offset.y };
70
+ this._handleCallback();
71
+
72
+ this.velocity = { x: 0, y: 0 }; // Reset Velocity
73
+ this.movement = { x: 0, y: 0 };
74
+ }, 200);
75
+
76
+ // normalize wheel values, especially for Firefox
77
+ if (deltaMode === 1) {
78
+ deltaX *= LINE_HEIGHT;
79
+ deltaY *= LINE_HEIGHT;
80
+ } else if (deltaMode === 2) {
81
+ deltaX *= PAGE_HEIGHT;
82
+ deltaY *= PAGE_HEIGHT;
83
+ }
84
+
85
+ this.delta = { x: deltaX, y: deltaY };
86
+ this.movement = {
87
+ x: this.movement.x + deltaX,
88
+ y: this.movement.y + deltaY,
89
+ };
90
+ this.offset = {
91
+ x: this.translation.x + this.movement.x,
92
+ y: this.translation.y + this.movement.y,
93
+ };
94
+
95
+ const diffX = this.movement.x - this.previousMovement.x;
96
+ const diffY = this.movement.y - this.previousMovement.y;
97
+
98
+ this.direction = {
99
+ x: Math.sign(diffX),
100
+ y: Math.sign(diffY),
101
+ };
102
+
103
+ this.velocity = {
104
+ x: clamp(
105
+ diffX / t / 1000,
106
+ -1 * Gesture._VELOCITY_LIMIT,
107
+ Gesture._VELOCITY_LIMIT
108
+ ),
109
+ y: clamp(
110
+ diffY / t / 1000,
111
+ -1 * Gesture._VELOCITY_LIMIT,
112
+ Gesture._VELOCITY_LIMIT
113
+ ),
114
+ };
115
+
116
+ this.previousMovement = {
117
+ x: this.movement.x,
118
+ y: this.movement.y,
119
+ };
120
+
121
+ this._handleCallback();
122
+ }
123
+ }
@@ -1,4 +1,4 @@
1
- export * from "./DragGesture";
2
- export * from "./MouseMoveGesture";
3
- export * from "./ScrollGesture";
4
- export * from "./WheelGesture";
1
+ export * from "./DragGesture";
2
+ export * from "./MouseMoveGesture";
3
+ export * from "./ScrollGesture";
4
+ export * from "./WheelGesture";
@@ -1,67 +1,67 @@
1
- type MouseEventType =
2
- | "click"
3
- | "dblclick"
4
- | "mousedown"
5
- | "mousemove"
6
- | "mouseup"
7
- | "touchstart"
8
- | "touchmove"
9
- | "touchend"
10
- | "mouseenter"
11
- | "mouseleave"
12
- | "mouseout"
13
- | "mouseover"
14
- | "scroll"
15
- | "wheel"
16
- | "contextmenu";
17
-
18
- type DomTargetTypes = Array<Window | Document | HTMLElement>;
19
-
20
- /**
21
- * Attach single document / window event / HTMLElement
22
- */
23
- function attachEvent(
24
- domTargets: DomTargetTypes,
25
- event: MouseEventType,
26
- callback: (e: any) => void,
27
- capture: any = false
28
- ) {
29
- domTargets.forEach((target) => {
30
- target.addEventListener(event, callback, capture);
31
- });
32
-
33
- return function () {
34
- domTargets.forEach((target) => {
35
- target.removeEventListener(event, callback, capture);
36
- });
37
- };
38
- }
39
-
40
- /**
41
- * Attach multiple document / window event / HTMLElement
42
- */
43
- export function attachEvents(
44
- domTargets: DomTargetTypes,
45
- events: Array<
46
- [event: MouseEventType, callback: (e: any) => void, capture?: any]
47
- >
48
- ) {
49
- const subscribers = new Map();
50
-
51
- events.forEach(function ([event, callback, capture = false]) {
52
- subscribers.set(event, attachEvent(domTargets, event, callback, capture));
53
- });
54
-
55
- return function (eventKeys?: Array<string>) {
56
- for (const [eventKey, subscriber] of subscribers.entries()) {
57
- if (!eventKeys) {
58
- subscriber();
59
- return;
60
- }
61
-
62
- if (eventKeys.indexOf(eventKey) !== -1) {
63
- subscriber();
64
- }
65
- }
66
- };
67
- }
1
+ type MouseEventType =
2
+ | "click"
3
+ | "dblclick"
4
+ | "mousedown"
5
+ | "mousemove"
6
+ | "mouseup"
7
+ | "touchstart"
8
+ | "touchmove"
9
+ | "touchend"
10
+ | "mouseenter"
11
+ | "mouseleave"
12
+ | "mouseout"
13
+ | "mouseover"
14
+ | "scroll"
15
+ | "wheel"
16
+ | "contextmenu";
17
+
18
+ type DomTargetTypes = Array<Window | Document | HTMLElement>;
19
+
20
+ /**
21
+ * Attach single document / window event / HTMLElement
22
+ */
23
+ function attachEvent(
24
+ domTargets: DomTargetTypes,
25
+ event: MouseEventType,
26
+ callback: (e: any) => void,
27
+ capture: any = false
28
+ ) {
29
+ domTargets.forEach((target) => {
30
+ target.addEventListener(event, callback, capture);
31
+ });
32
+
33
+ return function () {
34
+ domTargets.forEach((target) => {
35
+ target.removeEventListener(event, callback, capture);
36
+ });
37
+ };
38
+ }
39
+
40
+ /**
41
+ * Attach multiple document / window event / HTMLElement
42
+ */
43
+ export function attachEvents(
44
+ domTargets: DomTargetTypes,
45
+ events: Array<
46
+ [event: MouseEventType, callback: (e: any) => void, capture?: any]
47
+ >
48
+ ) {
49
+ const subscribers = new Map();
50
+
51
+ events.forEach(function ([event, callback, capture = false]) {
52
+ subscribers.set(event, attachEvent(domTargets, event, callback, capture));
53
+ });
54
+
55
+ return function (eventKeys?: Array<string>) {
56
+ for (const [eventKey, subscriber] of subscribers.entries()) {
57
+ if (!eventKeys) {
58
+ subscriber();
59
+ return;
60
+ }
61
+
62
+ if (eventKeys.indexOf(eventKey) !== -1) {
63
+ subscriber();
64
+ }
65
+ }
66
+ };
67
+ }
@@ -1,5 +1,5 @@
1
- export * from "./useDrag";
2
- export * from "./useMouseMove";
3
- export * from "./useScroll";
4
- export * from "./useWheel";
5
- export * from "./useGesture";
1
+ export * from "./useDrag";
2
+ export * from "./useMouseMove";
3
+ export * from "./useScroll";
4
+ export * from "./useWheel";
5
+ export * from "./useGesture";
@@ -1,14 +1,14 @@
1
- import * as React from "react";
2
-
3
- import { DragEventType, UseDragConfig } from "../types";
4
- import { DragGesture } from "../controllers";
5
- import { useRecognizer } from "./useRecognizer";
6
-
7
- export function useDrag(
8
- callback: (event: DragEventType) => void,
9
- config?: UseDragConfig
10
- ) {
11
- const gesture = React.useRef(new DragGesture()).current;
12
-
13
- return useRecognizer([["drag", gesture, callback, config]]);
14
- }
1
+ import * as React from "react";
2
+
3
+ import { DragEventType, UseDragConfig } from "../types";
4
+ import { DragGesture } from "../controllers";
5
+ import { useRecognizer } from "./useRecognizer";
6
+
7
+ export function useDrag(
8
+ callback: (event: DragEventType) => void,
9
+ config?: UseDragConfig
10
+ ) {
11
+ const gesture = React.useRef(new DragGesture()).current;
12
+
13
+ return useRecognizer([["drag", gesture, callback, config]]);
14
+ }