Toggle.vue 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <template>
  2. <Switch
  3. v-model="modelValue"
  4. @click="toggle"
  5. :class="[
  6. modelValue ? 'bg-cyan-500' : 'bg-grey-300',
  7. '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. >
  10. <span class="sr-only">Use setting</span>
  11. <span
  12. :class="[
  13. modelValue ? 'translate-x-5' : 'translate-x-0',
  14. 'pointer-events-none relative inline-block h-5 w-5 rounded-full bg-white shadow transform ring-0 transition ease-in-out duration-200',
  15. ]"
  16. >
  17. <span
  18. :class="[
  19. modelValue ? 'opacity-0 ease-out duration-100' : 'opacity-100 ease-in duration-200',
  20. 'absolute inset-0 h-full w-full flex items-center justify-center transition-opacity',
  21. ]"
  22. aria-hidden="true"
  23. >
  24. <svg class="h-3 w-3 text-grey-400" fill="none" viewBox="0 0 12 12">
  25. <path
  26. d="M4 8l2-2m0 0l2-2M6 6L4 4m2 2l2 2"
  27. stroke="currentColor"
  28. stroke-width="2"
  29. stroke-linecap="round"
  30. stroke-linejoin="round"
  31. />
  32. </svg>
  33. </span>
  34. <span
  35. :class="[
  36. modelValue ? 'opacity-100 ease-in duration-200' : 'opacity-0 ease-out duration-100',
  37. 'absolute inset-0 h-full w-full flex items-center justify-center transition-opacity',
  38. ]"
  39. aria-hidden="true"
  40. >
  41. <svg class="h-3 w-3 text-cyan-500" fill="currentColor" viewBox="0 0 12 12">
  42. <path
  43. 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"
  44. />
  45. </svg>
  46. </span>
  47. </span>
  48. </Switch>
  49. </template>
  50. <script>
  51. import { Switch } from '@headlessui/vue'
  52. export default {
  53. props: ['modelValue'],
  54. components: {
  55. Switch,
  56. },
  57. methods: {
  58. toggle() {
  59. this.$emit('update:modelValue', !this.modelValue)
  60. this.modelValue ? this.$emit('off') : this.$emit('on')
  61. },
  62. },
  63. }
  64. </script>