Add NavigationBar component
This commit is contained in:
parent
702e0c5445
commit
f0ce4afda3
3 changed files with 207 additions and 6 deletions
|
|
@ -17,7 +17,7 @@ import androidx.compose.runtime.Composable
|
||||||
import androidx.compose.ui.Alignment
|
import androidx.compose.ui.Alignment
|
||||||
import androidx.compose.ui.Modifier
|
import androidx.compose.ui.Modifier
|
||||||
import androidx.compose.ui.draw.clip
|
import androidx.compose.ui.draw.clip
|
||||||
import androidx.compose.ui.graphics.Color
|
import androidx.compose.ui.text.TextStyle
|
||||||
import androidx.compose.ui.text.rememberTextMeasurer
|
import androidx.compose.ui.text.rememberTextMeasurer
|
||||||
import androidx.compose.ui.unit.dp
|
import androidx.compose.ui.unit.dp
|
||||||
import io.element.android.compound.theme.ElementTheme
|
import io.element.android.compound.theme.ElementTheme
|
||||||
|
|
@ -41,13 +41,14 @@ private const val MAX_COUNT_STRING = "+$MAX_COUNT"
|
||||||
fun CounterAtom(
|
fun CounterAtom(
|
||||||
count: Int,
|
count: Int,
|
||||||
modifier: Modifier = Modifier,
|
modifier: Modifier = Modifier,
|
||||||
|
textStyle: TextStyle = CounterAtomDefaults.textStyle,
|
||||||
|
isCritical: Boolean = false,
|
||||||
) {
|
) {
|
||||||
if (count < 1) return
|
if (count < 1) return
|
||||||
val countAsText = when (count) {
|
val countAsText = when (count) {
|
||||||
in 0..MAX_COUNT -> count.toString()
|
in 0..MAX_COUNT -> count.toString()
|
||||||
else -> MAX_COUNT_STRING
|
else -> MAX_COUNT_STRING
|
||||||
}
|
}
|
||||||
val textStyle = ElementTheme.typography.fontBodyMdMedium
|
|
||||||
val textMeasurer = rememberTextMeasurer()
|
val textMeasurer = rememberTextMeasurer()
|
||||||
// Measure the maximum count string size
|
// Measure the maximum count string size
|
||||||
val textLayoutResult = textMeasurer.measure(
|
val textLayoutResult = textMeasurer.measure(
|
||||||
|
|
@ -58,19 +59,30 @@ fun CounterAtom(
|
||||||
val squareSize = maxOf(textSize.width, textSize.height)
|
val squareSize = maxOf(textSize.width, textSize.height)
|
||||||
Box(
|
Box(
|
||||||
modifier = modifier
|
modifier = modifier
|
||||||
.size(squareSize.toDp() + 1.dp)
|
.size(squareSize.toDp() + 1.dp)
|
||||||
.clip(CircleShape)
|
.clip(CircleShape)
|
||||||
.background(ElementTheme.colors.iconSuccessPrimary)
|
.background(
|
||||||
|
if (isCritical) {
|
||||||
|
ElementTheme.colors.iconCriticalPrimary
|
||||||
|
} else {
|
||||||
|
ElementTheme.colors.iconAccentPrimary
|
||||||
|
}
|
||||||
|
)
|
||||||
) {
|
) {
|
||||||
Text(
|
Text(
|
||||||
modifier = Modifier.align(Alignment.Center),
|
modifier = Modifier.align(Alignment.Center),
|
||||||
text = countAsText,
|
text = countAsText,
|
||||||
style = textStyle,
|
style = textStyle,
|
||||||
color = Color.White,
|
color = ElementTheme.colors.textOnSolidPrimary,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
object CounterAtomDefaults {
|
||||||
|
val textStyle: TextStyle
|
||||||
|
@Composable get() = ElementTheme.typography.fontBodyMdMedium
|
||||||
|
}
|
||||||
|
|
||||||
@PreviewsDayNight
|
@PreviewsDayNight
|
||||||
@Composable
|
@Composable
|
||||||
internal fun CounterAtomPreview() = ElementPreview {
|
internal fun CounterAtomPreview() = ElementPreview {
|
||||||
|
|
@ -79,5 +91,6 @@ internal fun CounterAtomPreview() = ElementPreview {
|
||||||
CounterAtom(count = 4)
|
CounterAtom(count = 4)
|
||||||
CounterAtom(count = 99)
|
CounterAtom(count = 99)
|
||||||
CounterAtom(count = 100)
|
CounterAtom(count = 100)
|
||||||
|
CounterAtom(count = 4, isCritical = true)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,186 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2025 New Vector Ltd.
|
||||||
|
*
|
||||||
|
* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
|
||||||
|
* Please see LICENSE files in the repository root for full details.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package io.element.android.libraries.designsystem.theme.components
|
||||||
|
|
||||||
|
import androidx.compose.foundation.interaction.MutableInteractionSource
|
||||||
|
import androidx.compose.foundation.layout.Box
|
||||||
|
import androidx.compose.foundation.layout.RowScope
|
||||||
|
import androidx.compose.foundation.layout.WindowInsets
|
||||||
|
import androidx.compose.foundation.layout.offset
|
||||||
|
import androidx.compose.material3.MaterialTheme
|
||||||
|
import androidx.compose.material3.NavigationBarDefaults
|
||||||
|
import androidx.compose.material3.NavigationBarItemColors
|
||||||
|
import androidx.compose.material3.NavigationBarItemDefaults
|
||||||
|
import androidx.compose.material3.contentColorFor
|
||||||
|
import androidx.compose.runtime.Composable
|
||||||
|
import androidx.compose.ui.Modifier
|
||||||
|
import androidx.compose.ui.graphics.Color
|
||||||
|
import androidx.compose.ui.graphics.vector.ImageVector
|
||||||
|
import androidx.compose.ui.tooling.preview.Preview
|
||||||
|
import androidx.compose.ui.unit.Dp
|
||||||
|
import androidx.compose.ui.unit.dp
|
||||||
|
import io.element.android.compound.theme.ElementTheme
|
||||||
|
import io.element.android.compound.tokens.generated.CompoundIcons
|
||||||
|
import io.element.android.libraries.designsystem.atomic.atoms.CounterAtom
|
||||||
|
import io.element.android.libraries.designsystem.preview.ElementThemedPreview
|
||||||
|
import io.element.android.libraries.designsystem.preview.PreviewGroup
|
||||||
|
import androidx.compose.material3.NavigationBarItem as MaterialNavigationBarItem
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
fun NavigationBar(
|
||||||
|
modifier: Modifier = Modifier,
|
||||||
|
containerColor: Color = ElementNavigationBarDefaults.containerColor,
|
||||||
|
contentColor: Color = MaterialTheme.colorScheme.contentColorFor(containerColor),
|
||||||
|
tonalElevation: Dp = ElementNavigationBarDefaults.tonalElevation,
|
||||||
|
windowInsets: WindowInsets = ElementNavigationBarDefaults.windowInsets,
|
||||||
|
content: @Composable RowScope.() -> Unit
|
||||||
|
) {
|
||||||
|
androidx.compose.material3.NavigationBar(
|
||||||
|
modifier = modifier,
|
||||||
|
containerColor = containerColor,
|
||||||
|
contentColor = contentColor,
|
||||||
|
tonalElevation = tonalElevation,
|
||||||
|
windowInsets = windowInsets,
|
||||||
|
content = content
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
object ElementNavigationBarDefaults {
|
||||||
|
val containerColor: Color
|
||||||
|
@Composable get() = if (ElementTheme.isLightTheme) {
|
||||||
|
ElementTheme.colors.bgSubtlePrimary
|
||||||
|
} else {
|
||||||
|
ElementTheme.colors.textOnSolidPrimary
|
||||||
|
}
|
||||||
|
|
||||||
|
val tonalElevation: Dp = NavigationBarDefaults.Elevation
|
||||||
|
|
||||||
|
val windowInsets: WindowInsets
|
||||||
|
@Composable get() = NavigationBarDefaults.windowInsets
|
||||||
|
}
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
fun RowScope.NavigationBarItem(
|
||||||
|
selected: Boolean,
|
||||||
|
onClick: () -> Unit,
|
||||||
|
icon: @Composable () -> Unit,
|
||||||
|
modifier: Modifier = Modifier,
|
||||||
|
enabled: Boolean = true,
|
||||||
|
label: @Composable (() -> Unit)? = null,
|
||||||
|
alwaysShowLabel: Boolean = true,
|
||||||
|
colors: NavigationBarItemColors = ElementNavigationBarItemDefaultsDefaults.colors(),
|
||||||
|
interactionSource: MutableInteractionSource? = null
|
||||||
|
) {
|
||||||
|
MaterialNavigationBarItem(
|
||||||
|
selected = selected,
|
||||||
|
onClick = onClick,
|
||||||
|
icon = icon,
|
||||||
|
modifier = modifier,
|
||||||
|
enabled = enabled,
|
||||||
|
label = label,
|
||||||
|
alwaysShowLabel = alwaysShowLabel,
|
||||||
|
colors = colors,
|
||||||
|
interactionSource = interactionSource,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
object ElementNavigationBarItemDefaultsDefaults {
|
||||||
|
@Composable
|
||||||
|
fun colors() = NavigationBarItemDefaults.colors().copy(
|
||||||
|
selectedIconColor = ElementTheme.colors.iconPrimary,
|
||||||
|
selectedTextColor = ElementTheme.colors.textPrimary,
|
||||||
|
unselectedIconColor = ElementTheme.colors.iconTertiary,
|
||||||
|
unselectedTextColor = ElementTheme.colors.textDisabled,
|
||||||
|
selectedIndicatorColor = Color.Transparent,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
fun NavigationBarIcon(
|
||||||
|
imageVector: ImageVector,
|
||||||
|
count: Int,
|
||||||
|
isCritical: Boolean,
|
||||||
|
) {
|
||||||
|
Box {
|
||||||
|
Icon(
|
||||||
|
imageVector = imageVector,
|
||||||
|
contentDescription = null,
|
||||||
|
)
|
||||||
|
CounterAtom(
|
||||||
|
modifier = Modifier.offset(11.dp, (-11).dp),
|
||||||
|
textStyle = ElementTheme.typography.fontBodyXsMedium,
|
||||||
|
count = count,
|
||||||
|
isCritical = isCritical,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Composable
|
||||||
|
fun NavigationBarText(
|
||||||
|
text: String,
|
||||||
|
) {
|
||||||
|
Text(
|
||||||
|
text = text,
|
||||||
|
style = ElementTheme.typography.fontBodySmMedium,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Preview(group = PreviewGroup.AppBars)
|
||||||
|
@Composable
|
||||||
|
internal fun NavigationBarPreview() = ElementThemedPreview {
|
||||||
|
NavigationBar {
|
||||||
|
NavigationBarItem(
|
||||||
|
icon = {
|
||||||
|
NavigationBarIcon(
|
||||||
|
imageVector = CompoundIcons.ChatSolid(),
|
||||||
|
count = 5,
|
||||||
|
isCritical = false,
|
||||||
|
)
|
||||||
|
},
|
||||||
|
label = {
|
||||||
|
NavigationBarText(
|
||||||
|
text = "Chats"
|
||||||
|
)
|
||||||
|
},
|
||||||
|
selected = true,
|
||||||
|
onClick = {},
|
||||||
|
)
|
||||||
|
NavigationBarItem(
|
||||||
|
icon = {
|
||||||
|
NavigationBarIcon(
|
||||||
|
imageVector = CompoundIcons.ChatSolid(),
|
||||||
|
count = 5,
|
||||||
|
isCritical = true,
|
||||||
|
)
|
||||||
|
},
|
||||||
|
label = {
|
||||||
|
NavigationBarText(
|
||||||
|
text = "Teams"
|
||||||
|
)
|
||||||
|
},
|
||||||
|
selected = false,
|
||||||
|
onClick = {},
|
||||||
|
)
|
||||||
|
NavigationBarItem(
|
||||||
|
icon = {
|
||||||
|
NavigationBarIcon(
|
||||||
|
imageVector = CompoundIcons.ChatSolid(),
|
||||||
|
count = 0,
|
||||||
|
isCritical = false,
|
||||||
|
)
|
||||||
|
},
|
||||||
|
label = {
|
||||||
|
NavigationBarText(
|
||||||
|
text = "Other"
|
||||||
|
)
|
||||||
|
},
|
||||||
|
selected = false,
|
||||||
|
onClick = {},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -34,6 +34,8 @@ class KonsistComposableTest {
|
||||||
// Add some exceptions...
|
// Add some exceptions...
|
||||||
"InvisibleButton",
|
"InvisibleButton",
|
||||||
"OutlinedButton",
|
"OutlinedButton",
|
||||||
|
"NavigationBarIcon",
|
||||||
|
"NavigationBarText",
|
||||||
"SimpleAlertDialogContent",
|
"SimpleAlertDialogContent",
|
||||||
"TextButton",
|
"TextButton",
|
||||||
)
|
)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue