-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswitch.lit.js
106 lines (100 loc) · 2.29 KB
/
switch.lit.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import {LitElement, html, css} from 'https://unpkg.com/@polymer/lit-element@latest/lit-element.js?module'
class Switch extends LitElement {
static get styles() {
return [
css`
:host {
--switch: hsl(0, 0%, 73%);
--switch-active: hsl(231, 48%, 48%);
--switch-knob-radius: 10px;
--switch-radius: 7px;
--switch-offset: calc(var(--switch-knob-radius) - var(--switch-radius));
}
.switch {
position: relative;
display: block;
width: 36px;
height: calc(2 * var(--switch-knob-radius));
margin: var(--switch-offset) 0;
}
.switch input:focus ~ .switchbg {
outline: 1px solid rgb(77, 144, 254);
}
.switch input:checked ~ .switchbg{
background: var(--switch-active);
filter: saturate(50%) brightness(150%)
}
.switch input:checked ~ .nub {
background: var(--switch-active);
right: 0
}
.switch input ~ .switchbg {
background-color: var(--switch);
}
.switchbg {
display: block;
width: 36px;
height: calc(2 * var(--switch-radius));
border-radius: var(--switch-radius);
position: absolute;
top: 0;
margin: var(--switch-offset) 0;
}
.switch input {
width: 36px;
height: calc(2 * var(--switch-knob-radius));
margin: 0;
opacity: 0;
position: absolute;
}
.nub {
width: calc(2 * var(--switch-knob-radius));
height: calc(2 * var(--switch-knob-radius));
position: absolute;
top: 0;
border-radius: 50%;
background: white;
z-index: 1;
box-shadow: 0px 0px 5px rgba(0,0,0,0.2);
}
`
]
}
static get properties() {
return {
name: {
type: String
},
checked: {
type: Boolean
},
}
}
constructor() {
super()
this.name = "switch"
this.checked = false
}
toggleChecked() {
this.checked = !this.checked;
this.toggleEvent();
}
toggleEvent() {
let event = new CustomEvent('toggle', {
detail: {
message: 'Switch Toggled'
}
});
this.dispatchEvent(event);
}
render() {
return html`
<label class="switch" role="tab">
<input type="checkbox" name="${this.name}" id="nub" ?checked=${this.checked} @change="${this.toggleChecked}">
<span class="nub"></span>
<div class="switchbg"></div>
</label>
`
}
}
customElements.define("mk-switch", Switch)