Composables
Docs Login
Vela

Button

Jetpack Compose Component

Download
package com.composables.vela.components

import androidx.compose.foundation.background
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import com.composables.compose.ripple.rememberRippleIndication
import com.composables.vela.theme.ProvideContentColor
import com.composables.vela.theme.ProvideTextStyle
import com.composables.vela.theme.Space
import com.composables.vela.theme.Theme
import com.composeunstyled.UnstyledButton

enum class ButtonVariant {
    Primary,
    Secondary,
    Destructive,
    Ghost,
}

@Composable
fun Button(
    onClick: () -> Unit,
    modifier: Modifier = Modifier,
    enabled: Boolean = true,
    variant: ButtonVariant = ButtonVariant.Primary,
    content: @Composable () -> Unit,
) {
    val (container, contentColor) =
        when (variant) {
            ButtonVariant.Secondary -> Theme.colors.surfaceVariant to Theme.colors.onSurfaceVariant
            ButtonVariant.Destructive -> Theme.colors.destructive to Theme.colors.onDestructive
            ButtonVariant.Ghost -> Color.Transparent to Theme.colors.primary
            else -> Theme.colors.primary to Theme.colors.onPrimary
        }
    UnstyledButton(
        onClick = onClick,
        enabled = enabled,
        modifier =
            modifier
                .alpha(if (enabled) 1f else 0.4f)
                .clip(Theme.shapes.fullyRounded)
                .background(container),
        contentPadding = PaddingValues(horizontal = Space * 5, vertical = Space * 3),
        indication = rememberRippleIndication(color = contentColor),
        interactionSource = remember { MutableInteractionSource() },
    ) {
        ProvideContentColor(contentColor) { ProvideTextStyle(Theme.typography.label, content) }
    }
}

More components from Vela

by Alex Styl

Search screens and components

Screen flows

Screens

UI Elements

One Android lesson, every Wednesday

Building Android apps?

What Alex learned building Android apps for 14 years, so you don't have to. Reads in under a minute.

Join 3,500+ Android developers. Unsubscribe any time.

You're subscribed

The next lesson lands in on Wednesday.