Skip to content

Commit 9d849e6

Browse files
committed
added disabled state
1 parent 9e1122f commit 9d849e6

File tree

5 files changed

+56
-5
lines changed

5 files changed

+56
-5
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ Join our [Discord channel](https://discord.gg/WhX2nG6GTQ) or [open an issue](htt
116116
| --- | --- | --- | --- |
117117
| **id** | `string` | `toggle` | The `id` attribute of input field. Make sure to customize when using more toggles on a single page. |
118118
| **name** | `string` | `toggle` | The `name` attribute of input field. |
119+
| **disabled** | `boolean` | `false` | Whether the toggle should be disabled. |
119120
| **falseValue** | `string\|number\|boolean` | `false` | The value when the toggle is `off`. |
120121
| **trueValue** | `string\|number\|boolean` | `true` | The value when toggle is `on`. |
121122
| **offLabel** | `string` | | The label when toggle is `off`. |
@@ -125,9 +126,12 @@ Join our [Discord channel](https://discord.gg/WhX2nG6GTQ) or [open an issue](htt
125126
| **speed** | `number` | `300` | The speed of toggle switch in `milliseconds`. |
126127
| **offBackground** | `string` | `#d4e0e7` | The color of background when toggle is `off`. |
127128
| **onBackground** | `string` | `#41b883` | The color of background when toggle is `on`. |
128-
| **offTextColor** | `string` | `#888888` | The color of text when toggle is `off`. |
129+
| **disabledBackground** | `string` | `#d4e0e7` | The color of background when toggle is `disabled`. |
130+
| **offTextColor** | `string` | `#80878c` | The color of text when toggle is `off`. |
129131
| **onTextColor** | `string` | `#ffffff` | The color of text when toggle is `on`. |
132+
| **disabledTextColor** | `string` | `#80878c` | The color of text when toggle is `disabled`. |
130133
| **handleColor** | `string` | `#ffffff` | The background color of toggle handle. |
134+
| **disabledHandleColor** | `string` | `#f2faff` | The background color of toggle handle when toggle is `disabled`. |
131135
| **fontSize** | `string` | `13px` | The font size of toggle labels. |
132136

133137
## Events

src/Toggle.vue

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
type="checkbox"
88
:name="name"
99
:id="id"
10+
:disabled="disabled"
1011
:checked="checked"
1112
:trueValue="trueValue"
1213
:falseValue="falseValue"
@@ -61,6 +62,11 @@
6162
required: false,
6263
default: 'toggle'
6364
},
65+
disabled: {
66+
type: Boolean,
67+
required: false,
68+
default: false,
69+
},
6470
falseValue: {
6571
type: [String, Number, Boolean],
6672
required: false,
@@ -106,21 +112,36 @@
106112
required: false,
107113
default: '#41b883'
108114
},
115+
disabledBackground: {
116+
type: String,
117+
required: false,
118+
default: '#d4e0e7'
119+
},
109120
offTextColor: {
110121
type: String,
111122
required: false,
112-
default: '#888888'
123+
default: '#80878c'
113124
},
114125
onTextColor: {
115126
type: String,
116127
required: false,
117128
default: '#ffffff'
118129
},
130+
disabledTextColor: {
131+
type: String,
132+
required: false,
133+
default: '#80878c'
134+
},
119135
handleColor: {
120136
type: String,
121137
required: false,
122138
default: '#ffffff'
123139
},
140+
disabledHandleColor: {
141+
type: String,
142+
required: false,
143+
default: '#f2faff'
144+
},
124145
fontSize: {
125146
type: String,
126147
required: false,

src/composables/useStyle.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,21 @@ import { computed, toRefs } from 'composition-api'
22

33
export default function useStyle (props, context, dependencies)
44
{
5-
const { width, height, speed, offBackground, onBackground, offTextColor,
6-
onTextColor, fontSize, handleColor, } = toRefs(props)
5+
const { width, height, speed, offBackground, onBackground, disabledBackground, offTextColor,
6+
onTextColor, disabledTextColor, fontSize, handleColor, disabledHandleColor, } = toRefs(props)
77

88
// ============== COMPUTED ==============
99

1010
const cssVars = computed(() => {
1111
let cssVars = {
1212
'--toggle-off-background': offBackground.value,
1313
'--toggle-on-background': onBackground.value,
14+
'--toggle-disabled-background': disabledBackground.value,
1415
'--toggle-off-text-color': offTextColor.value,
1516
'--toggle-on-text-color': onTextColor.value,
17+
'--toggle-disabled-text-color': disabledTextColor.value,
1618
'--toggle-handle-color': handleColor.value,
19+
'--toggle-disabled-handle-color': disabledHandleColor.value,
1720
'--toggle-height': height.value + 'px',
1821
'--toggle-width': width.value + 'px',
1922
'--toggle-speed': (speed.value / 1000) + 's',

tests/unit/composables/useStyle.spec.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,24 @@ describe('useStyle', () => {
1010
speed: 500,
1111
offBackground: '#e8e8e8',
1212
onBackground: 'blue',
13+
disabledBackground: 'red',
1314
offTextColor: '#111111',
1415
onTextColor: '#f1f1f1',
16+
disabledTextColor: '#222222',
1517
handleColor: '#000000',
18+
disabledHandleColor: '#121212',
1619
fontSize: '14px'
1720
})
1821

1922
expect(toggle.vm.cssVars).toStrictEqual({
2023
'--toggle-off-background': '#e8e8e8',
2124
'--toggle-on-background': 'blue',
25+
'--toggle-disabled-background': 'red',
2226
'--toggle-off-text-color': '#111111',
2327
'--toggle-on-text-color': '#f1f1f1',
28+
'--toggle-disabled-text-color': '#222222',
2429
'--toggle-handle-color': '#000000',
30+
'--toggle-disabled-handle-color': '#121212',
2531
'--toggle-height': '50px',
2632
'--toggle-width': '200px',
2733
'--toggle-speed': '0.5s',

themes/default.scss

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252

5353
.toggle-off {
5454
display: flex;
55-
color: #888888;
55+
color: #80878c;
5656
color: var(--toggle-off-text-color);
5757
}
5858
}
@@ -79,5 +79,22 @@
7979
display: none;
8080
}
8181
}
82+
83+
&:disabled + label {
84+
background: #d4e0e7;
85+
background: var(--toggle-disabled-background);
86+
cursor: not-allowed;
87+
88+
&:before {
89+
background: #f2faff;
90+
background: var(--toggle-disabled-handle-color);
91+
}
92+
93+
.toggle-on,
94+
.toggle-off {
95+
color: #80878c;
96+
color: var(--toggle-disabled-text-color);
97+
}
98+
}
8299
}
83100
}

0 commit comments

Comments
 (0)