react-ui-animate 1.4.4 → 2.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.
@@ -1,55 +1,18 @@
1
- import * as React from "react";
2
- import {
3
- useTransition,
4
- TransitionValue,
5
- ResultType,
6
- } from "@raidipesh78/re-motion";
7
- import { InitialConfigType, getInitialConfig } from "./getInitialConfig";
1
+ import { useTransition, UseTransitionConfig } from '@raidipesh78/re-motion';
8
2
 
9
3
  // useAnimatedValue value type
10
4
  type AnimatedValueType = number | string;
5
+ export interface UseAnimatedValueConfig extends UseTransitionConfig {}
11
6
 
12
- /**
13
- * getValue checks for type of initialValue and throws error
14
- * for type other than AnimatedValueType
15
- */
16
- const getValue = (value: AnimatedValueType) => {
17
- if (typeof value === "number" || typeof value === "string") {
18
- return value;
19
- } else {
20
- throw new Error(
21
- "Invalid Value! Animated value only accepts string or number."
22
- );
23
- }
7
+ type Length = number | string;
8
+ type AssignValue = {
9
+ toValue: Length;
10
+ config?: UseAnimatedValueConfig;
24
11
  };
25
-
26
- // General config type
27
- export interface GenericAnimationConfig {
28
- duration?: number;
29
- mass?: number;
30
- friction?: number;
31
- tension?: number;
32
- easing?: (t: number) => number;
33
- delay?: number;
34
- }
35
-
36
- export interface UseAnimatedValueConfig extends GenericAnimationConfig {
37
- animationType?: InitialConfigType;
38
- onAnimationEnd?: (value: ResultType) => void;
39
- listener?: (value: number) => void;
40
- immediate?: boolean;
41
- }
42
-
43
- export type ValueReturnType =
44
- | TransitionValue
45
- | number
46
- | string
47
- | { toValue: number | string; immediate?: boolean };
48
-
49
- interface UseAnimatedValueReturn {
50
- value: ValueReturnType;
51
- currentValue: number | string;
52
- }
12
+ export type ValueType =
13
+ | Length
14
+ | AssignValue
15
+ | ((update: (next: AssignValue) => Promise<any>) => void);
53
16
 
54
17
  /**
55
18
  * useAnimatedValue for animated transitions
@@ -57,72 +20,42 @@ interface UseAnimatedValueReturn {
57
20
  export function useAnimatedValue(
58
21
  initialValue: AnimatedValueType,
59
22
  config?: UseAnimatedValueConfig
60
- ): UseAnimatedValueReturn {
61
- const _isInitial = React.useRef(true);
62
- const _initialValue: number | string = getValue(initialValue);
63
-
64
- const animationType = config?.animationType ?? "ease"; // Defines default animation
65
- const onAnimationEnd = config?.onAnimationEnd;
66
- const listener = config?.listener;
67
-
68
- const [animation, setAnimation] = useTransition(_initialValue, {
69
- ...getInitialConfig(animationType),
70
- ...config,
71
- onRest: function (result: any) {
72
- if (result.finished) {
73
- onAnimationEnd && onAnimationEnd(result.value);
74
- }
75
- },
76
- onChange: function (value: number) {
77
- listener && listener(value);
78
- },
79
- });
80
-
81
- // doesn't fire on initial render
82
- React.useEffect(() => {
83
- if (!_isInitial.current) {
84
- setAnimation({ toValue: _initialValue });
85
- }
86
- _isInitial.current = false;
87
- }, [_initialValue]);
23
+ ) {
24
+ const [animation, setAnimation] = useTransition(initialValue, config);
88
25
 
89
26
  const targetObject: {
90
27
  value: any;
91
- currentValue: string | number;
28
+ currentValue: number | string;
92
29
  } = {
93
30
  value: animation,
94
31
  currentValue: animation.get(),
95
32
  };
96
33
 
97
34
  return new Proxy(targetObject, {
98
- set: function (
99
- _,
100
- key,
101
- value: number | string | { toValue: number | string; immediate?: boolean }
102
- ) {
103
- if (key === "value") {
104
- if (typeof value === "number" || typeof value === "string") {
35
+ set: function (_, key, value: ValueType) {
36
+ if (key === 'value') {
37
+ if (typeof value === 'number' || typeof value === 'string') {
105
38
  setAnimation({ toValue: value });
106
- } else if (typeof value === "object") {
107
- setAnimation({ toValue: value.toValue, immediate: value?.immediate });
39
+ } else if (typeof value === 'object' || typeof value === 'function') {
40
+ setAnimation(value);
108
41
  }
109
42
 
110
43
  return true;
111
44
  }
112
45
 
113
- throw new Error("You cannot set any other property to animation node.");
46
+ throw new Error('You cannot set any other property to animation node.');
114
47
  },
115
48
  get: function (_, key) {
116
- if (key === "value") {
49
+ if (key === 'value') {
117
50
  return animation;
118
51
  }
119
52
 
120
- if (key === "currentValue") {
53
+ if (key === 'currentValue') {
121
54
  return animation.get();
122
55
  }
123
56
 
124
57
  throw new Error(
125
- "You cannot access any other property from animation node."
58
+ 'You cannot access any other property from animation node.'
126
59
  );
127
60
  },
128
61
  });
@@ -1,48 +1,41 @@
1
- import * as React from "react";
1
+ import * as React from 'react';
2
2
  import {
3
3
  useTransition,
4
4
  TransitionValue,
5
- UseTransitionConfig,
6
- } from "@raidipesh78/re-motion";
5
+ UseMountConfig,
6
+ } from '@raidipesh78/re-motion';
7
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
- }
8
+ export interface UseMountedValueConfig extends UseMountConfig {}
19
9
 
20
10
  /**
21
11
  * useMountedValue handles mounting and unmounting of a component
22
12
  * @param state - boolean
23
13
  * @param config - useTransitionConfig
24
- * @returns mountedValueFunction with a callback with argument ( animationNode, mounted )
14
+ * @returns mountedValueFunction with a callback with argument ( { value: animationNode }, mounted )
25
15
  */
26
16
  export function useMountedValue(state: boolean, config: UseMountedValueConfig) {
27
- const [initial, setInitial] = React.useState(true);
17
+ const initial = React.useRef(true);
28
18
  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;
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);
35
28
 
36
29
  React.useEffect(() => {
37
30
  if (state) {
38
- setInitial(true);
31
+ initial.current = true;
39
32
  setMounted(true);
40
33
  } else {
41
- setInitial(false);
34
+ initial.current = false;
42
35
  setAnimation(
43
36
  {
44
37
  toValue: exit,
45
- duration: exitDuration,
38
+ config: exitConfig,
46
39
  },
47
40
  function ({ finished }) {
48
41
  if (finished) {
@@ -54,22 +47,17 @@ export function useMountedValue(state: boolean, config: UseMountedValueConfig) {
54
47
  }, [state]);
55
48
 
56
49
  React.useEffect(() => {
57
- if (mounted && initial) {
58
- setAnimation(
59
- {
60
- toValue: enter,
61
- duration: enterDuration,
62
- },
63
- function () {
64
- return;
65
- }
66
- );
50
+ if (mounted && initial.current) {
51
+ setAnimation({
52
+ toValue: enter,
53
+ config: enterConfig,
54
+ });
67
55
  }
68
- }, [mounted, initial]);
56
+ }, [mounted, initial.current]);
69
57
 
70
58
  return function (
71
59
  callback: (
72
- { value }: { value: TransitionValue },
60
+ { value: animation }: { value: TransitionValue },
73
61
  mounted: boolean
74
62
  ) => React.ReactNode
75
63
  ) {
@@ -1,8 +1,8 @@
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";
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
6
 
7
7
  export class DragGesture extends Gesture {
8
8
  movementStart: Vector2 = withDefault(0, 0);
@@ -20,12 +20,12 @@ export class DragGesture extends Gesture {
20
20
  this._subscribe = attachEvents(
21
21
  [window],
22
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)],
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
29
  ]
30
30
  );
31
31
  }
@@ -36,7 +36,7 @@ export class DragGesture extends Gesture {
36
36
  // will not be triggered
37
37
  _cancelEvents() {
38
38
  if (this._subscribe) {
39
- this._subscribe(["mousedown", "mousemove", "touchstart", "touchmove"]);
39
+ this._subscribe(['mousedown', 'mousemove', 'touchstart', 'touchmove']);
40
40
  }
41
41
  }
42
42
 
@@ -63,7 +63,7 @@ export class DragGesture extends Gesture {
63
63
  }
64
64
 
65
65
  pointerDown(e: any) {
66
- if (e.type === "touchstart") {
66
+ if (e.type === 'touchstart') {
67
67
  this.movementStart = {
68
68
  x: e.touches[0].clientX,
69
69
  y: e.touches[0].clientY,
@@ -118,12 +118,12 @@ export class DragGesture extends Gesture {
118
118
  if (this.isActive) {
119
119
  e.preventDefault();
120
120
  const now = Date.now();
121
- const deltaTime = Math.min(now - this.lastTimeStamp, 64);
121
+ const deltaTime = clamp(now - this.lastTimeStamp, 0.1, 64);
122
122
  this.lastTimeStamp = now;
123
123
 
124
124
  const t = deltaTime / 1000;
125
125
 
126
- if (e.type === "touchmove") {
126
+ if (e.type === 'touchmove') {
127
127
  this.movement = {
128
128
  x:
129
129
  this.initialMovement.x +
@@ -170,6 +170,7 @@ export class DragGesture extends Gesture {
170
170
  if (this.isActive) {
171
171
  this.isActive = false;
172
172
  this._handleCallback();
173
+ this._cancelEvents();
173
174
  this._initEvents();
174
175
  }
175
176
  }
@@ -0,0 +1,9 @@
1
+ /**
2
+ * @param { number } ms - number of milliseconds to delay code execution
3
+ * @returns Promise
4
+ */
5
+ export function delay(ms: number) {
6
+ return new Promise((resolve) => {
7
+ setTimeout(() => resolve(null), ms);
8
+ });
9
+ }
@@ -1 +1,2 @@
1
- export * from "./isDefined";
1
+ export * from './isDefined';
2
+ export * from './delay';