Skip to content

Commit 69f99d6

Browse files
committed
Changes
1 parent da20060 commit 69f99d6

File tree

5 files changed

+72
-32
lines changed

5 files changed

+72
-32
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ npm install vue-toggle-component --save
2424
#### Example
2525
```html
2626
<template>
27-
<VueToggle title="Toggle me" id="1"/>
27+
<VueToggle title="Toggle me" name="VueToggle"/>
2828
</template>
2929

3030
<script>

src/App.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<template>
2-
<VueToggle title="Toggle me" name="VueToggle" disabled/>
2+
<VueToggle title="Toggle me" name="VueToggle"/>
33
</template>
44

55
<script>

src/components/VueToggle.vue

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
11
<template>
2-
<section :class="{'dark': darkTheme, 'disabled': disabled}" :title="title" class="wrapper">
2+
<section
3+
:class="{'dark': darkTheme, 'disabled': disabled}"
4+
:title="title"
5+
class="wrapper"
6+
>
37
<input
48
:id="`_${name}`"
59
v-model="toggleState"
610
:disabled="disabled"
711
:name="name"
812
class="toggle"
913
type="checkbox"
10-
@click="toggleState = !toggleState"
1114
/>
12-
<label :for="name" :style="[toggleState && {'background': activeColor}]" class="toggler"/>
13-
<span class="title" @click="toggleState = !toggleState" v-text="title"/>
15+
<label
16+
:for="name"
17+
:style="[toggleState && {'background': activeColor}]"
18+
class="toggler"
19+
@click="toggle"
20+
/>
21+
<span class="title" @click="toggle">{{title}}</span>
1422
</section>
1523
</template>
1624

@@ -32,6 +40,13 @@ export default {
3240
toggleState: this.toggled
3341
}
3442
},
43+
44+
methods: {
45+
toggle() {
46+
if (this.disabled) return;
47+
this.toggleState = !this.toggleState
48+
}
49+
},
3550
}
3651
</script>
3752

@@ -58,6 +73,12 @@ export default {
5873
background: none;
5974
}
6075
76+
.disabled & {
77+
&:hover {
78+
cursor: not-allowed;
79+
}
80+
}
81+
6182
.dark & {
6283
color: white;
6384
}

vue-toggle-component/README.md

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,11 @@ vue-toggle-component makes an ease to use, lightweight and highly customizable t
66
![npm](https://img.shields.io/npm/v/vue-toggle-component)
77
![GitHub Repo stars](https://img.shields.io/github/stars/niels-bosman/vue-toggle-component?style=social)
88

9-
109
![Light theme](https://user-images.githubusercontent.com/25898715/116152862-c273f400-a6e6-11eb-8b4d-1017b92d14a5.gif)
1110
![Dark theme](https://user-images.githubusercontent.com/25898715/116152879-c7d13e80-a6e6-11eb-87b3-9b606184ba1e.gif)
12-
## Getting Started
1311

12+
## Getting Started
1413
### Installation
15-
1614
#### Installing the package
1715
```sh
1816
# install with yarn
@@ -22,12 +20,11 @@ yarn add vue-toggle-component
2220
npm install vue-toggle-component --save
2321
```
2422

25-
2623
### Usage
2724
#### Example
2825
```html
2926
<template>
30-
<VueToggle title="Toggle me" id="1"/>
27+
<VueToggle title="Toggle me" name="VueToggle"/>
3128
</template>
3229

