You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The following CSS variables can be used to customize the toggle button when using `default.css`:
153
+
```CSS
154
+
var(--toggle-width, 3rem);
155
+
var(--toggle-height, 1.25rem);
156
+
var(--toggle-border, 0.125rem) solid;
157
+
var(--toggle-font-size, 0.75rem);
158
+
var(--toggle-duration, 150ms);
159
+
var(--toggle-bg-on, #10b981);
160
+
var(--toggle-bg-off, #e5e7eb);
161
+
var(--toggle-bg-on-disabled, #d1d5db);
162
+
var(--toggle-bg-off-disabled, #e5e7eb);
163
+
var(--toggle-border-on, #10b981);
164
+
var(--toggle-border-off, #e5e7eb);
165
+
var(--toggle-border-on-disabled, #d1d5db);
166
+
var(--toggle-border-off-disabled, #e5e7eb);
167
+
var(--toggle-text-on, #ffffff);
168
+
var(--toggle-text-off, #374151);
169
+
var(--toggle-text-on-disabled, #9ca3af);
170
+
var(--toggle-text-off-disabled, #9ca3af);
171
+
var(--toggle-handle-enabled, #ffffff);
172
+
var(--toggle-handle-disabled, #f3f4f6);
173
+
```
174
+
175
+
You might override them globally:
176
+
177
+
```css
178
+
:root {
179
+
--toggle-bg-on: red;
180
+
--toggle-border-on: red;
181
+
}
182
+
```
183
+
184
+
Or on an instance level:
185
+
186
+
```vue
187
+
<Toggle v-model="value" class="my-toggle-red" />
188
+
<Toggle v-model="value" class="my-toggle-blue" />
189
+
```
190
+
191
+
```css
192
+
.my-toggle-red {
193
+
--toggle-bg-on: red;
194
+
--toggle-border-on: red;
195
+
}
196
+
197
+
.my-toggle-blue {
198
+
--toggle-bg-on: blue;
199
+
--toggle-border-on: blue;
200
+
}
201
+
```
202
+
203
+
## Styling with Tailwind CSS
204
+
205
+
The `Toggle` component accepts a `classes` property where you can override default class names. In this case you don't need to required `default.css`. Here's a default styling for Tailwind CSS:
This way you can customize the parts of the toggle button without having to worry about over-riding the same type of utility classes, like backgrounds or text colors.
233
+
234
+
## Accessibility
235
+
236
+
By default the `on` and `off` labels are being read by the screenreaders. If you provide the `labelledby` property that will be read instead of the labels. You might also add a `describedby` property to provide further description.
237
+
238
+
```vue
239
+
<div>
240
+
<label id="toggle-label">Turn on notifications</label>
<small id="toggle-description">Turn this on if you'd like to receive in-app notifications.</small>
244
+
```
245
+
148
246
## Support
149
247
150
248
Join our [Discord channel](https://discord.gg/WhX2nG6GTQ) or [open an issue](https://github.com/vueform/toggle/issues).
@@ -156,15 +254,14 @@ Join our [Discord channel](https://discord.gg/WhX2nG6GTQ) or [open an issue](htt
156
254
|**id**|`string`|`toggle`| The `id` attribute of input field. Make sure to customize when using more toggles on a single page. |
157
255
|**name**|`string`|`toggle`| The `name` attribute of input field. |
158
256
|**disabled**|`boolean`|`false`| Whether the toggle should be disabled. |
257
+
|**required**|`boolean`|`false`| Whether the HTML5 required attribute should be used for toggle (using an invisible fake input). |
159
258
|**falseValue**|`string\|number\|boolean`|`false`| The value when the toggle is `off`. |
160
259
|**trueValue**|`string\|number\|boolean`|`true`| The value when toggle is `on`. |
161
260
|**offLabel**|`string`|| The label when toggle is `off`. |
162
261
|**onLabel**|`string`|| The label when toggle is `on`. |
163
-
|**width**|`number`|`54`| The width of the toggle in `px`. |
164
-
|**height**|`number`|`24`| The height of the toggle in `px`. |
165
-
|**speed**|`number`|`300`| The speed of toggle switch in `milliseconds`. |
166
-
|**colors**|`object`|| The colors of toggle. Default:<br/>`{`<br/> `background: {`<br/> `on: '#41b883',`<br/> `off: '#d4e0e7',`<br/> `disabled: '#d4e0e7'`<br/> `},`<br/> `text: {`<br/> `on: '#ffffff',`<br/> `off: '#80878c',`<br/> `disabled: '#80878c'`<br/> `},`<br/> `handle: {`<br/> `on: '#ffffff',`<br/> `off: '#ffffff',`<br/> `disabled: '#f2faff'`<br/> `}`|
167
-
|**fontSize**|`string`|`13px`| The font size of toggle labels. |
262
+
|**labelledby**|`string`|| The `aria-labelledby` attribute. |
263
+
|**describedby**|`string`|| The `aria-describedby` attribute. |
264
+
|**classes**|`object`|| The object of classes to be applied for different parts of the toggle. Default: `{`<br> `container: 'toggle-container',`<br> `toggle: 'toggle',`<br> `toggleOn: 'toggle-on',`<br> `toggleOff: 'toggle-off',`<br> `toggleOnDisabled: 'toggle-on-disabled',`<br> `toggleOffDisabled: 'toggle-off-disabled',`<br> `handle: 'toggle-handle',`<br> `handleOn: 'toggle-handle-on',`<br> `handleOff: 'toggle-handle-off',`<br> `handleOnDisabled: 'toggle-handle-on-disabled',`<br> `handleOffDisabled: 'toggle-handle-off-disabled',`<br> `label: 'toggle-label',`<br>`}`.<br> The default value can be used with `default.css` and style can be customized with [CSS variables](#styling-with-css-vars). Alternatively this can be overridden with utility classes like [Tailwind CSS](#styling-with-tailwind-css). |
168
265
169
266
## Events
170
267
@@ -174,17 +271,16 @@ Join our [Discord channel](https://discord.gg/WhX2nG6GTQ) or [open an issue](htt
174
271
175
272
## Slots
176
273
177
-
| Slot | Description |
178
-
| --- | --- |
179
-
|**off**| Rendered when the toggle is `off` (by default `offLabel`). |
180
-
|**on**| Rendered when the toggle is `on` (by default `onLabel`). |
274
+
| Slot | Attributes | Description |
275
+
| --- | --- | --- |
276
+
|**label**|`checked`, `classList`| The label of the toggle element. The `checked` attribute determines whether the toggle is *on* or *off* so you can display the label accordingly. The `classList` contains the resolved class names. |
181
277
182
278
## Examples
183
279
184
280
*[Default toggle](#default-toggle)
185
281
*[Toggle with labels](#toggle-with-labels)
186
282
*[Toggle with custom values](#toggle-with-custom-values)
187
-
*[Toggle with custom styles](#toggle-with-custom-styles)
283
+
*[Toggle with custom labels](#toggle-with-custom-labels)
188
284
189
285
### Default toggle
190
286
@@ -194,53 +290,45 @@ Join our [Discord channel](https://discord.gg/WhX2nG6GTQ) or [open an issue](htt
194
290
/>
195
291
```
196
292
197
-
[JSFiddle - Example #1](https://jsfiddle.net/quc03rf4/)
293
+
[JSFiddle - Example #1](https://jsfiddle.net/4ckev9rx/)
198
294
199
295
### Toggle with labels
200
296
201
297
```vue
202
298
<Toggle
203
299
v-model="value"
204
-
id="example2"
205
300
on-label="On"
206
301
off-label="Off"
207
302
/>
208
303
```
209
304
210
-
[JSFiddle - Example #2](https://jsfiddle.net/quc03rf4/)
305
+
[JSFiddle - Example #2](https://jsfiddle.net/4ckev9rx/)
211
306
212
-
### Toggle with custom values
307
+
### Toggle with custom value
213
308
214
309
```vue
215
310
<Toggle
216
311
v-model="value"
217
-
id="example3"
218
312
true-value="on"
219
313
false-value="off"
220
314
/>
221
315
```
222
316
223
-
[JSFiddle - Example #3](https://jsfiddle.net/quc03rf4/)
317
+
[JSFiddle - Example #3](https://jsfiddle.net/4ckev9rx/)
0 commit comments