[BLOG POST] Compose Unstyled: The missing Design System layer for Compose UI #106
Replies: 5 comments
-
Thx a lot for the article. In my current project I experimented a lot to identify the way how to leverage the design tokens during the migration from Views to Composable. I want to share the findings, may be it will help somebody to migrate to the Compose. Let's say
Given this information, it becomes quite difficult to create a theme as the article says, without context, we don't have access to the raw values. How I solved it:
and the usage of the Spacing looks like:
Same was done for the aspect ration, colors, typographies, etc. |
Beta Was this translation helpful? Give feedback.
-
additional benefit is that you can use those semantic tokens outside of the Composable functions if you want/need.
|
Beta Was this translation helpful? Give feedback.
-
@vysarafanov Compose Unstyle's Here is how you would use your existing XML theme attributes: val spacing = ThemeProperty<Dp>("spacing")
val small = ThemeToken<Dp>("small")
val medium = ThemeToken<Dp>("medium")
val large = ThemeToken<Dp>("large")
@Composable
fun Resources.Theme.resolveDp(attr: Int): Dp {
val density = LocalDensity.current
return remember(density, attr) {
val typedValue = TypedValue()
resolveAttribute(attr, typedValue, false)
with(density) {
typedValue.getDimension(resources.displayMetrics).toDp()
}
}
}
val MyTheme = buildTheme {
val context = LocalContext.current
val xmlTheme = context.theme
properties[spacing] = mapOf(
small to xmlTheme.resolveDp(R.attr.spacingS),
medium to xmlTheme.resolveDp(R.attr.spacingM),
large to xmlTheme.resolveDp(R.attr.spacingL),
)
}
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
MyTheme {
Column {
Text("Small = ${Theme[spacing][small]}")
Text("Medium = ${Theme[spacing][medium]}")
Text("Large = ${Theme[spacing][large]}")
}
}
}
}
}
|
Beta Was this translation helpful? Give feedback.
-
Heads up. This is now officially supported in 1.40.0 (https://composables.com/blog/compose-unstyled-1.40.0) |
Beta Was this translation helpful? Give feedback.
-
Looks nice, thank you for the update 🥳 |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I just published a new blog post about Compose Unstyled:
Compose Unstyled: The missing Design System layer for Compose UI
Opened this Discussion for anyone that wants to leave their thoughts, comments or feedback on the article.
Beta Was this translation helpful? Give feedback.
All reactions