Remote Compose Storybook Русский GitHub

remoteComponent()

A child sized to a fraction of its parent's measured height.

remoteComponent(): 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

Lets an element know its parent's real size on the device, so it can size itself as a fraction of it.

Good for

  • Bars and fills that are a percentage of their container

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.layout.RemoteAlignment
import androidx.compose.remote.creation.compose.layout.RemoteBox
import androidx.compose.remote.creation.compose.layout.RemoteColumn
import androidx.compose.remote.creation.compose.layout.RemoteComposable
import androidx.compose.remote.creation.compose.layout.RemoteText
import androidx.compose.remote.creation.compose.layout.remoteComponent
import androidx.compose.remote.creation.compose.modifier.RemoteModifier
import androidx.compose.remote.creation.compose.modifier.background
import androidx.compose.remote.creation.compose.modifier.fillMaxWidth
import androidx.compose.remote.creation.compose.modifier.height
import androidx.compose.remote.creation.compose.modifier.size
import androidx.compose.remote.creation.compose.state.rc
import androidx.compose.remote.creation.compose.state.rdp
import androidx.compose.remote.creation.compose.state.rf
import androidx.compose.remote.creation.compose.state.rs
import androidx.compose.remote.creation.compose.state.toRemoteDp
import androidx.compose.runtime.Composable
import androidx.compose.ui.graphics.Color

@Composable
@RemoteComposable
fun ComponentSizeExpression(fraction: Float, parentHeight: Int) {
    RemoteColumn(
        modifier = RemoteModifier.size(220.rdp, parentHeight.rdp).background(Color(0xFFF1F3F4).rc),
    ) {
        // remoteComponent() reads the enclosing component's measured size at play time.
        val parent = remoteComponent()
        RemoteBox(
            modifier =
                RemoteModifier.fillMaxWidth()
                    .height((parent.height * fraction.rf).toRemoteDp())
                    .background(Color(0xFF1E88E5).rc),
            contentAlignment = RemoteAlignment.Center,
        ) {
            RemoteText("parent.height × $fraction".rs, color = Color.White.rc)
        }
    }
}

API

creation.compose.layout.remoteComponent

RemoteComponent remoteComponent()
RemoteComponent remoteComponent(RemoteStateScope)

creation.compose.layout.RemoteComponent

class RemoteComponent
  RemoteFloat centerX
  RemoteFloat centerY
  RemoteFloat height
  RemoteOffset positionInParent
  RemoteOffset positionInRoot
  RemoteFloat width

Reference

More in Canvas