Remote Compose Storybook Русский GitHub

clickable + valueChangeAction · Toggle

A boolean flipped on tap, selecting colour and text.

clickable + valueChangeAction · Toggle: the document drawn by the Remote Compose player
Open the live, interactive demo →

Change its controls, read the remote document it produces and the diff after every change.

What it is

Makes something tappable and changes what is shown immediately on the device — toggles, counters, selections — with no trip to the server.

Good for

  • Switches and checkboxes
  • Steppers and counters
  • Tabs and selections

Code

The story's source with androidx.compose.remote imports. CI compiles every snippet on this site against AndroidX.

import androidx.compose.remote.creation.compose.action.valueChangeAction
import androidx.compose.remote.creation.compose.layout.RemoteAlignment
import androidx.compose.remote.creation.compose.layout.RemoteBox
import androidx.compose.remote.creation.compose.layout.RemoteComposable
import androidx.compose.remote.creation.compose.layout.RemoteText
import androidx.compose.remote.creation.compose.modifier.RemoteModifier
import androidx.compose.remote.creation.compose.modifier.RemoteRole
import androidx.compose.remote.creation.compose.modifier.background
import androidx.compose.remote.creation.compose.modifier.clickable
import androidx.compose.remote.creation.compose.modifier.clip
import androidx.compose.remote.creation.compose.modifier.padding
import androidx.compose.remote.creation.compose.shapes.RemoteRoundedCornerShape
import androidx.compose.remote.creation.compose.state.MutableRemoteBoolean
import androidx.compose.remote.creation.compose.state.rc
import androidx.compose.remote.creation.compose.state.rdp
import androidx.compose.remote.creation.compose.state.rs
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.graphics.Color

@Composable
@RemoteComposable
fun ClickToggle(onColor: Color, offColor: Color) {
    // State lives in the document: the player toggles it on tap, no server round-trip.
    val isOn = remember { MutableRemoteBoolean(false) }
    RemoteBox(
        RemoteModifier.clip(RemoteRoundedCornerShape(12.rdp))
            .background(isOn.select(onColor.rc, offColor.rc))
            .clickable(valueChangeAction(isOn, !isOn), role = RemoteRole.Switch)
            .padding(horizontal = 24.rdp, vertical = 14.rdp),
        contentAlignment = RemoteAlignment.Center,
    ) {
        RemoteText(
            isOn.select("ON — tap to turn off".rs, "OFF — tap to turn on".rs),
            color = Color.White.rc,
        )
    }
}

API

creation.compose.modifier.clickable

RemoteModifier clickable(RemoteModifier, RemoteAction onClick, optional boolean enabled, optional RemoteRole? role)

creation.compose.action.valueChangeAction

<T> RemoteAction valueChangeAction(MutableRemoteState<T> remoteState, RemoteState<T> updatedValue)

creation.compose.modifier.RemoteRole

value class RemoteRole
  RemoteRole.Companion Companion
  RemoteRole Button
  RemoteRole Carousel
  RemoteRole Checkbox
  RemoteRole DropdownList
  RemoteRole Image
  RemoteRole RadioButton
  RemoteRole Switch
  RemoteRole Tab
  RemoteRole ValuePicker

Reference

Other stories of this component

More in Interaction