CompositionLocalAccessorScope

Known direct subclasses
CustomStyleScope

An interface that is the base interface for all styles scopes.

StyleResolverScope

The type of the scope of StyleResolver.resolve that allows the block to read the fully resolved values from a style.

Known indirect subclasses
CommonStyleScope

A CommonStyleScope is the scope used by a CommonStyle.

StyleScope

A StyleScope is the receiver scope of a Style lambda.


Summary

Public properties

T

An extension property that allows accessing the current value of a composition local in the context of this scope.

Cmn

Extension functions

inline T

Evaluates a query against the current UiMediaScope from a CompositionLocalAccessorScope.

Cmn

Public properties

CompositionLocal.currentValue

val CompositionLocal<T>.currentValue: T

An extension property that allows accessing the current value of a composition local in the context of this scope. This scope is the type of the this parameter when in a computed composition. Computed composition locals can be provided by either using compositionLocalWithComputedDefaultOf or by using the ProvidableCompositionLocal.providesComputed infix operator.

import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.compositionLocalOf

val LocalValue = compositionLocalOf { 10 }
val LocalLargerValue = compositionLocalOf { 12 }

@Composable
fun App() {
    CompositionLocalProvider(
        LocalLargerValue providesComputed { LocalValue.currentValue + 10 }
    ) {
        SomeScreen()
    }
}

Extension functions

CompositionLocalAccessorScope.mediaQuery

@ExperimentalMediaQueryApi
inline fun <T : Any?> CompositionLocalAccessorScope.mediaQuery(query: UiMediaScope.() -> T): T

Evaluates a query against the current UiMediaScope from a CompositionLocalAccessorScope.

If called within a snapshot-aware context, the specific property reads within the query will be tracked, and the scope will be invalidated when any of those properties change.

import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.style.MutableStyleState
import androidx.compose.foundation.style.Style
import androidx.compose.foundation.style.hovered
import androidx.compose.foundation.style.pressed
import androidx.compose.foundation.style.size
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.UiMediaScope.PointerPrecision
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.mediaQuery
import androidx.compose.ui.unit.dp

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

ClickableStyleableBox(
    onClick = {},
    style = {
        background(Color.Green)

        // Dynamic size based on window Size
        if (mediaQuery { windowWidth > 600.dp && windowHeight > 400.dp }) {
            size(200.dp)
        } else {
            size(150.dp)
        }

        // Hover state for fine pointer input
        if (mediaQuery { pointerPrecision == PointerPrecision.Fine }) {
            hovered { background(Color.Yellow) }
        }
        pressed { background(Color.Red) }
    },
)
Parameters
query: UiMediaScope.() -> T

A lambda expression with UiMediaScope as its receiver, representing the condition to evaluate.

Returns
T

The immediate result of the query.