Toggle.vue 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <template>
  2. <button
  3. @click="toggle"
  4. type="button"
  5. :aria-pressed="value.toString()"
  6. :class="this.value ? 'bg-cyan-500' : 'bg-grey-300'"
  7. class="relative inline-flex flex-shrink-0 h-6 w-11 border-2 border-transparent rounded-full cursor-pointer transition-colors ease-in-out duration-200 focus:outline-none"
  8. >
  9. <span class="sr-only">Use setting</span>
  10. <span
  11. :class="this.value ? 'translate-x-5' : 'translate-x-0'"
  12. class="relative inline-block h-5 w-5 rounded-full bg-white shadow transform ring-0 transition ease-in-out duration-200"
  13. >
  14. <span
  15. :class="this.value ? 'opacity-0 ease-out duration-100' : 'opacity-100 ease-in duration-200'"
  16. class="absolute inset-0 h-full w-full flex items-center justify-center transition-opacity"
  17. aria-hidden="true"
  18. >
  19. <svg class="h-3 w-3 text-grey-400" fill="none" viewBox="0 0 12 12">
  20. <path
  21. d="M4 8l2-2m0 0l2-2M6 6L4 4m2 2l2 2"
  22. stroke="currentColor"
  23. stroke-width="2"
  24. stroke-linecap="round"
  25. stroke-linejoin="round"
  26. />
  27. </svg>
  28. </span>
  29. <span
  30. :class="this.value ? 'opacity-100 ease-in duration-200' : 'opacity-0 ease-out duration-100'"
  31. class="absolute inset-0 h-full w-full flex items-center justify-center transition-opacity"
  32. aria-hidden="true"
  33. >
  34. <svg class="h-3 w-3 text-cyan-500" fill="currentColor" viewBox="0 0 12 12">
  35. <path
  36. d="M3.707 5.293a1 1 0 00-1.414 1.414l1.414-1.414zM5 8l-.707.707a1 1 0 001.414 0L5 8zm4.707-3.293a1 1 0 00-1.414-1.414l1.414 1.414zm-7.414 2l2 2 1.414-1.414-2-2-1.414 1.414zm3.414 2l4-4-1.414-1.414-4 4 1.414 1.414z"
  37. />
  38. </svg>
  39. </span>
  40. </span>
  41. </button>
  42. </template>
  43. <script>
  44. export default {
  45. props: ['value'],
  46. methods: {
  47. toggle() {
  48. this.$emit('input', !this.value)
  49. this.value ? this.$emit('off') : this.$emit('on')
  50. },
  51. },
  52. }
  53. </script>