How do you handle conditional classes? `div("App-t...
# javascript
p
How do you handle conditional classes?
div("App-toolbar-button " + "App-toolbar-button__selected".takeIf { state.pageSelected == 0 }) {
How to get rid of strings concat?
c
My first thought is to put the conditional logic into a read-only property, and then interpolate that property into the class string.
Copy code
kotlin
div("App-toolbar-button $selectedClass")

val selectedClass: String
    get() = if(state.pageSelected == 0) "App-toolbar-button__selected" else ""
l
You can also use a collection of
String
and use
joinToString(" ")
on it
p
Dirty solution, and too verbose imho
j
Fix your DSL to expose classes as a list
p
Seems, I found pretty solution
Copy code
div("App-toolbar-button") {
  attrs {
    if (pageSelected == 0) classes += "App-toolbar-button__selected"
  }
}
It allows to use static classes in tag, and conditional classes in attrs.