Alex Styl
06/19/2024, 11:17 AM@Composable
Theme function.
You can then access those properties from any child composable of the theme function. (code example in 🧵 thread)
It features:
🤸♀️ Flexibility to its core: you control both how your app and API looks
✨ Beautiful defaults: comes with a set of colors, text styles and shapes to build your design system with (which you can customize)
🙅 An API, NOT a design system: Instead of giving you components with required properties, it gives you the API that your components can use to be styled.
Checkout the source code & more examples at: https://github.com/composablehorizons/composethemeAlex Styl
06/19/2024, 11:17 AM// define your tokens
val primary = DesignToken<Color>("primary")
val background = DesignToken<Color>("background")
// creates a @Composable Theme function
val Theme = buildComposeTheme {
colors = mapOf(
primary to Color.Red,
background to Color.Gray,
)
}
@Composable
fun App() {
// use the ComposeTheme object to access the respective token values
Theme {
Box(Modifier.fillMaxSize().background(ComposeTheme.colors[background])) {
Box(Modifier.size(56.dp).background(ComposeTheme.colors[primary]))
}
}
}
kenkyee
06/19/2024, 12:22 PMAlex Styl
06/19/2024, 12:30 PMMaterialTheme
object to get the Material's design system properties.
If you are building anything that is slightly off Material, you end up building your own components anyway, and this is where ComposeTheme is handyAlex Styl
06/19/2024, 12:37 PMval accent = DesignToken<Color>("accent")
val text = DesignToken<Color>("text")
val NewTheme = buildComposeTheme {
colors = mapOf(
accent to Color.Yellow,
text to Color.Yellow,
)
}
@Composable
fun UsingWithMaterialDemo() {
NewTheme {
CustomButton(onClick = {}) {
BasicText("Click me")
}
}
}
@Composable
fun CustomButton(onClick: () -> Unit, content: @Composable RowScope.() -> Unit) {
// this is the Material 3 button
Button(
onClick = onClick,
content = content,
colors = ButtonColors(
containerColor = ComposeTheme.colors[accent],
disabledContainerColor = ComposeTheme.colors[accent].copy(alpha = 0.33f),
contentColor = ComposeTheme.colors[text],
disabledContentColor = ComposeTheme.colors[text].copy(alpha = 0.33f)
)
)
}
kenkyee
06/19/2024, 12:40 PMAlex Styl
06/19/2024, 12:42 PMkenkyee
06/19/2024, 1:40 PMAlex Styl
06/19/2024, 3:46 PMjames
06/20/2024, 1:28 AMcomposetheme-material3
so those who have 100% custom components aren’t forced to depend on material libs
excellent library though, great workAlex Styl
06/20/2024, 8:35 AMAlex Styl
06/21/2024, 2:06 PMval buttonLabel = DesignToken<TextStyle>("buttonLabel")
val Material3ThemeExtended = buildComposeTheme {
textStyles = DesignTokens(
buttonLabel to TextStyle(fontSize = 12.sp, lineHeight = 16.sp)
)
extendMaterial3 {
colorScheme = lightColorScheme(
primary = Color.Red,
)
typography = Typography()
shapes = Shapes()
}
}
@Composable
fun App() {
Material3ThemeExtended {
Button(onClick = { }) { // this button is rendered Red
Text("Click me!", style = ComposeTheme.textStyles[buttonLabel])
}
}
}
A bunch of other goodies added, such as being able to use any kind of Type you want for your design system. More info at https://github.com/composablehorizons/composetheme