aboutsummaryrefslogtreecommitdiff
path: root/src/components/ui/button.tsx
blob: 3486ff767c97fd39721bbfd5dda18dfa47773506 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import React from 'react';
import {
    ActivityIndicator,
    StyleSheet,
    Text,
    TouchableOpacity,
    type TouchableOpacityProps,
    View,
} from 'react-native';
import Animated, {
    useAnimatedStyle,
    useSharedValue,
    withSpring,
} from 'react-native-reanimated';
import { Colors, Radius, Spacing, Typography } from '@/constants/theme';

const AnimatedTouchable = Animated.createAnimatedComponent(TouchableOpacity);

interface ButtonProps extends TouchableOpacityProps {
    label: string;
    variant?: 'primary' | 'secondary' | 'ghost' | 'danger';
    loading?: boolean;
    fullWidth?: boolean;
}

export function Button({
    label,
    variant = 'primary',
    loading = false,
    fullWidth = false,
    onPress,
    disabled,
    style,
    ...rest
}: ButtonProps) {
    const scale = useSharedValue(1);

    const animatedStyle = useAnimatedStyle(() => ({
        transform: [{ scale: scale.value }],
    }));

    function handlePressIn() {
        scale.value = withSpring(0.96, { damping: 15, stiffness: 400 });
    }

    function handlePressOut() {
        scale.value = withSpring(1, { damping: 15, stiffness: 400 });
    }

    const containerStyle = [
        styles.base,
        styles[variant],
        fullWidth && styles.fullWidth,
        (disabled || loading) && styles.disabled,
        style,
    ];

    const textColor =
        variant === 'secondary' || variant === 'ghost'
            ? Colors.textPrimary
            : Colors.white;

    return (
        <AnimatedTouchable
            style={[animatedStyle, containerStyle]}
            onPress={onPress}
            onPressIn={handlePressIn}
            onPressOut={handlePressOut}
            disabled={disabled || loading}
            activeOpacity={1}
            {...rest}
        >
            {loading ? (
                <ActivityIndicator color={textColor} size="small" />
            ) : (
                <Text style={[styles.label, { color: textColor }]}>{label}</Text>
            )}
        </AnimatedTouchable>
    );
}

interface IconButtonProps {
    onPress: () => void;
    children: React.ReactNode;
    accessibilityLabel: string;
}

export function IconButton({ onPress, children, accessibilityLabel }: IconButtonProps) {
    return (
        <TouchableOpacity
            style={styles.iconButton}
            onPress={onPress}
            hitSlop={{ top: 8, bottom: 8, left: 8, right: 8 }}
            accessibilityLabel={accessibilityLabel}
            accessibilityRole="button"
        >
            <View>{children}</View>
        </TouchableOpacity>
    );
}

const styles = StyleSheet.create({
    base: {
        height: 52,
        borderRadius: Radius.md,
        alignItems: 'center',
        justifyContent: 'center',
        paddingHorizontal: Spacing.lg,
        minWidth: 44, // touch target mínimo
    },
    primary: {
        backgroundColor: Colors.primary,
    },
    secondary: {
        backgroundColor: Colors.surfaceElevated,
        borderWidth: 1,
        borderColor: Colors.border,
    },
    ghost: {
        backgroundColor: Colors.transparent,
    },
    danger: {
        backgroundColor: Colors.negative,
    },
    fullWidth: {
        width: '100%',
    },
    disabled: {
        opacity: 0.5,
    },
    label: {
        ...Typography.bodyLarge,
        fontFamily: 'DMSans_600SemiBold',
    },
    iconButton: {
        width: 44,
        height: 44,
        alignItems: 'center',
        justifyContent: 'center',
    },
});