<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">
      <Tick class="checkmark" />
    </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
>
  .label {
    font-size: 0.875rem;
    display: flex;
    align-items: center;
    cursor: pointer;

    &.disabled {
      cursor: not-allowed;
    }
  }

  .checkbox {
    position: absolute;
    width: 0;
    height: 0;
    padding: 0;
    margin: 0;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    border: 1px;
    visibility: visible;
    white-space: nowrap;

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

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

        & .checkmark {
          display: block;
        }
      }

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

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

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

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

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

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

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