Using Compose on the Web without Wasm
Jun 4, 2023 · 3 minute read · Commentscode
kotlinkmpweb
At KotlinConf this year, JetBrains announced that Compose for Web will be powered by WebAssembly. The example repository was very compelling, showing that a composable function can now be written on Android and shared across to iOS and web. I quickly got to work updating one of our internal projects to support this and was able to get it working fairly quickly. My coworker, Efeturi, said, “it’s nice but, too bad it’s still in alpha.” Little did I realize that trying to answer that simple comment would lead me to discovering something I never realized before - you can actually use the same Composable functions on the web today without WebAssembly (and thus without the alpha dependency).
But first, a bit of history…
History
In 2021, JetBrains announced a way to write Compose for the browser. This method, which allowed us to write Compose applications for web using something very similar to what we’d write using HTML and CSS, is now referred to as Compose HTML.
renderComposable(rootElementId = "root") {
Div({ style { padding(25.px) } }) {
Input(type = InputType.Text, attrs = {
onInput { inputText.value = it.value }
onKeyUp {
if (it.key == "Enter") {
searchText.value = inputText.value
}
}
})
Button(attrs = { onClick { } }) {
Text("Search")
}
}
}
This is really powerful for people developing for the web. For everyone else, it’d be a lot more convenient to be able to use the same @Composable
functions we wrote for Android or iOS on the web.
Versions of Compose for Web
While going through the example repository, I noticed that the web ImageViewer sample supported two platforms - WebAssembly and JavaScript. I asked about this on Kotlin Slack, and received an answer about this from Slava Kuzmich, who works on Wasm at JetBrains:
They both use Skia library compiled to WebAssembly. The difference is that
js
target compiles your Kotlin code to JavaScript, whilewasm
compiles Kotlin code to WebAssembly.
We have two versions of Compose:
- “Compose for Web” is compatible with Android/Desktop/iOS but it doesn’t use HTML/DOM elements and renders everything on canvas using Skia.
- “Compose HTML” is the older version that uses different HTML-like APIs to generate DOM.
We currently focus more on the canvas version.
Therefore, Compose Multiplatform actually supports 2 primary flavors of Compose - Compose HTML and Compose for Web (which can target JS or WebAssembly). The latter is multiplatform and relies on Skia for rendering. The former is only available for usage in Kotlin/JS.
Adopting Compose on the Web
It ends up being pretty straight forward to adapt Compose on the Web in an app. I adopted the code in the wasm example repository’s jsMain
directory and Gradle files to replace Compose HTML in my PrayerTimes KMP Example repository with Compose for Web targeting JavaScript in this pr.
Note that the official example, while a great guide for getting things running on the web targeting both JavaScript and WebAssembly, only runs with WebAssembly as of the writing of this post (i.e. :webApp:wasmRun
worked for me, but :webApp:jsRun
does not).
While it seems that targeting WebAssembly will be the most future proof way to have Compose on the web due to its performance and some massive work around optimizing sizes by the teams, there are three reasons to target JavaScript in the meanwhile today:
- it doesn’t need a
dev
version of Compose today - it works on more browsers without needing to set flags for GC, etc. It works on Safari, for example, which currently doesn’t support Kotlin/Wasm.
- it’s easy to support wasm from this, as the official example shows.