What's the difference between `Button(onClick = {}...
# compose
o
What's the difference between
Button(onClick = {})
and
Button(modifier = Modifier.clickable {})
? Any reason why there are two different API's for telling the button what to do on click? Which one do people generally use?
👍 3
l
Button is a material-vended composable, and conforms to the specifications of a material button. The modifier is a bit more low level (but not much) and is a building block that you should use if you want to make a custom button or if you want to make a different non-“button” element clickable
💯 1
Note that you could have a design system that only depends on foundation (where Modifier.clickable lives) and not on material at all.
👍 2
o
My probable thought. Makes sense, thanks. I guess the reason for Modifier to exist, is to prevent functions to have huge signatures/ large amounts of parameters?
l
That is one of the benefits of modifiers, yes
z
This is an interesting question - if you passed the modifier to a material button, would that just work? Or would it confuse the button and break accessibility or something?
l
i am not 100% sure but I suspect the touch events would get consumed by the modifier in the button and the “higher up” clickable modifier would end up not triggering.
a
Leland's answer is right, and also yes it would break accessibility by creating two different accessibility-focusable nodes, one for the modifier and one for the button onClick
Note that as of beta01, some components like
Checkbox
take nullable onClick so that you can disable the inner accessibility-focusable node when you're providing your own clickable modifier.
z
Would you consider adding a lint check to warn against passing a redundant clickable modifier?
💯 1
☝️ 2
👍 2
l
anything we can detect and have a very low false-positive rate on i think we are open to add a lint option for
i don’t think we’ve considered this one but we probably should. willing to file a bug on it?
z
can do 👍