Composables
Docs Login
Vela

Icon 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.size
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 androidx.compose.ui.semantics.contentDescription
import androidx.compose.ui.semantics.semantics
import androidx.compose.ui.unit.dp
import com.composables.compose.ripple.rememberRippleIndication
import com.composables.vela.theme.LocalContentColor
import com.composables.vela.theme.ProvideContentColor
import com.composables.vela.theme.Theme
import com.composeunstyled.UnstyledButton

enum class IconButtonVariant {
    Ghost,
    Filled,
    Tonal,
}

enum class IconButtonSize {
    Small,
    Medium,
}

@Composable
fun IconButton(
    onClick: () -> Unit,
    contentDescription: String,
    modifier: Modifier = Modifier,
    enabled: Boolean = true,
    variant: IconButtonVariant = IconButtonVariant.Ghost,
    size: IconButtonSize = IconButtonSize.Medium,
    content: @Composable () -> Unit,
) {
    val (container, contentColor) =
        when {
            variant == IconButtonVariant.Filled && !enabled ->
                Theme.colors.surfaceVariant to Theme.colors.onBackgroundMuted
            variant == IconButtonVariant.Filled -> Theme.colors.primary to Theme.colors.onPrimary
            variant == IconButtonVariant.Tonal ->
                Theme.colors.surfaceVariant to Theme.colors.onSurfaceVariant
            else -> Color.Transparent to LocalContentColor.current
        }
    UnstyledButton(
        onClick = onClick,
        enabled = enabled,
        modifier =
            modifier
                .alpha(if (enabled || variant == IconButtonVariant.Filled) 1f else 0.4f)
                .size(if (size == IconButtonSize.Small) 32.dp else 40.dp)
                .clip(Theme.shapes.fullyRounded)
                .background(container)
                .semantics { this.contentDescription = contentDescription },
        indication = rememberRippleIndication(color = contentColor),
        interactionSource = remember { MutableInteractionSource() },
    ) {
        ProvideContentColor(contentColor, 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.