Skip to content

Commit da1a2b3

Browse files
committed
2.0 release
1 parent 4058ea7 commit da1a2b3

26 files changed

+615
-329
lines changed

CHANGELOG.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,11 @@
1-
## v1.1.0
1+
## v2.0.0
2+
3+
> `2021-06-03`
4+
5+
### 🎉 Features
6+
- **BREAKING:** Removed `width`, `height`, `speed`, `colors`, `fontSize` properties and `off`, `on` slots.
7+
- **BREAKING:** Completely updated `default` style and classnames.
8+
- Added `required`, `classes`, `labelledby`, `describedby` properties and `label` slot.
29

310
> `2021-01-06`
411

README.md

Lines changed: 121 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
<img alt="npm" src="https://img.shields.io/npm/v/@vueform/toggle">
2222
</a>
2323

24-
<h1>Vue 3 Toggle</h1>
24+
<h1>Vue 3 Toggle with Tailwind support</h1>
2525

2626
<a href="https://vueform.com?ref=github" target="_blank">
2727
<br>
@@ -53,17 +53,19 @@
5353
## Toggle features
5454

5555
* Vue 2 & 3 support
56+
* Tailwind & utility class support
5657
* No dependencies
5758
* Lightweight (<2 kB gzipped)
5859
* 100% coverage
60+
* TypeScript support
61+
* Accessibility support
5962
* ESM support
60-
* Fully configurable
61-
* Dynamic styles
63+
* Configrable styles via CSS vars
6264
* On / Off labels
6365

6466
## Demo
6567

66-
Check out our [demo](https://jsfiddle.net/quc03rf4/).
68+
Check out our [demo](https://jsfiddle.net/4ckev9rx/).
6769

6870
## Installation
6971

@@ -145,6 +147,102 @@ After that make sure to change the imported module to Vue 2 version of Toggle, a
145147
import Toggle from '@vueform/toggle/dist/toggle.vue2.js'
146148
```
147149

150+
## Styling with CSS vars
151+
152+
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:
206+
207+
``` vue
208+
<Toggle v-model="value" :classes="{
209+
container: 'inline-block',
210+
toggle: 'flex w-12 h-5 rounded-full relative cursor-pointer transition items-center box-content border-2 text-xs leading-none',
211+
toggleOn: 'bg-green-500 border-green-500 justify-start text-white',
212+
toggleOff: 'bg-gray-200 border-gray-200 justify-end text-gray-700',
213+
toggleOnDisabled: 'bg-gray-300 border-gray-300 justify-start text-gray-400 cursor-not-allowed',
214+
toggleOffDisabled: 'bg-gray-200 border-gray-200 justify-end text-gray-400 cursor-not-allowed',
215+
handle: 'inline-block bg-white w-5 h-5 top-0 rounded-full absolute transition-all',
216+
handleOn: 'left-full transform -translate-x-full',
217+
handleOff: 'left-0',
218+
handleOnDisabled: 'bg-gray-100 left-full transform -translate-x-full',
219+
handleOffDisabled: 'bg-gray-100 left-0',
220+
label: 'text-center w-8 border-box whitespace-nowrap select-none',
221+
}" />
222+
```
223+
224+
If the button is **enabled** and **on** the `toggle` and `toggleOn` classes will be added to the toggle div:
225+
```flex w-12 h-5 rounded-full relative cursor-pointer transition items-center box-content border-2 text-xs leading-none bg-green-500 border-green-500 justify-start text-white```
226+
227+
Likewise if the the button is **disabled** and **on** the `toggle` and `toggleOnDisabled` classes will be added:
228+
```flex w-12 h-5 rounded-full relative cursor-pointer transition items-center box-content border-2 text-xs leading-none bg-gray-300 border-gray-300 justify-start text-gray-400 cursor-not-allowed```
229+
230+
The same is true `off` states and `handle`.
231+
232+
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>
241+
<Toggle v-model="value" labelledby="toggle-label" describedby="toggle-description" />
242+
</div>
243+
<small id="toggle-description">Turn this on if you'd like to receive in-app notifications.</small>
244+
```
245+
148246
## Support
149247

150248
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
156254
| **id** | `string` | `toggle` | The `id` attribute of input field. Make sure to customize when using more toggles on a single page. |
157255
| **name** | `string` | `toggle` | The `name` attribute of input field. |
158256
| **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). |
159258
| **falseValue** | `string\|number\|boolean` | `false` | The value when the toggle is `off`. |
160259
| **trueValue** | `string\|number\|boolean` | `true` | The value when toggle is `on`. |
161260
| **offLabel** | `string` | | The label when toggle is `off`. |
162261
| **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/>&nbsp;&nbsp;`background: {`<br/>&nbsp;&nbsp;&nbsp;&nbsp;`on: '#41b883',`<br/>&nbsp;&nbsp;&nbsp;&nbsp;`off: '#d4e0e7',`<br/>&nbsp;&nbsp;&nbsp;&nbsp;`disabled: '#d4e0e7'`<br/>&nbsp;&nbsp;`},`<br/>&nbsp;&nbsp;`text: {`<br/>&nbsp;&nbsp;&nbsp;&nbsp;`on: '#ffffff',`<br/>&nbsp;&nbsp;&nbsp;&nbsp;`off: '#80878c',`<br/>&nbsp;&nbsp;&nbsp;&nbsp;`disabled: '#80878c'`<br/>&nbsp;&nbsp;`},`<br/>&nbsp;&nbsp;`handle: {`<br/>&nbsp;&nbsp;&nbsp;&nbsp;`on: '#ffffff',`<br/>&nbsp;&nbsp;&nbsp;&nbsp;`off: '#ffffff',`<br/>&nbsp;&nbsp;&nbsp;&nbsp;`disabled: '#f2faff'`<br/>&nbsp;&nbsp;`}` |
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>&nbsp;&nbsp;`container: 'toggle-container',`<br>&nbsp;&nbsp;`toggle: 'toggle',`<br>&nbsp;&nbsp;`toggleOn: 'toggle-on',`<br>&nbsp;&nbsp;`toggleOff: 'toggle-off',`<br>&nbsp;&nbsp;`toggleOnDisabled: 'toggle-on-disabled',`<br>&nbsp;&nbsp;`toggleOffDisabled: 'toggle-off-disabled',`<br>&nbsp;&nbsp;`handle: 'toggle-handle',`<br>&nbsp;&nbsp;`handleOn: 'toggle-handle-on',`<br>&nbsp;&nbsp;`handleOff: 'toggle-handle-off',`<br>&nbsp;&nbsp;`handleOnDisabled: 'toggle-handle-on-disabled',`<br>&nbsp;&nbsp;`handleOffDisabled: 'toggle-handle-off-disabled',`<br>&nbsp;&nbsp;`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). |
168265

169266
## Events
170267

@@ -174,17 +271,16 @@ Join our [Discord channel](https://discord.gg/WhX2nG6GTQ) or [open an issue](htt
174271

175272
## Slots
176273

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. |
181277

182278
## Examples
183279

184280
* [Default toggle](#default-toggle)
185281
* [Toggle with labels](#toggle-with-labels)
186282
* [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)
188284

189285
### Default toggle
190286

@@ -194,53 +290,45 @@ Join our [Discord channel](https://discord.gg/WhX2nG6GTQ) or [open an issue](htt
194290
/>
195291
```
196292

