Custom snackbar for React native Application
In This article, we are going to see about creating a custom snackbar for React native application using react-native npm.
Install React Native
npm i react-native
I have configured snack bar for success, failure, notification,notificationwithIcon, and offline.


import React, { useState, useEffect } from 'react';
import { View, Text, TouchableOpacity, Animated, Alert } from 'react-native';
//Import utils
import { EMPTY_STRING } from '_common/constants/genericConstants';
import { SnackbarProps } from './types';
import I18n from '_common/i18n';
//Import assets
import { images } from '_assets';
//Import styles
import { SnackBarStyle } from './style';
const Snackbar: React.FC<SnackbarProps> = ({
position = 'Bottom',
header,
message,
snackBarType,
duration = 1000,
actionLabel,
onActionPress,
onDismissSnackbar,
isPermanent = false,
}) => {
const [isVisible, setIsVisible] = useState<boolean>(false);
const fadeAnim = useState<Animated.Value>(new Animated.Value(0))[0];
useEffect(() => {
showSnackbar();
}, []);
const showSnackbar = () => {
setIsVisible(true);
Animated.timing(fadeAnim, {
toValue: 1,
duration: 300,
useNativeDriver: true,
}).start(() => {
if (!isPermanent) {
console.log("Snackbar")
setTimeout(() => {
hideSnackbar();
}, duration);
}
});
};
const hideSnackbar = () => {
Animated.timing(fadeAnim, {
toValue: 0,
duration: 300,
useNativeDriver: true,
}).start(() => {
setIsVisible(false);
onDismissSnackbar?.();
});
};
const handleActionPress = () => {
hideSnackbar();
onActionPress?.();
};
let icon: React.ReactNode;
switch (snackBarType) {
case 'Success':
icon = <images.TaskSubmitSuccess />;
break;
case 'Failure':
icon = <images.Cancelcon />;
break;
case 'Notification':
icon = <images.TaskSubmitPrimary />;
break;
case 'NotificationWithoutIcon':
icon = null;
break;
case 'Offline':
icon = <images.Offline />;
break;
default:
break;
}
return isVisible ? (
<Animated.View
style={[
SnackBarStyle.SnackbarContainer,
SnackBarStyle[position],
SnackBarStyle[snackBarType],
{ opacity: fadeAnim },
]}>
<View style={SnackBarStyle.SnackbarContentContainer}>
<View style={SnackBarStyle.IconAndMessageContainer}>
{icon}
<View style={SnackBarStyle.MessageContainer}>
{header && <Text style={SnackBarStyle.HeaderStyle}>{header}</Text>}
<Text style={SnackBarStyle.MessageText}>{message}</Text>
</View>
</View>
{actionLabel && (
<TouchableOpacity onPress={handleActionPress}>
<Text style={SnackBarStyle.ActionLabel}>{actionLabel}</Text>
</TouchableOpacity>
)}
</View>
</Animated.View>
) : null;
};
//Specifies the default props
Snackbar.defaultProps = {
actionLabel: EMPTY_STRING,
onActionPress: () => {
Alert.alert(I18n.t('COMMON.YET_TO_BE_IMPLEMENTED'));
},
};
export default Snackbar;
Style.ts
import {Dimensions, StyleSheet} from 'react-native';
import {Colors, Typography} from '_common/theme';
import {
fontPixel,
pixelSizeHorizontal,
pixelSizeVertical,
} from '_common/theme/metrics';
export const SnackBarStyle = StyleSheet.create({
SnackbarContainer: {
position: 'absolute',
left: 0,
right: 0,
paddingHorizontal: pixelSizeHorizontal(12),
paddingVertical: pixelSizeVertical(16),
marginHorizontal: pixelSizeHorizontal(16),
marginVertical: pixelSizeVertical(16),
borderRadius: 3,
width: Dimensions.get('screen').width - pixelSizeHorizontal(32),
// marginBottom: pixelSizeVertical(10),
},
Top: {
top: 0,
},
Bottom: {
bottom: 0,
},
NotificationWithoutIcon: {
backgroundColor: Colors.BLUE_V1,
},
Success: {
backgroundColor: Colors.GREEN_V3,
},
Failure: {
backgroundColor: Colors.RED_V6,
},
Notification: {
backgroundColor: Colors.BLUE_V1,
},
SnackbarContentContainer: {
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'space-between',
},
Offline: {
backgroundColor: Colors.RED_V6,
},
IconAndMessageContainer: {
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'space-between',
flex: 0.9,
},
MessageContainer: {
flex: 0.95,
},
HeaderStyle: {
fontFamily: Typography.FONT_FAMILY_INTER_REGULAR,
color: Colors.WHITE,
fontSize: fontPixel(18),
marginBottom: pixelSizeVertical(9),
},
MessageText: {
fontFamily: Typography.FONT_FAMILY_INTER_REGULAR,
color: Colors.WHITE,
fontSize: fontPixel(16),
},
ActionLabel: {
fontFamily: Typography.FONT_FAMILY_INTER_MEDIUM,
color: Colors.WHITE,
fontSize: fontPixel(16),
},
});
At lost you can call a snackbar using
<Snackbar
snackBarType='Success'
message='Back Online'
onDismissSnackbar={() => {
}}
/>
It will work for both Android and IOS applications.
Hope this information is useful. Thank you for reading until the end. If you have any queries kindly comment and email me at k.prem3009@gmail.com
Comments
Post a Comment