Skip to content

Commit 971a343

Browse files
committed
Using RN's animated to create animation
1 parent c1fb63b commit 971a343

File tree

4 files changed

+19
-6
lines changed

4 files changed

+19
-6
lines changed

example/yalc.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"version": "v1",
33
"packages": {
44
"react-native-rooster": {
5-
"signature": "d88fd51dd4a08cf35f2c7733a5a201d9",
5+
"signature": "77004ccfec48a41c2ab01f3788dd35b5",
66
"file": true
77
}
88
}

src/components/Toast/index.tsx

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import React, { useCallback, useMemo } from 'react';
2-
import { Platform } from 'react-native';
1+
import React, { useCallback, useLayoutEffect, useMemo } from 'react';
2+
import { Animated, Platform } from 'react-native';
33

44
import useToast from 'hooks/useToast';
55

@@ -14,12 +14,22 @@ interface IToastComponent {
1414
const Toast: React.FC<IToastComponent> = (props) => {
1515
const { removeToast } = useToast();
1616

17+
const fadeInAnimation = useMemo(() => new Animated.Value(0), []);
18+
1719
const {
1820
keyboardHeight,
1921
message: { id, message, title, type },
2022
toastConfig: { font, bgColor },
2123
} = props;
2224

25+
useLayoutEffect(() => {
26+
Animated.timing(fadeInAnimation, {
27+
toValue: 1,
28+
duration: 500,
29+
useNativeDriver: false,
30+
}).start();
31+
}, [fadeInAnimation]);
32+
2333
/**
2434
* @TODO find a better way to handle Toast's visibility when keyboard is opened.
2535
* On Android KeyboardAvoidingView works perfectly, but not on iOS.
@@ -41,6 +51,7 @@ const Toast: React.FC<IToastComponent> = (props) => {
4151
type={type}
4252
bgColor={bgColor}
4353
onPress={handleTapToDismiss}
54+
style={{ opacity: fadeInAnimation }}
4455
>
4556
{title && <Title fontFamilyBold={font?.fontFamilyBold}>{title}</Title>}
4657
<Message fontFamilyRegular={font?.fontFamilyRegular}>{message}</Message>

src/components/Toast/styles.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Platform } from 'react-native';
1+
import { Animated, Platform, TouchableOpacity } from 'react-native';
22
import styled, { css } from 'styled-components/native';
33

44
interface IContainer {
@@ -17,7 +17,9 @@ interface IMessageAndTitle {
1717
fontFamilyRegular?: string | null | undefined;
1818
}
1919

20-
export const Container = styled.TouchableOpacity<IContainer>`
20+
const AnimatedButton = Animated.createAnimatedComponent(TouchableOpacity);
21+
22+
export const Container = styled(AnimatedButton)<IContainer>`
2123
align-self: center;
2224
position: absolute;
2325

src/providers/ToastProvider.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const ToastProvider: React.FC = ({ children }) => {
1313
warning: '#ff9100',
1414
info: '#7890f0',
1515
},
16-
timeToDismiss: 1500,
16+
timeToDismiss: 3000,
1717
});
1818

1919
const addToast = useCallback(

0 commit comments

Comments
 (0)