GithubHelp home page GithubHelp logo

drewjbartlett / vue-switches Goto Github PK

View Code? Open in Web Editor NEW
209.0 8.0 42.0 29 KB

An on/off switch component for Vue.js with theme support.

Home Page: http://drewjbartlett.com/demos/vue-switches/

Vue 22.04% CSS 77.96%
vue bulma theme-support bootstrap switch-control demo vue-components

vue-switches's Introduction

Vue Switches

npm npm

A Vue.js component for simple switches with theme support for bulma, bootstrap and custom themes. See a live demo here.

Installation

npm install vue-switches --save

Basic Usage

import Switches from 'vue-switches';

new Vue({

    components: {
        Switches
    },

    data () {
        return {
            enabled: false
        }
    }
};
<switches v-model="enabled"></switches>

Props

Prop Description
label A static label to always display whether on or off.
text-enabled The text that displays when enabled.
text-disabled The text that displays when disabled.
theme Which theme to use.
color Which color to use.
type-bold Bigger style.
emit-on-mount By default, a "changed" event is emitted when the component mounts. To disable this, set this to false.

Theme Support

Out of the box vue-switches supports a default, bulma, bootstrap themes. To use them you can do as follows:

Providing no theme or color props would render a switch of default theme and color.

 <switches v-model="enabled"></switches>

Available colors for default are default, red, blue, green, and yellow, and orange.

<switches v-model="enabled" theme="bulma" color="default"></switches>

Available colors for Bulma are default, primary, red, blue, green, and yellow.

In addition support for bootstrap can be used as follows:

<switches v-model="enabled" theme="bootstrap" color="danger"></switches>

Available colors for bootstrap are default, primary, success, info, warning, and danger.

Styles

Out of the box vue-switches has two styles: default and bold. By default the switch is not bold. To enable the bold style you can set type-bold to true like this:

<switches v-model="enabled" type-bold="true"></switches>

A demo of all themes and styles can be seen here.

Making Your Own Themes

Vue Switcher has a base class of .vue-switcher. For an unchecked switch a class of .vue-switcher--unchecked is applied. Lastly, for the theme and color props a class is also applied. So for a bulma theme of color primary the classes .vue-switcher-theme--bulma and .vue-switcher-color--primary.

This:

<switches v-model="enabled" type-bold="false" theme="custom" color="blue"></switches>

Would render the classes .vue-switcher-theme--custom and .vue-switcher-color--blue. Sass for this theme could look like:

.vue-switcher-theme--custom {
    &.vue-switcher-color--blue {
        div {
            background-color: lighten(blue, 10%);

            &:after {
                // for the circle on the switch
                background-color: darken(blue, 5%);
            }
        }

        &.vue-switcher--unchecked {
            div {
                background-color: lighten(blue, 30%);

                &:after {
                    background-color: lighten(blue, 10%);
                }
            }
        }
    }
}

For better understanding, below is how the class object is rendered.

classObject () {

            const {color, enabled, theme, typeBold, disabled} = this;

            return {
                'vue-switcher': true,
                ['vue-switcher--unchecked']: !enabled,
                ['vue-switcher--disabled']: disabled,
                ['vue-switcher--bold']: typeBold,
                ['vue-switcher--bold--unchecked']: typeBold && !enabled,
                [`vue-switcher-theme--${theme}`]: color,
                [`vue-switcher-color--${color}`]: color,
            };

        }

vue-switches's People

Contributors

dannyfeliz avatar drewjbartlett avatar francois2metz avatar rambii avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

vue-switches's Issues

Node SASS compiling error

I'm using laravel mix for building

95% emitting                                                                           

 ERROR  Failed to compile with 1 errors                                                                                                                                                                                    07:58:56

 error  in ./~/vue-switches/switches.vue

Module build failed: Error: Node Sass does not yet support your current environment: OS X 64-bit with Unsupported runtime (57)
For more information on which environments are supported please see:
https://github.com/sass/node-sass/releases/tag/v4.5.2
    at module.exports (/Users/fortael/PhpstormProjects/many-templates/node_modules/node-sass/lib/binding.js:13:13)
    at Object.<anonymous> (/Users/fortael/PhpstormProjects/many-templates/node_modules/node-sass/lib/index.js:14:35)
    at Module._compile (module.js:569:30)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:503:32)
    at tryModuleLoad (module.js:466:12)
    at Function.Module._load (module.js:458:3)
    at Module.require (module.js:513:17)
    at require (internal/module.js:11:18)
    at Object.<anonymous> (/Users/fortael/PhpstormProjects/many-templates/node_modules/sass-loader/lib/loader.js:3:14)
    at Module._compile (module.js:569:30)
    at Object.Module._extensions..js (module.js:580:10)
    at Module.load (module.js:503:32)
    at tryModuleLoad (module.js:466:12)
    at Function.Module._load (module.js:458:3)
    at Module.require (module.js:513:17)
    at require (internal/module.js:11:18)
    at loadLoader (/Users/fortael/PhpstormProjects/many-templates/node_modules/loader-runner/lib/loadLoader.js:13:17)
    at iteratePitchingLoaders (/Users/fortael/PhpstormProjects/many-templates/node_modules/loader-runner/lib/LoaderRunner.js:169:2)
    at iteratePitchingLoaders (/Users/fortael/PhpstormProjects/many-templates/node_modules/loader-runner/lib/LoaderRunner.js:165:10)
    at /Users/fortael/PhpstormProjects/many-templates/node_modules/loader-runner/lib/LoaderRunner.js:173:18
    at loadLoader (/Users/fortael/PhpstormProjects/many-templates/node_modules/loader-runner/lib/loadLoader.js:36:3)
    at iteratePitchingLoaders (/Users/fortael/PhpstormProjects/many-templates/node_modules/loader-runner/lib/LoaderRunner.js:169:2)
    at iteratePitchingLoaders (/Users/fortael/PhpstormProjects/many-templates/node_modules/loader-runner/lib/LoaderRunner.js:165:10)
    at /Users/fortael/PhpstormProjects/many-templates/node_modules/loader-runner/lib/LoaderRunner.js:173:18
    at loadLoader (/Users/fortael/PhpstormProjects/many-templates/node_modules/loader-runner/lib/loadLoader.js:36:3)
    at iteratePitchingLoaders (/Users/fortael/PhpstormProjects/many-templates/node_modules/loader-runner/lib/LoaderRunner.js:169:2)
    at runLoaders (/Users/fortael/PhpstormProjects/many-templates/node_modules/loader-runner/lib/LoaderRunner.js:362:2)
    at NormalModule.doBuild (/Users/fortael/PhpstormProjects/many-templates/node_modules/webpack/lib/NormalModule.js:179:3)
    at NormalModule.build (/Users/fortael/PhpstormProjects/many-templates/node_modules/webpack/lib/NormalModule.js:268:15)

 @ ./~/vue-style-loader!./~/css-loader!./~/vue-loader/lib/style-compiler?{"id":"data-v-6d1dbd09","scoped":false,"hasInlineConfig":true}!./~/sass-loader/lib/loader.js!./~/vue-loader/lib/selector.js?type=styles&index=0!./~/vue-switches/switches.vue 4:14-256
 @ ./~/vue-switches/switches.vue
 @ ./dist/_admin.js
 @ multi ./dist/_admin.js

