Skip to content

Commit

Permalink
Using RN's animated to create animation
Browse files Browse the repository at this point in the history
  • Loading branch information
mCodex committed Sep 26, 2020
1 parent c1fb63b commit 971a343
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 6 deletions.
2 changes: 1 addition & 1 deletion example/yalc.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"version": "v1",
"packages": {
"react-native-rooster": {
"signature": "d88fd51dd4a08cf35f2c7733a5a201d9",
"signature": "77004ccfec48a41c2ab01f3788dd35b5",
"file": true
}
}
Expand Down
15 changes: 13 additions & 2 deletions src/components/Toast/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useCallback, useMemo } from 'react';
import { Platform } from 'react-native';
import React, { useCallback, useLayoutEffect, useMemo } from 'react';
import { Animated, Platform } from 'react-native';

import useToast from 'hooks/useToast';

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

const fadeInAnimation = useMemo(() => new Animated.Value(0), []);

const {
keyboardHeight,
message: { id, message, title, type },
toastConfig: { font, bgColor },
} = props;

useLayoutEffect(() => {
Animated.timing(fadeInAnimation, {
toValue: 1,
duration: 500,
useNativeDriver: false,
}).start();
}, [fadeInAnimation]);

/**
* @TODO find a better way to handle Toast's visibility when keyboard is opened.
* On Android KeyboardAvoidingView works perfectly, but not on iOS.
Expand All @@ -41,6 +51,7 @@ const Toast: React.FC<IToastComponent> = (props) => {
type={type}
bgColor={bgColor}
onPress={handleTapToDismiss}
style={{ opacity: fadeInAnimation }}
>
{title && <Title fontFamilyBold={font?.fontFamilyBold}>{title}</Title>}
<Message fontFamilyRegular={font?.fontFamilyRegular}>{message}</Message>
Expand Down
6 changes: 4 additions & 2 deletions src/components/Toast/styles.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Platform } from 'react-native';
import { Animated, Platform, TouchableOpacity } from 'react-native';
import styled, { css } from 'styled-components/native';

interface IContainer {
Expand All @@ -17,7 +17,9 @@ interface IMessageAndTitle {
fontFamilyRegular?: string | null | undefined;
}

export const Container = styled.TouchableOpacity<IContainer>`
const AnimatedButton = Animated.createAnimatedComponent(TouchableOpacity);

export const Container = styled(AnimatedButton)<IContainer>`
align-self: center;
position: absolute;
Expand Down
2 changes: 1 addition & 1 deletion src/providers/ToastProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const ToastProvider: React.FC = ({ children }) => {
warning: '#ff9100',
info: '#7890f0',
},
timeToDismiss: 1500,
timeToDismiss: 3000,
});

const addToast = useCallback(
Expand Down

0 comments on commit 971a343

Please sign in to comment.