_switch.scss 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. @use 'sass:math';
  2. $switch-width: 36px;
  3. $switch-height: 18px;
  4. $switch-border: 2px;
  5. $switch-outline: 3px;
  6. $switch-border-radius: math.div(($switch-height + $switch-border * 2), 2);
  7. .switch {
  8. display: inline-flex;
  9. align-items: center;
  10. margin: 5px 0;
  11. position: relative;
  12. // Disable forms styling that's applied to the <label> tag
  13. width: unset;
  14. float: unset;
  15. &__tick {
  16. width: 12px;
  17. height: $switch-height;
  18. position: absolute;
  19. top: 50%;
  20. transform: translate(5px, -50%);
  21. color: $color-white;
  22. @media (forced-colors: active) {
  23. color: SelectedItemText;
  24. }
  25. }
  26. &__toggle {
  27. position: relative;
  28. cursor: pointer;
  29. &::before,
  30. &::after {
  31. content: '';
  32. transition: all 100ms cubic-bezier(0.4, 0, 0.2, 1);
  33. display: block;
  34. }
  35. &::before {
  36. height: $switch-height + $switch-border * 2;
  37. width: $switch-width + $switch-border * 2;
  38. border-radius: $switch-border-radius;
  39. background: theme('colors.grey.400');
  40. border: $switch-border solid theme('colors.grey.400');
  41. }
  42. &::after {
  43. box-sizing: border-box;
  44. position: absolute;
  45. top: 50%;
  46. transform: translate($switch-border, -50%);
  47. height: $switch-height;
  48. width: $switch-height;
  49. border: $switch-border solid $color-white;
  50. border-radius: theme('borderRadius.full');
  51. background-color: $color-white;
  52. }
  53. }
  54. [type='checkbox']:checked + &__toggle::before {
  55. background: $color-teal;
  56. border-color: $color-teal;
  57. @media (forced-colors: active) {
  58. background: SelectedItem;
  59. border-color: SelectedItem;
  60. }
  61. }
  62. [type='checkbox']:checked + &__toggle::after {
  63. transform: translate(calc(#{$switch-width + $switch-border} - 100%), -50%);
  64. }
  65. [type='checkbox']:disabled + &__toggle {
  66. cursor: not-allowed;
  67. filter: grayscale(100%);
  68. opacity: 0.3;
  69. }
  70. [type='checkbox']:focus + &__toggle {
  71. outline: $color-focus-outline solid $switch-outline;
  72. }
  73. [type='checkbox'] {
  74. position: absolute;
  75. opacity: 0;
  76. pointer-events: none;
  77. width: 100%;
  78. height: 100%;
  79. }
  80. }