Is there a way to pass down a Modifier to a `@Comp...
# compose
z
Is there a way to pass down a Modifier to a
@Composable () -> Unit
without changing the signautre, and without wrapping the composable in another layout?
a
Composition local?
z
Yes, I shouldve mentioned that I also dont want to use that, it would just be an invisible contract in this case
a
I guess there are no other ways
z
Id be surprised if there are any, but thought Id at ask anyway, Ive been surprised before 😃
g
Why you don't want to change signature?
Using composition local for this looks as an abuse of local composition
z
I dont think Id remember to use the Modifier if I changed the signature to @Composable (Modifier) -> Unit. Kind of like how Scaffold::content has a lint warning to make sure you use the passed in padding.
s
Might be worth it to ask what you are trying to do that requires passing down a modifier. I’ve done this before in the past and I’ve kinda regretted it both by misusing the modifier as I am inside a ColumnScope for example on the call site, but the one using it is not, so that modifier does not work. Or a case where I was passing in some paddings that were supposed to be common for some screens, but then one screen needed to go edge to edge and needed a
contentPadding: PaddingValues
instead of just applying a padding modifier, so again this didn’t work well for me 😄
z
Always is 🙂 My
@Composable () -> Unit
is for a label inside a bunch of my components like button, chips, etc. Now Id like for them to animate their size whenever the text changes. I can wrap the label in a box with the modifier, but that doesnt get me the results I want, adding it directly to the label does. Im prepared to go pretty far to avoid having to specify the modifier in each call site, Id even prefer doing it the box way even if the text is a bit cut off during the animation.
s
How is the text being cut-off with the box that does not happen without the box? Could it be a scenario where you gotta do something like this?
Copy code
AnimatedVisibility(
  visible = isExpanded,
  enter = fadeIn() + expandVertically(clip = false, expandFrom = Alignment.Top),
  exit = fadeOut() + shrinkVertically(clip = false, shrinkTowards = Alignment.Top), // Or Alignment.CenterHorizontally or something like that if you want the animation to happen from the center our. Use the preview animation tooling to see the animation play out frame by frame
)
To do the animation without clipping?
z
I think Ive wasted everyones time. Sorry. Theres no longer a difference in behavior when I apply the modifier to the wrapping box vs directly on the text.
s
One less way to accidentally misuse your composable by forgetting to use the modifier, counts as a big win to me 😄
🎉 3
z
Always remember propagateMinConstraints on box too for this use case. Drives me up a wall that none of the main compose layouts do this by default
sad panda 1
z
I’ve already given this suggestion internally, but more external voices can’t hurt 😉
s
Thanks for that link I had missed that message! Posted this https://kotlinlang.slack.com/archives/CJLTWPH7S/p1709652431782599?thread_ts=1708198639.005699&cid=CJLTWPH7S along with an example of what we had to do in order to avoid this problem just because m3.Card happened to have a Column internally which made it impossible for us to work around it without having to copy the Card to not have it use a Column internally
👍🏻 1