Avatars
Avatars

Initials fallback

A fallback avatar that derives initials and a stable color from a name.

DK
RP
MN

Installation

caveui components are copy-paste Jetpack Compose built entirely on Material 3 — there's no caveui dependency to add. Make sure Material 3 is on your classpath (it ships with the Compose BOM), then copy the Usage snippet below into your project.

kotlin
// build.gradle.kts (module)
dependencies {
    implementation(platform("androidx.compose:compose-bom:2025.06.00"))
    implementation("androidx.compose.material3:material3")
}

Usage

kotlin
@Composable
fun InitialsAvatar(name: String) {
    val palette = listOf(
        Color(0xFF6366F1), Color(0xFF0EA5E9), Color(0xFF10B981),
        Color(0xFFF59E0B), Color(0xFFEF4444), Color(0xFFEC4899),
    )
    val color = remember(name) {
        palette[name.hashCode().absoluteValue % palette.size]
    }
    val initials = remember(name) {
        name.trim().split(" ")
            .mapNotNull { it.firstOrNull()?.uppercase() }
            .take(2)
            .joinToString("")
    }
    Box(
        Modifier.size(48.dp).clip(CircleShape).background(color),
        contentAlignment = Alignment.Center,
    ) {
        Text(initials, color = Color.White, style = MaterialTheme.typography.titleSmall)
    }
}