Composables
Docs Login
Vela

Side Drawer

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.Column
import androidx.compose.foundation.layout.ColumnScope
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.WindowInsetsSides
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.navigationBars
import androidx.compose.foundation.layout.only
import androidx.compose.foundation.layout.safeDrawing
import androidx.compose.foundation.layout.statusBars
import androidx.compose.foundation.layout.union
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.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.layout
import androidx.compose.ui.unit.dp
import com.composables.vela.theme.ProvideContentColor
import com.composables.vela.theme.Theme
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

private const val DrawerWidthFraction = 0.85f
private val MaxDrawerWidth = 360.dp

@Composable
fun SideDrawer(
    visible: Boolean,
    onDismiss: () -> Unit,
    modifier: Modifier = Modifier,
    content: @Composable ColumnScope.() -> Unit,
) {
    val drawer = remember {
        UnstyledDrawerState(
            initialValue = if (visible) DrawerPosition.Open else DrawerPosition.Closed,
            snapPoints =
                DrawerSnapPoints {
                    DrawerPosition.Closed at DrawerSnapPoint.Zero
                    DrawerPosition.Open at DrawerSnapPoint.ContentSize
                },
        )
    }
    LaunchedEffect(visible) {
        drawer.targetValue = if (visible) DrawerPosition.Open else DrawerPosition.Closed
    }
    UnstyledDrawer(
        state = drawer,
        modifier = modifier,
        placement = DrawerPlacement.Start,
        presentation = DrawerPresentation.Modal,
        dismissOnClickOutside = true,
        onDismissed = { onDismiss() },
        overlay = {
            Overlay(
                Modifier.fillMaxSize().background(Color.Black.copy(alpha = 0.6f)),
                enter = fadeIn(),
                exit = fadeOut(),
            )
        },
    ) {
        Viewport(Modifier.fillMaxSize()) {
            Panel(
                Modifier.fillMaxHeight()
                    .layout { measurable, constraints ->
                        val width =
                            minOf(
                                (constraints.maxWidth * DrawerWidthFraction).toInt(),
                                MaxDrawerWidth.roundToPx(),
                            )
                        val placeable =
                            measurable.measure(constraints.copy(minWidth = width, maxWidth = width))
                        layout(width, placeable.height) { placeable.place(0, 0) }
                    }
                    .background(Theme.colors.surface)
                    .windowInsetsPadding(
                        WindowInsets.statusBars
                            .union(WindowInsets.navigationBars)
                            .union(WindowInsets.safeDrawing.only(WindowInsetsSides.Start))
                    )
            ) {
                // Unstyled keeps a closed drawer laid out, so its rows would stay reachable by
                // screen readers.
                val showContent =
                    visible ||
                        drawer.currentValue != DrawerPosition.Closed ||
                        drawer.targetValue != DrawerPosition.Closed
                ProvideContentColor(Theme.colors.onSurface) {
                    Column(Modifier.fillMaxSize()) { if (showContent) content() }
                }
            }
        }
    }
}

private enum class DrawerPosition {
    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.