Skip to content

Commit c32f9de

Browse files
committed
Refactor Button by using composition structure
1 parent ccdcb8c commit c32f9de

File tree

1 file changed

+50
-30
lines changed
  • core/designsystem/src/main/java/io/foundy/core/designsystem/component

1 file changed

+50
-30
lines changed

core/designsystem/src/main/java/io/foundy/core/designsystem/component/Button.kt

+50-30
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import androidx.compose.foundation.shape.RoundedCornerShape
1111
import androidx.compose.material3.Button
1212
import androidx.compose.material3.ButtonColors
1313
import androidx.compose.material3.ButtonDefaults
14+
import androidx.compose.material3.ProvideTextStyle
1415
import androidx.compose.material3.Text
1516
import androidx.compose.runtime.Composable
1617
import androidx.compose.ui.Alignment
@@ -39,9 +40,7 @@ fun ButtonDefaults.camstudyTextButtonColors(
3940
@Composable
4041
internal fun RawButton(
4142
modifier: Modifier = Modifier,
42-
label: String,
43-
enableLabelSizeAnimation: Boolean = false,
44-
leadingIcon: CamstudyIcon? = null,
43+
leading: @Composable (() -> Unit)? = null,
4544
shape: Shape = RoundedCornerShape(8.dp),
4645
colors: ButtonColors = ButtonDefaults.buttonColors(
4746
containerColor = CamstudyTheme.colorScheme.primary,
@@ -51,7 +50,8 @@ internal fun RawButton(
5150
),
5251
enabled: Boolean = true,
5352
border: BorderStroke? = null,
54-
onClick: () -> Unit
53+
onClick: () -> Unit,
54+
content: @Composable () -> Unit,
5555
) {
5656
Button(
5757
modifier = modifier,
@@ -66,29 +66,42 @@ internal fun RawButton(
6666
)
6767
) {
6868
Row(verticalAlignment = Alignment.CenterVertically) {
69-
if (leadingIcon != null) {
70-
CamstudyIcon(
71-
modifier = Modifier.size(20.dp),
72-
icon = leadingIcon,
73-
contentDescription = null,
74-
)
69+
if (leading != null) {
70+
leading()
7571
Spacer(modifier = Modifier.width(10.dp))
7672
}
77-
Text(
78-
modifier = if (enableLabelSizeAnimation) {
79-
Modifier.animateContentSize()
80-
} else {
81-
Modifier
82-
},
83-
text = label,
84-
style = CamstudyTheme.typography.titleMedium.copy(
73+
ProvideTextStyle(
74+
value = CamstudyTheme.typography.titleMedium.copy(
8575
fontWeight = FontWeight.Medium
8676
)
87-
)
77+
) {
78+
content()
79+
}
8880
}
8981
}
9082
}
9183

84+
@Composable
85+
private fun DefaultContentText(text: String, enableLabelSizeAnimation: Boolean = false) {
86+
Text(
87+
modifier = if (enableLabelSizeAnimation) {
88+
Modifier.animateContentSize()
89+
} else {
90+
Modifier
91+
},
92+
text = text,
93+
)
94+
}
95+
96+
@Composable
97+
private fun DefaultLeadingIcon(icon: CamstudyIcon) {
98+
CamstudyIcon(
99+
modifier = Modifier.size(20.dp),
100+
icon = icon,
101+
contentDescription = null,
102+
)
103+
}
104+
92105
@Composable
93106
fun CamstudyOutlinedButton(
94107
modifier: Modifier = Modifier,
@@ -110,19 +123,21 @@ fun CamstudyOutlinedButton(
110123
RawButton(
111124
modifier = modifier,
112125
onClick = onClick,
113-
label = label,
114126
shape = shape,
115127
enabled = enabled,
116-
enableLabelSizeAnimation = enableLabelSizeAnimation,
117-
leadingIcon = leadingIcon,
128+
leading = if (leadingIcon != null) {
129+
{ DefaultLeadingIcon(icon = leadingIcon) }
130+
} else null,
118131
border = BorderStroke(width = 1.dp, color = borderColor),
119132
colors = ButtonDefaults.buttonColors(
120133
containerColor = containerColor,
121134
contentColor = contentColor,
122135
disabledContainerColor = colorScheme.systemBackground,
123136
disabledContentColor = colorScheme.systemUi04
124137
),
125-
)
138+
) {
139+
DefaultContentText(text = label, enableLabelSizeAnimation = enableLabelSizeAnimation)
140+
}
126141
}
127142

128143
@Composable
@@ -139,18 +154,20 @@ fun CamstudyContainedButton(
139154
RawButton(
140155
modifier = modifier,
141156
onClick = onClick,
142-
label = label,
143157
shape = shape,
144-
enableLabelSizeAnimation = enableLabelSizeAnimation,
145-
leadingIcon = leadingIcon,
158+
leading = if (leadingIcon != null) {
159+
{ DefaultLeadingIcon(icon = leadingIcon) }
160+
} else null,
146161
enabled = enabled,
147162
colors = ButtonDefaults.buttonColors(
148163
containerColor = colorScheme.primary,
149164
contentColor = colorScheme.systemBackground,
150165
disabledContainerColor = colorScheme.systemUi02,
151166
disabledContentColor = colorScheme.systemUi05
152167
),
153-
)
168+
) {
169+
DefaultContentText(text = label, enableLabelSizeAnimation = enableLabelSizeAnimation)
170+
}
154171
}
155172

156173
@Composable
@@ -166,12 +183,15 @@ fun CamstudyTextButton(
166183
RawButton(
167184
modifier = modifier,
168185
onClick = onClick,
169-
label = label,
170186
shape = shape,
171-
leadingIcon = leadingIcon,
187+
leading = if (leadingIcon != null) {
188+
{ DefaultLeadingIcon(icon = leadingIcon) }
189+
} else null,
172190
enabled = enabled,
173191
colors = colors,
174-
)
192+
) {
193+
DefaultContentText(text = label)
194+
}
175195
}
176196

177197
@Preview

0 commit comments

Comments
 (0)