Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,5 @@
.externalNativeBuild
.cxx
local.properties
.kotlin
.idea
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package dev.hrach.navigation.bottomsheet

import androidx.compose.foundation.layout.ExperimentalLayoutApi
import androidx.compose.material3.BottomSheetDefaults
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.ModalBottomSheet
import androidx.compose.material3.ModalBottomSheetProperties
import androidx.compose.material3.SheetState
import androidx.compose.material3.contentColorFor
import androidx.compose.material3.rememberModalBottomSheetState
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
Expand All @@ -16,17 +18,31 @@ import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.SaveableStateHolder
import androidx.compose.runtime.saveable.rememberSaveableStateHolder
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.Shape
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.navigation.NavBackStackEntry
import androidx.navigation.compose.LocalOwnersProvider
import kotlinx.coroutines.CancellationException
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow

@OptIn(ExperimentalMaterial3Api::class)
@Composable
public fun BottomSheetHost(
navigator: BottomSheetNavigator,
modifier: Modifier = Modifier,
sheetMaxWidth: Dp = BottomSheetDefaults.SheetMaxWidth,
shape: Shape = BottomSheetDefaults.ExpandedShape,
containerColor: Color = BottomSheetDefaults.ContainerColor,
contentColor: Color = contentColorFor(containerColor),
tonalElevation: Dp = 0.dp,
scrimColor: Color = BottomSheetDefaults.ScrimColor,
dragHandle:
@Composable()
(() -> Unit)? = { BottomSheetDefaults.DragHandle() },
) {
val saveableStateHolder = rememberSaveableStateHolder()

Expand All @@ -47,9 +63,16 @@ public fun BottomSheetHost(
val backStackEntry = sheetBackStackState.second.getOrNull(i)
BottomSheetHost(
navigator = navigator,
modifier = modifier,
sheetMaxWidth = sheetMaxWidth,
shape = shape,
containerColor = containerColor,
contentColor = contentColor,
tonalElevation = tonalElevation,
scrimColor = scrimColor,
dragHandle = dragHandle,
saveableStateHolder = saveableStateHolder,
targetBackStackEntry = backStackEntry,
modifier = modifier,
)
}
}
Expand All @@ -67,9 +90,18 @@ private fun Flow<List<NavBackStackEntry>>.zipWithPreviousCount(): Flow<Pair<Int,
@Composable
private fun BottomSheetHost(
navigator: BottomSheetNavigator,
modifier: Modifier = Modifier,
sheetMaxWidth: Dp,
shape: Shape,
containerColor: Color,
contentColor: Color,
tonalElevation: Dp,
scrimColor: Color,
dragHandle:
@Composable()
(() -> Unit)?,
saveableStateHolder: SaveableStateHolder,
targetBackStackEntry: NavBackStackEntry?,
modifier: Modifier = Modifier,
) {
val destination = targetBackStackEntry?.destination as? BottomSheetNavigator.Destination
val sheetState = rememberModalBottomSheetState(
Expand All @@ -90,22 +122,38 @@ private fun BottomSheetHost(
}

BottomSheetHost(
sheetState = sheetState,
navigator = navigator,
modifier = modifier,
sheetMaxWidth = sheetMaxWidth,
shape = shape,
containerColor = containerColor,
contentColor = contentColor,
tonalElevation = tonalElevation,
scrimColor = scrimColor,
dragHandle = dragHandle,
sheetState = sheetState,
saveableStateHolder = saveableStateHolder,
backStackEntry = backStackEntry ?: return,
modifier = modifier,
)
}

@OptIn(ExperimentalMaterial3Api::class, ExperimentalLayoutApi::class)
@Composable
private fun BottomSheetHost(
sheetState: SheetState,
navigator: BottomSheetNavigator,
modifier: Modifier = Modifier,
sheetMaxWidth: Dp,
shape: Shape,
containerColor: Color,
contentColor: Color,
tonalElevation: Dp,
scrimColor: Color,
dragHandle:
@Composable()
(() -> Unit)?,
sheetState: SheetState,
saveableStateHolder: SaveableStateHolder,
backStackEntry: NavBackStackEntry,
modifier: Modifier = Modifier,
) {
LaunchedEffect(backStackEntry) {
sheetState.show()
Expand All @@ -115,10 +163,17 @@ private fun BottomSheetHost(
val destination = backStackEntry.destination as BottomSheetNavigator.Destination

ModalBottomSheet(
sheetState = sheetState,
properties = ModalBottomSheetProperties(securePolicy = destination.securePolicy),
onDismissRequest = { navigator.dismiss(backStackEntry) },
modifier = modifier,
sheetState = sheetState,
sheetMaxWidth = sheetMaxWidth,
shape = shape,
containerColor = containerColor,
contentColor = contentColor,
tonalElevation = tonalElevation,
scrimColor = scrimColor,
dragHandle = dragHandle,
properties = ModalBottomSheetProperties(securePolicy = destination.securePolicy),
) {
LaunchedEffect(backStackEntry) {
navigator.onTransitionComplete(backStackEntry)
Expand Down
1 change: 0 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import java.util.Properties
import kotlin.math.sign
import org.jetbrains.kotlin.gradle.dsl.JvmTarget
import org.jetbrains.kotlin.gradle.tasks.KotlinJvmCompile

Expand Down
13 changes: 12 additions & 1 deletion demo/src/main/kotlin/dev/hrach/navigation/demo/NavHost.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package dev.hrach.navigation.demo

import androidx.compose.foundation.shape.CornerSize
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.ui.unit.dp
import androidx.navigation.NavHostController
import androidx.navigation.compose.NavHost
import androidx.navigation.compose.composable
Expand Down Expand Up @@ -36,5 +39,13 @@ internal fun NavHost(
bottomSheet<Destinations.BottomSheet> { BottomSheet(navController) }
}
ModalSheetHost(modalSheetNavigator, containerColor = MaterialTheme.colorScheme.background)
BottomSheetHost(bottomSheetNavigator)
BottomSheetHost(
bottomSheetNavigator,
shape = RoundedCornerShape( // optional, just an example of bottom sheet custom property
topStart = CornerSize(12.dp),
topEnd = CornerSize(12.dp),
bottomStart = CornerSize(0.dp),
bottomEnd = CornerSize(0.dp),
),
)
}