Skip to content

Commit

Permalink
Allows custom values for Sheets' velocity and positional thresholds
Browse files Browse the repository at this point in the history
Fixes: #20
  • Loading branch information
alexstyl committed Oct 5, 2024
1 parent 1e5eeff commit c02b608
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 9 deletions.
41 changes: 33 additions & 8 deletions core/src/commonMain/kotlin/BottomSheet.kt
Original file line number Diff line number Diff line change
Expand Up @@ -60,22 +60,27 @@ private fun Saver(
density: Density,
coroutineScope: CoroutineScope,
sheetDetents: List<SheetDetent>,
velocityThreshold: () -> Float,
positionalThreshold: (totalDistance: Float) -> Float,
): Saver<BottomSheetState, *> = mapSaver(save = { mapOf("detent" to it.currentDetent.identifier) }, restore = { map ->
val selectedDetentName = map["detent"]
BottomSheetState(
initialDetent = sheetDetents.first { it.identifier == selectedDetentName },
detents = sheetDetents,
density = density,
animationSpec = animationSpec,
coroutineScope = coroutineScope,
animationSpec = animationSpec,
velocityThreshold = velocityThreshold,
positionalThreshold = positionalThreshold,
)
})

@Composable
public fun rememberBottomSheetState(
initialDetent: SheetDetent,
detents: List<SheetDetent> = listOf(SheetDetent.Hidden, SheetDetent.FullyExpanded),
animationSpec: AnimationSpec<Float> = tween()
animationSpec: AnimationSpec<Float> = tween(),
velocityThreshold: () -> Dp = { 125.dp },
positionalThreshold: (totalDistance: Dp) -> Dp = { 56.dp }
): BottomSheetState {
val density = LocalDensity.current
val scope = rememberCoroutineScope()
Expand All @@ -85,14 +90,33 @@ public fun rememberBottomSheetState(
density = density,
sheetDetents = detents,
coroutineScope = scope,
velocityThreshold = {
with(density) {
velocityThreshold().toPx()
}
},
positionalThreshold = { totalDistance ->
with(density) {
positionalThreshold(totalDistance.toDp()).toPx()
}
}
)
) {
BottomSheetState(
initialDetent = initialDetent,
detents = detents,
coroutineScope = scope,
animationSpec = animationSpec,
density = density,
velocityThreshold = {
with(density) {
velocityThreshold().toPx()
}
},
positionalThreshold = { totalDistance ->
with(density) {
positionalThreshold(totalDistance.toDp()).toPx()
}
}
)
}
}
Expand Down Expand Up @@ -126,8 +150,9 @@ public class BottomSheetState internal constructor(
initialDetent: SheetDetent,
internal val detents: List<SheetDetent>,
private val coroutineScope: CoroutineScope,
density: Density,
animationSpec: AnimationSpec<Float>
animationSpec: AnimationSpec<Float>,
velocityThreshold: () -> Float,
positionalThreshold: (totalDistance: Float) -> Float
) {
init {
check(detents.isNotEmpty()) {
Expand All @@ -152,8 +177,8 @@ public class BottomSheetState internal constructor(

internal val coreAnchoredDraggableState = CoreAnchoredDraggableState(
initialValue = initialDetent,
positionalThreshold = { distance -> with(density) { 56.dp.toPx() } },
velocityThreshold = { with(density) { 125.dp.toPx() } },
positionalThreshold = positionalThreshold,
velocityThreshold = velocityThreshold,
animationSpec = animationSpec
)

Expand Down
8 changes: 7 additions & 1 deletion core/src/commonMain/kotlin/ModalBottomSheet.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.input.key.Key
import androidx.compose.ui.input.pointer.pointerInput
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import kotlinx.coroutines.delay

public data class ModalSheetProperties(
Expand All @@ -38,12 +40,16 @@ public fun rememberModalBottomSheetState(
initialDetent: SheetDetent,
detents: List<SheetDetent> = listOf(SheetDetent.Hidden, SheetDetent.FullyExpanded),
animationSpec: AnimationSpec<Float> = tween(),
velocityThreshold: () -> Dp = { 125.dp },
positionalThreshold: (totalDistance: Dp) -> Dp = { 56.dp }
): ModalBottomSheetState {
val actualDetents = (setOf(SheetDetent.Hidden) + detents).toList()
val sheetState = rememberBottomSheetState(
initialDetent = SheetDetent.Hidden,
detents = detents,
animationSpec = animationSpec
animationSpec = animationSpec,
velocityThreshold = velocityThreshold,
positionalThreshold = positionalThreshold,
)
return rememberSaveable(
saver = mapSaver(
Expand Down

0 comments on commit c02b608

Please sign in to comment.