react-ui-animate 2.0.0-rc.7 → 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
|
@@ -0,0 +1,395 @@
|
|
|
1
|
+
import {
|
|
2
|
+
rgbaToHex,
|
|
3
|
+
hexToRgba,
|
|
4
|
+
COLOR_NUMBER_REGEX,
|
|
5
|
+
HEX_NAME_COLOR,
|
|
6
|
+
colorNames,
|
|
7
|
+
} from './Colors';
|
|
8
|
+
import { isFluidValue } from '../helpers';
|
|
9
|
+
import { FluidValue } from '../controllers/FluidValue';
|
|
10
|
+
|
|
11
|
+
type ExtrapolateType = 'identity' | 'extend' | 'clamp';
|
|
12
|
+
|
|
13
|
+
const _internalInterpolate = (
|
|
14
|
+
val: number,
|
|
15
|
+
arr: any,
|
|
16
|
+
extrapolateLeft: ExtrapolateType,
|
|
17
|
+
extrapolateRight: ExtrapolateType
|
|
18
|
+
) => {
|
|
19
|
+
const [inputMin, inputMax, outputMin, outputMax] = arr;
|
|
20
|
+
let result: number = val;
|
|
21
|
+
|
|
22
|
+
// EXTRAPOLATE
|
|
23
|
+
if (result < inputMin) {
|
|
24
|
+
if (extrapolateLeft === 'identity') {
|
|
25
|
+
return result;
|
|
26
|
+
} else if (extrapolateLeft === 'clamp') {
|
|
27
|
+
result = inputMin;
|
|
28
|
+
} else if (extrapolateLeft === 'extend') {
|
|
29
|
+
// noop
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
if (result > inputMax) {
|
|
34
|
+
if (extrapolateRight === 'identity') {
|
|
35
|
+
return result;
|
|
36
|
+
} else if (extrapolateRight === 'clamp') {
|
|
37
|
+
result = inputMax;
|
|
38
|
+
} else if (extrapolateRight === 'extend') {
|
|
39
|
+
// noop
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (outputMin === outputMax) {
|
|
44
|
+
return outputMin;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (inputMin === inputMax) {
|
|
48
|
+
if (val <= inputMin) {
|
|
49
|
+
return outputMin;
|
|
50
|
+
}
|
|
51
|
+
return outputMax;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// Input Range
|
|
55
|
+
if (inputMin === -Infinity) {
|
|
56
|
+
result = -result;
|
|
57
|
+
} else if (inputMax === Infinity) {
|
|
58
|
+
result = result - inputMin;
|
|
59
|
+
} else {
|
|
60
|
+
result = (result - inputMin) / (inputMax - inputMin);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
// Output Range
|
|
64
|
+
if (outputMin === -Infinity) {
|
|
65
|
+
result = -result;
|
|
66
|
+
} else if (outputMax === Infinity) {
|
|
67
|
+
result = result + outputMin;
|
|
68
|
+
} else {
|
|
69
|
+
result = result * (outputMax - outputMin) + outputMin;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return result;
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
const _getNarrowedInputArray = function (
|
|
76
|
+
x: number,
|
|
77
|
+
input: number[],
|
|
78
|
+
output: Array<number | string>
|
|
79
|
+
): Array<number | string> {
|
|
80
|
+
const length = input.length;
|
|
81
|
+
let narrowedInput: Array<number | string> = [];
|
|
82
|
+
|
|
83
|
+
// Boundaries
|
|
84
|
+
if (x < input[0]) {
|
|
85
|
+
narrowedInput = [input[0], input[1], output[0], output[1]];
|
|
86
|
+
} else if (x > input[length - 1]) {
|
|
87
|
+
narrowedInput = [
|
|
88
|
+
input[length - 2],
|
|
89
|
+
input[length - 1],
|
|
90
|
+
output[length - 2],
|
|
91
|
+
output[length - 1],
|
|
92
|
+
];
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// Narrow the input and output ranges
|
|
96
|
+
for (let i = 1; i < length; ++i) {
|
|
97
|
+
if (x <= input[i]) {
|
|
98
|
+
narrowedInput = [input[i - 1], input[i], output[i - 1], output[i]];
|
|
99
|
+
break;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return narrowedInput;
|
|
104
|
+
};
|
|
105
|
+
|
|
106
|
+
const _getColorInterpolate = (value: number, narrowedInput: Array<string>) => {
|
|
107
|
+
const [inputMin, inputMax, outputMin, outputMax] = narrowedInput;
|
|
108
|
+
|
|
109
|
+
const outputMinProcessed = hexToRgba(outputMin);
|
|
110
|
+
const outputMaxProcessed = hexToRgba(outputMax);
|
|
111
|
+
|
|
112
|
+
const red = _internalInterpolate(
|
|
113
|
+
value,
|
|
114
|
+
[inputMin, inputMax, outputMinProcessed.r, outputMaxProcessed.r],
|
|
115
|
+
'clamp',
|
|
116
|
+
'clamp'
|
|
117
|
+
);
|
|
118
|
+
|
|
119
|
+
const green = _internalInterpolate(
|
|
120
|
+
value,
|
|
121
|
+
[inputMin, inputMax, outputMinProcessed.g, outputMaxProcessed.g],
|
|
122
|
+
'clamp',
|
|
123
|
+
'clamp'
|
|
124
|
+
);
|
|
125
|
+
|
|
126
|
+
const blue = _internalInterpolate(
|
|
127
|
+
value,
|
|
128
|
+
[inputMin, inputMax, outputMinProcessed.b, outputMaxProcessed.b],
|
|
129
|
+
'clamp',
|
|
130
|
+
'clamp'
|
|
131
|
+
);
|
|
132
|
+
|
|
133
|
+
const alpha = _internalInterpolate(
|
|
134
|
+
value,
|
|
135
|
+
[inputMin, inputMax, outputMinProcessed.a, outputMaxProcessed.a],
|
|
136
|
+
'clamp',
|
|
137
|
+
'clamp'
|
|
138
|
+
);
|
|
139
|
+
|
|
140
|
+
return rgbaToHex({ r: red, g: green, b: blue, a: alpha });
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
const _getArrayInterpolate = (
|
|
144
|
+
value: number,
|
|
145
|
+
narrowedInput: Array<any>,
|
|
146
|
+
_extrapolateLeft: ExtrapolateType,
|
|
147
|
+
_extrapolateRight: ExtrapolateType
|
|
148
|
+
) => {
|
|
149
|
+
const [inputMin, inputMax, outputMin, outputMax] = narrowedInput;
|
|
150
|
+
|
|
151
|
+
if (outputMin.length === outputMax.length) {
|
|
152
|
+
return outputMin.map((val: any, index: number) => {
|
|
153
|
+
if (typeof val === 'string') {
|
|
154
|
+
// IF IT IS STRING THEN IT MUST BE HEX COLOR
|
|
155
|
+
return _getColorInterpolate(value, [
|
|
156
|
+
inputMin,
|
|
157
|
+
inputMax,
|
|
158
|
+
val,
|
|
159
|
+
outputMax[index],
|
|
160
|
+
]);
|
|
161
|
+
} else {
|
|
162
|
+
return _internalInterpolate(
|
|
163
|
+
value,
|
|
164
|
+
[inputMin, inputMax, val, outputMax[index]],
|
|
165
|
+
_extrapolateLeft,
|
|
166
|
+
_extrapolateRight
|
|
167
|
+
);
|
|
168
|
+
}
|
|
169
|
+
});
|
|
170
|
+
} else {
|
|
171
|
+
throw new Error("Array length doesn't match");
|
|
172
|
+
}
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
export const _getTemplateString = (str: string) => {
|
|
176
|
+
return str.replace(COLOR_NUMBER_REGEX, '$');
|
|
177
|
+
};
|
|
178
|
+
|
|
179
|
+
const _getParsedStringArray = (str: any) => {
|
|
180
|
+
return str.match(COLOR_NUMBER_REGEX).map((v: string) => {
|
|
181
|
+
if (v.indexOf('#') !== -1) {
|
|
182
|
+
return v;
|
|
183
|
+
} else {
|
|
184
|
+
return Number(v);
|
|
185
|
+
}
|
|
186
|
+
});
|
|
187
|
+
};
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Function returns if the template of two strings are matched
|
|
191
|
+
* i.e. they can be interpolated
|
|
192
|
+
* @param str1 - first string
|
|
193
|
+
* @param str2 - second string
|
|
194
|
+
* @returns boolean indicating two strings matched or not
|
|
195
|
+
*/
|
|
196
|
+
export const stringMatched = (str1: string, str2: string) =>
|
|
197
|
+
_getTemplateString(str1).trim().replace(/\s/g, '') ===
|
|
198
|
+
_getTemplateString(str2).trim().replace(/\s/g, '');
|
|
199
|
+
|
|
200
|
+
/**
|
|
201
|
+
* Function which proccess the
|
|
202
|
+
* hexadecimal colors to its proper formats
|
|
203
|
+
* @param str - string
|
|
204
|
+
* @returns hex color string
|
|
205
|
+
*/
|
|
206
|
+
export const getProcessedColor = (str: string) => {
|
|
207
|
+
return str.replace(HEX_NAME_COLOR, function (match: any) {
|
|
208
|
+
if (match.indexOf('#') !== -1) {
|
|
209
|
+
return rgbaToHex(hexToRgba(match));
|
|
210
|
+
} else if (Object.prototype.hasOwnProperty.call(colorNames, match)) {
|
|
211
|
+
return colorNames[match];
|
|
212
|
+
} else {
|
|
213
|
+
throw new Error('String cannot be parsed!');
|
|
214
|
+
}
|
|
215
|
+
});
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
export interface ExtrapolateConfig {
|
|
219
|
+
extrapolate?: ExtrapolateType;
|
|
220
|
+
extrapolateRight?: ExtrapolateType;
|
|
221
|
+
extrapolateLeft?: ExtrapolateType;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* interpolateNumbers to interpolate the numeric value
|
|
226
|
+
* @param value - number
|
|
227
|
+
* @param inputRange
|
|
228
|
+
* @param outputRange
|
|
229
|
+
* @param extrapolateConfig
|
|
230
|
+
* @returns - number | string
|
|
231
|
+
*/
|
|
232
|
+
export function interpolateNumbers(
|
|
233
|
+
value: number,
|
|
234
|
+
inputRange: Array<number>,
|
|
235
|
+
outputRange: Array<number | string>,
|
|
236
|
+
extrapolateConfig?: ExtrapolateConfig
|
|
237
|
+
) {
|
|
238
|
+
const extrapolate = extrapolateConfig?.extrapolate;
|
|
239
|
+
const extrapolateLeft = extrapolateConfig?.extrapolateLeft;
|
|
240
|
+
const extrapolateRight = extrapolateConfig?.extrapolateRight;
|
|
241
|
+
|
|
242
|
+
const narrowedInput = _getNarrowedInputArray(value, inputRange, outputRange);
|
|
243
|
+
|
|
244
|
+
let _extrapolateLeft: ExtrapolateType = 'extend';
|
|
245
|
+
if (extrapolateLeft !== undefined) {
|
|
246
|
+
_extrapolateLeft = extrapolateLeft;
|
|
247
|
+
} else if (extrapolate !== undefined) {
|
|
248
|
+
_extrapolateLeft = extrapolate;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
let _extrapolateRight: ExtrapolateType = 'extend';
|
|
252
|
+
if (extrapolateRight !== undefined) {
|
|
253
|
+
_extrapolateRight = extrapolateRight;
|
|
254
|
+
} else if (extrapolate !== undefined) {
|
|
255
|
+
_extrapolateRight = extrapolate;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
if (outputRange.length) {
|
|
259
|
+
if (typeof outputRange[0] === 'number') {
|
|
260
|
+
return _internalInterpolate(
|
|
261
|
+
value,
|
|
262
|
+
narrowedInput,
|
|
263
|
+
_extrapolateLeft,
|
|
264
|
+
_extrapolateRight
|
|
265
|
+
);
|
|
266
|
+
} else if (Array.isArray(outputRange[0])) {
|
|
267
|
+
return _getArrayInterpolate(
|
|
268
|
+
value,
|
|
269
|
+
narrowedInput,
|
|
270
|
+
_extrapolateLeft,
|
|
271
|
+
_extrapolateRight
|
|
272
|
+
);
|
|
273
|
+
} else {
|
|
274
|
+
const [inputMin, inputMax, outputMin, outputMax] = narrowedInput;
|
|
275
|
+
|
|
276
|
+
const processedOutputMin = getProcessedColor(outputMin as string);
|
|
277
|
+
const processedOutputMax = getProcessedColor(outputMax as string);
|
|
278
|
+
|
|
279
|
+
let templateString = _getTemplateString(processedOutputMin);
|
|
280
|
+
|
|
281
|
+
if (stringMatched(processedOutputMin, processedOutputMax)) {
|
|
282
|
+
const outputMinParsed = _getParsedStringArray(processedOutputMin);
|
|
283
|
+
const outputMaxParsed = _getParsedStringArray(processedOutputMax);
|
|
284
|
+
|
|
285
|
+
const result = _getArrayInterpolate(
|
|
286
|
+
value,
|
|
287
|
+
[inputMin, inputMax, outputMinParsed, outputMaxParsed],
|
|
288
|
+
_extrapolateLeft,
|
|
289
|
+
_extrapolateRight
|
|
290
|
+
);
|
|
291
|
+
|
|
292
|
+
for (const v of result) templateString = templateString.replace('$', v);
|
|
293
|
+
return templateString;
|
|
294
|
+
} else {
|
|
295
|
+
throw new Error("Output range doesn't match string format!");
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
} else {
|
|
299
|
+
throw new Error('Output range cannot be Empty');
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/**
|
|
304
|
+
* interpolateTransitionValue to interpolating TransitionValue type value
|
|
305
|
+
* @param value
|
|
306
|
+
* @param inputRange
|
|
307
|
+
* @param outputRange
|
|
308
|
+
* @param extrapolateConfig
|
|
309
|
+
* @returns TransitionValue
|
|
310
|
+
*/
|
|
311
|
+
export const interpolateTransitionValue = (
|
|
312
|
+
value: FluidValue,
|
|
313
|
+
inputRange: Array<number>,
|
|
314
|
+
outputRange: Array<number | string>,
|
|
315
|
+
extrapolateConfig?: ExtrapolateConfig
|
|
316
|
+
) => {
|
|
317
|
+
return {
|
|
318
|
+
...value,
|
|
319
|
+
isInterpolation: true,
|
|
320
|
+
interpolationConfig: {
|
|
321
|
+
inputRange,
|
|
322
|
+
outputRange,
|
|
323
|
+
extrapolateConfig,
|
|
324
|
+
},
|
|
325
|
+
};
|
|
326
|
+
};
|
|
327
|
+
|
|
328
|
+
/**
|
|
329
|
+
* interpolate function to interpolate both transition
|
|
330
|
+
* as well as numerical value
|
|
331
|
+
* @param value
|
|
332
|
+
* @param inputRange
|
|
333
|
+
* @param outputRange
|
|
334
|
+
* @param extrapolateConfig
|
|
335
|
+
*/
|
|
336
|
+
export const interpolate = (
|
|
337
|
+
value: number | FluidValue,
|
|
338
|
+
inputRange: Array<number>,
|
|
339
|
+
outputRange: Array<number | string>,
|
|
340
|
+
extrapolateConfig?: ExtrapolateConfig
|
|
341
|
+
) => {
|
|
342
|
+
if (isFluidValue(value)) {
|
|
343
|
+
return interpolateTransitionValue(
|
|
344
|
+
value as FluidValue,
|
|
345
|
+
inputRange,
|
|
346
|
+
outputRange,
|
|
347
|
+
extrapolateConfig
|
|
348
|
+
);
|
|
349
|
+
} else if (typeof value === 'number') {
|
|
350
|
+
return interpolateNumbers(
|
|
351
|
+
value,
|
|
352
|
+
inputRange,
|
|
353
|
+
outputRange,
|
|
354
|
+
extrapolateConfig
|
|
355
|
+
);
|
|
356
|
+
} else {
|
|
357
|
+
throw new Error(`'${typeof value}' cannot be interpolated!`);
|
|
358
|
+
}
|
|
359
|
+
};
|
|
360
|
+
|
|
361
|
+
/**
|
|
362
|
+
* Determines if two values can be interpolated.
|
|
363
|
+
* This function checks if two values, either numbers or strings,
|
|
364
|
+
* can be interpolated by ensuring they are of the same type and, in the case of strings,
|
|
365
|
+
* that they are compatible for interpolation based on processed color values.
|
|
366
|
+
*
|
|
367
|
+
* @param previousValue - The previous value to compare. Can be a number or a string.
|
|
368
|
+
* @param newValue - The new value to compare. Can be a number or a string.
|
|
369
|
+
* @returns True if interpolation is possible, false otherwise.
|
|
370
|
+
*/
|
|
371
|
+
export function canInterpolate(
|
|
372
|
+
previousValue: number | string,
|
|
373
|
+
newValue: number | string
|
|
374
|
+
): boolean {
|
|
375
|
+
if (typeof previousValue !== typeof newValue) {
|
|
376
|
+
return false;
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
if (typeof newValue === 'number') {
|
|
380
|
+
return true;
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
if (typeof previousValue === 'string') {
|
|
384
|
+
const processedPreviousValue = getProcessedColor(previousValue);
|
|
385
|
+
const processedNewValue = getProcessedColor(newValue);
|
|
386
|
+
|
|
387
|
+
return (
|
|
388
|
+
processedPreviousValue !== processedNewValue &&
|
|
389
|
+
_getTemplateString(processedPreviousValue) ===
|
|
390
|
+
_getTemplateString(processedNewValue)
|
|
391
|
+
);
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
return false;
|
|
395
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { rgbaToHex, hexToRgba } from '../Colors';
|
|
2
|
+
|
|
3
|
+
describe('Colors', () => {
|
|
4
|
+
it('rgbaToHex() function to convert rgba object to hex string', () => {
|
|
5
|
+
expect(rgbaToHex({ r: 255, g: 255, b: 255, a: 1 })).toStrictEqual(
|
|
6
|
+
'#ffffffff'
|
|
7
|
+
);
|
|
8
|
+
expect(rgbaToHex({ r: 252, g: 186, b: 3, a: 1 })).toStrictEqual(
|
|
9
|
+
'#fcba03ff'
|
|
10
|
+
);
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
it('hexToRgba() function to convert hex string to rgba object', () => {
|
|
14
|
+
expect(hexToRgba('#ffffff')).toStrictEqual({
|
|
15
|
+
r: 255,
|
|
16
|
+
g: 255,
|
|
17
|
+
b: 255,
|
|
18
|
+
a: 1,
|
|
19
|
+
});
|
|
20
|
+
|
|
21
|
+
expect(hexToRgba('#fff')).toStrictEqual({
|
|
22
|
+
r: 255,
|
|
23
|
+
g: 255,
|
|
24
|
+
b: 255,
|
|
25
|
+
a: 1,
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
expect(hexToRgba('#2a6e55')).toStrictEqual({
|
|
29
|
+
r: 42,
|
|
30
|
+
g: 110,
|
|
31
|
+
b: 85,
|
|
32
|
+
a: 1,
|
|
33
|
+
});
|
|
34
|
+
});
|
|
35
|
+
});
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import { makeFluid } from './makeFluid';
|
|
2
|
+
|
|
3
|
+
import type { FluidProps } from '../types/fluid';
|
|
4
|
+
|
|
5
|
+
type HTMLTags = keyof JSX.IntrinsicElements;
|
|
6
|
+
|
|
7
|
+
const tags: HTMLTags[] = [
|
|
8
|
+
'a',
|
|
9
|
+
'abbr',
|
|
10
|
+
'address',
|
|
11
|
+
'area',
|
|
12
|
+
'article',
|
|
13
|
+
'aside',
|
|
14
|
+
'audio',
|
|
15
|
+
'b',
|
|
16
|
+
'base',
|
|
17
|
+
'bdi',
|
|
18
|
+
'bdo',
|
|
19
|
+
'big',
|
|
20
|
+
'blockquote',
|
|
21
|
+
'body',
|
|
22
|
+
'br',
|
|
23
|
+
'button',
|
|
24
|
+
'canvas',
|
|
25
|
+
'caption',
|
|
26
|
+
'center',
|
|
27
|
+
'cite',
|
|
28
|
+
'code',
|
|
29
|
+
'col',
|
|
30
|
+
'colgroup',
|
|
31
|
+
'data',
|
|
32
|
+
'datalist',
|
|
33
|
+
'dd',
|
|
34
|
+
'del',
|
|
35
|
+
'details',
|
|
36
|
+
'dfn',
|
|
37
|
+
'dialog',
|
|
38
|
+
'div',
|
|
39
|
+
'dl',
|
|
40
|
+
'dt',
|
|
41
|
+
'em',
|
|
42
|
+
'embed',
|
|
43
|
+
'fieldset',
|
|
44
|
+
'figcaption',
|
|
45
|
+
'figure',
|
|
46
|
+
'footer',
|
|
47
|
+
'form',
|
|
48
|
+
'h1',
|
|
49
|
+
'h2',
|
|
50
|
+
'h3',
|
|
51
|
+
'h4',
|
|
52
|
+
'h5',
|
|
53
|
+
'h6',
|
|
54
|
+
'head',
|
|
55
|
+
'header',
|
|
56
|
+
'hgroup',
|
|
57
|
+
'hr',
|
|
58
|
+
'html',
|
|
59
|
+
'i',
|
|
60
|
+
'iframe',
|
|
61
|
+
'img',
|
|
62
|
+
'input',
|
|
63
|
+
'ins',
|
|
64
|
+
'kbd',
|
|
65
|
+
'keygen',
|
|
66
|
+
'label',
|
|
67
|
+
'legend',
|
|
68
|
+
'li',
|
|
69
|
+
'link',
|
|
70
|
+
'main',
|
|
71
|
+
'map',
|
|
72
|
+
'mark',
|
|
73
|
+
'menu',
|
|
74
|
+
'menuitem',
|
|
75
|
+
'meta',
|
|
76
|
+
'meter',
|
|
77
|
+
'nav',
|
|
78
|
+
'noindex',
|
|
79
|
+
'noscript',
|
|
80
|
+
'object',
|
|
81
|
+
'ol',
|
|
82
|
+
'optgroup',
|
|
83
|
+
'option',
|
|
84
|
+
'output',
|
|
85
|
+
'p',
|
|
86
|
+
'param',
|
|
87
|
+
'picture',
|
|
88
|
+
'pre',
|
|
89
|
+
'progress',
|
|
90
|
+
'q',
|
|
91
|
+
'rp',
|
|
92
|
+
'rt',
|
|
93
|
+
'ruby',
|
|
94
|
+
's',
|
|
95
|
+
'samp',
|
|
96
|
+
'search',
|
|
97
|
+
'section',
|
|
98
|
+
'select',
|
|
99
|
+
'small',
|
|
100
|
+
'source',
|
|
101
|
+
'span',
|
|
102
|
+
'strong',
|
|
103
|
+
'style',
|
|
104
|
+
'sub',
|
|
105
|
+
'summary',
|
|
106
|
+
'sup',
|
|
107
|
+
'table',
|
|
108
|
+
'template',
|
|
109
|
+
'tbody',
|
|
110
|
+
'td',
|
|
111
|
+
'textarea',
|
|
112
|
+
'tfoot',
|
|
113
|
+
'th',
|
|
114
|
+
'thead',
|
|
115
|
+
'time',
|
|
116
|
+
'title',
|
|
117
|
+
'tr',
|
|
118
|
+
'track',
|
|
119
|
+
'u',
|
|
120
|
+
'ul',
|
|
121
|
+
'var',
|
|
122
|
+
'video',
|
|
123
|
+
'wbr',
|
|
124
|
+
|
|
125
|
+
// SVG Tags
|
|
126
|
+
'svg',
|
|
127
|
+
'animate',
|
|
128
|
+
'animateMotion',
|
|
129
|
+
'animateTransform',
|
|
130
|
+
'circle',
|
|
131
|
+
'clipPath',
|
|
132
|
+
'defs',
|
|
133
|
+
'desc',
|
|
134
|
+
'ellipse',
|
|
135
|
+
'feBlend',
|
|
136
|
+
'feColorMatrix',
|
|
137
|
+
'feComponentTransfer',
|
|
138
|
+
'feComposite',
|
|
139
|
+
'feConvolveMatrix',
|
|
140
|
+
'feDiffuseLighting',
|
|
141
|
+
'feDisplacementMap',
|
|
142
|
+
'feDistantLight',
|
|
143
|
+
'feDropShadow',
|
|
144
|
+
'feFlood',
|
|
145
|
+
'feFuncA',
|
|
146
|
+
'feFuncB',
|
|
147
|
+
'feFuncG',
|
|
148
|
+
'feFuncR',
|
|
149
|
+
'feGaussianBlur',
|
|
150
|
+
'feImage',
|
|
151
|
+
'feMerge',
|
|
152
|
+
'feMergeNode',
|
|
153
|
+
'feMorphology',
|
|
154
|
+
'feOffset',
|
|
155
|
+
'fePointLight',
|
|
156
|
+
'feSpecularLighting',
|
|
157
|
+
'feSpotLight',
|
|
158
|
+
'feTile',
|
|
159
|
+
'feTurbulence',
|
|
160
|
+
'filter',
|
|
161
|
+
'foreignObject',
|
|
162
|
+
'g',
|
|
163
|
+
'image',
|
|
164
|
+
'line',
|
|
165
|
+
'linearGradient',
|
|
166
|
+
'marker',
|
|
167
|
+
'mask',
|
|
168
|
+
'metadata',
|
|
169
|
+
'mpath',
|
|
170
|
+
'path',
|
|
171
|
+
'pattern',
|
|
172
|
+
'polygon',
|
|
173
|
+
'polyline',
|
|
174
|
+
'radialGradient',
|
|
175
|
+
'rect',
|
|
176
|
+
'set',
|
|
177
|
+
'stop',
|
|
178
|
+
'switch',
|
|
179
|
+
'symbol',
|
|
180
|
+
'text',
|
|
181
|
+
'textPath',
|
|
182
|
+
'tspan',
|
|
183
|
+
'use',
|
|
184
|
+
'view',
|
|
185
|
+
];
|
|
186
|
+
|
|
187
|
+
type WithFluid = {
|
|
188
|
+
[k in keyof JSX.IntrinsicElements]: React.ComponentType<
|
|
189
|
+
FluidProps<HTMLElement>
|
|
190
|
+
>;
|
|
191
|
+
};
|
|
192
|
+
|
|
193
|
+
export const fluid: WithFluid = {} as WithFluid;
|
|
194
|
+
|
|
195
|
+
tags.forEach((tag) => {
|
|
196
|
+
(fluid as any)[tag] = makeFluid(tag);
|
|
197
|
+
});
|