Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.Density
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.Velocity
import androidx.compose.ui.unit.dp
import kotlinx.coroutines.launch
import kotlin.math.roundToInt

Expand Down Expand Up @@ -142,6 +143,8 @@ fun <T : Any> BottomSheetScaffold(
scaffoldState: BottomSheetScaffoldState<T>,
sheetContent: @Composable ColumnScope.() -> Unit,
modifier: Modifier = Modifier,
sheetAlignment: Alignment.Horizontal = Alignment.CenterHorizontally,
sheetPaddingHorizontal: Dp = 0.dp,
sheetMaxWidth: Dp = BottomSheetDefaults.SheetMaxWidth,
sheetShape: Shape = BottomSheetDefaults.ExpandedShape,
sheetContainerColor: Color = BottomSheetDefaults.ContainerColor,
Expand All @@ -160,6 +163,8 @@ fun <T : Any> BottomSheetScaffold(

BottomSheetScaffoldLayout(
modifier = modifier,
sheetAlignment = sheetAlignment,
sheetPaddingHorizontal = sheetPaddingHorizontal,
topBar = topBar,
body = content,
snackbarHost = {
Expand Down Expand Up @@ -206,6 +211,8 @@ fun <T : Any> BottomSheetScaffold(
@Composable
internal fun BottomSheetScaffoldLayout(
modifier: Modifier,
sheetAlignment: Alignment.Horizontal,
sheetPaddingHorizontal: Dp,
topBar: @Composable (() -> Unit)?,
body: @Composable (innerPadding: PaddingValues) -> Unit,
bottomSheet: @Composable (layoutHeight: Int) -> Unit,
Expand Down Expand Up @@ -245,7 +252,18 @@ internal fun BottomSheetScaffoldLayout(

layout(width = layoutWidth, height = layoutHeight) {
val sheetOffsetY = sheetOffset().roundToInt()
val sheetOffsetX = Integer.max(0, (layoutWidth - sheetPlaceable.width) / 2)
val sheetOffsetX = when (sheetAlignment) {
Alignment.Start -> {
0 + sheetPaddingHorizontal.toPx().toInt()
}
Alignment.CenterHorizontally -> {
Integer.max(0, (layoutWidth - sheetPlaceable.width) / 2)
}
Alignment.End -> {
Integer.max(0, (layoutWidth - sheetPlaceable.width - sheetPaddingHorizontal.toPx().toInt()))
}
else -> throw IllegalArgumentException()
}

val snackbarOffsetX = (layoutWidth - snackbarPlaceable.width) / 2

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,19 @@ package io.morfly.bottomsheet.sample.bottomsheet
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.tooling.preview.Devices
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import io.morfly.bottomsheet.sample.bottomsheet.common.BottomSheetContent
import io.morfly.bottomsheet.sample.bottomsheet.common.MapScreenContent
Expand All @@ -23,7 +28,10 @@ import io.morfly.compose.bottomsheet.material3.rememberBottomSheetState
import io.morfly.compose.bottomsheet.material3.requireSheetVisibleHeightDp

@Composable
fun CustomFinalizedDemoScreen() {
fun CustomFinalizedDemoScreen(
sheetAlignment: Alignment.Horizontal = Alignment.CenterHorizontally,
sheetPaddingHorizontal: Dp = 0.dp,
) {
var isInitialState by rememberSaveable { mutableStateOf(true) }

val sheetState = rememberBottomSheetState(
Expand Down Expand Up @@ -53,6 +61,8 @@ fun CustomFinalizedDemoScreen() {
sheetContent = {
BottomSheetContent()
},
sheetAlignment = sheetAlignment,
sheetPaddingHorizontal = sheetPaddingHorizontal,
content = {
val bottomPadding by remember {
derivedStateOf { sheetState.requireSheetVisibleHeightDp() }
Expand All @@ -68,4 +78,53 @@ fun CustomFinalizedDemoScreen() {
},
modifier = Modifier.fillMaxSize(),
)
}

@Preview(name = "Portrait Mode", showBackground = true, device = Devices.PIXEL_7)
@Preview(
name = "Landscape Mode",
showBackground = true,
heightDp = 360,
widthDp = 800,
device = "spec:width=360dp,height=800dp,dpi=420,isRound=false,chinSize=0dp,orientation=landscape",
)
@Composable
private fun Preview_AlignmentCenter() {
MaterialTheme {
CustomFinalizedDemoScreen()
}
}

@Preview(
name = "Landscape Mode",
showBackground = true,
heightDp = 360,
widthDp = 800,
device = "spec:width=360dp,height=800dp,dpi=420,isRound=false,chinSize=0dp,orientation=landscape",
)
@Composable
private fun Preview_AlignmentStart() {
MaterialTheme {
CustomFinalizedDemoScreen(
sheetAlignment = Alignment.Start,
sheetPaddingHorizontal = 16.dp,
)
}
}

@Preview(
name = "Landscape Mode",
showBackground = true,
heightDp = 360,
widthDp = 800,
device = "spec:width=360dp,height=800dp,dpi=420,isRound=false,chinSize=0dp,orientation=landscape",
)
@Composable
private fun Preview_AlignmentEnd() {
MaterialTheme {
CustomFinalizedDemoScreen(
sheetAlignment = Alignment.End,
sheetPaddingHorizontal = 16.dp,
)
}
}