1515 :aria-checked =" toggleState"
1616 :aria-disabled =" disabled"
1717 :aria-readonly =" disabled"
18+ :aria-labelledby =" labelId"
1819 :style =" toggleState && {background: activeColor}"
1920 class =" m-toggle__content"
2021 role =" checkbox"
21- @click =" toggleState = !toggleState "
22+ @click =" toggle "
2223 />
2324 <label
25+ :id =" labelId"
2426 :for =" id"
2527 :style =" [{fontSize: `${fontSize}px`, fontWeight}]"
2628 class =" m-toggle__label"
2729 >
28- {{title}}
30+ {{ title }}
2931 </label >
3032 </section >
3133</template >
@@ -35,14 +37,14 @@ export default {
3537 name: ' VueToggle' ,
3638
3739 props: {
38- activeColor: {type: String , default: ' #9FD6AE' },
39- darkTheme: { type: Boolean , default: false },
40- disabled: { type: Boolean , default: false },
41- fontSize: { type: Number , default: 16 },
42- fontWeight: { type: String , default: ' normal' },
43- name: { type: String , required: true },
44- title: { type: String , required: true },
45- toggled: { type: Boolean , default: false },
40+ activeColor: { type: String , default: ' #9FD6AE' },
41+ darkTheme: { type: Boolean , default: false },
42+ disabled: { type: Boolean , default: false },
43+ fontSize: { type: Number , default: 16 },
44+ fontWeight: { type: String , default: ' normal' },
45+ name: { type: String , required: true },
46+ title: { type: String , required: true },
47+ toggled: { type: Boolean , default: false },
4648 },
4749
4850 data () {
@@ -51,16 +53,28 @@ export default {
5153 }
5254 },
5355
56+ methods: {
57+ toggle () {
58+ if (this .disabled ) return ;
59+ this .toggleState = ! this .toggleState
60+ }
61+ },
62+
5463 computed: {
5564 id () {
56- return ` _${ this .name .replace (/ / g , ' ' ).toLowerCase ()} ` ;
57- }
65+ return this .name .replace (/ / g , ' ' ).toLowerCase ();
66+ },
67+ labelId () {
68+ return ` ${ this .id } -label`
69+ },
5870 },
5971}
6072 </script >
6173
6274<style lang="scss" scoped>
6375.m-toggle {
76+ $self : &;
77+ $toggle-spacing : 2px ;
6478 display : flex ;
6579 align-items : center ;
6680 margin : 0 -5px ;
@@ -70,8 +84,6 @@ export default {
7084 margin : 0 5px ;
7185 }
7286
73- $self : &;
74-
7587 & __label {
7688 user-select : none ;
7789
@@ -90,7 +102,7 @@ export default {
90102 & :checked {
91103 & + #{$self } __content {
92104 & :after {
93- left : 50% ;
105+ left : calc ( 50% + #{ $toggle-spacing } ) ;
94106 }
95107 }
96108 }
@@ -102,7 +114,7 @@ export default {
102114 border-radius : 2em ;
103115 height : 2em ;
104116 outline : 0 ;
105- padding : 2 px ;
117+ padding : $toggle-spacing ;
106118 transition : background .4s ease ;
107119 width : 4em ;
108120 will-change : background ;
@@ -116,16 +128,13 @@ export default {
116128 left : 0 ;
117129 position : relative ;
118130 transition : left .2s ease ;
119- width : 50% ;
131+ width : calc ( 50% - #{ $toggle-spacing } ) ;
120132 will-change : left ;
121133 }
122134
123135 .disabled & {
124136 opacity : 50% ;
125-
126- & :hover {
127- cursor : not-allowed ;
128- }
137+ cursor : not-allowed ;
129138 }
130139
131140 .dark & {
0 commit comments