AlertDialogPreview

Embed
<script src="https://composables.com/embed/@alexstyl/components/d67709230ef08266efeaeb01.js"></script>
AlertDialogPreview.kt
import androidx.compose.animation.core.CubicBezierEasing
import androidx.compose.animation.core.tween
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.animation.scaleIn
import androidx.compose.animation.scaleOut
import androidx.compose.foundation.background
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.heightIn
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.widthIn
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.dropShadow
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.graphics.TransformOrigin
import androidx.compose.ui.graphics.isSpecified
import androidx.compose.ui.graphics.shadow.Shadow
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.DpOffset
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.composables.compose.ripple.rememberRippleIndication
import com.composeunstyled.DialogPanel
import com.composeunstyled.LocalTextStyle
import com.composeunstyled.ProvideContentColor
import com.composeunstyled.ProvideTextStyle
import com.composeunstyled.Scrim
import com.composeunstyled.Text
import com.composeunstyled.UnstyledButton
import com.composeunstyled.UnstyledDialog
import com.composeunstyled.theme.ColorScheme
import com.composeunstyled.theme.Theme
import com.composeunstyled.theme.ThemeProperty
import com.composeunstyled.theme.ThemeToken
import com.composeunstyled.theme.buildThemeV2

private const val DialogEnterDurationMillis = 400
private const val DialogExitDurationMillis = 200
private const val DialogEnterFadeDurationMillis = 150
private const val DialogExitFadeDurationMillis = 100

private val EmphasizedDecelerateEasing = CubicBezierEasing(0.05f, 0.7f, 0.1f, 1f)
private val EmphasizedAccelerateEasing = CubicBezierEasing(0.3f, 0f, 0.8f, 0.15f)

@Composable
fun AlertDialog(
    visible: Boolean,
    onDismissRequest: () -> Unit,
    modifier: Modifier = Modifier,
    icon: (@Composable () -> Unit)? = null,
    title: (@Composable () -> Unit)? = null,
    text: @Composable () -> Unit,
    positiveButton: @Composable () -> Unit,
    neutralButton: (@Composable () -> Unit)? = null,
    negativeButton: (@Composable () -> Unit)? = null,
    shape: Shape = AlertDialogShape,
    backgroundColor: Color = Color.Unspecified,
    contentColor: Color = Color.Unspecified,
    supportingTextColor: Color = Color.Unspecified,
    shadow: Shadow? = null,
) {
    AlertDialogPanel(
        visible = visible,
        onDismissRequest = onDismissRequest,
        modifier = modifier,
        paneTitle = "Alert dialog",
        shape = shape,
        backgroundColor = backgroundColor.orThemeValue(dialogPanelColor),
        shadow = shadow ?: Theme[dialogShadows][dialogOverlayShadow],
        contentPadding = PaddingValues(24.dp),
    ) {
        AlertDialogContent(
            icon = icon,
            title = title,
            text = text,
            positiveButton = positiveButton,
            neutralButton = neutralButton,
            negativeButton = negativeButton,
            contentColor = contentColor.orThemeValue(dialogOnPanelColor),
            supportingTextColor = supportingTextColor.orThemeValue(dialogMutedColor),
        )
    }
}

@Composable
fun AlertDialog(
    visible: Boolean,
    onDismissRequest: () -> Unit,
    modifier: Modifier = Modifier,
    paneTitle: String = "Alert dialog",
    shape: Shape = AlertDialogShape,
    backgroundColor: Color = Color.Unspecified,
    contentColor: Color = Color.Unspecified,
    contentPadding: PaddingValues = PaddingValues(24.dp),
    shadow: Shadow? = null,
    content: @Composable () -> Unit,
) {
    AlertDialogPanel(
        visible = visible,
        onDismissRequest = onDismissRequest,
        modifier = modifier,
        paneTitle = paneTitle,
        shape = shape,
        backgroundColor = backgroundColor.orThemeValue(dialogPanelColor),
        shadow = shadow ?: Theme[dialogShadows][dialogOverlayShadow],
        contentPadding = contentPadding,
    ) {
        ProvideContentColor(contentColor.orThemeValue(dialogOnPanelColor)) { content() }
    }
}

