Composables
Docs Login
Pairel

Modal Bottom Sheet

Jetpack Compose Component

Download
package com.composables.pairel.components

import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.ColumnScope
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.navigationBars
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.windowInsetsPadding
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.graphics.Color
import com.composables.pairel.theme.Space
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,
    modifier: Modifier = Modifier,
    title: (@Composable () -> Unit)? = null,
    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
    }
    UnstyledDrawer(
        state = drawer,
        modifier = modifier,
        placement = DrawerPlacement.Bottom,
        presentation = DrawerPresentation.InPlace,
        dismissOnClickOutside = true,
        onDismissed = { onDismiss() },
        overlay = {
            Overlay(
                Modifier.fillMaxSize().background(Color.Black.copy(alpha = 0.35f)),
                enter = fadeIn(),
                exit = fadeOut(),
            )
        },
    ) {
        Viewport(Modifier.fillMaxSize()) {
            Panel(
                Modifier.fillMaxWidth()
                    .windowInsetsPadding(WindowInsets.navigationBars)
                    .padding(Space * 4)
            ) {
                Box(Modifier.fillMaxWidth(), contentAlignment = Alignment.TopCenter) {
                    SheetContent(
                        title = title,
                        content = content,
                    )
                }
            }
        }
    }
}

private enum class SheetPosition {
    Closed,
    Open,
}

More components from Pairel

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.