import type React from "react"; import { type PropsWithChildren, type ReactNode, useMemo, useRef, useState, } from "react"; import { Animated, Easing, Platform, Pressable, Text, TouchableOpacity, View, } from "react-native"; import { useHaptic } from "@/hooks/useHaptic"; import { Loader } from "./Loader"; export interface ButtonProps extends React.ComponentProps { onPress?: () => void; className?: string; textClassName?: string; disabled?: boolean; children?: string | ReactNode; loading?: boolean; color?: "purple" | "red" | "black" | "transparent"; iconRight?: ReactNode; iconLeft?: ReactNode; justify?: "center" | "between"; } export const Button: React.FC> = ({ onPress, className = "", textClassName = "", disabled = false, loading = false, color = "purple", iconRight, iconLeft, children, justify = "center", ...props }) => { const [focused, setFocused] = useState(false); const scale = useRef(new Animated.Value(1)).current; const animateTo = (v: number) => Animated.timing(scale, { toValue: v, duration: 130, easing: Easing.out(Easing.quad), useNativeDriver: true, }).start(); const getColorClasses = (color: string, focused: boolean) => { switch (color) { case "purple": return focused ? "bg-purple-500 border-2 border-white" : "bg-purple-600 border border-purple-700"; case "red": return "bg-red-600"; case "black": return "bg-neutral-900"; case "transparent": return "bg-transparent"; default: return "bg-purple-600 border border-purple-700"; } }; const colorClasses = useMemo( () => getColorClasses(color, focused), [color, focused], ); const lightHapticFeedback = useHaptic("light"); const handlePress = () => { if (!loading && !disabled && onPress) { onPress(); lightHapticFeedback(); } }; const getTextClasses = () => { const baseClasses = "text-white font-bold text-base"; const disabledClass = disabled ? " text-gray-300" : ""; const rightMargin = iconRight ? " mr-2" : ""; const leftMargin = iconLeft ? " ml-2" : ""; return `${baseClasses}${disabledClass} ${textClassName}${rightMargin}${leftMargin}`; }; const getJustifyClass = () => { return justify === "between" ? "justify-between" : "justify-center"; }; const renderTVButton = () => ( { setFocused(true); animateTo(1.08); }} onBlur={() => { setFocused(false); animateTo(1); }} > {children} ); const renderTouchButton = () => ( {loading ? ( ) : ( {iconLeft || } {children} {iconRight || } )} ); return Platform.isTV ? renderTVButton() : renderTouchButton(); };