Skip to content

Commit

Permalink
Merge pull request #2 from terrakok/add_containment
Browse files Browse the repository at this point in the history
Add BottomSheet demo
  • Loading branch information
terrakok authored Nov 1, 2023
2 parents bfd6f16 + 30b4bb4 commit 8a0c1ec
Show file tree
Hide file tree
Showing 3 changed files with 162 additions and 8 deletions.
23 changes: 20 additions & 3 deletions composeApp/src/commonMain/kotlin/com/github/terrakok/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ import androidx.compose.material3.SnackbarHostState
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.material3.rememberBottomSheetScaffoldState
import androidx.compose.material3.BottomSheetScaffold
import androidx.compose.material3.BottomSheetScaffoldState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.compositionLocalOf
Expand All @@ -46,6 +49,10 @@ const val narrowScreenWidthThreshold = 1300
val LocalSnackbarHostState =
compositionLocalOf<SnackbarHostState> { error("SnackbarHostState is not found") }

@OptIn(ExperimentalMaterial3Api::class)
val LocalBottomSheetScaffoldState =
compositionLocalOf<BottomSheetScaffoldState> { error("BottomSheetScaffoldState is not found") }

data class Screen(
val title: String,
val icon: ImageVector,
Expand Down Expand Up @@ -134,10 +141,20 @@ internal fun App() = AppTheme {
}
}
}
CompositionLocalProvider(
LocalSnackbarHostState provides snackbarHostState
val scaffoldState = rememberBottomSheetScaffoldState()
BottomSheetScaffold(
scaffoldState = scaffoldState,
sheetPeekHeight = 0.dp,
sheetContent = {
BottomSheetContent()
}
) {
selectedScreen.content()
CompositionLocalProvider(
LocalSnackbarHostState provides snackbarHostState,
LocalBottomSheetScaffoldState provides scaffoldState
) {
selectedScreen.content()
}
}
}
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package com.github.terrakok

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.SnackbarHostState
import androidx.compose.material3.*
import androidx.compose.runtime.Composable
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import kotlinx.coroutines.launch

@Composable
fun ComponentScreen() {
Expand All @@ -25,5 +24,7 @@ fun ComponentScreen() {
Communication()
Spacer(Modifier.size(16.dp))
Navigation()
Spacer(Modifier.size(16.dp))
Containment()
}
}
136 changes: 136 additions & 0 deletions composeApp/src/commonMain/kotlin/com/github/terrakok/Containment.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
package com.github.terrakok

import androidx.compose.foundation.layout.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.*
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.unit.dp
import kotlinx.coroutines.launch

@Composable
fun Containment() {
val modalBottomSheetInfoUrl =
"https://developer.android.com/reference/kotlin/androidx/compose/material3/package-summary?hl=en#ModalBottomSheet(kotlin.Function0,androidx.compose.ui.Modifier,androidx.compose.material3.SheetState,androidx.compose.ui.unit.Dp,androidx.compose.ui.graphics.Shape,androidx.compose.ui.graphics.Color,androidx.compose.ui.graphics.Color,androidx.compose.ui.unit.Dp,androidx.compose.ui.graphics.Color,kotlin.Function0,androidx.compose.foundation.layout.WindowInsets,androidx.compose.ui.window.SecureFlagPolicy,kotlin.Function1)"

ParentSection("Containment") {
ChildSection(
title = "Bottom sheet",
infoUrl = modalBottomSheetInfoUrl
) {
BottomSheetDemo()
}
}
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
private fun BottomSheetDemo() {
var isModalSheetOpen by rememberSaveable { mutableStateOf(false) }

OutlinedCard {
Row(
modifier = Modifier
.requiredWidthIn(400.dp)
.width(600.dp)
.padding(16.dp)
) {
TextButton(
modifier = Modifier
.padding(horizontal = 8.dp, vertical = 4.dp)
.weight(1f),
enabled = true,
onClick = {
isModalSheetOpen = true
},
content = { Text("Show modal bottom sheet") }
)

var bottomSheetShown by remember { mutableStateOf(false) }
val scope = rememberCoroutineScope()
val bottomSheetScaffoldState = LocalBottomSheetScaffoldState.current
TextButton(
modifier = Modifier
.padding(horizontal = 8.dp, vertical = 4.dp)
.weight(1f),
enabled = true,
onClick = {
bottomSheetShown = !bottomSheetShown
scope.launch {
if (bottomSheetShown) {
bottomSheetScaffoldState.bottomSheetState.expand()
} else {
bottomSheetScaffoldState.bottomSheetState.hide()
}
}
},
content = {
Text("${if(bottomSheetShown) "Hide" else "Show"} bottom sheet")
}
)
}
}

val bottomSheetState = rememberModalBottomSheetState(skipPartiallyExpanded = true)

if (isModalSheetOpen) {
ModalBottomSheet(
onDismissRequest = { isModalSheetOpen = false },
sheetState = bottomSheetState,
) {
BottomSheetContent()
}
}
}

@Composable
internal fun ColumnScope.BottomSheetContent() {
Row(modifier = Modifier.padding(vertical = 20.dp).align(Alignment.CenterHorizontally)) {
BottomSheetButton(
title = "Share",
icon = Icons.Outlined.Share
)
BottomSheetButton(
title = "Add to",
icon = Icons.Outlined.Add
)
BottomSheetButton(
title = "Trash",
icon = Icons.Outlined.Delete
)
BottomSheetButton(
title = "Archive",
icon = Icons.Outlined.Archive
)
BottomSheetButton(
title = "Settings",
icon = Icons.Outlined.Settings
)
BottomSheetButton(
title = "Favorite",
icon = Icons.Outlined.Favorite
)
}
}

@Composable
private fun BottomSheetButton(
icon: ImageVector,
title: String
) {
NavigationRailItem(
selected = false,
onClick = { },
icon = {
Icon(
imageVector = icon,
contentDescription = null
)
},
label = { Text(title) }
)
}

0 comments on commit 8a0c1ec

Please sign in to comment.