Skip to content

Commit

Permalink
Return the amount of sheet scrolled from the bottom via offset
Browse files Browse the repository at this point in the history
  • Loading branch information
alexstyl committed Oct 26, 2024
1 parent 67a092d commit 61feb93
Showing 1 changed file with 8 additions and 11 deletions.
19 changes: 8 additions & 11 deletions core/src/commonMain/kotlin/BottomSheet.kt
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ public class BottomSheetState internal constructor(

internal var closestDentToTop: Float by mutableStateOf(Float.NaN)

internal var containerHeight = Float.NaN
internal var fullContentHeight = Float.NaN

internal val coreAnchoredDraggableState = CoreAnchoredDraggableState(
initialValue = initialDetent,
Expand Down Expand Up @@ -180,11 +180,11 @@ public class BottomSheetState internal constructor(
get() = coreAnchoredDraggableState.progress

public val offset: Float by derivedStateOf {
if (coreAnchoredDraggableState.offset.isNaN()) {
if (coreAnchoredDraggableState.offset.isNaN() || closestDentToTop.isNaN()) {
1f
} else {
val offsetFromTop = coreAnchoredDraggableState.offset - closestDentToTop
1f - (offsetFromTop / containerHeight)
fullContentHeight - offsetFromTop
}
}

Expand Down Expand Up @@ -222,25 +222,22 @@ public fun BottomSheet(

BoxWithConstraints(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.TopCenter) {
var containerHeight by remember { mutableStateOf(Dp.Unspecified) }
state.containerHeight = Float.NaN
state.fullContentHeight = Float.NaN

val density = LocalDensity.current

Box(
modifier = Modifier.matchParentSize()
.onSizeChanged {
containerHeight = with(density) { it.height.toDp() }
state.containerHeight = it.height.toFloat()
}
.onSizeChanged { containerHeight = with(density) { it.height.toDp() } }
) {
Box(
contentAlignment = Alignment.TopCenter,
modifier = Modifier
.let {
if (containerHeight != Dp.Unspecified) {
it.onSizeChanged {
val sheetHeight = with(density) { it.height.toDp() }

it.onSizeChanged { sheetSize ->
val sheetHeight = with(density) { sheetSize.height.toDp() }
state.fullContentHeight = sheetSize.height.toFloat()
val anchors = CoreDraggableAnchors {
with(density) {
state.closestDentToTop = Float.NaN
Expand Down

0 comments on commit 61feb93

Please sign in to comment.