New Compose Multiplatform components arrived on Composables UICheck it out →

Modifier in Compose Ui

focusTarget

Common

Add this modifier to a component to make it focusable.

Focus state is stored within this modifier. The bounds of this modifier reflect the bounds of the focus box.

Last updated:

Installation

dependencies {
   implementation("androidx.compose.ui:ui:1.7.0-beta04")
}

Overloads


fun Modifier.focusTarget()

Code Example

FocusableSampleUsingLowerLevelFocusTarget

@Composable
@Sampled
fun FocusableSampleUsingLowerLevelFocusTarget() {
    var color by remember { mutableStateOf(Black) }
    Box(
        Modifier
            .border(2.dp, color)
            // The onFocusChanged should be added BEFORE the focusTarget that is being observed.
            .onFocusChanged { color = if (it.isFocused) Green else Black }
            .focusTarget()
    )
}