@@ -3,6 +3,7 @@ import { Text, StyleSheet, View, Pressable } from "react-native";
3
3
import { colors , paddings , rounded , textStyles } from '../../style' ;
4
4
import LoadingDots from "../../animations/LoadingDots" ;
5
5
6
+ // Define styles for different button types
6
7
const typeStyles = {
7
8
primary : { backgroundColor : colors . violet500 , textColor : colors . white } ,
8
9
secondary : { backgroundColor : colors . grey800 , textColor : colors . white } ,
@@ -19,6 +20,7 @@ const typeStyles = {
19
20
} ,
20
21
} ;
21
22
23
+ // Define styles for different button states
22
24
const stateStyles = {
23
25
default : {
24
26
primary : { backgroundColor : colors . violet500 } ,
@@ -52,17 +54,19 @@ const stateStyles = {
52
54
} ,
53
55
} ;
54
56
57
+ // Define styles for different button sizes
55
58
const sizeStyles = {
56
59
1 : { height : 32 , paddingHorizontal : paddings . p_12 } ,
57
60
2 : { height : 40 , paddingHorizontal : paddings . p_16 } ,
58
61
3 : { height : 48 , paddingHorizontal : paddings . p_16 } ,
59
62
4 : { height : 56 , paddingHorizontal : paddings . p_24 } ,
60
63
} ;
61
64
65
+ // Function to combine styles based on type, state, and size
62
66
const getCombinedStyles = ( type , state , size ) => {
63
- const typeStyle = typeStyles [ type ] ;
64
- const stateStyle = stateStyles [ state ] ?. [ type ] ;
65
- const sizeStyle = sizeStyles [ size ] ;
67
+ const typeStyle = typeStyles [ type ] ; // Get styles based on button type
68
+ const stateStyle = stateStyles [ state ] ?. [ type ] ; // Get styles based on button state
69
+ const sizeStyle = sizeStyles [ size ] ; // Get styles based on button size
66
70
67
71
return {
68
72
...typeStyle ,
@@ -71,28 +75,38 @@ const getCombinedStyles = (type, state, size) => {
71
75
} ;
72
76
} ;
73
77
78
+ // --------------------------------------------
79
+ // CustomButton component definition
80
+ // --------------------------------------------
74
81
const CustomButton = ( {
75
- containerStyle,
76
- onPress,
77
- text = "" ,
78
- type = "secondary" ,
79
- size = 4 ,
80
- leftIcon,
81
- rightIcon,
82
- loading = false ,
83
- disabled = false ,
84
- scaling = "full" ,
82
+ containerStyle, // Custom styles for the button container
83
+ onPress, // Function to call when the button is pressed
84
+ text = "" , // Text to display on the button
85
+ type = "secondary" , // Button type (primary, secondary, tertiary, ghost)
86
+ size = 4 , // Button size (1 to 4)
87
+ leftIcon, // Icon to display on the left side of the button
88
+ rightIcon, // Icon to display on the right side of the button
89
+ loading = false , // Whether the button is in a loading state
90
+ disabled = false , // Whether the button is disabled
91
+ scaling = "full" , // Scaling option for the button
85
92
} ) => {
93
+ // State to manage button state (default, pressed, etc.)
86
94
const [ buttonState , setButtonState ] = useState ( 'default' ) ;
87
- const state = loading ? 'loading' : ( disabled ? 'disabled' : buttonState ) ;
88
- const combinedStyles = getCombinedStyles ( type , state , size ) ;
95
+ const state = loading ? 'loading' : ( disabled ? 'disabled' : buttonState ) ; // Determine the current state of the button
96
+ const combinedStyles = getCombinedStyles ( type , state , size ) ; // Get the combined styles based on type, state, and size
89
97
98
+ // --------------------------------------------
99
+ // Handlers for button press events
100
+ // --------------------------------------------
101
+
102
+ // Set state to pressed when the button is pressed in
90
103
const handlePressIn = ( ) => {
91
104
if ( ! disabled && ! loading ) {
92
105
setButtonState ( 'pressed' ) ;
93
106
}
94
107
} ;
95
108
109
+ // Set state to default when the button is released
96
110
const handlePressOut = ( ) => {
97
111
if ( ! disabled && ! loading ) {
98
112
setButtonState ( 'default' ) ;
@@ -101,64 +115,68 @@ const CustomButton = ({
101
115
102
116
const handlePress = ( ) => {
103
117
if ( ! disabled && ! loading && onPress ) {
104
- onPress ( ) ;
118
+ onPress ( ) ; // Call the onPress function when the button is pressed
105
119
}
106
120
} ;
107
121
122
+ // --------------------------------------------
123
+ // Render the button
124
+ // --------------------------------------------
108
125
return (
109
126
< Pressable
110
127
style = { [
111
128
{
112
- backgroundColor : combinedStyles . backgroundColor ,
113
- height : combinedStyles . height ,
114
- borderWidth : combinedStyles . borderWidth || 0 ,
115
- borderColor : combinedStyles . borderColor || "transparent" ,
116
- paddingHorizontal : combinedStyles . paddingHorizontal ,
129
+ backgroundColor : combinedStyles . backgroundColor , // Set background color based on combined styles
130
+ height : combinedStyles . height , // Set height based on combined styles
131
+ borderWidth : combinedStyles . borderWidth || 0 , // Set border width based on combined styles
132
+ borderColor : combinedStyles . borderColor || "transparent" , // Set border color based on combined styles
133
+ paddingHorizontal : combinedStyles . paddingHorizontal , // Set horizontal padding based on combined styles
117
134
borderRadius : rounded . rounded_md ,
118
135
alignItems : "center" ,
119
136
justifyContent : "center" ,
120
- ...( scaling === "full" ? { flexGrow : 1 } : { alignSelf : "flex-start" } ) ,
121
- maxHeight : combinedStyles . height ,
137
+ ...( scaling === "full" ? { flexGrow : 1 } : { alignSelf : "flex-start" } ) , // Sets scaling based on `scaling`: `auto` adjusts to font size, otherwise it ajusts to the container.
138
+ maxHeight : combinedStyles . height , // Set max height based on combined styles
122
139
} ,
123
- containerStyle ,
140
+ containerStyle , // Apply custom container styles
124
141
] }
125
- onPress = { handlePress }
126
- onPressIn = { handlePressIn }
127
- onPressOut = { handlePressOut }
128
- disabled = { disabled || loading }
142
+ onPress = { handlePress } // Handle button press
143
+ onPressIn = { handlePressIn } // Handle button press in
144
+ onPressOut = { handlePressOut } // Handle button press out
145
+ disabled = { disabled || loading } // Disable button if disabled or loading
129
146
>
130
147
< View style = { styles . contentContainer } >
131
- { leftIcon && < View style = { styles . iconLeft } > { leftIcon } </ View > }
148
+ { leftIcon && < View style = { styles . iconLeft } > { leftIcon } </ View > } { /* Render left icon if provided */ }
132
149
{ loading ? (
133
150
< View style = { styles . loadingContainer } >
134
- < LoadingDots />
151
+ < LoadingDots /> { /* Render loading dots if loading */ }
135
152
</ View >
136
153
) : (
137
154
< Text style = { [ styles . text , { color : combinedStyles . textColor , textDecorationLine : combinedStyles . textDecorationLine || "none" } ] } >
138
- { text }
155
+ { text } { /* Button Text */ }
139
156
</ Text >
140
157
) }
141
- { rightIcon && < View style = { styles . iconRight } > { rightIcon } </ View > }
158
+ { rightIcon && < View style = { styles . iconRight } > { rightIcon } </ View > } { /* Render right icon if provided */ }
142
159
</ View >
143
160
</ Pressable >
144
161
) ;
145
162
} ;
146
163
164
+ // Styles for the button content
147
165
const styles = StyleSheet . create ( {
148
166
text : {
149
- ...textStyles . text_base_bold ,
150
- textAlign : "center" ,
167
+ ...textStyles . text_base_bold , // Apply base bold text style
168
+ textAlign : "center" , // Center align text
151
169
} ,
152
170
contentContainer : {
153
- flexDirection : "row" ,
171
+ flexDirection : "row" , // Arrange content in a row (needed for icons)
154
172
alignItems : "center" ,
155
173
justifyContent : "center" ,
156
- height : "100%" ,
174
+ height : "100%"
157
175
} ,
158
176
loadingContainer : {
159
177
flexDirection : 'row' ,
160
178
justifyContent : 'center' ,
161
- alignItems : 'center' ,
179
+ alignItems : 'center'
162
180
} ,
163
181
iconLeft : {
164
182
marginRight : 8 ,
0 commit comments