Toggle.vue 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <template>
  2. <div
  3. class="cursor-pointer flex-nowrap inline-flex items-center justify-center"
  4. @click="$emit('change')"
  5. >
  6. <label ref="toggle" class="toggle" :class="{ on: on }">
  7. <span class="handle"></span>
  8. </label>
  9. <label class="cursor-pointer pl-0 align-middle">
  10. <slot></slot>
  11. </label>
  12. </div>
  13. </template>
  14. <script>
  15. import { defineComponent } from "@nuxtjs/composition-api"
  16. export default defineComponent({
  17. props: {
  18. on: {
  19. type: Boolean,
  20. default: false,
  21. },
  22. },
  23. })
  24. </script>
  25. <style scoped lang="scss">
  26. $useBorder: true;
  27. $borderColor: var(--divider-color);
  28. $activeColor: var(--divider-color);
  29. $inactiveColor: var(--divider-color);
  30. $inactiveHandleColor: var(--secondary-light-color);
  31. $activeHandleColor: var(--accent-color);
  32. $width: 1.6rem;
  33. $height: 0.6rem;
  34. $indicatorHeight: 0.4rem;
  35. $indicatorWidth: 0.4rem;
  36. $handleSpacing: 0.1rem;
  37. $transition: all 0.2s ease-in-out;
  38. .toggle {
  39. @apply relative;
  40. @apply flex;
  41. @apply items-center;
  42. @apply justify-center;
  43. @apply rounded-full;
  44. @apply p-0;
  45. @apply mr-4;
  46. @apply cursor-pointer;
  47. @apply flex-shrink-0;
  48. width: $width;
  49. height: $height;
  50. border: if($useBorder, 2px solid $borderColor, none);
  51. background-color: if($useBorder, transparent, $inactiveColor);
  52. box-sizing: initial;
  53. .handle {
  54. @apply absolute;
  55. @apply flex;
  56. @apply flex-shrink-0;
  57. @apply inset-0;
  58. @apply rounded-full;
  59. @apply pointer-events-none;
  60. transition: $transition;
  61. margin: $handleSpacing;
  62. background-color: $inactiveHandleColor;
  63. width: $indicatorWidth;
  64. height: $indicatorHeight;
  65. }
  66. &.on {
  67. // background-color: $activeColor;
  68. border-color: $activeColor;
  69. .handle {
  70. background-color: $activeHandleColor;
  71. left: #{$width - $height};
  72. }
  73. }
  74. }
  75. </style>