-
Notifications
You must be signed in to change notification settings - Fork 0
/
classmap-and-stylemap.js
73 lines (65 loc) · 1.71 KB
/
classmap-and-stylemap.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import {LitElement, html, css} from 'lit';
import { classMap } from 'lit/directives/class-map.js';
import { styleMap } from 'lit/directives/style-map.js';
/*
Adapted from https://lit.dev/playground/#sample=examples/directive-class-map
and https://lit.dev/playground/#sample=examples/directive-style-map
*/
export class MyElement extends LitElement {
static properties = {
enabled: {type: Boolean},
hidden: {type: Boolean},
};
// Standard Lit way of applying styles, used by classMap in this example
static styles = css`
.enabled {
background: lightgreen;
}
.hidden {
opacity: 0.2;
}
.padded {
padding: 10px;
}
`;
constructor() {
super();
this.enabled = true;
this.hidden = false;
}
render() {
const classesForClassMap = {
enabled: this.enabled,
hidden: this.hidden,
padded: true,
};
const stylesForStyleMap = {
backgroundColor: this.enabled ? 'lightgreen' : 'transparent',
opacity: this.hidden ? '0.2' : '1',
padding: '10px',
};
return html`
<h3>classMap directive example</h3>
<p class=${classMap(classesForClassMap)}>Classy text</p>
<hr>
<h3>styleMap directive example</h3>
<p style=${styleMap(stylesForStyleMap)}>Hello style!</p>
<hr>
<label>
<input type="checkbox" .checked=${this.enabled} @change=${this.toggleEnabled}>
Enabled
</label>
<label>
<input type="checkbox" .checked=${this.hidden} @change=${this.toggleHidden}>
Hidden
</label>
`;
}
toggleEnabled() {
this.enabled = !this.enabled;
}
toggleHidden() {
this.hidden = !this.hidden;
}
}
customElements.define('my-element', MyElement);