UNPKG

2.18 kBMarkdownView Raw
1# CSS Classes manipulation
2
3You can set CSS classes in a static way:
4```
5{DIV class: "elevation-8 round"}
6```
7
8or in a bounded way:
9```
10{DIV class: {Bind style}}
11```
12
13You can also bind the existence of a given class to a boolean property:
14```
15// Add class `elevation-8` if and only if `pressed === true`.
16{DIV class.elevation-8: {Bind pressed}}
17```
18```
19// Add class `highlight` if and only if `pressed === false`.
20{DIV class.|highlight: {Bind pressed}}
21```
22```
23// If `pressed === true`, add class `elevation-8`, otherwise add class 'elevation-2'.
24{DIV class.elevation-8|elevation-2: {Bind pressed}}
25```
26
27And if you need a more complex logic to set classes, you can use code behind:
28```
29// As soon as `flat` or `pressed` has changed, call teh code behind function
30// `computeClass()` to return an array of classes to set.
31{DIV class.*: {Bind [flat, pressed] computeClasses}}
32```
33
34It is possible to define a list of functions:
35```
36{DIV class.*: [
37 {Bind [flat, pressed] computeClassesWhenPressed}
38 {Bind [flat, enabled] computeClassesWhenEnabled}
39]}
40```
41
42The functions (for instance `computeClassesWhenPressed`) are defined in the CODE_BEHIND object.
43They just need to return an array of CSS classes names.
44
45Imagine the following XJS:
46```js
47{View DIV
48 view.attribs: {
49 flat: {boolean}
50 pressed: {boolean}
51 }
52 class.*: {Bind [flat, pressed] computeClasses}
53}
54```
55And the JS code:
56```js
57var CODE_BEHIND: {
58 computeClasses: function() {
59 if( !this.pressed ) return;
60 return this.flat ? ["flat", "pressed"] : "pressed";
61 }
62};
63```
64
65Then you get this:
66
67| Trigger | Applied classes |
68| ---------------- | --------------- |
69| flat := false | |
70| pressed := true | pressed |
71| flat := true | flat pressed |
72| pressed := false | flat |
73
74You're probably surprised to get `flat` in the last row of this table.
75In fact, the algorithm is as follows:
76* If this is not the first time the current attribute triggers the `computeClasses` function then
77 * Remove the CSS classes previously added.
78* Add the CSS classes in the returned array.
79