forked from mozilla-mobile/fenix
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
For mozilla-mobile#27401 - refactor SelectableChip into Chip composable
- Loading branch information
1 parent
e052493
commit 7575c0d
Showing
2 changed files
with
112 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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,97 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
package org.mozilla.fenix.compose | ||
|
||
import android.content.res.Configuration | ||
import androidx.compose.foundation.background | ||
import androidx.compose.foundation.layout.Box | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.fillMaxHeight | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.foundation.selection.selectable | ||
import androidx.compose.material.MaterialTheme | ||
import androidx.compose.material.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.draw.clip | ||
import androidx.compose.ui.graphics.Color | ||
import androidx.compose.ui.text.TextStyle | ||
import androidx.compose.ui.text.capitalize | ||
import androidx.compose.ui.text.intl.Locale | ||
import androidx.compose.ui.tooling.preview.Preview | ||
import androidx.compose.ui.unit.dp | ||
import androidx.compose.ui.unit.sp | ||
import org.mozilla.fenix.theme.FirefoxTheme | ||
|
||
/** | ||
* Default layout for a clickable chip. | ||
* | ||
* @param text [String] displayed in this chip. Ideally should only be one word. | ||
* @param modifider [Modifier] used to be applied to the layout of the chip. | ||
* @param textColor optional text [Color] for the chip. | ||
* @param backgroundColor optional background [Color] for the chip. | ||
* @param onClick optional Callback for when the user taps this chip. | ||
*/ | ||
@Composable | ||
fun Chip( | ||
text: String, | ||
modifier: Modifier = Modifier, | ||
textColor: Color = FirefoxTheme.colors.textActionPrimary, | ||
backgroundColor: Color = FirefoxTheme.colors.actionPrimary, | ||
onClick: (() -> Unit)? = null, | ||
) { | ||
Box( | ||
modifier = modifier | ||
.selectable(false) { | ||
if (onClick != null) { | ||
onClick() | ||
} | ||
} | ||
.clip(MaterialTheme.shapes.small) | ||
.background(color = backgroundColor) | ||
.padding(horizontal = 16.dp, vertical = 10.dp), | ||
) { | ||
Text( | ||
text = text.capitalize(Locale.current), | ||
style = TextStyle(fontSize = 14.sp), | ||
color = textColor, | ||
) | ||
} | ||
} | ||
|
||
/** | ||
* An example of a selectable chip. | ||
*/ | ||
@Composable | ||
@Preview(uiMode = Configuration.UI_MODE_NIGHT_YES) | ||
@Preview(uiMode = Configuration.UI_MODE_NIGHT_NO) | ||
private fun SelectableChipPreview() { | ||
val selectedState = remember { mutableStateOf(false) } | ||
|
||
FirefoxTheme { | ||
Column( | ||
modifier = Modifier.fillMaxHeight() | ||
.background(FirefoxTheme.colors.layer1), | ||
) { | ||
Chip( | ||
text = "Chirp", | ||
textColor = if (selectedState.value) { | ||
FirefoxTheme.colors.textActionTertiary | ||
} else { | ||
FirefoxTheme.colors.textActionPrimary | ||
}, | ||
backgroundColor = if (selectedState.value) { | ||
FirefoxTheme.colors.actionTertiary | ||
} else { | ||
FirefoxTheme.colors.actionPrimary | ||
}, | ||
) { | ||
selectedState.value = !selectedState.value | ||
} | ||
} | ||
} | ||
} |
This file contains 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