/**
* Will elevate the composable if it's sticking to the top of the parent.
* Used for drawing shadows on sticky headers when they are at the top.
*/
@Composable
fun Modifier.showShadowIfStickingToTop(
topElevation: Dp = 4.dp,
elevationTransitionLabel: String = "elevate-if-at-top-transition",
): Modifier {
var isAtTop by rememberSaveable { mutableStateOf(false) }
val elevationDp by animateDpAsState(
if (isAtTop) topElevation else 0.dp,
label = elevationTransitionLabel,
)
return onPlaced { layoutCoordinates ->
with(layoutCoordinates) {
val parentRootOffsetY =
(parentLayoutCoordinates?.localToRoot(Offset.Zero) ?: Offset.Zero).y
val ownRootOffset = localToRoot(Offset.Zero).y
val topOffset = ownRootOffset - parentRootOffsetY
isAtTop = topOffset == 0f
}
}.shadow(elevationDp)
}
s
Stylianos Gakis
08/16/2024, 3:25 PM
You probably want to turn this into a modifier using
composed {
right?
An extension on modifier also being a composable itself looks odd, never seen it before tbh.