react-ui-animate 2.0.0 → 3.0.0-rc.0
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/.vscode/settings.json +3 -3
- package/LICENSE +21 -21
- package/README.md +115 -115
- package/dist/animation/animationType.d.ts +15 -15
- package/dist/animation/core/animation/Animation.d.ts +16 -0
- package/dist/animation/core/animation/FluidValue.d.ts +21 -0
- package/dist/animation/core/animation/RequestAnimationFrame.d.ts +8 -0
- package/dist/animation/core/animation/SpringAnimation.d.ts +41 -0
- package/dist/animation/core/animation/TimingAnimation.d.ts +35 -0
- package/dist/animation/core/animation/TransitionValue.d.ts +21 -0
- package/dist/animation/core/controllers/Animation.d.ts +16 -0
- package/dist/animation/core/controllers/FluidValue.d.ts +32 -0
- package/dist/animation/core/controllers/RequestAnimationFrame.d.ts +8 -0
- package/dist/animation/core/controllers/SpringAnimation.d.ts +41 -0
- package/dist/animation/core/controllers/TimingAnimation.d.ts +35 -0
- package/dist/animation/core/easing/Bezier.d.ts +8 -0
- package/dist/animation/core/easing/Easing.d.ts +40 -0
- package/dist/animation/core/helpers/camelCaseToKebabCase.d.ts +8 -0
- package/dist/animation/core/helpers/camelToDash.d.ts +7 -0
- package/dist/animation/core/helpers/canInterpolate.d.ts +11 -0
- package/dist/animation/core/helpers/getAnimatableObject.d.ts +22 -0
- package/dist/animation/core/helpers/getCleanProps.d.ts +10 -0
- package/dist/animation/core/helpers/getCssValue.d.ts +8 -0
- package/dist/animation/core/helpers/getNonAnimatableStyle.d.ts +6 -0
- package/dist/animation/core/helpers/index.d.ts +5 -0
- package/dist/animation/core/helpers/isDefined.d.ts +12 -0
- package/dist/animation/core/helpers/isFluidValue.d.ts +6 -0
- package/dist/animation/core/index.d.ts +11 -0
- package/dist/animation/core/interpolation/Colors.d.ts +25 -0
- package/dist/animation/core/interpolation/Interpolation.d.ts +80 -0
- package/dist/animation/core/interpolation/__tests__/Colors.test.d.ts +1 -0
- package/dist/animation/core/react/Tags.d.ts +3 -0
- package/dist/animation/core/react/TransformStyles.d.ts +8 -0
- package/dist/animation/core/react/animated.d.ts +29 -0
- package/dist/animation/core/react/combineRefs.d.ts +2 -0
- package/dist/animation/core/react/fluid.d.ts +6 -0
- package/dist/animation/core/react/functions/camelToDash.d.ts +7 -0
- package/dist/animation/core/react/functions/getAnimatableObject.d.ts +22 -0
- package/dist/animation/core/react/functions/getCleanProps.d.ts +4 -0
- package/dist/animation/core/react/functions/getCssValue.d.ts +8 -0
- package/dist/animation/core/react/functions/getNonAnimatableStyle.d.ts +6 -0
- package/dist/animation/core/react/functions/index.d.ts +7 -0
- package/dist/animation/core/react/functions/isDefined.d.ts +6 -0
- package/dist/animation/core/react/functions/isTransitionValue.d.ts +6 -0
- package/dist/animation/core/react/helpers/camelToDash.d.ts +7 -0
- package/dist/animation/core/react/helpers/canInterpolate.d.ts +7 -0
- package/dist/animation/core/react/helpers/getAnimatableObject.d.ts +22 -0
- package/dist/animation/core/react/helpers/getCleanProps.d.ts +4 -0
- package/dist/animation/core/react/helpers/getCssValue.d.ts +8 -0
- package/dist/animation/core/react/helpers/getNonAnimatableStyle.d.ts +6 -0
- package/dist/animation/core/react/helpers/index.d.ts +7 -0
- package/dist/animation/core/react/helpers/isDefined.d.ts +6 -0
- package/dist/animation/core/react/helpers/isFluidValue.d.ts +6 -0
- package/dist/animation/core/react/helpers/isTransitionValue.d.ts +6 -0
- package/dist/animation/core/react/makeFluid.d.ts +30 -0
- package/dist/animation/core/react/transforms.d.ts +6 -0
- package/dist/animation/core/react/useFluidValue.d.ts +9 -0
- package/dist/animation/core/react/useMount.d.ts +20 -0
- package/dist/animation/core/react/useMounts.d.ts +26 -0
- package/dist/animation/core/react/useTransition.d.ts +9 -0
- package/dist/animation/core/react/useTransitions.d.ts +11 -0
- package/dist/animation/core/types/animation.d.ts +54 -0
- package/dist/animation/core/types/common.d.ts +4 -0
- package/dist/animation/core/types/fluid.d.ts +19 -0
- package/dist/animation/core/types/index.d.ts +2 -0
- package/dist/animation/getInitialConfig.d.ts +3 -3
- package/dist/animation/index.d.ts +6 -6
- package/dist/animation/interpolation.d.ts +21 -21
- package/dist/animation/lib/animationType.d.ts +15 -0
- package/dist/animation/lib/getInitialConfig.d.ts +3 -0
- package/dist/animation/lib/index.d.ts +5 -0
- package/dist/animation/lib/interpolation.d.ts +20 -0
- package/dist/animation/lib/modules/AnimatedBlock.d.ts +8 -0
- package/dist/animation/lib/modules/AnimatedImage.d.ts +8 -0
- package/dist/animation/lib/modules/AnimatedInline.d.ts +8 -0
- package/dist/animation/lib/modules/MountedBlock.d.ts +29 -0
- package/dist/animation/lib/modules/ScrollableBlock.d.ts +22 -0
- package/dist/animation/lib/modules/TransitionBlock.d.ts +18 -0
- package/dist/animation/lib/modules/index.d.ts +6 -0
- package/dist/animation/lib/useAnimatedValue.d.ts +17 -0
- package/dist/animation/lib/useMountedValue.d.ts +14 -0
- package/dist/animation/modules/AnimatedBlock.d.ts +8 -8
- package/dist/animation/modules/AnimatedImage.d.ts +8 -8
- package/dist/animation/modules/AnimatedInline.d.ts +8 -8
- package/dist/animation/modules/MountedBlock.d.ts +29 -18
- package/dist/animation/modules/ScrollableBlock.d.ts +21 -21
- package/dist/animation/modules/TransitionBlock.d.ts +17 -17
- package/dist/animation/modules/index.d.ts +6 -6
- package/dist/animation/useAnimatedValue.d.ts +22 -22
- package/dist/animation/useMountedValue.d.ts +15 -15
- package/dist/gestures/controllers/DragGesture.d.ts +17 -17
- package/dist/gestures/controllers/Gesture.d.ts +20 -20
- package/dist/gestures/controllers/MouseMoveGesture.d.ts +13 -13
- package/dist/gestures/controllers/ScrollGesture.d.ts +14 -14
- package/dist/gestures/controllers/WheelGesture.d.ts +15 -15
- package/dist/gestures/controllers/index.d.ts +4 -4
- package/dist/gestures/eventAttacher.d.ts +11 -11
- package/dist/gestures/helpers/eventAttacher.d.ts +11 -0
- package/dist/gestures/helpers/index.d.ts +1 -0
- package/dist/gestures/helpers/math.d.ts +34 -0
- package/dist/gestures/helpers/withDefault.d.ts +4 -0
- package/dist/gestures/hooks/index.d.ts +5 -5
- package/dist/gestures/hooks/useDrag.d.ts +4 -4
- package/dist/gestures/hooks/useGesture.d.ts +9 -9
- package/dist/gestures/hooks/useMouseMove.d.ts +4 -4
- package/dist/gestures/hooks/useRecognizer.d.ts +10 -10
- package/dist/gestures/hooks/useScroll.d.ts +4 -4
- package/dist/gestures/hooks/useWheel.d.ts +4 -4
- package/dist/gestures/index.d.ts +2 -2
- package/dist/gestures/math.d.ts +34 -34
- package/dist/gestures/types.d.ts +51 -51
- package/dist/gestures/withDefault.d.ts +4 -4
- package/dist/hooks/index.d.ts +3 -3
- package/dist/hooks/useMeasure.d.ts +14 -14
- package/dist/hooks/useOutsideClick.d.ts +2 -2
- package/dist/hooks/useWindowDimension.d.ts +9 -9
- package/dist/index.d.ts +9 -5
- package/dist/index.js +2758 -1052
- package/dist/index.js.map +1 -1
- package/dist/utils/delay.d.ts +5 -5
- package/dist/utils/index.d.ts +1 -2
- package/dist/utils/isDefined.d.ts +1 -1
- package/ecosystem.config.js +12 -0
- package/example/README.md +46 -0
- package/example/package-lock.json +19597 -0
- package/example/package.json +45 -0
- package/example/public/favicon.ico +0 -0
- package/example/public/index.html +20 -0
- package/example/public/logo192.png +0 -0
- package/example/public/logo512.png +0 -0
- package/example/public/manifest.json +25 -0
- package/example/public/robots.txt +3 -0
- package/example/src/App.tsx +41 -0
- package/example/src/components/Draggable.tsx +46 -0
- package/example/src/components/Gestures.tsx +151 -0
- package/example/src/components/Interpolation.tsx +21 -0
- package/example/src/components/Loop.tsx +48 -0
- package/example/src/components/MountedBlock.tsx +25 -0
- package/example/src/components/MouseMove.tsx +59 -0
- package/example/src/components/MultistageTransition.tsx +34 -0
- package/example/src/components/Scroll.tsx +39 -0
- package/example/src/components/ScrollableBlock.tsx +27 -0
- package/example/src/components/SnapTo.tsx +55 -0
- package/example/src/components/TransitionBlock.tsx +37 -0
- package/example/src/components/Wheel.tsx +39 -0
- package/example/src/components/index.ts +18 -0
- package/example/src/components/svgLine.tsx +48 -0
- package/example/src/components/useAnimatedValue.tsx +57 -0
- package/example/src/components/useMountedValue.tsx +62 -0
- package/example/src/index.css +8 -0
- package/example/src/index.tsx +16 -0
- package/example/tsconfig.json +26 -0
- package/package.json +48 -48
- package/{rollup.config.js → rollup.config.mjs} +18 -18
- package/src/animation/core/controllers/Animation.ts +27 -0
- package/src/animation/core/controllers/FluidValue.ts +97 -0
- package/src/animation/core/controllers/RequestAnimationFrame.ts +13 -0
- package/src/animation/core/controllers/SpringAnimation.ts +218 -0
- package/src/animation/core/controllers/TimingAnimation.ts +152 -0
- package/src/animation/core/easing/Bezier.ts +146 -0
- package/src/animation/core/easing/Easing.ts +132 -0
- package/src/animation/core/helpers/camelCaseToKebabCase.ts +10 -0
- package/src/animation/core/helpers/getCleanProps.ts +16 -0
- package/src/animation/core/helpers/getCssValue.ts +60 -0
- package/src/animation/core/helpers/index.ts +5 -0
- package/src/animation/core/helpers/isDefined.ts +14 -0
- package/src/animation/core/helpers/isFluidValue.ts +11 -0
- package/src/animation/core/index.ts +16 -0
- package/src/animation/core/interpolation/Colors.ts +232 -0
- package/src/animation/core/interpolation/Interpolation.ts +395 -0
- package/src/animation/core/interpolation/__tests__/Colors.test.tsx +35 -0
- package/src/animation/core/react/fluid.ts +197 -0
- package/src/animation/core/react/makeFluid.ts +294 -0
- package/src/animation/core/react/transforms.ts +90 -0
- package/src/animation/core/react/useFluidValue.ts +43 -0
- package/src/animation/core/react/useMount.ts +58 -0
- package/src/animation/core/types/animation.d.ts +56 -0
- package/src/animation/core/types/common.d.ts +4 -0
- package/src/animation/core/types/fluid.d.ts +38 -0
- package/src/animation/{animationType.ts → lib/animationType.ts} +17 -17
- package/src/animation/{getInitialConfig.ts → lib/getInitialConfig.ts} +61 -61
- package/src/animation/lib/index.ts +12 -0
- package/src/animation/{interpolation.ts → lib/interpolation.ts} +47 -48
- package/src/animation/{modules → lib/modules}/AnimatedBlock.ts +8 -8
- package/src/animation/{modules → lib/modules}/AnimatedImage.ts +8 -8
- package/src/animation/{modules → lib/modules}/AnimatedInline.ts +8 -8
- package/src/animation/lib/modules/MountedBlock.tsx +51 -0
- package/src/animation/{modules → lib/modules}/ScrollableBlock.tsx +68 -69
- package/src/animation/{modules → lib/modules}/TransitionBlock.tsx +28 -29
- package/src/animation/{modules → lib/modules}/index.ts +6 -6
- package/src/animation/{useAnimatedValue.ts → lib/useAnimatedValue.ts} +60 -71
- package/src/animation/{useMountedValue.ts → lib/useMountedValue.ts} +18 -19
- package/src/gestures/controllers/DragGesture.ts +178 -177
- package/src/gestures/controllers/Gesture.ts +54 -54
- package/src/gestures/controllers/MouseMoveGesture.ts +111 -111
- package/src/gestures/controllers/ScrollGesture.ts +107 -107
- package/src/gestures/controllers/WheelGesture.ts +123 -123
- package/src/gestures/controllers/index.ts +4 -4
- package/src/gestures/{eventAttacher.ts → helpers/eventAttacher.ts} +67 -67
- package/src/gestures/helpers/index.ts +1 -0
- package/src/gestures/{math.ts → helpers/math.ts} +120 -120
- package/src/gestures/{withDefault.ts → helpers/withDefault.ts} +3 -3
- package/src/gestures/hooks/index.ts +5 -5
- package/src/gestures/hooks/useDrag.ts +14 -14
- package/src/gestures/hooks/useGesture.ts +38 -38
- package/src/gestures/hooks/useMouseMove.ts +11 -11
- package/src/gestures/hooks/useRecognizer.ts +59 -59
- package/src/gestures/hooks/useScroll.ts +11 -11
- package/src/gestures/hooks/useWheel.ts +11 -11
- package/src/gestures/{types.ts → types/index.d.ts} +49 -49
- package/src/hooks/index.ts +3 -3
- package/src/hooks/useMeasure.ts +132 -133
- package/src/hooks/useOutsideClick.ts +36 -36
- package/src/hooks/useWindowDimension.ts +58 -59
- package/src/index.ts +42 -5
- package/src/utils/delay.ts +9 -9
- package/src/utils/index.ts +1 -2
- package/tsconfig.json +24 -25
- package/src/animation/index.ts +0 -10
- package/src/animation/modules/MountedBlock.tsx +0 -25
- package/src/gestures/index.ts +0 -2
- package/src/utils/isDefined.ts +0 -4
|
@@ -1,71 +1,60 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { AnimationConfigUtils } from './animationType';
|
|
3
|
-
|
|
4
|
-
// useAnimatedValue value type
|
|
5
|
-
type Length = number | string;
|
|
6
|
-
|
|
7
|
-
export interface UseAnimatedValueConfig extends
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
return
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
throw new Error(
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
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
|
+
import { useFluidValue, FluidValueConfig, FluidValue } from '../core';
|
|
2
|
+
import { AnimationConfigUtils } from './animationType';
|
|
3
|
+
|
|
4
|
+
// useAnimatedValue value type
|
|
5
|
+
type Length = number | string;
|
|
6
|
+
|
|
7
|
+
export interface UseAnimatedValueConfig extends FluidValueConfig {}
|
|
8
|
+
|
|
9
|
+
/**
|
|
10
|
+
* `useAnimatedValue` returns an animation value with `.value` and `.currentValue` property which is
|
|
11
|
+
* initialized when passed to argument (`initialValue`). The retured value persist until the lifetime of
|
|
12
|
+
* a component. It doesnot cast any re-renders which can is very good for performance optimization.
|
|
13
|
+
*
|
|
14
|
+
* @param { string | number } initialValue - Initial value
|
|
15
|
+
* @param { UseAnimatedValueConfig } config - Animation configuration object.
|
|
16
|
+
*/
|
|
17
|
+
export function useAnimatedValue(
|
|
18
|
+
initialValue: Length,
|
|
19
|
+
config?: UseAnimatedValueConfig
|
|
20
|
+
) {
|
|
21
|
+
const [animation, setAnimation] = useFluidValue(initialValue, {
|
|
22
|
+
...AnimationConfigUtils.EASE,
|
|
23
|
+
...config,
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
const targetObject: {
|
|
27
|
+
value: FluidValue | string | number | undefined;
|
|
28
|
+
currentValue: number | string;
|
|
29
|
+
} = {
|
|
30
|
+
value: animation,
|
|
31
|
+
currentValue: animation.get(),
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
return new Proxy(targetObject, {
|
|
35
|
+
set: function (_, key, value: any) {
|
|
36
|
+
if (key === 'value') {
|
|
37
|
+
if (typeof value === 'number' || typeof value === 'string') {
|
|
38
|
+
setAnimation(value);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return true;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
throw new Error('You cannot set any other property to animation node.');
|
|
45
|
+
},
|
|
46
|
+
get: function (_, key) {
|
|
47
|
+
if (key === 'value') {
|
|
48
|
+
return animation;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
if (key === 'currentValue') {
|
|
52
|
+
return animation.get();
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
throw new Error(
|
|
56
|
+
'You cannot access any other property from animation node.'
|
|
57
|
+
);
|
|
58
|
+
},
|
|
59
|
+
});
|
|
60
|
+
}
|
|
@@ -1,19 +1,18 @@
|
|
|
1
|
-
import
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
* `
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
11
|
-
* @param {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
}
|
|
1
|
+
import { useMount, UseMountConfig, FluidValue } from '../core';
|
|
2
|
+
|
|
3
|
+
export interface UseMountedValueConfig extends UseMountConfig {}
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* `useMountedValue` handles mounting and unmounting of a component which captures current state
|
|
7
|
+
* passed as an arugment (`state`) and exposes the shadow state which handles the mount and unmount
|
|
8
|
+
* of a component.
|
|
9
|
+
*
|
|
10
|
+
* @param { boolean } state - Boolean indicating the component should mount or unmount.
|
|
11
|
+
* @param { UseMountedValueConfig } config - Animation configuration.
|
|
12
|
+
*/
|
|
13
|
+
export function useMountedValue(state: boolean, config: UseMountedValueConfig) {
|
|
14
|
+
const mv = useMount(state, config);
|
|
15
|
+
return (
|
|
16
|
+
cb: (value: { value: FluidValue }, mounted: boolean) => React.ReactNode
|
|
17
|
+
) => mv((a, m) => cb({ value: a }, m));
|
|
18
|
+
}
|
|
@@ -1,177 +1,178 @@
|
|
|
1
|
-
import { attachEvents } from '../eventAttacher';
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
//
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
[
|
|
23
|
-
|
|
24
|
-
['
|
|
25
|
-
['
|
|
26
|
-
['
|
|
27
|
-
['
|
|
28
|
-
['
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
//
|
|
36
|
-
//
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
this.
|
|
77
|
-
this.
|
|
78
|
-
this.
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
// if
|
|
94
|
-
|
|
95
|
-
const
|
|
96
|
-
const
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
const
|
|
122
|
-
this.lastTimeStamp
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
-
|
|
151
|
-
Gesture._VELOCITY_LIMIT
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
-
|
|
156
|
-
Gesture._VELOCITY_LIMIT
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
this.
|
|
173
|
-
this.
|
|
174
|
-
this.
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
}
|
|
1
|
+
import { attachEvents } from '../helpers/eventAttacher';
|
|
2
|
+
import { clamp } from '../helpers/math';
|
|
3
|
+
import { withDefault } from '../helpers/withDefault';
|
|
4
|
+
import { Gesture } from './Gesture';
|
|
5
|
+
|
|
6
|
+
import type { Vector2 } from '../types';
|
|
7
|
+
|
|
8
|
+
export class DragGesture extends Gesture {
|
|
9
|
+
movementStart: Vector2 = withDefault(0, 0);
|
|
10
|
+
initialMovement: Vector2 = withDefault(0, 0);
|
|
11
|
+
movement: Vector2 = withDefault(0, 0);
|
|
12
|
+
previousMovement: Vector2 = withDefault(0, 0);
|
|
13
|
+
translation: Vector2 = withDefault(0, 0);
|
|
14
|
+
offset: Vector2 = withDefault(0, 0);
|
|
15
|
+
velocity: Vector2 = withDefault(0, 0);
|
|
16
|
+
|
|
17
|
+
// @override
|
|
18
|
+
// initialize the events
|
|
19
|
+
_initEvents() {
|
|
20
|
+
if (this.targetElement || this.targetElements.length > 0) {
|
|
21
|
+
this._subscribe = attachEvents(
|
|
22
|
+
[window],
|
|
23
|
+
[
|
|
24
|
+
['mousedown', this.pointerDown.bind(this)],
|
|
25
|
+
['mousemove', this.pointerMove.bind(this)],
|
|
26
|
+
['mouseup', this.pointerUp.bind(this)],
|
|
27
|
+
['touchstart', this.pointerDown.bind(this), { passive: false }],
|
|
28
|
+
['touchmove', this.pointerMove.bind(this), { passive: false }],
|
|
29
|
+
['touchend', this.pointerUp.bind(this)],
|
|
30
|
+
]
|
|
31
|
+
);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// @override - cancel events
|
|
36
|
+
// we only canceled down and move events because mouse up
|
|
37
|
+
// will not be triggered
|
|
38
|
+
_cancelEvents() {
|
|
39
|
+
if (this._subscribe) {
|
|
40
|
+
this._subscribe(['mousedown', 'mousemove', 'touchstart', 'touchmove']);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
_handleCallback() {
|
|
45
|
+
if (this.callback) {
|
|
46
|
+
this.callback({
|
|
47
|
+
args: [this.currentIndex],
|
|
48
|
+
down: this.isActive,
|
|
49
|
+
movementX: this.movement.x,
|
|
50
|
+
movementY: this.movement.y,
|
|
51
|
+
offsetX: this.translation.x,
|
|
52
|
+
offsetY: this.translation.y,
|
|
53
|
+
velocityX: this.velocity.x,
|
|
54
|
+
velocityY: this.velocity.y,
|
|
55
|
+
distanceX: Math.abs(this.movement.x),
|
|
56
|
+
distanceY: Math.abs(this.movement.y),
|
|
57
|
+
directionX: Math.sign(this.movement.x),
|
|
58
|
+
directionY: Math.sign(this.movement.y),
|
|
59
|
+
cancel: () => {
|
|
60
|
+
this._cancelEvents();
|
|
61
|
+
},
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
pointerDown(e: any) {
|
|
67
|
+
if (e.type === 'touchstart') {
|
|
68
|
+
this.movementStart = {
|
|
69
|
+
x: e.touches[0].clientX,
|
|
70
|
+
y: e.touches[0].clientY,
|
|
71
|
+
};
|
|
72
|
+
} else {
|
|
73
|
+
this.movementStart = { x: e.clientX, y: e.clientY };
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
this.movement = { x: 0, y: 0 };
|
|
77
|
+
this.offset = { x: this.translation.x, y: this.translation.y };
|
|
78
|
+
this.previousMovement = { x: 0, y: 0 };
|
|
79
|
+
this.velocity = { x: 0, y: 0 };
|
|
80
|
+
|
|
81
|
+
// find current selected element
|
|
82
|
+
const currElem = this.targetElements.find((elem: any) => elem === e.target);
|
|
83
|
+
|
|
84
|
+
if (e.target === this.targetElement || currElem) {
|
|
85
|
+
this.isActive = true;
|
|
86
|
+
e.preventDefault();
|
|
87
|
+
|
|
88
|
+
// set args
|
|
89
|
+
if (currElem) {
|
|
90
|
+
this.currentIndex = this.targetElements.indexOf(currElem);
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
// if initial function is defined then call it to get initial movementX and movementY
|
|
94
|
+
// if only select to bounded draggable element
|
|
95
|
+
const initial = this.config?.initial && this.config.initial();
|
|
96
|
+
const initialMovementX = initial?.movementX;
|
|
97
|
+
const initialMovementY = initial?.movementY;
|
|
98
|
+
|
|
99
|
+
this.initialMovement = {
|
|
100
|
+
x: initialMovementX ?? 0,
|
|
101
|
+
y: initialMovementY ?? 0,
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
this.movement = {
|
|
105
|
+
x: this.initialMovement.x,
|
|
106
|
+
y: this.initialMovement.y,
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
this.previousMovement = {
|
|
110
|
+
x: this.initialMovement.x,
|
|
111
|
+
y: this.initialMovement.y,
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
this._handleCallback();
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
pointerMove(e: any) {
|
|
119
|
+
if (this.isActive) {
|
|
120
|
+
e.preventDefault();
|
|
121
|
+
const now = Date.now();
|
|
122
|
+
const deltaTime = clamp(now - this.lastTimeStamp, 0.1, 64);
|
|
123
|
+
this.lastTimeStamp = now;
|
|
124
|
+
|
|
125
|
+
const t = deltaTime / 1000;
|
|
126
|
+
|
|
127
|
+
if (e.type === 'touchmove') {
|
|
128
|
+
this.movement = {
|
|
129
|
+
x:
|
|
130
|
+
this.initialMovement.x +
|
|
131
|
+
(e.touches[0].clientX - this.movementStart.x),
|
|
132
|
+
y:
|
|
133
|
+
this.initialMovement.y +
|
|
134
|
+
(e.touches[0].clientY - this.movementStart.y),
|
|
135
|
+
};
|
|
136
|
+
} else {
|
|
137
|
+
this.movement = {
|
|
138
|
+
x: this.initialMovement.x + (e.clientX - this.movementStart.x),
|
|
139
|
+
y: this.initialMovement.y + (e.clientY - this.movementStart.y),
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
this.translation = {
|
|
144
|
+
x: this.offset.x + this.movement.x,
|
|
145
|
+
y: this.offset.y + this.movement.y,
|
|
146
|
+
};
|
|
147
|
+
|
|
148
|
+
this.velocity = {
|
|
149
|
+
x: clamp(
|
|
150
|
+
(this.movement.x - this.previousMovement.x) / t / 1000,
|
|
151
|
+
-1 * Gesture._VELOCITY_LIMIT,
|
|
152
|
+
Gesture._VELOCITY_LIMIT
|
|
153
|
+
),
|
|
154
|
+
y: clamp(
|
|
155
|
+
(this.movement.y - this.previousMovement.y) / t / 1000,
|
|
156
|
+
-1 * Gesture._VELOCITY_LIMIT,
|
|
157
|
+
Gesture._VELOCITY_LIMIT
|
|
158
|
+
),
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
this.previousMovement = {
|
|
162
|
+
x: this.movement.x,
|
|
163
|
+
y: this.movement.y,
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
this._handleCallback();
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
pointerUp() {
|
|
171
|
+
if (this.isActive) {
|
|
172
|
+
this.isActive = false;
|
|
173
|
+
this._handleCallback();
|
|
174
|
+
this._cancelEvents();
|
|
175
|
+
this._initEvents();
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}
|
|
@@ -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
|
+
}
|