react-native-toastier
Version:
A Fully Customizable Toast Component for React Native.
108 lines • 2.77 kB
JavaScript
import React, { useEffect } from 'react';
import { Animated, Text, TouchableOpacity, View } from 'react-native';
import theme from '../theme';
import styled from './styles';
import useToastState from '../hooks/useToastState';
export const defaultDuration = 15000;
const Toast = ({
message,
children,
duration: timing,
theme: toastTheme,
type = 'default',
open,
onClose,
onOpen,
left,
right,
contentContainerStyle,
textContainerStyle,
messageStyle,
titleStyle,
offset,
title,
animation = 'zoomIn',
position = 'bottom'
}) => {
const {
state,
action
} = useToastState({
position,
animation,
offset,
onClose
});
const {
showToast,
animation: Animation
} = state;
const {
startAnimations,
finishAnimations
} = action;
const Theme = {
toast: toastTheme
};
const duration = timing || defaultDuration;
useEffect(() => {
let timeout;
if (open) {
onOpen?.();
startAnimations();
timeout = setTimeout(() => {
hideToast();
}, duration);
} else {
hideToast();
}
return () => clearTimeout(timeout);
}, [open, duration, showToast]);
const hideToast = () => {
finishAnimations();
};
const getPositionStyle = () => {
switch (position) {
case 'top':
return {
top: 0
};
case 'bottom':
return {
bottom: 0
};
default:
return {
bottom: 0
};
}
};
if (!open) {
return null;
}
return /*#__PURE__*/React.createElement(Animated.View, {
style: [styled.styles.container, getPositionStyle(), {
opacity: showToast
}, Animation]
}, /*#__PURE__*/React.createElement(View, {
style: [styled.styles.toastContainer, theme?.toast?.[type], Theme?.toast?.[type], contentContainerStyle].flat()
}, left ?? null, children ?? /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(View, {
style: [styled.styles.textContainer, textContainerStyle].flat()
}, title && /*#__PURE__*/React.createElement(Text, {
style: [styled.styles.title, {
color: Theme?.toast?.[type]?.color
}, titleStyle].flat()
}, title), message && /*#__PURE__*/React.createElement(Text, {
style: [styled.styles.message, {
color: Theme?.toast?.[type]?.color || theme?.toast?.[type]?.color
}, messageStyle].flat()
}, message))), right ?? /*#__PURE__*/React.createElement(TouchableOpacity, {
onPress: hideToast
}, /*#__PURE__*/React.createElement(Text, {
style: [styled.styles.iconStyle, {
color: Theme?.toast?.[type]?.color || theme?.toast?.[type]?.color
}, titleStyle].flat()
}, "x"))));
};
export default Toast;
//# sourceMappingURL=index.js.map