-
Notifications
You must be signed in to change notification settings - Fork 12
/
toggle-btn.vue
165 lines (160 loc) · 3.85 KB
/
toggle-btn.vue
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<template>
<!--Binding css variables to use as height/width of :before -> the slider -->
<div class="toggle-slider" :style="getStyleObject">
<label class="switch">
<input v-model="isActive" type="checkbox" @click="setNewToggleState">
<span class="track">
<span class="handle"></span>
</span>
</label>
</div>
</template>
<script>
const PROP_KEYS = {
DIAMETER: 'diameter',
COLOR: 'color',
BORDER_RADIOUS: 'borderRadius',
BORDER_WIDTH: 'borderWidth',
WIDTH: 'width',
HEIGHT: 'height',
ACTIVE_COLOR: 'activeColor'
};
export default {
name: 'toggleButton',
props: ["options"],
data() {
return {
handle: {
diameter: 30, // optional
distance: 40, // optional
color: '#fff', // optional
borderRadius: "50%", // optional
},
track: {
color: '#ccc', // optional
width: 70, // optional
height: 30, // optional
activeColor: '#2196F3', // optional
borderWidth: 0, // optional
borderRadius: '34px', // optional
},
isActive: true,
}
},
computed: {
getHandleDistance: function() {
let handleDistance = 0;
if (this.options && this.options.handle && this.options.track) {
handleDistance = this.options.track.width - this.options.handle.diameter;
} else {
handleDistance = this.handle.distance;
}
return handleDistance;
},
getStyleObject: function(){
let styleObj = {
'--handle-diameter' : this.handle.diameter + 'px',
'--handle-color': this.handle.color,
'--handle-border-radius': this.handle.borderRadius,
'--handle-distance': this.getHandleDistance + 'px',
'--track-color': this.track.color,
'--track-width': this.track.width + 'px',
'--track-height': this.track.height + 'px',
'--track-active-color': this.track.activeColor,
'--track-border-width': this.track.borderWidth + 'px',
'--track-border-radius': this.track.borderRadius
};
return styleObj;
}
},
methods: {
setConfigData() {
if (!this.options) {
return;
}
if (this.options.handle) {
[
PROP_KEYS.COLOR,
PROP_KEYS.DIAMETER,
PROP_KEYS.BORDER_RADIOUS
].forEach(element => {
this.setBindedProp('handle', element);
});
}
if (this.options.track) {
// TODO - track border width, track border radius - 24.12.18
[
PROP_KEYS.COLOR,
PROP_KEYS.WIDTH,
PROP_KEYS.HEIGHT,
PROP_KEYS.ACTIVE_COLOR,
PROP_KEYS.BORDER_WIDTH,
PROP_KEYS.BORDER_RADIOUS
].forEach(element => {
this.setBindedProp('track', element);
});
}
if (this.options.isActive) {
this.isActive = this.options.isActive;
}
},
setBindedProp(key, propToBind) {
if (this.option[key][propToBind]) {
this[key][propToBind] = this.option[key][propToBind];
}
},
setNewToggleState() {
this.isActive = !this.isActive;
this.$emit('setIsActive', this.isActive);
}
},
created() {
this.setConfigData();
}
}
</script>
<style scoped lang="scss">
.switch {
position: relative;
display: inline-block;
width: var(--track-width);
height: var(--track-height);
input {
display: none;
}
.track {
display: flex;
align-items: center;
position: absolute;
width: 100%;
height: 100%;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: var(--track-color);
cursor: pointer;
border: var(--track-border-width) solid var(--track-color);
border-radius: var(--track-border-radius);
transition: .4s;
.handle {
display: flex;
width: var(--handle-diameter);
height: var(--handle-diameter);
background-color: var(--handle-color);
border-radius: var(--handle-border-radius);
transition: .4s;
}
}
input:checked+.track {
background-color: var(--track-active-color);
border: var(--track-border-width) solid var(--track-active-color);
}
input:focus+.track {
box-shadow: 0 0 1px var(--track-active-color);
}
input:checked+.track>.handle {
transform: translateX(var(--handle-distance));
}
}
</style>