Composables
Docs Login
Vela

Modal Bottom Sheet

Jetpack Compose Component

Download
package com.composables.vela.components

import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ColumnScope
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.WindowInsetsSides
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.heightIn
import androidx.compose.foundation.layout.ime
import androidx.compose.foundation.layout.navigationBars
import androidx.compose.foundation.layout.only
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.safeDrawing
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.statusBars
import androidx.compose.foundation.layout.union
import androidx.compose.foundation.layout.widthIn
import androidx.compose.foundation.layout.windowInsetsPadding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import com.composables.vela.theme.Icons
import com.composables.vela.theme.ProvideContentColor
import com.composables.vela.theme.Space
import com.composables.vela.theme.Theme
import com.composables.vela.theme.X
import com.composeunstyled.DrawerPlacement
import com.composeunstyled.DrawerPresentation
import com.composeunstyled.DrawerSnapPoint
import com.composeunstyled.DrawerSnapPoints
import com.composeunstyled.Overlay
import com.composeunstyled.Panel
import com.composeunstyled.UnstyledDrawer
import com.composeunstyled.UnstyledDrawerState
import com.composeunstyled.Viewport

@Composable
fun ModalBottomSheet(
    visible: Boolean,
    onDismiss: () -> Unit,
    title: String,
    modifier: Modifier = Modifier,
    content: @Composable ColumnScope.() -> Unit,
) {
    val drawer = remember {
        UnstyledDrawerState(
            initialValue = if (visible) SheetPosition.Open else SheetPosition.Closed,
            snapPoints =
                DrawerSnapPoints {
                    SheetPosition.Closed at DrawerSnapPoint.Zero
                    SheetPosition.Open at DrawerSnapPoint.ContentSize
                },
        )
    }
    LaunchedEffect(visible) {
        drawer.targetValue = if (visible) SheetPosition.Open else SheetPosition.Closed
    }
    val shape = RoundedCornerShape(topStart = 28.dp, topEnd = 28.dp)
    UnstyledDrawer(
        state = drawer,
        modifier = modifier,
        placement = DrawerPlacement.Bottom,
        // Modal opens above the side drawer and lets drags that start outside the panel move it.
        presentation = DrawerPresentation.Modal,
        dismissOnClickOutside = true,
        onDismissed = { onDismiss() },
        overlay = {
            Overlay(
                Modifier.fillMaxSize().background(Color.Black.copy(alpha = 0.6f)),
                enter = fadeIn(),
                exit = fadeOut(),
            )
        },
    ) {
        // The overlay draws inside the viewport, so the viewport stays edge to edge and the panel
        // keeps clear of the status bar.
        Viewport(Modifier.fillMaxSize()) {
            Panel(
                Modifier.windowInsetsPadding(WindowInsets.statusBars)
                    .padding(top = Space * 2)
                    .widthIn(max = 640.dp)
                    .fillMaxWidth()
                    .clip(shape)
                    .background(Theme.colors.surface)
                    .windowInsetsPadding(
                        WindowInsets.navigationBars
                            .union(WindowInsets.ime)
                            .union(WindowInsets.safeDrawing.only(WindowInsetsSides.Horizontal))
                    )
            ) {
                // Unstyled keeps a closed sheet laid out, so its content would stay reachable by
                // screen readers.
                val showContent =
                    visible ||
                        drawer.currentValue != SheetPosition.Closed ||
                        drawer.targetValue != SheetPosition.Closed
                if (showContent)
                    ProvideContentColor(Theme.colors.onSurface) {
                        Column(Modifier.fillMaxWidth()) {
                            Box(
                                Modifier.align(Alignment.CenterHorizontally)
                                    .padding(top = Space * 2)
                                    .size(width = 36.dp, height = 4.dp)
                                    .clip(Theme.shapes.fullyRounded)
                                    .background(Theme.colors.border)
                            )
                            Box(
                                Modifier.fillMaxWidth()
                                    .padding(horizontal = Space * 3, vertical = Space * 2)
                            ) {
                                IconButton(
                                    onClick = onDismiss,
                                    contentDescription = "Close",
                                    variant = IconButtonVariant.Tonal,
                                    size = IconButtonSize.Small,
                                    modifier = Modifier.align(Alignment.CenterStart),
                                ) {
                                    Icon(
                                        Icons.X,
                                        contentDescription = null,
                                        modifier = Modifier.size(16.dp),
                                    )
                                }
                                Text(
                                    title,
                                    style = Theme.typography.title,
                                    modifier = Modifier.align(Alignment.Center),
                                )
                            }
                            Column(
                                Modifier.fillMaxWidth()
                                    .heightIn(max = 560.dp)
                                    .verticalScroll(rememberScrollState())
                                    .padding(
                                        start = Space * 4,
                                        end = Space * 4,
                                        top = Space,
                                        bottom = Space * 6,
                                    ),
                                verticalArrangement = Arrangement.spacedBy(Space * 3),
                                content = content,
                            )
                        }
                    }
            }
        }
    }
}

private enum class SheetPosition {
    Closed,
    Open,
}

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.