Skip to content

Commit 5751b59

Browse files
authored
Merge pull request #12 from niels-bosman/feature/upgrades
Feature/upgrades
2 parents d5c5a80 + 482f931 commit 5751b59

File tree

9 files changed

+8120
-8446
lines changed

9 files changed

+8120
-8446
lines changed

README.md

Lines changed: 13 additions & 20 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,16 +20,15 @@ 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>
34-
import VueToggle from "./components/VueToggle";
31+
import VueToggle from "vue-toggle-component";
3532
3633
export default {
3734
name: 'App',
@@ -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

59-
| vue Version | vue-toggle-component version |
55+
| 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

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" id="1"/>
2+
<VueToggle title="Toggle me" name="VueToggle"/>
33
</template>
44

55
<script>

src/components/VueToggle.vue

Lines changed: 38 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

@@ -57,6 +73,12 @@ export default {
5773
background: none;
5874
}
5975
76+
.disabled & {
77+
&:hover {
78+
cursor: not-allowed;
79+
}
80+
}
81+
6082
.dark & {
6183
color: white;
6284
}
@@ -100,6 +122,14 @@ export default {
100122
will-change: left;
101123
}
102124
125+
.disabled & {
126+
opacity: 50%;
127+
128+
&:hover {
129+
cursor: not-allowed;
130+
}
131+
}
132+
103133
.dark & {
104134
background: #374151;
105135
}

vue-toggle-component/README.md

Lines changed: 13 additions & 20 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,16 +20,15 @@ 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>
34-
import VueToggle from "./components/VueToggle";
31+
import VueToggle from "vue-toggle-component";
3532
3633
export default {
3734
name: 'App',
@@ -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

59-
| vue Version | vue-toggle-component version |
55+
| 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/dist/vue-toggle-component.esm.js

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ var script = {
1111
type: Boolean,
1212
default: false
1313
},
14-
id: {
15-
type: String,
16-
required: true
14+
disabled: {
15+
type: Boolean,
16+
default: false
1717
},
1818
name: {
19-
type: [String, Boolean],
20-
default: false
19+
type: String,
20+
required: true
2121
},
2222
title: {
2323
type: String,
@@ -33,36 +33,44 @@ var script = {
3333
return {
3434
toggleState: this.toggled
3535
};
36-
}
36+
},
3737

38+
methods: {
39+
toggle() {
40+
if (this.disabled) return;
41+
this.toggleState = !this.toggleState;
42+
}
43+
44+
}
3845
};
3946

40-
const _withId = /*#__PURE__*/withScopeId("data-v-121077f4");
47+
const _withId = /*#__PURE__*/withScopeId("data-v-84279cda");
4148

4249
const render = /*#__PURE__*/_withId((_ctx, _cache, $props, $setup, $data, $options) => {
4350
return openBlock(), createBlock("section", {
44-
class: ["wrapper", {
45-
dark: $props.darkTheme
46-
}],
51+
class: [{
52+
'dark': $props.darkTheme,
53+
'disabled': $props.disabled
54+
}, "wrapper"],
4755
title: $props.title
4856
}, [withDirectives(createVNode("input", {
49-
id: $props.id,
50-
name: $props.name,
57+
id: `_${$props.name}`,
5158
"onUpdate:modelValue": _cache[1] || (_cache[1] = $event => $data.toggleState = $event),
59+
disabled: $props.disabled,
60+
name: $props.name,
5261
class: "toggle",
53-
type: "checkbox",
54-
onClick: _cache[2] || (_cache[2] = $event => $data.toggleState = !$data.toggleState)
55-
}, null, 8, ["id", "name"]), [[vModelCheckbox, $data.toggleState]]), createVNode("label", {
56-
for: $props.id,
57-
class: "toggler",
62+
type: "checkbox"
63+
}, null, 8, ["id", "disabled", "name"]), [[vModelCheckbox, $data.toggleState]]), createVNode("label", {
64+
for: $props.name,
5865
style: [$data.toggleState && {
5966
'background': $props.activeColor
60-
}]
67+
}],
68+
class: "toggler",
69+
onClick: _cache[2] || (_cache[2] = (...args) => $options.toggle && $options.toggle(...args))
6170
}, null, 12, ["for"]), createVNode("span", {
6271
class: "title",
63-
textContent: toDisplayString($props.title),
64-
onClick: _cache[3] || (_cache[3] = $event => $data.toggleState = !$data.toggleState)
65-
}, null, 8, ["textContent"])], 10, ["title"]);
72+
onClick: _cache[3] || (_cache[3] = (...args) => $options.toggle && $options.toggle(...args))
73+
}, toDisplayString($props.title), 1)], 10, ["title"]);
6674
});
6775

