|
18 | 18 |
|
19 | 19 | package com.example.compose.snippets.designsystems.styles |
20 | 20 |
|
| 21 | +import androidx.compose.foundation.interaction.MutableInteractionSource |
| 22 | +import androidx.compose.foundation.layout.Box |
21 | 23 | import androidx.compose.foundation.style.ExperimentalFoundationStyleApi |
| 24 | +import androidx.compose.foundation.style.MutableStyleState |
22 | 25 | import androidx.compose.foundation.style.Style |
| 26 | +import androidx.compose.foundation.style.styleable |
23 | 27 | import androidx.compose.runtime.Composable |
| 28 | +import androidx.compose.runtime.remember |
24 | 29 | import androidx.compose.ui.Modifier |
25 | 30 | import androidx.compose.ui.graphics.Color |
26 | 31 |
|
@@ -77,3 +82,27 @@ fun BadButton( |
77 | 82 | ) { |
78 | 83 | } |
79 | 84 | // [END android_compose_styles_donts_default_style] |
| 85 | + |
| 86 | +// [START android_compose_styles_do_default_style] |
| 87 | +@Composable |
| 88 | +fun GoodButton( |
| 89 | + modifier: Modifier = Modifier, |
| 90 | + // ✅ Do: always pass it as a Style, do not pass other defaults |
| 91 | + style: Style = Style |
| 92 | +) { |
| 93 | + // [START_EXCLUDE] |
| 94 | + // this is a snippet of the BaseButton - see the full snippet in /components/Button.kt |
| 95 | + val effectiveInteractionSource = remember { |
| 96 | + MutableInteractionSource() |
| 97 | + } |
| 98 | + val styleState = remember(effectiveInteractionSource) { |
| 99 | + MutableStyleState(effectiveInteractionSource) |
| 100 | + } |
| 101 | + // [END_EXCLUDE] |
| 102 | + val defaultStyle = Style { background(Color.Red) } |
| 103 | + // ✅ Do Combine defaults inside with incoming parameter |
| 104 | + Box(modifier = modifier.styleable(styleState, defaultStyle, style)) { |
| 105 | + // your logic |
| 106 | + } |
| 107 | +} |
| 108 | +// [END android_compose_styles_do_default_style] |
0 commit comments