Skip to content

Commit abed92c

Browse files
committed
Initial Release
1 parent 84c5678 commit abed92c

File tree

5 files changed

+272
-69
lines changed

5 files changed

+272
-69
lines changed

README.md

Lines changed: 82 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,19 @@
1-
# vue-toggle-button
1+
# Vue Toggle Button
2+
3+
![Vue Toggle Button](./screenshot.png?raw=true "Vue Toggle Button")
4+
5+
## Table of contents
6+
---
7+
- [Getting started](#getting-started)
8+
- [Usage](#usage)
9+
- [Options](#options)
10+
- [Handle Event](#handle-event)
11+
- [Custom Label](#custom-label)
12+
- [Default State](#default-state)
13+
- [Disable Button](#disable-button)
14+
15+
## Getting started
216

3-
## Project setup
417
```
518
npm install
619
```
@@ -10,15 +23,76 @@ npm install
1023
npm run serve
1124
```
1225

13-
### Compiles and minifies for production
26+
## Usage
27+
```js
28+
<template>
29+
<div>
30+
<ToggleButton id="switch" />
31+
</div>
32+
</template>
33+
34+
<script>
35+
import ToggleButton from './components/ToggleButton.vue'
36+
37+
export default {
38+
components: {
39+
ToggleButton
40+
}
41+
}
42+
</script>
1443
```
15-
npm run build
44+
45+
## Options
46+
47+
### Handle Event
48+
```js
49+
<template>
50+
<div>
51+
<ToggleButton
52+
id="switch"
53+
v-on:change="eventHandler"
54+
/>
55+
</div>
56+
</template>
57+
58+
<script>
59+
import ToggleButton from './components/ToggleButton.vue'
60+
61+
export default {
62+
components: {
63+
ToggleButton
64+
},
65+
66+
methods: {
67+
eventHandler(value) {
68+
console.log(value);
69+
}
70+
}
71+
}
72+
</script>
1673
```
1774

18-
### Lints and fixes files
75+
### Custom Label
76+
```html
77+
<ToggleButton
78+
id="switch"
79+
labelEnableText="Live"
80+
labelDisableText="Draft"
81+
/>
1982
```
20-
npm run lint
83+
84+
### Default State
85+
```html
86+
<ToggleButton
87+
id="switch"
88+
:defaultState="true"
89+
/>
2190
```
2291

23-
### Customize configuration
24-
See [Configuration Reference](https://cli.vuejs.org/config/).
92+
### Disable Button
93+
```html
94+
<ToggleButton
95+
id="switch"
96+
:disabled="true"
97+
/>
98+
```

screenshot.png

54.7 KB
Loading

src/App.vue

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,51 @@
11
<template>
22
<div id="app">
33
<img alt="Vue logo" src="./assets/logo.png">
4-
<HelloWorld msg="Welcome to Your Vue.js App"/>
4+
<div class="box">
5+
<p><strong>Basic Toggle Button</strong></p>
6+
<ToggleButton />
7+
</div>
8+
<div class="box">
9+
<p><strong>Another Toggle Button with default state</strong></p>
10+
<ToggleButton id="secondary" :defaultState="true" />
11+
</div>
12+
<div class="box">
13+
<p><strong>Handle Event</strong></p>
14+
<div :class="{'active': active}" class="toggle_container">
15+
<ToggleButton id="event_handle" v-on:change="triggerEvent" />
16+
</div>
17+
</div>
18+
<div class="box">
19+
<p><strong>Custom Label Example</strong></p>
20+
<ToggleButton id="label_example" labelEnableText="True" labelDisableText="False" />
21+
</div>
22+
<div class="box">
23+
<p><strong>Disabled Example</strong></p>
24+
<ToggleButton id="disabled_example" :disabled="true" />
25+
</div>
526
</div>
627
</template>
728

829
<script>
9-
import HelloWorld from './components/HelloWorld.vue'
30+
import ToggleButton from './components/ToggleButton.vue'
1031
1132
export default {
1233
name: 'App',
34+
1335
components: {
14-
HelloWorld
36+
ToggleButton
37+
},
38+
39+
data() {
40+
return {
41+
active: false
42+
}
43+
},
44+
45+
methods: {
46+
triggerEvent(value) {
47+
this.active = value;
48+
}
1549
}
1650
}
1751
</script>
@@ -25,4 +59,21 @@ export default {
2559
color: #2c3e50;
2660
margin-top: 60px;
2761
}
62+
63+
.box {
64+
text-align: center;
65+
margin-bottom: 30px;
66+
}
67+
68+
.toggle_container {
69+
margin: 0px auto;
70+
background: #efefef;
71+
width: 120px;
72+
padding: 10px 0;
73+
border-radius: 30px;
74+
transition: all .25s;
75+
}
76+
.toggle_container.active {
77+
background: #e9ffef;
78+
}
2879
</style>

src/components/HelloWorld.vue

Lines changed: 0 additions & 58 deletions
This file was deleted.

src/components/ToggleButton.vue

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
<template>
2+
<label :for="id + '_button'" :class="{'active': isActive}" class="toggle__button">
3+
<span v-if="isActive" class="toggle__label">{{ enableText }}</span>
4+
<span v-if="! isActive" class="toggle__label">{{ disabledText }}</span>
5+
6+
<input type="checkbox" :disabled="disabled" :id="id + '_button'" v-model="checkedValue">
7+
<span class="toggle__switch"></span>
8+
</label>
9+
</template>
10+
11+
<script>
12+
export default {
13+
props: {
14+
disabled: {
15+
type: Boolean,
16+
default: false
17+
},
18+
19+
labelEnableText: {
20+
type: String,
21+
default: 'On'
22+
},
23+
24+
labelDisableText: {
25+
type: String,
26+
default: 'Off'
27+
},
28+
29+
id: {
30+
type: String,
31+
default: 'primary'
32+
},
33+
34+
defaultState: {
35+
type: Boolean,
36+
default: false
37+
}
38+
},
39+
40+
data() {
41+
return {
42+
currentState: this.defaultState
43+
}
44+
},
45+
46+
computed: {
47+
isActive() {
48+
return this.currentState;
49+
},
50+
51+
enableText() {
52+
return this.labelEnableText;
53+
},
54+
55+
disabledText() {
56+
return this.labelDisableText;
57+
},
58+
59+
checkedValue: {
60+
get() {
61+
return this.defaultState;
62+
},
63+
64+
set(newValue) {
65+
this.currentState = newValue;
66+
this.$emit('change', newValue);
67+
}
68+
}
69+
}
70+
}
71+
</script>
72+
73+
<style scoped>
74+
.toggle__button {
75+
vertical-align: middle;
76+
user-select: none;
77+
cursor: pointer;
78+
}
79+
.toggle__button input[type="checkbox"] {
80+
opacity: 0;
81+
position: absolute;
82+
width: 1px;
83+
height: 1px;
84+
}
85+
.toggle__button .toggle__switch {
86+
display:inline-block;
87+
height:12px;
88+
border-radius:6px;
89+
width:40px;
90+
background: #BFCBD9;
91+
box-shadow: inset 0 0 1px #BFCBD9;
92+
position:relative;
93+
margin-left: 10px;
94+
transition: all .25s;
95+
}
96+
97+
.toggle__button .toggle__switch::after,
98+
.toggle__button .toggle__switch::before {
99+
content: "";
100+
position: absolute;
101+
display: block;
102+
height: 18px;
103+
width: 18px;
104+
border-radius: 50%;
105+
left: 0;
106+
top: -3px;
107+
transform: translateX(0);
108+
transition: all .25s cubic-bezier(.5, -.6, .5, 1.6);
109+
}
110+
111+
.toggle__button .toggle__switch::after {
112+
background: #4D4D4D;
113+
box-shadow: 0 0 1px #666;
114+
}
115+
.toggle__button .toggle__switch::before {
116+
background: #4D4D4D;
117+
box-shadow: 0 0 0 3px rgba(0,0,0,0.1);
118+
opacity:0;
119+
}
120+
121+
.active .toggle__switch {
122+
background: #adedcb;
123+
box-shadow: inset 0 0 1px #adedcb;
124+
}
125+
126+
.active .toggle__switch::after,
127+
.active .toggle__switch::before{
128+
transform:translateX(40px - 18px);
129+
}
130+
131+
.active .toggle__switch::after {
132+
left: 23px;
133+
background: #53B883;
134+
box-shadow: 0 0 1px #53B883;
135+
}
136+
</style>

0 commit comments

Comments
 (0)