Implementing this interface allows your [Modifier.
CompositionLocalConsumingModifierSample
@Composable
fun CompositionLocalConsumingModifierSample() {
val localBackgroundColor = compositionLocalOf { Color.White }
class BackgroundColor :
Modifier.Node(), DrawModifierNode, CompositionLocalConsumerModifierNode {
override fun ContentDrawScope.draw() {
val backgroundColor = currentValueOf(localBackgroundColor)
drawRect(backgroundColor)
drawContent()
}
}
val backgroundColorElement =
object : ModifierNodeElement<BackgroundColor>() {
override fun create() = BackgroundColor()
override fun update(node: BackgroundColor) {}
override fun hashCode() = System.identityHashCode(this)
override fun equals(other: Any?) = (other === this)
override fun InspectorInfo.inspectableProperties() {
name = "backgroundColor"
}
}
fun Modifier.backgroundColor() = this then backgroundColorElement
Box(Modifier.backgroundColor()) { Text("Hello, world!") }
}