Toggle.vue 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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="
  8. relative
  9. inline-flex
  10. flex-shrink-0
  11. h-6
  12. w-11
  13. border-2 border-transparent
  14. rounded-full
  15. cursor-pointer
  16. transition-colors
  17. ease-in-out
  18. duration-200
  19. focus:outline-none
  20. "
  21. >
  22. <span class="sr-only">Use setting</span>
  23. <span
  24. :class="this.value ? 'translate-x-5' : 'translate-x-0'"
  25. class="
  26. relative
  27. inline-block
  28. h-5
  29. w-5
  30. rounded-full
  31. bg-white
  32. shadow
  33. transform
  34. ring-0
  35. transition
  36. ease-in-out
  37. duration-200
  38. "
  39. >
  40. <span
  41. :class="this.value ? 'opacity-0 ease-out duration-100' : 'opacity-100 ease-in duration-200'"
  42. class="absolute inset-0 h-full w-full flex items-center justify-center transition-opacity"
  43. aria-hidden="true"
  44. >
  45. <svg class="h-3 w-3 text-grey-400" fill="none" viewBox="0 0 12 12">
  46. <path
  47. d="M4 8l2-2m0 0l2-2M6 6L4 4m2 2l2 2"
  48. stroke="currentColor"
  49. stroke-width="2"
  50. stroke-linecap="round"
  51. stroke-linejoin="round"
  52. />
  53. </svg>
  54. </span>
  55. <span
  56. :class="this.value ? 'opacity-100 ease-in duration-200' : 'opacity-0 ease-out duration-100'"
  57. class="absolute inset-0 h-full w-full flex items-center justify-center transition-opacity"
  58. aria-hidden="true"
  59. >
  60. <svg class="h-3 w-3 text-cyan-500" fill="currentColor" viewBox="0 0 12 12">
  61. <path
  62. 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"
  63. />
  64. </svg>
  65. </span>
  66. </span>
  67. </button>
  68. </template>
  69. <script>
  70. export default {
  71. props: ['value'],
  72. methods: {
  73. toggle() {
  74. this.$emit('input', !this.value)
  75. this.value ? this.$emit('off') : this.$emit('on')
  76. },
  77. },
  78. }
  79. </script>