Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 36 additions & 13 deletions src/components/Toast.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,15 @@ import {
ViewStyle,
} from 'react-native';
import Animated, {
Easing,
ReduceMotion,
runOnJS,
useAnimatedStyle,
useSharedValue,
withSpring,
type WithSpringConfig,
withTiming,
type WithTimingConfig,
} from 'react-native-reanimated';
import { useSafeAreaInsets } from 'react-native-safe-area-context';
import {
Expand Down Expand Up @@ -114,13 +118,36 @@ export const Toast: FC<Props> = ({
const setPosition = useCallback(() => {
//control the position of the toast when rendering
//based on offset, visibility, keyboard, and toast height
let timingConfig: WithTimingConfig = {
duration: 300,
};
let springConfig: WithSpringConfig = {
stiffness: 80,
};
if (toast.animationConfig) {
const {
duration = 300,
easing = Easing.inOut(Easing.quad),
reduceMotion = ReduceMotion.System,
...spring
} = toast.animationConfig;
timingConfig = {
duration,
easing,
reduceMotion,
};
springConfig = spring;
}

if (toast.position === ToastPosition.TOP) {
offsetY.value = withTiming(toast.visible ? offset : startingY, {
duration: toast?.animationConfig?.animationDuration ?? 300,
});
position.value = withTiming(toast.visible ? offset : startingY, {
duration: toast?.animationConfig?.animationDuration ?? 300,
});
offsetY.value = withTiming(
toast.visible ? offset : startingY,
timingConfig
);
position.value = withTiming(
toast.visible ? offset : startingY,
timingConfig
);
} else {
let kbHeight = keyboardVisible ? keyboardHeight : 0;
const val = toast.visible
Expand All @@ -133,13 +160,9 @@ export const Toast: FC<Props> = ({
24
: startingY;

offsetY.value = withSpring(val, {
stiffness: toast?.animationConfig?.animationStiffness ?? 80,
});
offsetY.value = withSpring(val, springConfig);

position.value = withSpring(val, {
stiffness: toast?.animationConfig?.animationStiffness ?? 80,
});
position.value = withSpring(val, springConfig);
}
}, [
offset,
Expand Down Expand Up @@ -206,7 +229,7 @@ export const Toast: FC<Props> = ({
useEffect(() => {
//Control visibility of toast when rendering
opacity.value = withTiming(toast.visible ? 1 : 0, {
duration: toast?.animationConfig?.animationDuration ?? 300,
duration: toast?.animationConfig?.duration ?? 300,
});
}, [toast.visible, opacity, toast.animationConfig]);

Expand Down
9 changes: 6 additions & 3 deletions src/core/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import type { TextStyle, ViewStyle } from 'react-native';
import type {
WithTimingConfig,
WithSpringConfig,
} from 'react-native-reanimated';

export type ToastType = 'success' | 'error' | 'loading' | 'blank';
export enum ToastPosition {
Expand Down Expand Up @@ -53,9 +57,8 @@ export interface Toast {
isSwipeable?: boolean;
animationConfig?: {
flingPositionReturnDuration?: number;
animationStiffness?: number;
animationDuration?: number;
};
} & WithSpringConfig &
WithTimingConfig;
}

export type ToastOptions = Partial<
Expand Down