react-native-toastier
Version:
A Fully Customizable Toast Component for React Native.
108 lines (107 loc) • 2.97 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _react = require("react");
var _reactNative = require("react-native");
const width = _reactNative.Dimensions.get('window').width;
const useToastState = ({
position,
offset,
animation = 'zoomIn',
onClose
}) => {
const showToast = (0, _react.useRef)(new _reactNative.Animated.Value(0)).current;
const zoomAnimation = (0, _react.useRef)(new _reactNative.Animated.Value(0)).current;
const animatedValue = (0, _react.useMemo)(() => {
if (animation === 'slide' && position === 'bottom') {
return offset || 100;
}
if (animation === 'slideLeft') {
return offset || -width;
}
if (animation === 'slideRight') {
return offset || width;
}
return offset || -100;
}, [animation, position, offset]);
const slideAnimation = new _reactNative.Animated.Value(animatedValue);
const _animations = [_reactNative.Animated.timing(showToast, {
toValue: 1,
duration: 300,
useNativeDriver: true
})];
const startAnimations = () => {
if (animation === 'zoomIn') {
_animations.push(_reactNative.Animated.spring(zoomAnimation, {
toValue: 1,
friction: 6,
tension: 20,
useNativeDriver: true
}));
}
if (['slide', 'slideLeft', 'slideRight'].includes(animation)) {
_animations.push(_reactNative.Animated.spring(slideAnimation, {
toValue: 0,
friction: 6,
tension: 20,
useNativeDriver: true
}));
}
_reactNative.Animated.parallel(_animations).start();
};
const finishAnimations = () => {
if (animation === 'zoomIn') {
_animations.push(_reactNative.Animated.timing(zoomAnimation, {
toValue: 0,
duration: 300,
useNativeDriver: true
}));
}
if (['slide', 'slideLeft', 'slideRight'].includes(animation)) {
_animations.push(_reactNative.Animated.timing(slideAnimation, {
toValue: animatedValue,
duration: 300,
useNativeDriver: true
}));
}
_reactNative.Animated.parallel(_animations).start(() => onClose?.());
};
const _animation = (0, _react.useMemo)(() => {
if (animation === 'slide') {
return {
transform: [{
translateY: slideAnimation
}]
};
}
if (['slideLeft', 'slideRight']?.includes(animation)) {
return {
transform: [{
translateX: slideAnimation
}]
};
}
return {
transform: [{
scale: zoomAnimation.interpolate({
inputRange: [0, 1],
outputRange: [0, 1]
})
}]
};
}, [slideAnimation, zoomAnimation, animation, offset]);
return {
state: {
showToast,
animation: _animation
},
action: {
startAnimations,
finishAnimations
}
};
};
var _default = exports.default = useToastState;
//# sourceMappingURL=useToastState.js.map