Avoid mutating a prop

I'm getting the following error:

vue.esm.js?65d7:476 [Vue warn]: Avoid mutating a prop directly since the value will be overwritten whenever the parent component re-renders. Instead, use a data or computed property based on the prop's value. Prop being mutated: "value"

found in

---> at node_modules/vue-switch/switch.vue

The slider is working, but the v-model value is not changing.

Is the node-sass dependency necessary?

I installed the library and ended up with a 280MB node-sass dependency in my node modules directory. This was a deal-breaker for me and so I ended up just copying your source. Would it not be better to preprocess the scss to CSS before publishing?

Remove the selected prop ?

Hi!

The selected prop is not needed when using v-model. You have access to the value. Would you accept a pull request to remove it or you have a specific reason to keep it?

Styling Switches not working

Hello,

Thanks for making this component.

I am trying to style the switch using css. Below is my selector:

.parent .vue-switcher.vue-switcher-theme--bulma.vue-switcher-color--blue div{
        background-color: rgba(102, 0, 204, 0.685);
    }

.parent .vue-switcher.vue-switcher-theme--bulma.vue-switcher-color--blue div:after {
      background-color: #6600CC;
}

the style gets ignored during render.

Any form of help is welcome. Thanks

Bold-style switch is above baseline

In bold style, the switch is drawn a few pixels above the baseline, which makes it hard to have text and switches in one line:

