2020-06-15 14:00:46 +00:00
|
|
|
// Taken from https://github.com/react-navigation/react-navigation/blob/master/packages/stack/src/utils/conditional.tsx
|
|
|
|
import { Animated } from 'react-native';
|
|
|
|
|
|
|
|
const { add, multiply } = Animated;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Use an Animated Node based on a condition. Similar to Reanimated's `cond`.
|
|
|
|
*
|
|
|
|
* @param condition Animated Node representing the condition, must be 0 or 1, 1 means `true`, 0 means `false`
|
|
|
|
* @param main Animated Node to use if the condition is `true`
|
|
|
|
* @param fallback Animated Node to use if the condition is `false`
|
|
|
|
*/
|
2022-01-12 12:54:04 +00:00
|
|
|
export default function conditional(
|
2023-08-18 17:48:33 +00:00
|
|
|
condition: Animated.AnimatedInterpolation,
|
|
|
|
main: Animated.Animated,
|
|
|
|
fallback: Animated.Animated
|
|
|
|
): Animated.AnimatedAddition {
|
2020-06-15 14:00:46 +00:00
|
|
|
// To implement this behavior, we multiply the main node with the condition.
|
|
|
|
// So if condition is 0, result will be 0, and if condition is 1, result will be main node.
|
|
|
|
// Then we multiple reverse of the condition (0 if condition is 1) with the fallback.
|
|
|
|
// So if condition is 0, result will be fallback node, and if condition is 1, result will be 0,
|
|
|
|
// This way, one of them will always be 0, and other one will be the value we need.
|
|
|
|
// In the end we add them both together, 0 + value we need = value we need
|
|
|
|
return add(
|
|
|
|
multiply(condition, main),
|
|
|
|
multiply(
|
|
|
|
condition.interpolate({
|
|
|
|
inputRange: [0, 1],
|
|
|
|
outputRange: [1, 0]
|
|
|
|
}),
|
|
|
|
fallback
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|