Install vue-js-toggle-button via npm
masterInstall the package as a dependency in your project using npm.
npm install vue-js-toggle-button --saverepository·master·Indexed 21 days ago
https://github.com/euvl/vue-js-toggle-buttonA toggle button component for Vue.js 2+ that allows for the creation of switch buttons with customizable colors, labels, and animations. It supports v-model for two-way data binding, the :value prop for one-way data flow, and the @change event for state changes. The library can be integrated as a global plugin or a local component and is compatible with Nuxt.js SSR.
Install the package as a dependency in your project using npm.
npm install vue-js-toggle-button --saveYou can integrate the library into your Vue application in two ways: as a global plugin or as a specific component.
This makes the component available globally throughout your application.
This allows you to import only the component and register it locally or globally under a specific name.
// Option 1: Import plugin
import ToggleButton from 'vue-js-toggle-button'
Vue.use(ToggleButton)
// OR
// Option 2: Import component
import { ToggleButton } from 'vue-js-toggle-button'
Vue.component('ToggleButton', ToggleButton)To use the toggle button in a Nuxt.js project with Server-Side Rendering, you must register it as a plugin.
plugins/vue-js-toggle-button.js).nuxt.config.js.// nuxt.config.js
module.exports = {
plugins: ['~plugins/vue-js-toggle-button']
}
// plugins/vue-js-toggle-button.js
import Vue from 'vue'
import Button from 'vue-js-toggle-button'
Vue.use(Button)To use the ToggleButton component globally in your Vue.js application, use the install method provided by the default export. This registers the ToggleButton component under the name ToggleButton.
import Vue from 'vue';
import ToggleButtonPlugin from 'vue-js-toggle-button';
Vue.use(ToggleButtonPlugin);The ToggleButton can be used with v-model for two-way data binding, or with the :value prop for one-way data flow. You can also listen to the @change event to handle state changes manually.
<!-- Basic usage with event handler -->
<toggle-button @change="onChangeEventHandler"/>
<!-- Two-way data binding with v-model -->
<toggle-button v-model="myDataVariable"/>
<!-- Customized appearance and behavior -->
<toggle-button :value="false"
color="#82C7EB"
:sync="true"
:labels="true"/>
<!-- Custom labels for checked/unchecked states -->
<toggle-button :value="true"
:labels="{checked: 'Foo', unchecked: 'Bar'}"/>The following properties are available for the ToggleButton component:
| Name | Type | Default | Description |
| --- | --- | --- | --- |
| value | Boolean | false | Initial state of the toggle button |
| sync | Boolean | false | If set to `true`, will be watching changes in `value` property and overwrite the current state of the button whenever `value` prop changes |
| speed | Number | 300 | Transition time for the animation |
| disabled | Boolean | false | Button does not react on mouse events |
| color | [String, Object] | `#75C791` | If `String` - color of the button when checked <br>If `Object` - colors for the button when checked/unchecked or disabled<br>Example: `{checked: '#00FF00', unchecked: '#FF0000', disabled: '#CCCCCC'}` |
| css-colors | Boolean | false | If `true` - deactivates the setting of colors through inline styles in favor of using CSS styling |
| labels | [Boolean, Object] | false | If `Boolean` - shows/hides default labels ("on" and "off") <br>If `Object` - sets custom labels for both states. <br>Example: `{checked: 'Foo', unchecked: 'Bar'}` |
| switch-color | [String, Object] | `#BFCBD9` | If `String` - color or background property of the switch when checked <br>If `Object` - colors or background property for the switch when checked/uncheked <br>Example: `{checked: '#25EF02', unchecked: 'linear-gradient(red, yellow)'}` |
| width | Number | 50 | Width of the button |
| height | Number | 22 | Height of the button |
| margin | Number | 3 | Space between the switch and background border |
| name | String | undefined | Name to attach to the generated input field |
| font-size | Number | undefined | Font size |The following events are emitted by the ToggleButton component:
| Name | Description |
| --- | --- |
| change | Triggered whenever state of the component changes. <br>Contains: <br>`value` - state of the object <br>`srcEvent` - source click event |
| input | Input event for v-model |If you prefer to use the component locally within a specific Vue component rather than registering it globally, you can import the ToggleButton symbol directly from the package.
import { ToggleButton } from 'vue-js-toggle-button';
export default {
components: {
ToggleButton
}
};