6876
function styleInject(css, ref) {
@@ -92,11 +100,11 @@ function styleInject(css, ref) {
92100
}
93101
}
94102

95-
var css_248z = "\n.wrapper[data-v-121077f4] {\n display: flex;\n flex-wrap: wrap;\n padding: 5px;\n}\n.wrapper > *[data-v-121077f4] {\n cursor: pointer;\n margin: 0 5px;\n}\n.title[data-v-121077f4] {\n display: inline-block;\n font-weight: 700;\n line-height: 2em;\n vertical-align: middle;\n}\n.title[data-v-121077f4]::selection {\n background: none;\n}\n.dark .title[data-v-121077f4] {\n color: white;\n}\n.toggle[data-v-121077f4] {\n display: none;\n}\n.toggle[data-v-121077f4]:after, .toggle + .toggler[data-v-121077f4] {\n box-sizing: border-box;\n}\n.toggle[data-v-121077f4]:after::selection, .toggle + .toggler[data-v-121077f4]::selection {\n background: none;\n}\n.toggle + .toggler[data-v-121077f4] {\n background: #f0f0f0;\n border-radius: 2em;\n display: block;\n height: 2em;\n outline: 0;\n padding: 2px;\n position: relative;\n transition: background 0.4s ease;\n user-select: none;\n width: 4em;\n will-change: background;\n}\n.toggle + .toggler[data-v-121077f4]:after {\n background: white;\n border-radius: 50%;\n content: \"\";\n display: block;\n height: 100%;\n left: 0;\n position: relative;\n transition: left 0.2s ease;\n width: 50%;\n will-change: left;\n}\n.dark .toggle + .toggler[data-v-121077f4] {\n background: #374151;\n}\n.toggle:checked + .toggler[data-v-121077f4]:after {\n left: 50%;\n}\n";
103+
var css_248z = "\n.wrapper[data-v-84279cda] {\n display: flex;\n flex-wrap: wrap;\n padding: 5px;\n}\n.wrapper > *[data-v-84279cda] {\n cursor: pointer;\n margin: 0 5px;\n}\n.title[data-v-84279cda] {\n display: inline-block;\n font-weight: 700;\n line-height: 2em;\n vertical-align: middle;\n}\n.title[data-v-84279cda]::selection {\n background: none;\n}\n.disabled .title[data-v-84279cda]:hover {\n cursor: not-allowed;\n}\n.dark .title[data-v-84279cda] {\n color: white;\n}\n.toggle[data-v-84279cda] {\n display: none;\n}\n.toggle[data-v-84279cda]:after, .toggle + .toggler[data-v-84279cda] {\n box-sizing: border-box;\n}\n.toggle[data-v-84279cda]:after::selection, .toggle + .toggler[data-v-84279cda]::selection {\n background: none;\n}\n.toggle + .toggler[data-v-84279cda] {\n background: #f0f0f0;\n border-radius: 2em;\n display: block;\n height: 2em;\n outline: 0;\n padding: 2px;\n position: relative;\n transition: background 0.4s ease;\n user-select: none;\n width: 4em;\n will-change: background;\n}\n.toggle + .toggler[data-v-84279cda]:after {\n background: white;\n border-radius: 50%;\n content: \"\";\n display: block;\n height: 100%;\n left: 0;\n position: relative;\n transition: left 0.2s ease;\n width: 50%;\n will-change: left;\n}\n.disabled .toggle + .toggler[data-v-84279cda] {\n opacity: 50%;\n}\n.disabled .toggle + .toggler[data-v-84279cda]:hover {\n cursor: not-allowed;\n}\n.dark .toggle + .toggler[data-v-84279cda] {\n background: #374151;\n}\n.toggle:checked + .toggler[data-v-84279cda]:after {\n left: 50%;\n}\n\n";
96104
styleInject(css_248z);
97105

98106
script.render = render;
99-
script.__scopeId = "data-v-121077f4";
107+
script.__scopeId = "data-v-84279cda";
100108

101109
// Import vue component
102110
// IIFE injects install function into component, allowing component

0 commit comments

Comments
 (0)