Toggle.vue 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 font-semibold">
  10. <slot></slot>
  11. </label>
  12. </div>
  13. </template>
  14. <script setup lang="ts">
  15. withDefaults(
  16. defineProps<{
  17. on: Boolean
  18. }>(),
  19. {
  20. on: false,
  21. }
  22. )
  23. </script>
  24. <style scoped lang="scss">
  25. $useBorder: true;
  26. $borderColor: var(--divider-color);
  27. $activeColor: var(--divider-color);
  28. $inactiveColor: var(--divider-color);
  29. $inactiveHandleColor: var(--secondary-light-color);
  30. $activeHandleColor: var(--accent-color);
  31. $width: 1.6rem;
  32. $height: 0.6rem;
  33. $indicatorHeight: 0.4rem;
  34. $indicatorWidth: 0.4rem;
  35. $handleSpacing: 0.1rem;
  36. $transition: all 0.2s ease-in-out;
  37. .toggle {
  38. @apply relative;
  39. @apply flex;
  40. @apply items-center;
  41. @apply justify-center;
  42. @apply rounded-full;
  43. @apply p-0;
  44. @apply mr-4;
  45. @apply cursor-pointer;
  46. @apply flex-shrink-0;
  47. width: $width;
  48. height: $height;
  49. border: if($useBorder, 2px solid $borderColor, none);
  50. background-color: if($useBorder, transparent, $inactiveColor);
  51. box-sizing: initial;
  52. .handle {
  53. @apply absolute;
  54. @apply flex;
  55. @apply flex-shrink-0;
  56. @apply inset-0;
  57. @apply rounded-full;
  58. @apply pointer-events-none;
  59. transition: $transition;
  60. margin: $handleSpacing;
  61. background-color: $inactiveHandleColor;
  62. width: $indicatorWidth;
  63. height: $indicatorHeight;
  64. }
  65. &.on {
  66. // background-color: $activeColor;
  67. border-color: $activeColor;
  68. .handle {
  69. background-color: $activeHandleColor;
  70. left: #{$width - $height};
  71. }
  72. }
  73. }
  74. </style>