react-ui-animate 2.0.0 → 3.0.0-rc.1

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 (117) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +115 -115
  3. package/dist/animation/core/controllers/Animation.d.ts +16 -0
  4. package/dist/animation/core/controllers/FluidValue.d.ts +32 -0
  5. package/dist/animation/core/controllers/RequestAnimationFrame.d.ts +8 -0
  6. package/dist/animation/core/controllers/SpringAnimation.d.ts +41 -0
  7. package/dist/animation/core/controllers/TimingAnimation.d.ts +35 -0
  8. package/dist/animation/core/easing/Bezier.d.ts +8 -0
  9. package/dist/animation/core/easing/Easing.d.ts +40 -0
  10. package/dist/animation/core/helpers/camelCaseToKebabCase.d.ts +8 -0
  11. package/dist/animation/core/helpers/getCleanProps.d.ts +10 -0
  12. package/dist/animation/core/helpers/getCssValue.d.ts +8 -0
  13. package/dist/animation/core/helpers/index.d.ts +5 -0
  14. package/dist/animation/core/helpers/isDefined.d.ts +12 -0
  15. package/dist/animation/core/helpers/isFluidValue.d.ts +6 -0
  16. package/dist/animation/core/index.d.ts +11 -0
  17. package/dist/animation/core/interpolation/Colors.d.ts +25 -0
  18. package/dist/animation/core/interpolation/Interpolation.d.ts +80 -0
  19. package/dist/animation/core/interpolation/__tests__/Colors.test.d.ts +1 -0
  20. package/dist/animation/core/react/fluid.d.ts +6 -0
  21. package/dist/animation/core/react/makeFluid.d.ts +30 -0
  22. package/dist/animation/core/react/transforms.d.ts +6 -0
  23. package/dist/animation/core/react/useFluidValue.d.ts +9 -0
  24. package/dist/animation/core/react/useMount.d.ts +20 -0
  25. package/dist/animation/lib/animationType.d.ts +15 -0
  26. package/dist/animation/lib/getInitialConfig.d.ts +3 -0
  27. package/dist/animation/lib/index.d.ts +5 -0
  28. package/dist/animation/{interpolation.d.ts → lib/interpolation.d.ts} +20 -21
  29. package/dist/animation/lib/modules/AnimatedBlock.d.ts +8 -0
  30. package/dist/animation/lib/modules/AnimatedImage.d.ts +8 -0
  31. package/dist/animation/lib/modules/AnimatedInline.d.ts +8 -0
  32. package/dist/animation/lib/modules/MountedBlock.d.ts +29 -0
  33. package/dist/animation/{modules → lib/modules}/ScrollableBlock.d.ts +22 -21
  34. package/dist/animation/{modules → lib/modules}/TransitionBlock.d.ts +18 -17
  35. package/dist/animation/{modules → lib/modules}/index.d.ts +6 -6
  36. package/dist/animation/{useAnimatedValue.d.ts → lib/useAnimatedValue.d.ts} +17 -22
  37. package/dist/animation/{useMountedValue.d.ts → lib/useMountedValue.d.ts} +14 -15
  38. package/dist/gestures/controllers/DragGesture.d.ts +17 -17
  39. package/dist/gestures/controllers/Gesture.d.ts +20 -20
  40. package/dist/gestures/controllers/MouseMoveGesture.d.ts +13 -13
  41. package/dist/gestures/controllers/ScrollGesture.d.ts +14 -14
  42. package/dist/gestures/controllers/WheelGesture.d.ts +15 -15
  43. package/dist/gestures/controllers/index.d.ts +4 -4
  44. package/dist/gestures/helpers/eventAttacher.d.ts +11 -0
  45. package/dist/gestures/helpers/index.d.ts +1 -0
  46. package/dist/gestures/{math.d.ts → helpers/math.d.ts} +34 -34
  47. package/dist/gestures/{withDefault.d.ts → helpers/withDefault.d.ts} +4 -4
  48. package/dist/gestures/hooks/index.d.ts +5 -5
  49. package/dist/gestures/hooks/useDrag.d.ts +4 -4
  50. package/dist/gestures/hooks/useGesture.d.ts +9 -9
  51. package/dist/gestures/hooks/useMouseMove.d.ts +4 -4
  52. package/dist/gestures/hooks/useRecognizer.d.ts +10 -10
  53. package/dist/gestures/hooks/useScroll.d.ts +4 -4
  54. package/dist/gestures/hooks/useWheel.d.ts +4 -4
  55. package/dist/hooks/index.d.ts +3 -3
  56. package/dist/hooks/useMeasure.d.ts +14 -14
  57. package/dist/hooks/useOutsideClick.d.ts +2 -2
  58. package/dist/hooks/useWindowDimension.d.ts +9 -9
  59. package/dist/index.d.ts +9 -5
  60. package/dist/index.js +1 -1191
  61. package/dist/index.js.map +1 -1
  62. package/dist/utils/delay.d.ts +5 -5
  63. package/dist/utils/index.d.ts +1 -2
  64. package/package.json +49 -48
  65. package/.vscode/settings.json +0 -3
  66. package/dist/animation/animationType.d.ts +0 -15
  67. package/dist/animation/getInitialConfig.d.ts +0 -3
  68. package/dist/animation/index.d.ts +0 -6
  69. package/dist/animation/modules/AnimatedBlock.d.ts +0 -8
  70. package/dist/animation/modules/AnimatedImage.d.ts +0 -8
  71. package/dist/animation/modules/AnimatedInline.d.ts +0 -8
  72. package/dist/animation/modules/MountedBlock.d.ts +0 -18
  73. package/dist/gestures/eventAttacher.d.ts +0 -11
  74. package/dist/gestures/index.d.ts +0 -2
  75. package/dist/gestures/types.d.ts +0 -51
  76. package/dist/utils/isDefined.d.ts +0 -1
  77. package/rollup.config.js +0 -18
  78. package/src/animation/animationType.ts +0 -17
  79. package/src/animation/getInitialConfig.ts +0 -61
  80. package/src/animation/index.ts +0 -10
  81. package/src/animation/interpolation.ts +0 -48
  82. package/src/animation/modules/AnimatedBlock.ts +0 -8
  83. package/src/animation/modules/AnimatedImage.ts +0 -8
  84. package/src/animation/modules/AnimatedInline.ts +0 -8
  85. package/src/animation/modules/MountedBlock.tsx +0 -25
  86. package/src/animation/modules/ScrollableBlock.tsx +0 -69
  87. package/src/animation/modules/TransitionBlock.tsx +0 -29
  88. package/src/animation/modules/index.ts +0 -6
  89. package/src/animation/useAnimatedValue.ts +0 -71
  90. package/src/animation/useMountedValue.ts +0 -19
  91. package/src/gestures/controllers/DragGesture.ts +0 -177
  92. package/src/gestures/controllers/Gesture.ts +0 -54
  93. package/src/gestures/controllers/MouseMoveGesture.ts +0 -111
  94. package/src/gestures/controllers/ScrollGesture.ts +0 -107
  95. package/src/gestures/controllers/WheelGesture.ts +0 -123
  96. package/src/gestures/controllers/index.ts +0 -4
  97. package/src/gestures/eventAttacher.ts +0 -67
  98. package/src/gestures/hooks/index.ts +0 -5
  99. package/src/gestures/hooks/useDrag.ts +0 -14
  100. package/src/gestures/hooks/useGesture.ts +0 -38
  101. package/src/gestures/hooks/useMouseMove.ts +0 -11
  102. package/src/gestures/hooks/useRecognizer.ts +0 -59
  103. package/src/gestures/hooks/useScroll.ts +0 -11
  104. package/src/gestures/hooks/useWheel.ts +0 -11
  105. package/src/gestures/index.ts +0 -2
  106. package/src/gestures/math.ts +0 -120
  107. package/src/gestures/types.ts +0 -49
  108. package/src/gestures/withDefault.ts +0 -3
  109. package/src/hooks/index.ts +0 -3
  110. package/src/hooks/useMeasure.ts +0 -133
  111. package/src/hooks/useOutsideClick.ts +0 -36
  112. package/src/hooks/useWindowDimension.ts +0 -59
  113. package/src/index.ts +0 -5
  114. package/src/utils/delay.ts +0 -9
  115. package/src/utils/index.ts +0 -2
  116. package/src/utils/isDefined.ts +0 -4
  117. package/tsconfig.json +0 -25
