Skip to content

Button

Button is a basic interactive component in COUI, used to trigger actions or events. It provides multiple style options, including primary buttons, secondary buttons, and text buttons.

Import

kotlin
import io.github.suqi8.coui.kmp.basic.Button

Basic Usage

The Button component can be used to trigger actions or events:

kotlin
Button(
    onClick = { /* Handle click event */ }
) {
    Text("Button")
}

Button Types

COUI provides various types of buttons suitable for different scenarios and levels of importance:

Primary Button

kotlin
Button(
    onClick = { /* Handle click event */ },
    colors = ButtonDefaults.buttonColorsPrimary()
) {
    Text("Primary Button")
}

Secondary Button

kotlin
Button(
    onClick = { /* Handle click event */ },
    colors = ButtonDefaults.buttonColors()
) {
    Text("Secondary Button")
}

Text Button

kotlin
TextButton(
    text = "Text Button",
    onClick = { /* Handle click event */ }
)

Borderless Text Button

Corresponds to COUI Widget.COUI.Button.Large.Borderless / Translate: no fill, primary-tinted label.

kotlin
TextButton(
    text = "Borderless Button",
    onClick = { /* Handle click event */ },
    colors = ButtonDefaults.textButtonColorsBorderless()
)

Small Size Tier

Pass the Small metrics from ButtonDefaults together with a 14sp text style to get the COUI small size tier (Widget.COUI.Button.Small):

kotlin
TextButton(
    text = "Small Button",
    onClick = { /* Handle click event */ },
    cornerRadius = ButtonDefaults.CornerRadiusSmall,
    minWidth = ButtonDefaults.MinWidthSmall,
    minHeight = ButtonDefaults.MinHeightSmall,
    insideMargin = ButtonDefaults.InsideMarginSmall,
    textStyle = COUITheme.textStyles.button.copy(fontSize = 14.sp)
)

Component States

Disabled State

kotlin
Button(
    onClick = { /* Handle click event */ },
    enabled = false
) {
    Text("Disabled Button")
}

Properties

Button Properties

Property NameTypeDescriptionDefault ValueRequired
onClick() -> UnitCallback triggered on click-Yes
modifierModifierModifier applied to the buttonModifierNo
enabledBooleanWhether the button is clickabletrueNo
pressScaleEnabledBooleanWhether the button shrinks while pressed (COUIButton scaleEnable). Set to false for buttons that fill a container cell, such as a dialog button bar, so the only press feedback is the full-cell press tinttrueNo
cornerRadiusDpCorner radius of the buttonButtonDefaults.CornerRadiusNo
minWidthDpMinimum width of the buttonButtonDefaults.MinWidthNo
minHeightDpMinimum height of the buttonButtonDefaults.MinHeightNo
colorsButtonColorsButton color configurationButtonDefaults.buttonColors()No
insideMarginPaddingValuesInternal padding of the buttonButtonDefaults.InsideMarginNo
interactionSourceMutableInteractionSource?Interaction source for the buttonnullNo
indicationIndication?Indication for click interactions; null because the COUI press feedback (scale + press tint) is built innullNo
content@Composable RowScope.() -> UnitComposable function for button content-Yes

TextButton Properties

Property NameTypeDescriptionDefault ValueRequired
textStringText displayed on the button-Yes
onClick() -> UnitCallback triggered on click-Yes
modifierModifierModifier applied to the buttonModifierNo
enabledBooleanWhether the button is clickabletrueNo
pressScaleEnabledBooleanWhether the button shrinks while pressed (COUIButton scaleEnable). Set to false for buttons that fill a container cell, such as a dialog button bartrueNo
colorsTextButtonColorsText button color configurationButtonDefaults.textButtonColors()No
cornerRadiusDpCorner radius of the buttonButtonDefaults.CornerRadiusNo
minWidthDpMinimum width of the buttonButtonDefaults.MinWidthNo
minHeightDpMinimum height of the buttonButtonDefaults.MinHeightNo
insideMarginPaddingValuesInternal padding of the buttonButtonDefaults.InsideMarginNo
textStyleTextStyleText style of the label (pass a 14sp style with the Small metrics for the COUI small size tier)COUITheme.textStyles.buttonNo
interactionSourceMutableInteractionSource?Interaction source for the buttonnullNo
indicationIndication?Indication for click interactions; null because the COUI press feedback (scale + press tint) is built innullNo

ButtonDefaults Object

The ButtonDefaults object provides default values and color configurations for button components.

Constants

Constant NameTypeDescriptionDefault Value
PressedScaleFloatSmallest scale a button shrinks to while pressed (surfaces up to 48 x 48dp)0.92f
PressedBrightnessFloatLegacy COUIButton brightness value; retained for source compatibility only, no longer used0.8f
MinWidthDpMinimum width of the button58.dp
MinHeightDpMinimum height of the button44.dp
CornerRadiusDpCorner radius of the button22.dp
InsideMarginPaddingValuesInternal padding of the buttonPaddingValues(horizontal = 12.dp, vertical = 0.dp)
MinWidthSmallDpMinimum width of the small size tier (COUI Widget.COUI.Button.Small)52.dp
MinHeightSmallDpMinimum height of the small size tier28.dp
CornerRadiusSmallDpCorner radius of the small size tier (height-derived capsule)14.dp
InsideMarginSmallPaddingValuesInternal padding of the small size tierPaddingValues(horizontal = 12.dp, vertical = 4.dp)

Methods

Method NameTypeDescription
buttonColors()ButtonColorsCreates color configuration for secondary buttons
buttonColorsPrimary()ButtonColorsCreates color configuration for primary buttons
textButtonColors()TextButtonColorsCreates color configuration for secondary text buttons
textButtonColorsPrimary()TextButtonColorsCreates color configuration for primary text buttons
textButtonColorsBorderless()TextButtonColorsCreates color configuration for borderless / text buttons (transparent fill, themed label)

Advanced Usage

Button with Icon

kotlin
Button(
    onClick = { /* Handle click event */ }
) {
    Icon(
        imageVector = COUIIcons.Favorites,
        contentDescription = "Favorites"
    )
    Spacer(modifier = Modifier.width(8.dp))
    Text("Button with Icon")
}

Custom Style Button

kotlin
Button(
    onClick = { /* Handle click event */ },
    colors = ButtonDefaults.buttonColors(
        color = Color.Red.copy(alpha = 0.7f)
    ),
    cornerRadius = 8.dp
) {
    Text("Custom Button")
}

Loading State Button

kotlin
var isLoading by remember { mutableStateOf(false) }
val scope = rememberCoroutineScope()

Button(
    onClick = {
        isLoading = true
        // Simulate operation
        scope.launch {
            delay(2000)
            isLoading = false
        }
    },
    enabled = !isLoading
) {
     AnimatedVisibility(
        visible = isLoading
    ) {
        CircularProgressIndicator(
            modifier = Modifier.padding(end = 8.dp),
            size = 20.dp,
            strokeWidth = 4.dp
        )
    }
    Text("Submit")
}

Changelog

Released under the Apache-2.0 License