<template>
  <div class="label" for="">
    <input type="checkbox" :name="name" :class="{active:currentValue}" @click="currentValue=!currentValue">
    <div class="toggle">
      <span class="bar"></span>
      <span class="circle"></span>
    </div>
  </div>
</template>

<script>
  export default {
    name: 'ZghToggle',
    props: {
      value: {
        type: Boolean,
        default: false
      },
      name:{
        type: String,
        default: 'switch'
      }
    },
    data() {
      return {
        currentValue: this.value,
      }
    },
    watch: {
      currentValue(val) {
        this.$emit('input', val);
        this.$emit('change', val);
      },
      value() {
        this.currentValue = this.value;

      }
    }
  }
</script>

<style lang="less" scoped>
  @import url("../../assets/config.less");

  input[type="checkbox"] {
    display: block;
    -webkit-appearance: none;
    outline: none;
    position: absolute;
    left: 0;
    top: 0;
    opacity: 0;
    height: 100%;
    width: 100%;
    z-index: 1;
    &.active{
      & + .toggle {
        .bar {
          background-color: @common_cl;
          opacity: .7;
        }
        .circle {
          left: 100%;
          transform: translateX(-100%);
          background-color: @common_cl;
        }
      }
    }
  }

  .toggle {
    transition: all 450ms cubic-bezier(0.23, 1, 0.32, 1) 0ms;

    position: relative;
    display: block;
    flex-shrink: 0;
    width: 36px;
    margin-right: 0px;
    margin-left: 8px;
    padding: 4px 0px 6px 2px;
    .bar {
      display: block;
      transition: all 450ms cubic-bezier(0.23, 1, 0.32, 1) 0ms;
      width: 100%;
      height: 14px;
      border-radius: 30px;
      background-color: rgb(189, 189, 189);
    }
    .circle {
      color: rgba(0, 0, 0, 0.87);
      background-color: rgb(245, 245, 245);
      transition: all 450ms cubic-bezier(0.23, 1, 0.32, 1) 0ms;
      box-sizing: border-box;
      font-family: Roboto, sans-serif;
      -webkit-tap-highlight-color: rgba(0, 0, 0, 0);
      box-shadow: rgba(0, 0, 0, 0.12) 0px 1px 6px, rgba(0, 0, 0, 0.12) 0px 1px 4px;
      border-radius: 50%;
      position: absolute;
      top: 1px;
      left: 0px;
      width: 20px;
      height: 20px;
      line-height: 24px;
    }
  }
</style>
