Composables
Docs Login
Pairel

Disclosure

Jetpack Compose Component

Download
package com.composables.pairel.components

import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.animation.expandVertically
import androidx.compose.animation.shrinkVertically
import androidx.compose.foundation.background
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.rotate
import androidx.compose.ui.unit.dp
import com.composables.compose.ripple.rememberRippleIndication
import com.composables.pairel.theme.ArrowLeft
import com.composables.pairel.theme.Icons
import com.composables.pairel.theme.Theme
import com.composeunstyled.DisclosedContent
import com.composeunstyled.DisclosureButton
import com.composeunstyled.UnstyledDisclosure

@Composable
fun Disclosure(
    title: String,
    modifier: Modifier = Modifier,
    initiallyExpanded: Boolean = false,
    content: @Composable () -> Unit,
) {
    var expanded by remember(initiallyExpanded) { mutableStateOf(initiallyExpanded) }
    val interactionSource = remember { MutableInteractionSource() }
    val rotation by animateFloatAsState(if (expanded) 90f else -90f, label = "Disclosure arrow")
    UnstyledDisclosure(
        expanded = expanded,
        onExpandedChange = { expanded = it },
        modifier = modifier.clip(Theme.shapes.small).background(Theme.colors.surface),
    ) {
        Column {
            DisclosureButton(
                modifier = Modifier.fillMaxWidth(),
                interactionSource = interactionSource,
                indication = rememberRippleIndication(color = Theme.colors.accent),
            ) {
                Row(
                    modifier = Modifier.fillMaxWidth().padding(16.dp),
                    verticalAlignment = Alignment.CenterVertically,
                ) {
                    Text(title, modifier = Modifier.weight(1f), style = Theme.typography.label)
                    Icon(
                        imageVector = Icons.ArrowLeft,
                        contentDescription = null,
                        modifier = Modifier.rotate(rotation),
                    )
                }
            }
            DisclosedContent(enter = expandVertically(), exit = shrinkVertically()) {
                androidx.compose.foundation.layout.Box(
                    Modifier.padding(start = 16.dp, end = 16.dp, bottom = 16.dp)
                ) {
                    content()
                }
            }
        }
    }
}

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.