HTML Class Binding
The appearance of HTML elements are typically controlled by adding or removing classes dynamically. In MeltJS, you can bind to the class
to add or remove a set of CSS classes simultaneously.
Single Class Binding
You can toggle a class by binding to a Javascript expression. In the following example the completed
class will be added to the span
element if isCompleted
variable evaluates to true, removed if false.
Multiple Class Binding
MeltJS support a class.*={expression}
binding which allows you to easily add or remove multiple classes. The expression evaluation result can be either object or array.
Object Syntax
When the binding expression is evaluated as object, keys of the result get added to the class list when the expression given in the value evaluates to a truthy value, otherwise they are removed.
In the following example, foo
and bar
will be added to the span’s class list since their values evaluate to truthy values.
|
|
Array Syntax
When the binding expression is evaluated as an array, the items of the array will be added as classes.
In fact, array syntax is just a shorthand version of object syntax. The following code will the the same thing:
|
|
The binding syntax above may look strange at fist sight, but it’s the right syntax. The outer curly bracket is the binding syntax which means whatever inside is a Javascript expression and can be evaluated to a value. The inner curly bracket is just the object notation.
The array’s element can be object, in this case, MeltJS will apply the object rules to the items to modify the element’s classes.