Skip to content

Commit 535257d

Browse files
author
cnilton
committed
added color transition animation
1 parent 675472e commit 535257d

File tree

3 files changed

+46
-13
lines changed

3 files changed

+46
-13
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ yarn add [email protected]
4444

4545
- OBS. 2: if you don't want to use the new react-native-reanimated v2, **do not** upgrade to this version, install version 1.3.4 and [email protected]
4646

47-
# Version 1.3.4 **react-native-reanimated v1**
47+
# Version 1.3.4 or lower **react-native-reanimated v1**
4848

4949
- Last stable version of this lib with Software-Mansion react-native-reanimated v1.
5050

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "react-native-floating-label-input",
33
"description": "A simple and customizable React Native TextInput with it's placeholder always shown.",
4-
"version": "1.3.6",
4+
"version": "1.3.7",
55
"main": "index.tsx",
66
"private": false,
77
"repository": {

src/index.tsx

Lines changed: 44 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import {
1919
TouchableWithoutFeedback,
2020
LayoutChangeEvent,
2121
} from 'react-native';
22-
import Animated, { EasingNode, timing } from 'react-native-reanimated';
22+
import Animated, { EasingNode, timing, interpolateColors } from 'react-native-reanimated';
2323
import { styles } from './styles';
2424

2525
import makeVisibleWhite from './assets/make_visible_white.png';
@@ -215,6 +215,8 @@ const FloatingLabelInput: React.ForwardRefRenderFunction<InputRef, Props> = (
215215
...customLabelStyles,
216216
};
217217

218+
const [fontColorAnimated] = useState(new Animated.Value(0));
219+
218220
const [fontSizeAnimated] = useState(
219221
new Animated.Value(
220222
isFocused
@@ -253,27 +255,23 @@ const FloatingLabelInput: React.ForwardRefRenderFunction<InputRef, Props> = (
253255
);
254256

255257
useEffect(() => {
256-
if (!staticLabel) {
257258
if (isFocused === undefined) {
258259
if (value !== '' || isFocusedState) {
259260
setIsFocused(true);
260261
} else if (value === '' || value === null) {
261262
setIsFocused(false);
262263
}
263264
}
264-
}
265265
}, [value]);
266266

267267
useEffect(() => {
268-
if (!staticLabel) {
269268
if (isFocused !== undefined) {
270269
if (value !== '' || isFocused) {
271270
setIsFocused(true);
272271
} else {
273272
setIsFocused(false);
274273
}
275274
}
276-
}
277275
}, [isFocused, value]);
278276

279277
useEffect(() => {
@@ -289,7 +287,7 @@ const FloatingLabelInput: React.ForwardRefRenderFunction<InputRef, Props> = (
289287
}
290288
} else {
291289
animateBlur();
292-
}
290+
}
293291
}, [isFocusedState]);
294292

295293
useImperativeHandle(ref, () => ({
@@ -300,7 +298,7 @@ const FloatingLabelInput: React.ForwardRefRenderFunction<InputRef, Props> = (
300298
inputRef.current.blur();
301299
},
302300
}));
303-
301+
304302
useEffect(() => {
305303
if (
306304
!staticLabel &&
@@ -339,7 +337,19 @@ const FloatingLabelInput: React.ForwardRefRenderFunction<InputRef, Props> = (
339337
duration: animationDuration ? animationDuration : 300,
340338
easing: EasingNode.linear,
341339
}),
340+
timing(fontColorAnimated, {
341+
toValue: 1,
342+
duration: animationDuration ? animationDuration : 300,
343+
easing: EasingNode.linear,
344+
}),
342345
]).start();
346+
}else if(staticLabel &&
347+
isFocusedState){
348+
Animated.timing(fontColorAnimated, {
349+
toValue: 1,
350+
duration: animationDuration ? animationDuration : 300,
351+
easing: EasingNode.linear,
352+
}).start();
343353
}
344354
}, [halfTop]);
345355

@@ -377,13 +387,23 @@ const FloatingLabelInput: React.ForwardRefRenderFunction<InputRef, Props> = (
377387
duration: animationDuration ? animationDuration : 300,
378388
easing: EasingNode.linear,
379389
}),
390+
timing(fontColorAnimated, {
391+
toValue: 1,
392+
duration: animationDuration ? animationDuration : 300,
393+
easing: EasingNode.linear,
394+
}),
380395
]).start();
396+
}else{
397+
Animated.timing(fontColorAnimated, {
398+
toValue: 1,
399+
duration: animationDuration ? animationDuration : 300,
400+
easing: EasingNode.linear,
401+
}).start();
381402
}
382403
}
383404

384405
function animateBlur() {
385406
if (!staticLabel) {
386-
// Animated.parallel([
387407
ReactAnimated.parallel([
388408
// @ts-ignore
389409
timing(leftAnimated, {
@@ -409,7 +429,18 @@ const FloatingLabelInput: React.ForwardRefRenderFunction<InputRef, Props> = (
409429
duration: animationDuration ? animationDuration : 300,
410430
easing: EasingNode.linear,
411431
}),
432+
timing(fontColorAnimated, {
433+
toValue: 0,
434+
duration: animationDuration ? animationDuration : 300,
435+
easing: EasingNode.linear,
436+
}),
412437
]).start();
438+
}else{
439+
Animated.timing(fontColorAnimated, {
440+
toValue: 0,
441+
duration: animationDuration ? animationDuration : 300,
442+
easing: EasingNode.linear,
443+
}).start();
413444
}
414445
}
415446

@@ -463,6 +494,7 @@ const FloatingLabelInput: React.ForwardRefRenderFunction<InputRef, Props> = (
463494
? customShowPasswordImage || makeVisibleWhite
464495
: customHidePasswordImage || makeInvisibleWhite;
465496

497+
466498
const style: TextStyle = {
467499
...setGlobalStyles?.labelStyles,
468500
...labelStyles,
@@ -474,9 +506,10 @@ const FloatingLabelInput: React.ForwardRefRenderFunction<InputRef, Props> = (
474506
: !isFocusedState
475507
? customLabelStyles.fontSizeBlurred
476508
: customLabelStyles.fontSizeFocused,
477-
color: !isFocusedState
478-
? customLabelStyles.colorBlurred
479-
: customLabelStyles.colorFocused,
509+
color: interpolateColors(fontColorAnimated, {
510+
inputRange: [0, 1],
511+
outputColorRange: [customLabelStyles.colorBlurred,customLabelStyles.colorFocused]
512+
}),
480513
alignSelf: 'center',
481514
position: 'absolute',
482515
flex: 1,

0 commit comments

Comments
 (0)