@Composable
private fun AlertDialogContent(
    icon: (@Composable () -> Unit)?,
    title: (@Composable () -> Unit)?,
    text: @Composable () -> Unit,
    positiveButton: @Composable () -> Unit,
    neutralButton: (@Composable () -> Unit)?,
    negativeButton: (@Composable () -> Unit)?,
    contentColor: Color,
    supportingTextColor: Color,
) {
    ProvideContentColor(contentColor) {
        Column(
            modifier = Modifier.fillMaxWidth(),
            horizontalAlignment = Alignment.CenterHorizontally,
            verticalArrangement = Arrangement.spacedBy(16.dp),
        ) {
            if (icon != null) {
                Box(Modifier.fillMaxWidth(), contentAlignment = Alignment.Center) {
                    Box(Modifier.size(24.dp)) { icon() }
                }
            }

            if (title != null) {
                ProvideTextStyle(LocalTextStyle.current.merge(AlertDialogTitleTextStyle)) {
                    Box(Modifier.fillMaxWidth(), contentAlignment = Alignment.Center) { title() }
                }
            }

            Box(
                modifier =
                    Modifier
                        .fillMaxWidth()
                        .weight(1f, fill = false)
                        .verticalScroll(rememberScrollState()),
                contentAlignment = Alignment.Center,
            ) {
                ProvideContentColor(supportingTextColor) {
                    ProvideTextStyle(LocalTextStyle.current.merge(AlertDialogBodyTextStyle)) { text() }
                }
            }

            Column(
                modifier = Modifier.fillMaxWidth(),
                verticalArrangement = Arrangement.spacedBy(8.dp),
            ) {
                positiveButton()
                neutralButton?.invoke()
                negativeButton?.invoke()
            }
        }
    }
}

@Composable
private fun AlertDialogPanel(
    visible: Boolean,
    onDismissRequest: () -> Unit,
    modifier: Modifier,
    paneTitle: String,
    shape: Shape,
    backgroundColor: Color,
    shadow: Shadow,
    contentPadding: PaddingValues,
    content: @Composable () -> Unit,
) {
    UnstyledDialog(
        visible = visible,
        onDismissRequest = onDismissRequest,
        overlay = {
            Scrim(
                scrimColor = Theme[colors][dialogScrimColor],
                enter = fadeIn(
                    tween(
                        DialogEnterFadeDurationMillis,
                        easing = EmphasizedDecelerateEasing
                    )
                ),
                exit = fadeOut(
                    tween(
                        DialogExitFadeDurationMillis,
                        easing = EmphasizedAccelerateEasing
                    )
                ),
            )
        },
    ) {
        Box(
            modifier = Modifier
                .fillMaxSize()
                .padding(horizontal = 24.dp),
            contentAlignment = Alignment.Center,
        ) {
            DialogPanel(
                modifier =
                    modifier
                        .widthIn(min = 280.dp, max = 560.dp)
                        .fillMaxWidth()
                        .heightIn(max = 560.dp)
                        .dropShadow(shape, shadow)
                        .clip(shape)
                        .border(Dp.Hairline, Theme[colors][dialogOutlineColor], shape)
                        .background(backgroundColor, shape)
                        .padding(contentPadding),
                paneTitle = paneTitle,
                enter =
                    scaleIn(
                        initialScale = 0.92f,
                        transformOrigin = TransformOrigin.Center,
                        animationSpec = tween(
                            DialogEnterDurationMillis,
                            easing = EmphasizedDecelerateEasing
                        ),
                    ) + fadeIn(
                        tween(
                            DialogEnterFadeDurationMillis,
                            easing = EmphasizedDecelerateEasing
                        )
                    ),
                exit =
                    scaleOut(
                        targetScale = 0.92f,
                        transformOrigin = TransformOrigin.Center,
                        animationSpec = tween(
                            DialogExitDurationMillis,
                            easing = EmphasizedAccelerateEasing
                        ),
                    ) + fadeOut(
                        tween(
                            DialogExitFadeDurationMillis,
                            easing = EmphasizedAccelerateEasing
                        )
                    ),
                content = content,
            )
        }
    }
}

@Composable
fun PrimaryButton(
    label: String,
    onClick: () -> Unit,
    modifier: Modifier = Modifier,
) {
    UnstyledButton(
        onClick = onClick,
        contentPadding = PaddingValues(horizontal = 16.dp, vertical = 10.dp),
        indication = rememberRippleIndication(Color.White),
        modifier = modifier
            .clip(AlertDialogButtonShape)
            .background(Theme[colors][dialogPrimaryColor]),
    ) {
        Text(
            label,
            style = AlertDialogButtonTextStyle.copy(color = Theme[colors][dialogOnPrimaryColor])
        )
    }
}

