Skip to content

Commit 0155c68

Browse files
jacwrightConduitry
authored andcommitted
Adding docs for the class directive (#351)
1 parent cf66986 commit 0155c68

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed

content/guide/07-element-directives.md

+102
Original file line numberDiff line numberDiff line change
@@ -517,3 +517,105 @@ Use actions for things like:
517517
}
518518
}
519519
```
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.
526+
527+
```html
528+
<!-- { title: 'Classes' } -->
529+
<ul class="links">
530+
<li class:active="url === '/'"><a href="/" on:click="goto(event)">Home</a></li>
531+
<li class:active="url.startsWith('/blog')"><a href="/blog/" on:click="goto(event)">Blog</a></li>
532+
<li class:active="url.startsWith('/about')"><a href="/about/" on:click="goto(event)">About</a></li>
533+
</ul>
534+
535+
<script>
536+
export default {
537+
methods: {
538+
goto(event) {
539+
event.preventDefault();
540+
this.set({ url: event.target.pathname });
541+
}
542+
}
543+
}
544+
</script>
545+
546+
<style>
547+
.links {
548+
list-style: none;
549+
}
550+
.links li {
551+
float: left;
552+
padding: 10px;
553+
}
554+
/* 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 <a href="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.
573+
574+
```html
575+
<!-- { title: 'Classes shorthand' } -->
576+
<div class:active class:is-selected class:isAdmin>
577+
<p>Active? {active}</p>
578+
<p>Selected? {isSelected}</p>
579+
</div>
580+
<button on:click="set({ active: !active })">Toggle Active</button>
581+
<button on:click="set({ isSelected: !isSelected })">Toggle Selected</button>
582+
<button on:click="set({ isAdmin: !isAdmin })">Toggle Admin</button>
583+
584+
<script>
585+
export default {
586+
computed: {
587+
// Because shorthand relfects the var name, you must use component.set({ "is-selected": true }) or use a computed
588+
// property like this. It might be better to avoid shorthand for class names which are not valid variable names.
589+
"is-selected": ({ isSelected }) => isSelected
590+
}
591+
}
592+
</script>
593+
594+
<style>
595+
div {
596+
width: 300px;
597+
border: 1px solid #ccc;
598+
background: #eee;
599+
margin-bottom: 10px;
600+
}
601+
.active {
602+
background: #fff;
603+
}
604+
.is-selected {
605+
border-color: #99bbff;
606+
box-shadow: 0 0 6px #99bbff;
607+
}
608+
.isAdmin {
609+
outline: 2px solid red;
610+
}
611+
</style>
612+
```
613+
614+
```json
615+
/* { hidden: true } */
616+
{
617+
"active": true,
618+
"isSelected": false,
619+
"isAdmin": false,
620+
}
621+
```

0 commit comments

Comments
 (0)