@@ -1,69 +0,0 @@
1
- import * as React from 'react';
2
- import {
3
- useAnimatedValue,
4
- UseAnimatedValueConfig,
5
- ValueType,
6
- } from '../useAnimatedValue';
7
-
8
- interface ScrollableBlockProps {
9
- children?: (animation: { value: ValueType }) => React.ReactNode;
10
- direction?: 'single' | 'both';
11
- threshold?: number;
12
- animationConfig?: UseAnimatedValueConfig;
13
- }
14
-
15
- /**
16
- * ScrollableBlock - Higher order component to handle the entrance or exit animation
17
- * of a component when it enters or exit the viewport. Accepts child as a function with
18
- * `AnimatedValue` as its first argument which can be interpolated on input range [0, 1]
19
- * @prop { function } children - child as a function with `AnimatedValue` as its first argument.
20
- * @prop { 'single' | 'both' } direction - single applies animation on enter once, both applies on enter and exit.
21
- * @prop { number } threshold - should be in range 0 to 1 which equivalent to `IntersectionObserver` threshold.
22
- * @prop { UseAnimatedValueConfig } animationConfig - Animation config
23
- */
24
- export const ScrollableBlock = (props: ScrollableBlockProps) => {
25
- const {
26
- children,
27
- direction = 'single',
28
- animationConfig,
29
- threshold = 0.2,
30
- } = props;
31
- const scrollableBlockRef = React.useRef<HTMLDivElement>(null);
32
- const animation = useAnimatedValue(0, animationConfig); // 0: not intersecting | 1: intersecting
33
-
34
- React.useEffect(() => {
35
- const _scrollableBlock = scrollableBlockRef.current;
36
-
37
- const observer = new IntersectionObserver(
38
- function ([entry]) {
39
- const { isIntersecting } = entry;
40
-
41
- if (isIntersecting) {
42
- animation.value = 1;
43
- } else {
44
- if (direction === 'both') animation.value = 0;
45
- }
46
- },
47
- {
48
- root: null, // FOR VIEWPORT ONLY
49
- threshold,
50
- }
51
- );
52
-
53
- if (_scrollableBlock) {
54
- observer.observe(_scrollableBlock);
55
- }
56
-
57
- return () => {
58
- if (_scrollableBlock) {
59
- observer.unobserve(_scrollableBlock);
60
- }
61
- };
62
- }, []);
63
-
64
- return (
65
- <div ref={scrollableBlockRef}>
66
- {children && children({ value: animation.value })}
67
- </div>
68
- );
69
- };
@@ -1,29 +0,0 @@
1
- import * as React from 'react';
2
- import { bin } from '../../gestures/math';
3
- import {
4
- useAnimatedValue,
5
- UseAnimatedValueConfig,
6
- ValueType,
7
- } from '../useAnimatedValue';
8
-
9
- interface TransitionBlockProps {
10
- state: boolean;
11
- children: (animation: { value: ValueType }) => React.ReactNode;
12
- config?: UseAnimatedValueConfig;
13
- }
14
-
15
- /**
16
- * TransitionBlock - Higher order component which animates on state change.
17
- * @prop { boolean } state - Boolean indicating the current state of animation, usually `false = 0 and true = 1`.
18
- * @prop { function } children - Child as a function with `AnimatedValue` on `.value` property.
19
- * @prop { UseAnimatedValueConfig } config - Animation configuration.
20
- */
21
- export const TransitionBlock = ({
22
- state,
23
- children,
24
- config,
25
- }: TransitionBlockProps) => {
26
- const amv = useAnimatedValue(bin(state), config);
27
-
28
- return <>{children({ value: amv.value })}</>;
29
- };
@@ -1,6 +0,0 @@
1
- export * from './AnimatedBlock';
2
- export * from './AnimatedInline';
3
- export * from './AnimatedImage';
4
- export * from './MountedBlock';
5
- export * from './ScrollableBlock';
6
- export * from './TransitionBlock';
@@ -1,71 +0,0 @@
1
- import { useTransition, TransitionValueConfig } from '@raidipesh78/re-motion';
2
- import { AnimationConfigUtils } from './animationType';
3
-
4
- // useAnimatedValue value type
5
- type Length = number | string;
6
-
7
- export interface UseAnimatedValueConfig extends TransitionValueConfig {}
8
-
9
- type AssignValue = {
10
- toValue: Length;
11
- config?: UseAnimatedValueConfig;
12
- };
13
- export type ValueType =
14
- | Length
15
- | AssignValue
16
- | ((update: (next: AssignValue) => Promise<any>) => void);
17
-
18
- /**
19
- * `useAnimatedValue` returns an animation value with `.value` and `.currentValue` property which is
20
- * initialized when passed to argument (`initialValue`). The retured value persist until the lifetime of
21
- * a component. It doesnot cast any re-renders which can is very good for performance optimization.
22
- *
23
- * @param { string | number } initialValue - Initial value
24
- * @param { UseAnimatedValueConfig } config - Animation configuration object.
25
- */
26
- export function useAnimatedValue(
27
- initialValue: Length,
28
- config?: UseAnimatedValueConfig
29
- ) {
30
- const [animation, setAnimation] = useTransition(initialValue, {
31
- ...AnimationConfigUtils.EASE,
32
- ...config,
33
- });
34
-
35
- const targetObject: {
36
- value: ValueType;
37
- currentValue: number | string;
38
- } = {
39
- value: animation as any,
40
- currentValue: animation.get(),
41
- };
42
-
43
- return new Proxy(targetObject, {
44
- set: function (_, key, value: ValueType) {
45
- if (key === 'value') {
46
- if (typeof value === 'number' || typeof value === 'string') {
47
- setAnimation({ toValue: value });
48
- } else if (typeof value === 'object' || typeof value === 'function') {
49
- setAnimation(value);
50
- }
51
-
52
- return true;
53
- }
54
-
55
- throw new Error('You cannot set any other property to animation node.');
56
- },
57
- get: function (_, key) {
58
- if (key === 'value') {
59
- return animation;
60
- }
61
-
62
- if (key === 'currentValue') {
63
- return animation.get();
64
- }
65
-
66
- throw new Error(
67
- 'You cannot access any other property from animation node.'
68
- );
69
- },
70
- });
71
- }
@@ -1,19 +0,0 @@
1
- import * as React from 'react';
2
- import { useMount, UseMountConfig, FluidValue } from '@raidipesh78/re-motion';
3
-
4
- export interface UseMountedValueConfig extends UseMountConfig {}
5
-
6
- /**
7
- * `useMountedValue` handles mounting and unmounting of a component which captures current state
8
- * passed as an arugment (`state`) and exposes the shadow state which handles the mount and unmount
9
- * of a component.
10
- *
11
- * @param { boolean } state - Boolean indicating the component should mount or unmount.
12
- * @param { UseMountedValueConfig } config - Animation configuration.
13
- */
14
- export function useMountedValue(state: boolean, config: UseMountedValueConfig) {
15
- const mv = useMount(state, config);
16
- return (
17
- cb: (value: { value: FluidValue }, mounted: boolean) => React.ReactNode
18
- ) => mv((a, m) => cb({ value: a }, m));
19
- }
@@ -1,177 +0,0 @@
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 +0,0 @@
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,111 +0,0 @@
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 MouseMoveGesture extends Gesture {
8
- event?: MouseEvent;
9
- isActiveID?: any;
10
- movement: Vector2 = withDefault(0, 0);
11
- previousMovement: Vector2 = withDefault(0, 0);
12
- velocity: Vector2 = withDefault(0, 0);
13
- direction: Vector2 = withDefault(0, 0);
14
-
15
- // @override
16
- // initialize the events
17
- _initEvents() {
18
- if (this.targetElement) {
19
- this._subscribe = attachEvents(
20
- [this.targetElement],
21
- [['mousemove', this.onMouseMove.bind(this)]]
22
- );
23
- } else if (this.targetElements.length > 0) {
24
- this._subscribe = attachEvents(this.targetElements, [
25
- ['mousemove', this.onMouseMove.bind(this)],
26
- ]);
27
- } else {
28
- this._subscribe = attachEvents(
29
- [window],
30
- [['mousemove', this.onMouseMove.bind(this)]]
31
- );
32
- }
33
- }
34
-
35
- _handleCallback() {
36
- if (this.callback) {
37
- this.callback({
38
- args: [this.currentIndex],
39
- event: this.event,
40
- isMoving: this.isActive,
41
- target: this.event?.target,
42
- mouseX: this.movement.x,
43
- mouseY: this.movement.y,
44
- velocityX: this.velocity.x,
45
- velocityY: this.velocity.y,
46
- directionX: this.direction.x,
47
- directionY: this.direction.y,
48
- });
49
- }
50
- }
51
-
52
- onMouseMove(e: MouseEvent) {
53
- // find current selected element
54
- const currElem = this.targetElements.find((elem: any) => elem === e.target);
55
-
56
- // set args
57
- if (currElem) {
58
- this.currentIndex = this.targetElements.indexOf(currElem);
59
- }
60
-
61
- this.event = e;
62
-
63
- const now: number = Date.now();
64
- const deltaTime = Math.min(now - this.lastTimeStamp, 64);
65
- this.lastTimeStamp = now;
66
- const t = deltaTime / 1000; // seconds
67
-
68
- const x = e.clientX;
69
- const y = e.clientY;
70
-
71
- this.movement = { x, y };
72
-
73
- if (this.isActiveID !== -1) {
74
- this.isActive = true;
75
- clearTimeout(this.isActiveID);
76
- }
77
-
78
- this.isActiveID = setTimeout(() => {
79
- this.isActive = false;
80
- this.direction = { x: 0, y: 0 };
81
- this.velocity = { x: 0, y: 0 };
82
-
83
- this._handleCallback();
84
- }, 250); // Debounce 250 milliseconds
85
-
86
- const diffX = this.movement.x - this.previousMovement.x;
87
- const diffY = this.movement.y - this.previousMovement.y;
88
-
89
- this.direction = {
90
- x: Math.sign(diffX),
91
- y: Math.sign(diffY),
92
- };
93
-
94
- this.velocity = {
95
- x: clamp(
96
- diffX / t / 1000,
97
- -1 * Gesture._VELOCITY_LIMIT,
98
- Gesture._VELOCITY_LIMIT
99
- ),
100
- y: clamp(
101
- diffY / t / 1000,
102
- -1 * Gesture._VELOCITY_LIMIT,
103
- Gesture._VELOCITY_LIMIT
104
- ),
105
- };
106
-
107
- this.previousMovement = { x: this.movement.x, y: this.movement.y };
108
-
109
- this._handleCallback();
110
- }
111
- }
@@ -1,107 +0,0 @@
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
- }