3330
<script>
@@ -43,37 +40,33 @@ export default {
4340
```
4441

4542
#### Properties that vue-toggle-component uses
46-
| Property name | Description |
47-
| ------------- | ---------------------------- |
48-
| `activeColor` | The active color the checked toggler uses. This is a hex code. Default: `#9FD6AE` |
49-
| `darkTheme` | Give this prop to the component to use the dark mode feature as shown in the GIF above. |
50-
| `id` | The ID the input and the label use in the component. |
51-
| `name` | The name attributes that will be set in the input. |
52-
| `title` | The title that is shown after the toggler itself. |
53-
| `toggled` | The value the toggler has by default, by default this is `false` |
43+
| Property name | Type | Default | Required |Description |
44+
| ------------- | --------- | --------- | -------- | -------------------------------------------------------------------------------------------------- |
45+
| name | `String` | - | X | Set's the name value of the input (checkbox). Useful for persisting data. |
46+
| title | `String` | - | X | The title that is displayed next to the toggle. |
47+
| activeColor | `String` | `#9FD6AE` | - | The color that is displayed when the toggler is active. |
48+
| darkTheme | `Boolean` | `false` | - | Set's dark mode to active. (note that this will not change the background like in the preview GIF) |
49+
| disabled | `Boolean` | `false` | - | Disables the toggler. |
50+
| toggled | `Boolean` | `true` | - | Sets the default value for the toggler. |
5451

5552
### Vue version support
56-
5753
The main v1 version supports Vue 3.x only, for previous versions of Vue, check the following the table.
5854

5955
| Vue version | vue-toggle-component version |
6056
| ----------- | ---------------------------- |
6157
| `2.x` | `0.1.x` |
6258
| `3.x` | `1.x` |
63-
### Authors
6459

60+
### Authors
6561
#### Niels Bosman
66-
6762
www.github.com/niels-bosman
6863

6964
https://nielsbosman.dev
7065

7166
#### Mike van Egmond
72-
7367
www.github.com/mve
7468

7569
https://egmond.dev
7670

7771
### License
78-
7972
MIT

vue-toggle-component/src/vue-toggle-component.vue

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
11
<template>
2-
<section class="wrapper" :class="{dark: darkTheme}" :title="title">
2+
<section
3+
:class="{'dark': darkTheme, 'disabled': disabled}"
4+
:title="title"
5+
class="wrapper"
6+
>
37
<input
4-
:id="id"
5-
:name="name"
8+
:id="`_${name}`"
69
v-model="toggleState"
10+
:disabled="disabled"
11+
:name="name"
712
class="toggle"
813
type="checkbox"
9-
@click="toggleState = !toggleState"
1014
/>
11-
<label :for="id" class="toggler" :style="[toggleState && {'background': activeColor}]"/>
12-
<span class="title" v-text="title" @click="toggleState = !toggleState"/>
15+
<label
16+
:for="name"
17+
:style="[toggleState && {'background': activeColor}]"
18+
class="toggler"
19+
@click="toggle"
20+
/>
21+
<span class="title" @click="toggle">{{title}}</span>
1322
</section>
1423
</template>
1524

@@ -20,8 +29,8 @@ export default {
2029
props: {
2130
activeColor: {type: String, default: '#9FD6AE'},
2231
darkTheme: {type: Boolean, default: false},
23-
id: {type: String, required: true},
24-
name: {type: [String, Boolean], default: false},
32+
disabled: {type: Boolean, default: false },
33+
name: {type: String, required: true},
2534
title: {type: String, required: true},
2635
toggled: {type: Boolean, default: false},
2736
},
@@ -31,6 +40,13 @@ export default {
3140
toggleState: this.toggled
3241
}
3342
},
43+
44+
methods: {
45+
toggle() {
46+
if (this.disabled) return;
47+
this.toggleState = !this.toggleState
48+
}
49+
},
3450
}
3551
</script>
3652

@@ -53,6 +69,9 @@ export default {
5369
.title::selection {
5470
background: none;
5571
}
72+
.disabled .title:hover {
73+
cursor: not-allowed;
74+
}
5675
.dark .title {
5776
color: white;
5877
}
@@ -90,10 +109,17 @@ export default {
90109
width: 50%;
91110
will-change: left;
92111
}
112+
.disabled .toggle + .toggler {
113+
opacity: 50%;
114+
}
115+
.disabled .toggle + .toggler:hover {
116+
cursor: not-allowed;
117+
}
93118
.dark .toggle + .toggler {
94119
background: #374151;
95120
}
96121
.toggle:checked + .toggler:after {
97122
left: 50%;
98123
}
124+
99125
</style>

0 commit comments

Comments
 (0)