Looking to build some intuition around when props ...
# compose
s
Looking to build some intuition around when props are specified as direct composable function parameters vs as modifiers. Is there a good rule of thumb to get started with?
a
Direct parameters describe the content of the composable. Modifiers decorate the composable and its content as an opaque unit.
It is in the nature of a Button to click, so a material Button composable has an onClick parameter. What happens when you click the button is effectively part of the button's nature or content. You can implement your own kind of button by decorating a Box with a clickable modifier, creating the same kind of abstraction.
☝🏼 2
s
Ah, this is very helpful, thank you! I assume opaque here means "ignorant of content" (and content is not assumed to imply size; that's parent-dictated)?
a
Ignorant of content, yes.
👍 2
Content can imply size; measurement is a two way conversation between parent and child elements.
👍 1
The problem modifiers essentially solve is that Android's View.java is something north of 35kloc. Any time there was a cross-cutting new property that could be applied to any View, it had to be added to the View superclass itself.
Modifiers are a way to define generic properties that can apply to any UI element regardless of type without it needing to be part of a universal base class.
☝🏼 1
And order of modifiers is significant so that compose UI doesn't have to know the nature of specific modifiers to order them relative to one another; that's the responsibility of the code building the modifier chain instead, which lets new modifier concepts be defined independent of one another.
Android's handling of padding left/right/start/end and how it can originate from styles, direct attributes, background 9-patch drawables and more is horrendously complicated and difficult to maintain or reason about; compose's use of ordered modifiers makes all of this intent explicit instead and requires no knowledgeable base class to be aware of these concepts or how they interact.
s
💯 Fascinating, thank you! I think I was being confused somewhat by the custom Modifier extensions introduced by some scopes, which made Modifiers feels less "fully generic" and more specific; but that pattern actually just allows for "shades of" generic, really.
I have no example in mind, so this is a purely theoretical musing that may not even make sense, but: are there any types of "transformation" the old/messy/nonlinear code made possible that will be harder/impossible to replicate via the ordered application of Modifiers?
a
Not that we've come across so far
The scoped modifiers play the role of Android's LayoutParams; data consumed by the parent and placed by the integrator that is opaque to the child it describes
s
Ahh, right, but with more typing (no downcasting to MarginLayoutParams, for e.g.)?