You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: content/guide/07-element-directives.md
+102
Original file line number
Diff line number
Diff line change
@@ -517,3 +517,105 @@ Use actions for things like:
517
517
}
518
518
}
519
519
```
520
+
521
+
### Classes
522
+
523
+
Classes let you toggle element classes on and off. To use classes add the directive `class` followed by a colon and the class name you want toggled (`class:the-class-name="anExpression"`). The expression inside the directive's quotes will be evaluated and toggle the class on and off depending on the truthiness of the expression's result. You can only add class directives to elements.
524
+
525
+
This example adds the class `active` to `<li>` elements when the `url` property matches the path their links target.
/* classes added this way are processed with encapsulated styles, no need for :global() */
555
+
.active {
556
+
background: #eee;
557
+
}
558
+
</style>
559
+
```
560
+
561
+
```json
562
+
/* { hidden: true } */
563
+
{
564
+
"url": "/"
565
+
}
566
+
```
567
+
568
+
Classes will work with an existing class attribute on your element. If you find yourself adding multiple ternary statements inside a class attribute, classes can simplify your component. Classes are recognized by the compiler and <ahref="guide#scoped-styles">scoped correctly</a>.
569
+
570
+
If your class name is the same as a property in your component's state, you can use the shorthand of a class binding which drops the expression (`class:myProp`).
571
+
572
+
Note that class names with dashes in them do not usually make good shorthand classes since the property will also need a dash in it. The example below uses a computed property to make working with this easier, but it may be easier to not use the shorthand in cases like this.
0 commit comments