Skip to content

NavigationBar

NavigationBar is a bottom navigation bar component in COUI, used to create navigation menus fixed at the bottom of applications. It supports 2 to 5 navigation items, offering different display modes (icon only, text only, icon and text, icon with selected label).

FloatingNavigationBar is a floating-style bottom navigation bar component, also supporting 2 to 5 navigation items, showing icons only.

These components are typically used in conjunction with the Scaffold component to maintain consistent layout and behavior across different pages in the application.

Import

kotlin
import io.github.suqi8.coui.kmp.basic.NavigationBar
import io.github.suqi8.coui.kmp.basic.NavigationBarItem
import io.github.suqi8.coui.kmp.basic.FloatingNavigationBar
import io.github.suqi8.coui.kmp.basic.FloatingNavigationBarItem
import io.github.suqi8.coui.kmp.basic.NavigationBarDisplayMode
import io.github.suqi8.coui.kmp.basic.NavigationItem

Basic Usage

The NavigationBar component can be used to create bottom navigation menus fixed to the bottom:

kotlin
var selectedIndex by remember { mutableStateOf(0) }
val items = listOf("Home", "Profile", "Settings")
val icons = listOf(COUIIcons.VerticalSplit, COUIIcons.Contacts, COUIIcons.Settings)

Scaffold(
    bottomBar = {
        NavigationBar {
            items.forEachIndexed { index, label ->
                NavigationBarItem(
                    selected = selectedIndex == index,
                    onClick = { selectedIndex = index },
                    icon = icons[index],
                    label = label
                )
            }
        }
    }
)

FloatingNavigationBar

The FloatingNavigationBar component can be used to create floating navigation menus at the bottom:

kotlin
var selectedIndex by remember { mutableStateOf(0) }
val items = listOf("Home", "Profile", "Settings")
val icons = listOf(COUIIcons.VerticalSplit, COUIIcons.Contacts, COUIIcons.Settings)

Scaffold(
    bottomBar = {
        FloatingNavigationBar {
            items.forEachIndexed { index, label ->
                FloatingNavigationBarItem(
                    selected = selectedIndex == index,
                    onClick = { selectedIndex = index },
                    icon = icons[index],
                    label = label
                )
            }
        }
    }
)

Component States

Selected State

NavigationBarItem follows the COUI tab navigation state model: the selected item shows the icon and label at full label color, unselected items are dimmed, and the icon cross-fades between the two states over 180ms while the label color switches instantly. Labels always use medium font weight; pressing an unselected item previews the selected icon color. FloatingNavigationBarItem highlights the icon when selected.

Properties

Property NameTypeDescriptionDefault ValueRequired
modifierModifierModifier applied to the nav barModifierNo
colorColorBackground color of the nav barCOUITheme.colorScheme.backgroundNo
showDividerBooleanShow top divider line or nottrueNo
defaultWindowInsetsPaddingBooleanApply default window insets paddingtrueNo
modeNavigationBarDisplayModeDisplay mode for itemsNavigationBarDisplayMode.IconAndTextNo
content@Composable RowScope.()The content of the nav bar-Yes
Property NameTypeDescriptionDefault ValueRequired
selectedBooleanWhether the item is selected-Yes
onClick() -> UnitCallback when the item is clicked-Yes
iconImageVectorIcon of the item-Yes
labelStringLabel of the item-Yes
modifierModifierModifier applied to the itemModifierNo
enabledBooleanWhether the item is enabledtrueNo

FloatingNavigationBar Properties

Property NameTypeDescriptionDefault ValueRequired
modifierModifierModifier applied to the nav barModifierNo
colorColorBackground color of the nav barCOUITheme.colorScheme.surfaceContainerNo
cornerRadiusDpCorner radius of the nav barFloatingToolbarDefaults.CornerRadiusNo
horizontalAlignmentAlignment.HorizontalHorizontal alignment within its parentCenterHorizontallyNo
horizontalOutSidePaddingDpHorizontal padding outside the nav barFloatingNavigationBarDefaults.HorizontalOutSidePaddingNo
shadowElevationDpThe shadow elevation of the nav barFloatingNavigationBarDefaults.ShadowElevationNo
showDividerBooleanShow divider line around the nav barfalseNo
defaultWindowInsetsPaddingBooleanApply default window insets paddingtrueNo
content@Composable () -> UnitThe content of the nav bar-Yes

FloatingNavigationBarItem Properties

Property NameTypeDescriptionDefault ValueRequired
selectedBooleanWhether the item is selected-Yes
onClick() -> UnitCallback when the item is clicked-Yes
iconImageVectorIcon of the item-Yes
labelStringLabel of the item-Yes
modifierModifierModifier applied to the itemModifierNo
enabledBooleanWhether the item is enabledtrueNo

The NavigationBarDefaults object provides default values for NavigationBar and NavigationBarItem components.

Constants