@Composable
fun GhostButton(
    label: String,
    onClick: () -> Unit,
    modifier: Modifier = Modifier,
) {
    UnstyledButton(
        onClick = onClick,
        contentPadding = PaddingValues(horizontal = 16.dp, vertical = 10.dp),
        modifier = modifier.clip(AlertDialogButtonShape),
        indication = rememberRippleIndication(Theme[colors][dialogPrimaryColor]),
    ) {
        Text(
            label,
            style = AlertDialogButtonTextStyle.copy(color = Theme[colors][dialogPrimaryColor])
        )
    }
}

@Composable
private fun Color.orThemeValue(token: ThemeToken<Color>): Color {
    return if (isSpecified) this else Theme[colors][token]
}

private val colors = ThemeProperty<Color>("alertDialogColors")
private val dialogBackgroundColor = ThemeToken<Color>("background")
private val dialogPanelColor = ThemeToken<Color>("panel")
private val dialogOnPanelColor = ThemeToken<Color>("onPanel")
private val dialogMutedColor = ThemeToken<Color>("muted")
private val dialogPrimaryColor = ThemeToken<Color>("primary")
private val dialogOutlineColor = ThemeToken<Color>("outline")
private val dialogOnPrimaryColor = ThemeToken<Color>("onPrimary")
private val dialogScrimColor = ThemeToken<Color>("scrim")

private val dialogShadows = ThemeProperty<Shadow>("alertDialogShadows")
private val dialogOverlayShadow = ThemeToken<Shadow>("overlay")

val AlertDialogTheme = buildThemeV2 {
    defaultIndication = rememberRippleIndication()
    properties[colors] =
        mapOf(
            dialogBackgroundColor to Color(0xFFF5F5F5),
            dialogPanelColor to Color.White,
            dialogOnPanelColor to Color(0xFF171717),
            dialogMutedColor to Color(0xFF737373),
            dialogPrimaryColor to Color(0xFF171717),
            dialogOnPrimaryColor to Color.White,
            dialogScrimColor to Color.Black.copy(alpha = 0.33f),
            dialogOutlineColor to Color.Black.copy(0.33f)
        )
    properties[dialogShadows] = mapOf(dialogOverlayShadow to AlertDialogLightShadow)

    colorScheme(ColorScheme.Dark) {
        properties[colors] =
            mapOf(
                dialogBackgroundColor to Color(0xFF0A0A0A),
                dialogPanelColor to Color(0xFF171717),
                dialogOnPanelColor to Color(0xFFF5F5F5),
                dialogMutedColor to Color(0xFFA3A3A3),
                dialogPrimaryColor to Color(0xFFF5F5F5),
                dialogOnPrimaryColor to Color(0xFF171717),
                dialogScrimColor to Color.Black.copy(alpha = 0.64f),
            )
        properties[dialogShadows] = mapOf(dialogOverlayShadow to AlertDialogDarkShadow)
    }
}

private val AlertDialogShape = RoundedCornerShape(16.dp)
private val AlertDialogButtonShape = RoundedCornerShape(10.dp)
private val AlertDialogLightShadow =
    Shadow(
        radius = 24.dp,
        color = Color.Black,
        offset = DpOffset(0.dp, 8.dp),
        alpha = 0.16f,
    )
private val AlertDialogDarkShadow =
    Shadow(
        radius = 24.dp,
        color = Color.Black,
        offset = DpOffset(0.dp, 8.dp),
        alpha = 0.48f,
    )
private val AlertDialogTitleTextStyle =
    TextStyle(fontSize = 20.sp, lineHeight = 24.sp, fontWeight = FontWeight.Medium)
private val AlertDialogBodyTextStyle = TextStyle(fontSize = 16.sp, lineHeight = 22.sp)
private val AlertDialogButtonTextStyle =
    TextStyle(fontSize = 14.sp, fontWeight = FontWeight.SemiBold)

@Preview
@Composable
fun AlertDialogPreview() {
    AlertDialogTheme {
        AlertDialog(
            visible = true,
            onDismissRequest = {},
            title = { Text("Enable notifications?") },
            text = { Text("Notifications help you keep up with important updates from this app.") },
            positiveButton = {
                PrimaryButton(
                    label = "Allow!",
                    onClick = {},
                    modifier = Modifier.fillMaxWidth(),
                )
            },
            neutralButton = {
                GhostButton(
                    label = "Not now",
                    onClick = {},
                    modifier = Modifier.fillMaxWidth(),
                )
            },
        )
    }
}
build.gradle.kts
dependencies {
    implementation("org.jetbrains.compose.foundation:foundation:1.12.0")
    implementation("com.composables:ripple-indication:1.1.0")
    implementation("com.composables:composeunstyled:2.10.0")
}

MIT License

MIT License

Copyright (c) 2026 Alex Styl

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.