<template>
  <label
    class="label"
    :class="{
      disabled,
    }"
    @click.stop
  >
    <input
      type="checkbox"
      class="checkbox"
      :value="value"
      :checked="value"
      :name="name"
      :disabled="disabled"
      @click="onClick"
    />
    <span class="appearance">
      <span class="checkmark-wrapper">
        <Tick class="checkmark" />
      </span>
    </span>
    <slot />
  </label>
</template>

<script>
import Tick from './Tick'

export default {
  components: {
    Tick,
  },

  props: {
    name: {
      type: String,
    },
    value: {
      type: Boolean,
    },
    disabled: {
      type: Boolean,
    },
  },

  methods: {
    onClick(e) {
      this.$emit('input', !this.value)
      this.$emit('click', e)
    },
  },
}
</script>

<style
  lang="postcss"
  scoped
>
  @keyframes top-to-bottom {
    from {
      opacity: 0;
      transform: translate3d(0, -50%, 0);
    }

    /* stylelint-disable-next-line block-no-empty */
    to {}
  }

  .label {
    display: flex;
    align-items: center;
    line-height: 16px;
    font-size: 0.875rem;
    color: var(--fake-black);
    user-select: none;
    cursor: pointer;

    &.disabled {
      cursor: not-allowed;
    }

    .dark-mode & {
      color: white;
    }

    &:hover .checkbox:not(:checked):not(:disabled) + .appearance {
      border-color: var(--purple-light-mode);

      .dark-mode & {
        border-color: var(--blue-dark-mode);
      }
    }
  }

  .checkbox {
    display: none;

    &:disabled + .appearance {
      opacity: 0.4;
    }

    &:checked {
      & + .appearance {
        border-color: var(--purple-light-mode);

        & .checkmark-wrapper {
          background-color: var(--purple-light-mode);

          & .checkmark {
            display: block;
            animation: top-to-bottom 0.15s;
          }
        }

        .dark-mode & {
          border-color: var(--blue-dark-mode);

          & .checkmark-wrapper {
            background-color: var(--blue-dark-mode);
          }
        }
      }
    }
  }

  .appearance {
    display: inline-block;
    height: 16px;
    width: 16px;
    margin-right: 0.5rem;
    border: 1px solid var(--grey);
    border-radius: 2px;
    box-sizing: border-box;

    .dark-mode & {
      border-color: var(--grey-dark-mode);
    }
  }

  .checkmark-wrapper {
    display: block;
    width: 100%;
    height: 100%;
    background-color: initial;
    transition: background-color 0.15s;

    .dark-mode & {
      background-color: rgba(255, 255, 255, 0.2);
    }
  }

  .checkmark {
    display: none;
    width: 100%;
    height: 100%;
    stroke: white;

    .dark-mode & {
      stroke: var(--fake-black);
    }
  }
</style>
