react-ui-animate 1.4.6 → 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,75 +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({
108
- toValue: value.toValue,
109
- config: { immediate: value.immediate },
110
- });
39
+ } else if (typeof value === 'object' || typeof value === 'function') {
40
+ setAnimation(value);
111
41
  }
112
42
 
113
43
  return true;
114
44
  }
115
45
 
116
- 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.');
117
47
  },
118
48
  get: function (_, key) {
119
- if (key === "value") {
49
+ if (key === 'value') {
120
50
  return animation;
121
51
  }
122
52
 
123
- if (key === "currentValue") {
53
+ if (key === 'currentValue') {
124
54
  return animation.get();
125
55
  }
126
56
 
127
57
  throw new Error(
128
- "You cannot access any other property from animation node."
58
+ 'You cannot access any other property from animation node.'
129
59
  );
130
60
  },
131
61
  });
@@ -1,50 +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
- config: {
46
- duration: exitDuration,
47
- },
38
+ config: exitConfig,
48
39
  },
49
40
  function ({ finished }) {
50
41
  if (finished) {
@@ -56,24 +47,17 @@ export function useMountedValue(state: boolean, config: UseMountedValueConfig) {
56
47
  }, [state]);
57
48
 
58
49
  React.useEffect(() => {
59
- if (mounted && initial) {
60
- setAnimation(
61
- {
62
- toValue: enter,
63
- config: {
64
- duration: enterDuration,
65
- },
66
- },
67
- function () {
68
- return;
69
- }
70
- );
50
+ if (mounted && initial.current) {
51
+ setAnimation({
52
+ toValue: enter,
53
+ config: enterConfig,
54
+ });
71
55
  }
72
- }, [mounted, initial]);
56
+ }, [mounted, initial.current]);
73
57
 
74
58
  return function (
75
59
  callback: (
76
- { value }: { value: TransitionValue },
60
+ { value: animation }: { value: TransitionValue },
77
61
  mounted: boolean
78
62
  ) => React.ReactNode
79
63
  ) {
@@ -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';