rememberUpdatedStyleState

Functions summary

inline StyleState

Create, remember and update a StyleState for use as a parameter of a androidx.compose.ui.Modifier.styleable modifier.

Cmn

Functions

rememberUpdatedStyleState

@ExperimentalFoundationStyleApi
@Composable
inline fun rememberUpdatedStyleState(
    interactionSource: InteractionSource?,
    block: @Composable (MutableStyleState) -> Unit = {}
): StyleState

Create, remember and update a StyleState for use as a parameter of a androidx.compose.ui.Modifier.styleable modifier.

import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.style.Style
import androidx.compose.foundation.style.disabled
import androidx.compose.foundation.style.hovered
import androidx.compose.foundation.style.pressed
import androidx.compose.foundation.style.rememberUpdatedStyleState
import androidx.compose.foundation.style.styleable
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp

// Create a styleable clickable box
@Composable
fun ClickableStyleableBox(
    onClick: () -> Unit,
    modifier: Modifier = Modifier,
    style: Style = Style,
    enabled: Boolean = true,
) {
    val interactionSource = remember { MutableInteractionSource() }
    val styleState = rememberUpdatedStyleState(interactionSource) { it.isEnabled = enabled }
    Box(
        modifier =
            modifier
                .clickable(interactionSource = interactionSource, onClick = onClick)
                .styleable(styleState, style)
    )
}

// Create a 150x150 green box that is clickable
ClickableStyleableBox(
    onClick = {},
    style = {
        background(Color.Green)
        size(150.dp)
        hovered { background(Color.Yellow) }
        pressed { background(Color.Red) }
        disabled { background(Color.Gray) }
    },
)
Parameters
interactionSource: InteractionSource?

the interaction source to observe for the style state.

block: @Composable (MutableStyleState) -> Unit = {}

a lambda that will initializes or updates the style state.