Constant NameTypeDescriptionDefault Value
ItemHeightDpItem cell height56.dp
HorizontalPaddingDpHorizontal padding at both edges of the bar12.dp
ItemHorizontalPaddingDpHorizontal padding inside each item cell2.dp
IconSizeDpIcon size24.dp
IconTopPaddingDpTop padding for the icon9.dp
LabelBottomPaddingDpBottom padding for the label7.dp
LabelFontSizeTextUnitLabel font size10.sp
TextFontSizeTextUnitText font size (TextOnly mode)14.sp
IconFadeDurationMillisIntIcon cross-fade duration between states180

FloatingNavigationBarDefaults Object

The FloatingNavigationBarDefaults object provides default values for FloatingNavigationBar and FloatingNavigationBarItem components.

Constants

Constant NameTypeDescriptionDefault Value
HorizontalOutSidePaddingDpHorizontal outside padding36.dp
ShadowElevationDpShadow elevation1.dp
HorizontalPaddingDpHorizontal padding inside the bar12.dp
ItemSpacingDpSpacing between items12.dp
IconSizeDpIcon size28.dp
IconPaddingDpPadding around the icon10.dp
SelectedPressedAlphaFloatAlpha for selected pressed item0.5f
UnselectedPressedAlphaFloatAlpha for unselected pressed item0.6f
UnselectedAlphaFloatAlpha for unselected item0.4f
ValueDescription
IconAndTextShow both icon and text
IconOnlyShow icon only
TextOnlyShow text only
IconWithSelectedLabelShow icon always, show text only when selected

The display mode set on NavigationBar is delivered to its items through the LocalNavigationBarDisplayMode composition local. FloatingNavigationBar has no mode parameter — its items always show icons only.

NavigationItem is a convenience data class for holding a navigation item's label and icon.

Property NameTypeDescriptionDefault ValueRequired
labelStringLabel of the item-Yes
iconImageVectorIcon of the item-Yes

Advanced Usage

Custom Colors

kotlin
NavigationBar(
    color = Color.Red.copy(alpha = 0.3f)
) {
    // ... items ...
}

Without Divider

kotlin
NavigationBar(
    showDivider = false
) {
    // ... items ...
}

Handling Window Insets

kotlin
NavigationBar(
    defaultWindowInsetsPadding = false // Handle window insets padding manually
) {
    // ... items ...
}

Display Modes

kotlin
NavigationBar(
    // IconAndText (default) / IconOnly / TextOnly / IconWithSelectedLabel
    mode = NavigationBarDisplayMode.IconWithSelectedLabel
) {
    // ... items ...
}

FloatingNavigationBar

Custom Color and Corner Radius

kotlin
FloatingNavigationBar(
    color = COUITheme.colorScheme.primaryContainer,
    cornerRadius = 28.dp
) {
    // ... items ...
}

Custom Alignment and Padding

kotlin
FloatingNavigationBar(
    horizontalAlignment = Alignment.Start, // Align to start
    horizontalOutSidePadding = 16.dp // Set outside padding
) {
    // ... items ...
}

Divider and Shadow

kotlin
FloatingNavigationBar(
    showDivider = true, // Draw a thin divider ring around the bar
    shadowElevation = 0.dp // Disable the drop shadow
) {
    // ... items ...
}

Using with Page Navigation (Using Scaffold)

Using NavigationBar

kotlin
val pages = listOf("Home", "Profile", "Settings")
val icons = listOf(COUIIcons.VerticalSplit, COUIIcons.Contacts, COUIIcons.Settings)
var selectedIndex by remember { mutableStateOf(0) }

Scaffold(
    bottomBar = {
        NavigationBar {
            pages.forEachIndexed { index, label ->
                NavigationBarItem(
                    selected = selectedIndex == index,
                    onClick = { selectedIndex = index },
                    icon = icons[index],
                    label = label
                )
            }
        }
    }
) { paddingValues ->
    // Content area needs to consider padding
    Box(
        modifier = Modifier
            .fillMaxSize()
            .padding(paddingValues),
        contentAlignment = Alignment.Center
    ) {
        Text(
            text = "Current Page: ${pages[selectedIndex]}",
            style = COUITheme.textStyles.title1
        )
    }
}

Using FloatingNavigationBar

kotlin
val pages = listOf("Home", "Profile", "Settings")
val icons = listOf(COUIIcons.VerticalSplit, COUIIcons.Contacts, COUIIcons.Settings)
var selectedIndex by remember { mutableStateOf(0) }

Scaffold(
    bottomBar = {
        FloatingNavigationBar {
            pages.forEachIndexed { index, label ->
                FloatingNavigationBarItem(
                    selected = selectedIndex == index,
                    onClick = { selectedIndex = index },
                    icon = icons[index],
                    label = label
                )
            }
        }
    }
) { paddingValues ->
    // Content area needs to consider padding
    Box(
        modifier = Modifier
            .fillMaxSize()
            .padding(paddingValues),
        contentAlignment = Alignment.Center
    ) {
        Text(
            text = "Current Page: ${pages[selectedIndex]}",
            style = COUITheme.textStyles.title1
        )
    }
}

Changelog

Released under the Apache-2.0 License