Live Jetpack Compose inside any web page. No build, no server.
Add one script, write Compose in your HTML, and it renders in the page, powered by Fast Compose.
dino-game.mp4
Add the library to your page:
<script src="https://cdn.jsdelivr.net/npm/@fast-compose/[email protected]/compose-script.js"></script>Then write Compose anywhere in the page:
<script type="text/compose-content">
var count by remember { mutableStateOf<Int>(0) }
Button(onClick = { count++ }) { Text("Clicked $count times") }
</script>Try it live, and edit the code beside the result.
| Example | Shows |
|---|---|
| Sound Wave | An animated wave in a few lines of Compose |
| Dino Game | A playable endless runner on a Canvas, with keyboard and tap controls, collisions and a high score |
| Travel Explorer | A phone-sized screen with network images, a carousel and spring animations |
| Live Showcase | Animation, state, a small task app and @Preview in light and dark |
| Type | Shows | Size |
|---|---|---|
text/compose-content |
One composable, with no labels or frames | Set by the composable |
text/compose-preview |
Every @Preview function, with labels and frames |
400 px tall by default, or set by CSS |
Each script is replaced by a <compose-content> or <compose-preview> element with the script's id, class and
style, so pages can style them with CSS. Parse and runtime errors appear inside the element, and each one starts
when it scrolls into view.
While the .wasm files download, each element shows a soft loading animation, 160 × 48 px for compose-content
unless the script's style sets a size. Pages can restyle it with the [data-loading] attribute, e.g.
compose-content[data-loading] { background: #eee; }.
By default the code is the body of a composable: write the UI directly.
<script type="text/compose-content">
Column(Modifier.padding(16.dp)) {
Text("Hello from Compose!")
Button(onClick = {}) { Text("Click me") }
}
</script>To declare your own composables or classes, use data-composable.
With data-composable, the code is a whole Kotlin file and the attribute names the function to show. The function
must be a top-level @Composable function without parameters; the file may declare other functions and classes.
<script type="text/compose-content" data-composable="Profile">
data class User(val name: String, val role: String)
@Composable
fun UserRow(user: User) {
Column(Modifier.padding(8.dp)) {
Text(user.name)
Text(user.role, color = Color.Gray)
}
}
@Composable
fun Profile() {
Column {
UserRow(User("Ada", "Engineer"))
UserRow(User("Grace", "Admiral"))
}
}
</script>The composable sets the element's width and height, up to the width of its parent and the height of the window.
Content that fills or scrolls needs a size in the code, e.g. Modifier.height(300.dp).
A width or height in the script's style attribute is kept, in any CSS unit, and the composable is measured
within it; content larger than a fixed size is cut off. The composable still sets the side that is not given.
<script type="text/compose-content" style="width: 200px">
Text("Wraps within 200 px, and the composable sets the height")
</script>Sizes from stylesheets or classes are not read; to size by class, put the script in an element with that class.
The element sits in the text flow like an image (display: inline-block), so other elements can go beside it. For
its own line, add display: block to the script's style.
<script type="text/compose-content">
Button(onClick = {}) { Text("Click me") }
</script>
<label>Beside the button</label>The code is a whole Kotlin file. Every @Composable function with @Preview is shown with its name, size, background,
font scale and night mode.
<script type="text/compose-preview" style="height: 300px">
@Preview(name = "Light", showBackground = true)
@Composable
fun LightGreeting() {
Text("Hello", Modifier.padding(16.dp))
}
@Preview(name = "Dark", uiMode = Configuration.UI_MODE_NIGHT_YES, showBackground = true)
@Composable
fun DarkGreeting() {
Text("Hello", Modifier.padding(16.dp))
}
</script>The element is 400 px tall by default; set another height with CSS. Previews taller than the element scroll inside it.