New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@vueform/toggle

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@vueform/toggle

Vue 3 toggle component with Tailwind support.

  • 2.0.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
14K
increased by4.55%
Maintainers
1
Weekly downloads
 
Created
Source
CircleCI npm bundle size (scoped version) GitHub Discord npm

Vue 3 Toggle with Tailwind support






Sponsors

Other libraries

  • @vueform/multiselect - Vue 3 multiselect component with single select, multiselect and tagging options.
  • @vueform/slider - Vue 3 slider component with multihandles, tooltips merging and formatting.

Toggle features

  • Vue 2 & 3 support
  • Tailwind & utility class support
  • No dependencies
  • Lightweight (<2 kB gzipped)
  • 100% coverage
  • TypeScript support
  • Accessibility support
  • ESM support
  • Configrable styles via CSS vars
  • On / Off labels

Demo

Check out our demo.

Installation

npm install @vueform/toggle

Usage with Vue 3

<template>
  <div>
    <Toggle v-model="value" />
  </div>
</template>

<script>
  import Toggle from '@vueform/toggle'

  export default {
    components: {
      Toggle,
    },
    data() {
      return {
        value: true
      }
    }
  }
</script>

<style src="@vueform/toggle/themes/default.css"></style>

Using with Vue 2

When using Vue 2 install @vue/composition-api via npm/yarn first:

npm i @vue/composition-api --save-dev

Then install the plugin for Vue:

import Vue from 'vue'
import VueCompositionAPI from '@vue/composition-api'

Vue.use(VueCompositionAPI)

After that make sure to change the imported Toggle module to:

import Toggle from '@vueform/toggle/dist/toggle.vue2.js'

Using with Nuxt.js

First you need install @nuxtjs/composition-api:

npm i @nuxtjs/composition-api --save-dev

Then you need to enable it as a module in nuxt.config.js:

{
  buildModules: [
    '@nuxtjs/composition-api'
  ]
}

After that make sure to change the imported module to Vue 2 version of Toggle, as Nuxt.js still depends on that:

import Toggle from '@vueform/toggle/dist/toggle.vue2.js'

Styling with CSS vars

The following CSS variables can be used to customize the toggle button when using default.css:

var(--toggle-width, 3rem);
var(--toggle-height, 1.25rem);
var(--toggle-border, 0.125rem) solid;
var(--toggle-font-size, 0.75rem);
var(--toggle-duration, 150ms);
var(--toggle-bg-on, #10b981);
var(--toggle-bg-off, #e5e7eb);
var(--toggle-bg-on-disabled, #d1d5db);
var(--toggle-bg-off-disabled, #e5e7eb);
var(--toggle-border-on, #10b981);
var(--toggle-border-off, #e5e7eb);
var(--toggle-border-on-disabled, #d1d5db);
var(--toggle-border-off-disabled, #e5e7eb);
var(--toggle-text-on, #ffffff);
var(--toggle-text-off, #374151);
var(--toggle-text-on-disabled, #9ca3af);
var(--toggle-text-off-disabled, #9ca3af);
var(--toggle-handle-enabled, #ffffff);
var(--toggle-handle-disabled, #f3f4f6);

You might override them globally:

:root {
  --toggle-bg-on: red;
  --toggle-border-on: red;
}

Or on an instance level:

<Toggle v-model="value" class="my-toggle-red" />
<Toggle v-model="value" class="my-toggle-blue" />
.my-toggle-red {
  --toggle-bg-on: red;
  --toggle-border-on: red;
}

.my-toggle-blue {
  --toggle-bg-on: blue;
  --toggle-border-on: blue;
}

Styling with Tailwind CSS

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:

<Toggle v-model="value" :classes="{
  container: 'inline-block',
  toggle: 'flex w-12 h-5 rounded-full relative cursor-pointer transition items-center box-content border-2 text-xs leading-none',
  toggleOn: 'bg-green-500 border-green-500 justify-start text-white',
  toggleOff: 'bg-gray-200 border-gray-200 justify-end text-gray-700',
  toggleOnDisabled: 'bg-gray-300 border-gray-300 justify-start text-gray-400 cursor-not-allowed',
  toggleOffDisabled: 'bg-gray-200 border-gray-200 justify-end text-gray-400 cursor-not-allowed',
  handle: 'inline-block bg-white w-5 h-5 top-0 rounded-full absolute transition-all',
  handleOn: 'left-full transform -translate-x-full',
  handleOff: 'left-0',
  handleOnDisabled: 'bg-gray-100 left-full transform -translate-x-full',
  handleOffDisabled: 'bg-gray-100 left-0',
  label: 'text-center w-8 border-box whitespace-nowrap select-none',
}" />

If the button is enabled and on the toggle and toggleOn classes will be added to the toggle div: 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

Likewise if the the button is disabled and on the toggle and toggleOnDisabled classes will be added: 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

The same is true off states and handle.

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.

Accessibility

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.

<div>
  <label id="toggle-label">Turn on notifications</label>
  <Toggle v-model="value" labelledby="toggle-label" describedby="toggle-description" />
</div>
<small id="toggle-description">Turn this on if you'd like to receive in-app notifications.</small>

Support

Join our Discord channel or open an issue.

Basic props

NameTypeDefaultDescription
idstringtoggleThe id attribute of input field. Make sure to customize when using more toggles on a single page.
namestringtoggleThe name attribute of input field.
disabledbooleanfalseWhether the toggle should be disabled.
requiredbooleanfalseWhether the HTML5 required attribute should be used for toggle (using an invisible fake input).
falseValuestring|number|booleanfalseThe value when the toggle is off.
trueValuestring|number|booleantrueThe value when toggle is on.
offLabelstringThe label when toggle is off.
onLabelstringThe label when toggle is on.
labelledbystringThe aria-labelledby attribute.
describedbystringThe aria-describedby attribute.
classesobjectThe object of classes to be applied for different parts of the toggle. Default: {
  container: 'toggle-container',
  toggle: 'toggle',
  toggleOn: 'toggle-on',
  toggleOff: 'toggle-off',
  toggleOnDisabled: 'toggle-on-disabled',
  toggleOffDisabled: 'toggle-off-disabled',
  handle: 'toggle-handle',
  handleOn: 'toggle-handle-on',
  handleOff: 'toggle-handle-off',
  handleOnDisabled: 'toggle-handle-on-disabled',
  handleOffDisabled: 'toggle-handle-off-disabled',
  label: 'toggle-label',
}.
The default value can be used with default.css and style can be customized with CSS variables. Alternatively this can be overridden with utility classes like Tailwind CSS.

Events

EventAttributesDescription
@changevalueEmitted when the toggle changes.

Slots

SlotAttributesDescription
labelchecked, classListThe 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.

Examples

Default toggle

<Toggle
  v-model="value"
/>

JSFiddle - Example #1

Toggle with labels

<Toggle
  v-model="value"
  on-label="On"
  off-label="Off"
/>

JSFiddle - Example #2

Toggle with custom value

<Toggle
  v-model="value"
  true-value="on"
  false-value="off"
/>

JSFiddle - Example #3

Toggle with custom labels

<Toggle
  v-model="value"
>
  <template v-slot:label="{ checked, classList }">
    <span :class="classList.label">{{ checked ? 'On' : 'Off' }}</span>
  </template>
</Toggle>

JSFiddle - Example #4

About Vueform

Vueform streamlines the entire form building process in Vue 2 & 3. It comes with 30+ elements, file uploads, element nesting, 50+ validators, conditions, form steps, i18n including reactive configuration, API access, ESM modules and many more. Check out our live demos or see all the features and sign up for beta to get early access.

License

MIT

FAQs

Package last updated on 03 Jun 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc