What is the current way to add a click to a card.....
# compose
a
What is the current way to add a click to a card... that clips the ripple into the card shape?
j
Check you modifier order. You should apply clickable after padding to clip the ripple.
a
Copy code
modifier = Modifier
                    .padding(8.dp)
                    .clickable { }
                    .size(100.dp),
this is my current config
j
Ah I see, you’re talking about the “not rounded” edges of the ripple square ?
a
oh, sorry
yes
j
Perhaps applying a .clip() modifier too might help
Copy code
modifier = Modifier
                .clip(RoundedCornerShape(4.dp)),
with whatever dp is your corner radius
Check the order too in this case, IMHO the clip should happen before the clickable
a
oh, yes it seems to work
thanks
s
Would be better to move the clickable inside the card so you don't have to clip it
Copy code
Card {
    Box(modifier = Modifier.clickable())
}
👍 3
s
Adding a bit more context, if you want to extend this to arbitrary shapes / clips you can move the indicator inside the surface like Button does https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:compose/[…]oidx/compose/material/Button.kt;l=124?q=Button.kt&ss=androidx An example of usage with a triangle shape: https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:compose/[…]ape%20button%22&ss=androidx%2Fplatform%2Fframeworks%2Fsupport This is more relevant to a reusable composable, and clip is fine if you're comfortable doing a specific design. Just adding another solution that's more general purpose here for readers who found this chat in search :). cc @Alexandre Elias [G]
👍 1
The way to think about this is that .clickable() "wraps" the composable it's passed to, so passing it to surface is effectively:
Copy code
Clickable { // indicator here
   Surface { // clipping here
   }
}
Inverting that makes it work as expected:
Copy code
Surface { // clipping here
    Clickable { // indicator here
    }
}
a
Understandable! Thanks!
u
@Sean McQuillan [G] What if I want selectable() instead of clickable()? The seemingly general-purpose approach that you are suggesting does not seem to apply in this case. If I already know that I want clickable() I can handle it with a separate parameter anyway and force it to the end of the Modifiers chain, so I wouldn't really need to go generic with Surface(). The more interesting case is handling more arbitrary clickable/selectable modifiers, and it does not work for that.