197-
[JSFiddle - Example #1](https://jsfiddle.net/quc03rf4/)
293+
[JSFiddle - Example #1](https://jsfiddle.net/4ckev9rx/)
198294

199295
### Toggle with labels
200296

201297
``` vue
202298
<Toggle
203299
v-model="value"
204-
id="example2"
205300
on-label="On"
206301
off-label="Off"
207302
/>
208303
```
209304

210-
[JSFiddle - Example #2](https://jsfiddle.net/quc03rf4/)
305+
[JSFiddle - Example #2](https://jsfiddle.net/4ckev9rx/)
211306

212-
### Toggle with custom values
307+
### Toggle with custom value
213308

214309
``` vue
215310
<Toggle
216311
v-model="value"
217-
id="example3"
218312
true-value="on"
219313
false-value="off"
220314
/>
221315
```
222316

223-
[JSFiddle - Example #3](https://jsfiddle.net/quc03rf4/)
317+
[JSFiddle - Example #3](https://jsfiddle.net/4ckev9rx/)
224318

225-
### Toggle with custom styles
319+
### Toggle with custom labels
226320

227321
``` vue
228322
<Toggle
229323
v-model="value"
230-
id="example4"
231-
font-size="15px"
232-
:width="80"
233-
:height="30"
234-
:speed="500"
235-
:colors="{
236-
background: {
237-
on: '#35495e'
238-
}
239-
}"
240-
/>
324+
>
325+
<template v-slot:label="{ checked, classList }">
326+
<span :class="classList.label">{{ checked ? 'On' : 'Off' }}</span>
327+
</template>
328+
</Toggle>
241329
```
242330

243-
[JSFiddle - Example #4](https://jsfiddle.net/quc03rf4/)
331+
[JSFiddle - Example #4](https://jsfiddle.net/4ckev9rx/)
244332

245333
## About Vueform
246334

dist/toggle.global.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)