Composables
Docs Login
Pairel

Drawer

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.Column
import androidx.compose.foundation.layout.ColumnScope
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.widthIn
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
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.pairel.theme.ProvideContentColor
import com.composables.pairel.theme.Space
import com.composables.pairel.theme.Theme
import com.composeunstyled.DrawerPlacement
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 Drawer(
    initiallyVisible: Boolean,
    onDismiss: () -> Unit,
    modifier: Modifier = Modifier,
    content: @Composable ColumnScope.() -> Unit,
) {
    var visible by remember { mutableStateOf(initiallyVisible) }
    LaunchedEffect(initiallyVisible) { visible = initiallyVisible }
    val state = remember {
        UnstyledDrawerState(
            initialValue = if (visible) DrawerPosition.Open else DrawerPosition.Closed,
            snapPoints =
                DrawerSnapPoints {
                    DrawerPosition.Closed at DrawerSnapPoint.Zero
                    DrawerPosition.Open at DrawerSnapPoint.ContentSize
                },
        )
    }
    LaunchedEffect(visible) {
        state.targetValue = if (visible) DrawerPosition.Open else DrawerPosition.Closed
    }
    UnstyledDrawer(
        state = state,
        modifier = modifier,
        placement = DrawerPlacement.Start,
        dismissOnClickOutside = true,
        onDismissed = {
            visible = false
            onDismiss()
        },
        overlay = {
            Overlay(
                Modifier.fillMaxSize().background(Color.Black.copy(alpha = 0.35f)),
                enter = fadeIn(),
                exit = fadeOut(),
            )
        },
    ) {
        Viewport(Modifier.fillMaxSize()) {
            Panel(
                Modifier.fillMaxHeight()
                    .widthIn(max = 320.dp)
                    .clip(Theme.shapes.large)
                    .background(Theme.colors.surface)
            ) {
                Column(Modifier.padding(Space * 6)) {
                    ProvideContentColor(Theme.colors.onSurface) { content() }
                }
            }
        }
    }
}

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