import { GestureResponderEvent, Pressable } from "react-native"; import Animated, { interpolateColor, useAnimatedProps, useAnimatedStyle, useReducedMotion } from "react-native-reanimated"; import { AnimatedIcon, AnimatedIconWithColorTransition, IOIcons } from "../../components/icons"; import { IOColors, IOIconButtonStyles, hexToRgba } from "../../core"; import { useScaleAnimation } from "../../hooks"; import { WithTestID } from "../../utils/types"; export type IconButtonContained = WithTestID<{ icon: IOIcons; color?: "primary" | "neutral" | "contrast"; disabled?: boolean; accessibilityLabel: string; accessibilityHint?: string; onPress: (event: GestureResponderEvent) => void; }>; type ColorStates = { background: { default: string; pressed: string; disabled: string; }; icon: { default: string; pressed: string; disabled: string; }; }; const mapColorStates: Record< NonNullable, ColorStates > = { // Primary button primary: { background: { default: hexToRgba(IOColors["blueIO-500"], 0), pressed: hexToRgba(IOColors["blueIO-500"], 0.15), disabled: "transparent" }, icon: { default: IOColors["blueIO-500"], pressed: IOColors["blueIO-600"], disabled: hexToRgba(IOColors["blueIO-500"], 0.25) } }, // Neutral button neutral: { background: { default: IOColors.white, pressed: IOColors["grey-50"], disabled: "transparent" }, icon: { default: IOColors["grey-700"], pressed: IOColors.black, disabled: IOColors["grey-450"] } }, // Contrast button contrast: { background: { default: hexToRgba(IOColors.white, 0), pressed: hexToRgba(IOColors.white, 0.2), disabled: "transparent" }, icon: { default: IOColors.white, pressed: IOColors.white, disabled: hexToRgba(IOColors.white, 0.25) } } }; export const IconButtonContained = ({ icon, color = "primary", disabled = false, onPress, accessibilityLabel, accessibilityHint, testID }: IconButtonContained) => { const { progress, onPressIn, onPressOut, scaleAnimatedStyle } = useScaleAnimation("exaggerated"); const reducedMotion = useReducedMotion(); const backgroundColorAnimationStyle = useAnimatedStyle(() => ({ backgroundColor: interpolateColor( progress.value, [0, 1], [ mapColorStates[color].background.default, mapColorStates[color].background.pressed ] ) })); // Animate the color prop const iconColorAnimationStyle = useAnimatedProps(() => ({ color: interpolateColor( progress.value, [0, 1], [mapColorStates[color].icon.default, mapColorStates[color].icon.pressed] ) })); return ( {!disabled ? ( ) : ( )} ); };