Is there a way to check if a `LazyColumn`s `sticky...
# compose
a
Is there a way to check if a `LazyColumn`s
stickyHeader
is currently sticking? Edit: I made a
Modifier
myself, code in 🧵
This works great:
Copy code
/**
 * 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
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.
1
s
I think
@Composable fun Modifier.foo()
is recommended now instead of using
composed {}
. https://developer.android.com/develop/ui/compose/custom-modifiers#create_a_custom_modifier_using_a_composable_modifier_factory
👍 1
s
I feel like an ancient person, giving old advice 😂 Thanks a lot Sindre!
s
I mean, they did recommend replacing
@Composable fun Modifier.foo()
with
composed {}
in the past, so it's been kind of a bit back and forth. But anytime! 😀
a
Maybe @Stylianos Gakis is way ahead of us, since google will recommend replacing composable fun with composed in the near future again 😄
😂 1
s
Only time will tell, although I think we're supposed to use
Modifier.Node
stuff when possible. 😅