-
Notifications
You must be signed in to change notification settings - Fork 339
Add Compose Foundation Style snippets #817
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
riggaroo
wants to merge
3
commits into
main
Choose a base branch
from
feature/styles
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
compose/snippets/src/main/java/com/example/compose/snippets/styles/BasicComponents.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| /* | ||
| * Copyright 2026 The Android Open Source Project | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| @file:OptIn(ExperimentalFoundationStyleApi::class) | ||
|
|
||
| package com.example.compose.snippets.styles | ||
|
|
||
| import androidx.compose.foundation.clickable | ||
| import androidx.compose.foundation.focusable | ||
| import androidx.compose.foundation.hoverable | ||
| import androidx.compose.foundation.interaction.MutableInteractionSource | ||
| import androidx.compose.foundation.layout.Row | ||
| import androidx.compose.foundation.layout.RowScope | ||
| import androidx.compose.foundation.style.ExperimentalFoundationStyleApi | ||
| import androidx.compose.foundation.style.MutableStyleState | ||
| import androidx.compose.foundation.style.Style | ||
| import androidx.compose.foundation.style.styleable | ||
| import androidx.compose.foundation.text.BasicText | ||
| import androidx.compose.foundation.text.TextAutoSize | ||
| import androidx.compose.runtime.Composable | ||
| import androidx.compose.runtime.remember | ||
| import androidx.compose.ui.Alignment | ||
| import androidx.compose.ui.Modifier | ||
| import androidx.compose.ui.text.TextLayoutResult | ||
| import androidx.compose.ui.text.style.TextOverflow | ||
|
|
||
| @Composable | ||
| fun BaseCheckbox(modifier: Modifier = Modifier, style: Style = Style) { | ||
| // Your custom component applying the style via the styleable modifier | ||
| } | ||
|
|
||
| @ExperimentalFoundationStyleApi | ||
| @Composable | ||
| fun BaseText( | ||
| text: String, | ||
| modifier: Modifier = Modifier, | ||
| style: Style = Style, | ||
| onTextLayout: ((TextLayoutResult) -> Unit)? = null, | ||
| overflow: TextOverflow = TextOverflow.Clip, | ||
| softWrap: Boolean = true, | ||
| maxLines: Int = Int.MAX_VALUE, | ||
| minLines: Int = 1, | ||
| autoSize: TextAutoSize? = null, | ||
| ) { | ||
| BasicText( | ||
| text = text, | ||
| modifier = modifier.styleable(null, style), | ||
| onTextLayout = onTextLayout, | ||
| overflow = overflow, | ||
| softWrap = softWrap, | ||
| maxLines = maxLines, | ||
| minLines = minLines, | ||
| autoSize = autoSize, | ||
| ) | ||
| } | ||
|
|
||
| // Example of using a Style within your own component. | ||
| @ExperimentalFoundationStyleApi | ||
| @Composable | ||
| fun BaseButton( | ||
| onClick: () -> Unit, | ||
| modifier: Modifier = Modifier, | ||
| style: Style = Style, | ||
| enabled: Boolean = true, | ||
| interactionSource: MutableInteractionSource = remember { | ||
| MutableInteractionSource() | ||
| }, | ||
| content: @Composable RowScope.() -> Unit | ||
| ) { | ||
| val styleState = remember(interactionSource) { | ||
| MutableStyleState(interactionSource) | ||
| } | ||
| styleState.isEnabled = enabled | ||
| Row( | ||
| modifier = modifier | ||
| .hoverable(interactionSource = interactionSource) | ||
| .focusable(true, interactionSource = interactionSource) | ||
| .clickable( | ||
| enabled = enabled, | ||
| onClick = onClick, | ||
| interactionSource = interactionSource, | ||
| indication = null, | ||
| ) | ||
| .styleable(styleState, StylesThemes.LocalAppStyles.current.baseButtonStyle, style), | ||
| content = content, | ||
| verticalAlignment = Alignment.CenterVertically | ||
| ) | ||
| } | ||
|
|
||
94 changes: 94 additions & 0 deletions
94
compose/snippets/src/main/java/com/example/compose/snippets/styles/StyleDosDonts.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| /* | ||
| * Copyright 2026 The Android Open Source Project | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| @file:OptIn(ExperimentalFoundationStyleApi::class) | ||
|
|
||
| package com.example.compose.snippets.styles | ||
|
|
||
| import androidx.compose.foundation.style.ExperimentalFoundationStyleApi | ||
| import androidx.compose.foundation.style.Style | ||
| import androidx.compose.runtime.Composable | ||
| import androidx.compose.ui.Modifier | ||
|
|
||
| private object StyleDosDonts { | ||
|
|
||
| // [START android_compose_styles_basics_do_expose] | ||
| @Composable | ||
| fun GradientButton( | ||
| modifier: Modifier = Modifier, | ||
| // ✅ DO: for design system components, expose a style modifier to consumers to be able to customize the components | ||
| style: Style = Style | ||
| ) { | ||
| // Consume the style | ||
| } | ||
| // [END android_compose_styles_basics_do_expose] | ||
|
|
||
| // [START android_compose_styles_basics_do_replace] | ||
| // Before | ||
| /* | ||
| @Composable | ||
| fun Button(background: Color, fontColor: Color) { | ||
|
|
||
| } | ||
| */ | ||
| // After | ||
| // ✅ DO: Replace visual-based parameters with a style that includes same properties | ||
| @Composable | ||
| fun StyleButton(style: Style = Style) { | ||
|
|
||
| } | ||
| // [END android_compose_styles_basics_do_replace] | ||
|
|
||
| // [START android_compose_styles_basics_do_wrappers] | ||
| /* | ||
| @Composable | ||
| fun Button(modifier: Modifier = Modifier, | ||
| style: Style = Style) { | ||
| // Uses LocalTheme.appStyles.button + incoming style | ||
| } | ||
| */ | ||
|
|
||
| // ✅ Do create wrapper composables that expose common implementations of the same component | ||
| @Composable | ||
| fun WrapperGradientButton( | ||
| modifier: Modifier = Modifier, | ||
| style: Style = Style | ||
| ) { | ||
| // Uses LocalTheme.appStyles.button + LocalTheme.appStyles.gradientButton + incoming style - merge these styles | ||
| } | ||
| // [END android_compose_styles_basics_do_wrappers] | ||
|
|
||
| // [START android_compose_styles_basics_dont_layout] | ||
| @Composable | ||
| fun FavouriteScreen( | ||
| modifier: Modifier = Modifier, | ||
| // ❌ DONT add style parameter to Screen-level composables | ||
| style: Style = Style, | ||
| // etc | ||
| ) { | ||
|
|
||
| } | ||
|
|
||
| @Composable | ||
| fun CircularCustomLayout( | ||
| modifier: Modifier = Modifier, | ||
| // ❌ DONT add style parameters to composables where the main function is layout. | ||
| style: Style = Style | ||
| ) { | ||
|
|
||
| } | ||
| // [END android_compose_styles_basics_dont_layout] | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Mutating state like
styleState.isEnableddirectly within the body of a composable is a side-effect and can lead to unpredictable behavior. This mutation should be performed within aLaunchedEffectto ensure it happens in a controlled manner only when theenabledvalue changes.