image

Since it's already position: relative, an easy workaround is <switches style="top: 9px;">.

Fix attribute binding

When giving HTML attributes to the component (such as name or id), these are attached to the root of the component instead of being attached to the actual input (so that it can properly be used in a form).

There are two solutions :

  • add props that allow to passe name (and optionally id)
  • pass the attributes directly to the input

Incompatibility with vue 3

This plugin is not compatible with vue 3. Please provide a beta version that is compatible with vue 3

[solved] a question about new item added showing wrong color

<template>
                <tr v-for="(item, index) in items" :key="item.id">
                    <td>
                        <switches v-model="item.paid" :emit-on-mount="false" @input="togglePaid(item)" theme="bootstrap" :color="color(item.paid)"></switches>
                    </td>
</template>
<scripts>
            color(value){
                return value? 'success':'default'; 
            },
            add(item){
                this.items.unshift(item);
                this.hide_add_modal();
            },
</scripts>

when a new item is unshifted into items, even if the value is set false, the color is green 'success', but once reload the page, the color becomes default as expected.
solved: as dataForm send 'false' as string...

Touch support

When you open a switch on a mobile device, you should be able to change its state with swipe gesture.

make switches larger

Is there anyway to make the switches larger? I tried updating .vue-switcher--bold div but it didn't make any changes

Failed to load switches.vue

It keeps failing to load switches component due to syntax error, no idea why this is not working.

{ /node_modules/vue-switches/src/switches.vue:1
(function (exports, require, module, __filename, __dirname) { <template>
                                                              ^

SyntaxError: Unexpected token <
    at new Script (vm.js:74:7)
    at createScript (vm.js:246:10)
    at Object.runInThisContext (vm.js:298:10)
    at Module._compile (internal/modules/cjs/loader.js:670:28)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:713:10)
    at Module.load (internal/modules/cjs/loader.js:612:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:551:12)
    at Function.Module._load (internal/modules/cjs/loader.js:543:3)
    at Module.require (internal/modules/cjs/loader.js:650:17)
    at require (internal/modules/cjs/helpers.js:20:18)
    at r (/home/server2/1MM-apps/node_modules/vue-server-renderer/build.js:8330:16)
    at Object.<anonymous> (server-bundle.js:3677:18)
    at __webpack_require__ (webpack:/webpack/bootstrap 8f00aa7521ecf95414fa:25:0)
    at Object.<anonymous> (components/settings/Countdown.vue?32d3:1:0)
    at __webpack_require__ (webpack:/webpack/bootstrap 8f00aa7521ecf95414fa:25:0)
    at Object.<anonymous> (components/settings/Countdown.vue:1:0) statusCode: 500, name: 'SyntaxError' }

Support for IE11 (ES5)

Hello,

I've implemented vue-switches in a software solution, and concluded that vue-switches is not (yet) supported for IE11.

At https://github.com/drewjbartlett/vue-switches/blob/master/src/switches.vue#L58 IE11 is complaining about the mounted function. There are some other places where this happens.

The cause for this is because in ES6 the shorthand function for object initializers is introduced.

Reference: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Method_definitions

The VueJS documentation is suggesting to use the following style:

mounted: function () {
  this.$nextTick(function () {
    // Code that will run only after the
    // entire view has been rendered
  })
}

Reference: https://vuejs.org/v2/api/#mounted

Transcompiling the node_modules directory is not a really effecient option.

Option 1 - Manual "downgrade" the syntax to ES5

It would be fairly simple to implement this and support IE11 without the need of a transcompiler.

If there aren't any plans to fix this, I could fix it myself and send a PR.

Pros:

  • Able to support IE11

Cons:

  • Every time the source gets updated, it has to be checked if it is still compatible for IE11, if not then it has to be changed to older syntax

Option 2 - Transcompile the source

Transcompile the source to ES5 before distribubtion (eg. npm).

Pros:

  • Able to support IE11
  • Don't have to worry about using new syntax (ES6, ES7 etc.)

Cons:

  • Requires set-up to handle transcompiling (eg. Babel)

v-on events are not working

Hi!

I'm using your component, and I've noticed that v-on events (nor @change) are not working with your component, so I'm using watchers for my properties bound to switches.

The events support would be great improvement IMO.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.