Loading...
Loading...
Use this skill to integrate the Jetpack Compose Styles API into an Android project. This skill guides you through upgrading dependencies, setting up component themes, making custom components styleable, and migrating existing layout properties to use unified styles. Migrate custom design system components, replace hard coded parameters with Style attributes, and use Modifier.styleable for interaction states.
npx skill4agent add android/skills stylescompileSdkandroidx.compose.foundation:foundation1.12.0-alpha012026.04.01import androidx.compose.foundation.style.Stylebuild.gradle.ktskotlin {
compilerOptions {
jvmTarget = JvmTarget.fromTarget("17")
freeCompilerArgs.add("-opt-in=androidx.compose.foundation.style.ExperimentalFoundationStyleApi")
}
}Theme.ktLocalColorSchemeLocalTypographyLocalShapesandroidx.compose.material.MaterialThemeComponentStylesComponentStyles.ktJetsnackStylesobject ExampleComponentStyles {
val customButtonStyle: Style = {
}
val customTextFieldStyle: Style = {
}
}CompositionLocals@Immutable
class JetsnackTheme(
// other Design system properties
) {
companion object {
val colors: CustomThemingWithStyles.JetsnackColors
@Composable @ReadOnlyComposable
get() = LocalJetsnackTheme.current.colors
// ...
// add helper static reference
val styles: ComponentStyles = ComponentStyles
}
}StyleScopeCompositionLocalsval StyleScope.colors: JetsnackColors
get() = LocalJetsnackTheme.currentValue.colors
val StyleScope.typography: androidx.compose.material3.Typography
get() = LocalJetsnackTheme.currentValue.typography
val StyleScope.shapes: Shapes
get() = LocalJetsnackTheme.currentValue.shapesCustomButtonbackgroundColorshapetextStylecontentPaddingStyleScopestyle: Style = StyleMutableStyleStateisEnabledModifier.styleable()StyleComponentStyles.kt@Composable
fun CustomButton(
onClick: () -> Unit,
modifier: Modifier = Modifier,
backgroundColor: Color = JetsnackTheme.colors.brandLight,
disabledBackgroundColor: Color = JetsnackTheme.colors.brandSecondary,
shape: Shape = JetsnackTheme.shapes.extraLarge,
textStyle: TextStyle = JetsnackTheme.typography.labelLarge,
enabled: Boolean = true,
content: @Composable RowScope.() -> Unit,
) {
val interactionSource = remember { MutableInteractionSource() }
Row(
modifier
.clickable(onClick = onClick, indication = null, interactionSource = interactionSource)
.background(if (enabled) backgroundColor else disabledBackgroundColor, shape)
.defaultMinSize(58.dp, 40.dp),
horizontalArrangement = Arrangement.Center,
verticalAlignment = Alignment.CenterVertically,
content = content,
)
}// Exposed via ComponentStyles.kt
object ComponentStyles {
val buttonStyle = Style {
background(colors.brandLight)
shape(shapes.extraLarge)
minWidth(58.dp)
minHeight(40.dp)
textStyle(typography.labelLarge)
disabled {
background(colors.brandSecondary)
}
}
}
@Composable
fun CustomButton(
onClick: () -> Unit,
modifier: Modifier = Modifier,
style: Style = Style,
enabled: Boolean = true,
content: @Composable RowScope.() -> Unit,
) {
val interactionSource = remember { MutableInteractionSource() }
val styleState = rememberUpdatedStyleState(interactionSource) {
it.isEnabled = enabled
}
Row(
modifier
.clickable(onClick = onClick, indication = null, interactionSource = interactionSource)
.styleable(styleState, JetsnackTheme.styles.buttonStyle, style),
horizontalArrangement = Arrangement.Center,
verticalAlignment = Alignment.CenterVertically,
content = content,
)
}