1.21.0 (4 Oct 2021)
Add dynamic classes and styles with :=
For dynamic classes, set the class:=
attribute to a array, object, or string:
class:="['x', 'y']"
becomesclass="x y"
class:="{x: true, y: false}"
becomesclass="x"
class:="['x', {y: true, z: false}]"
becomesclass="x y"
class:="${active ? 'yes' : 'no'}"
becomesclass="yes"
is active is true, elseclass="no"
For dynamic styles, set the style:=
attribute to an object or string:
style:="{'font-size': `${size}px`, color: 'red'}"
becomesstyle="font-size:20px;color:red"
(when size=20).style:="font-size="${size}px; color: red"
also becomesstyle="font-size:20px;color:red"
(when size=20).
For dynamic attributes, set the <attr>:=
attribute to any string or boolean expression:
disabled:="true"
becomes "disabled"disabled:="false"
does not add the disabled attributetype:="isNumeric ? 'number' : 'text'"
setstype="number"
if isNumeric is truthy, elsetype="text"
For example, this defines an <add-class>
component:
<template $name="custom-input" active:boolean="true">
<style>
.round { border-radius: 20px; }
.active { border: 1px solid red; }
</style>
<input
class:="['round', {active: active}]"
style:="{background-color: active ? 'lightblue' : 'white'}"
disabled:="!active">
</template>
When you add this to your page:
Active: <custom-input active="true"></custom-input>
Inactive: <custom-input active="false"></custom-input>
... it renders: