_switch.scss 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. &__icon {
  16. width: 12px;
  17. height: $switch-height;
  18. position: absolute;
  19. top: 50%;
  20. transform: translate(calc(var(--w-direction-factor) * 5px), -50%);
  21. color: theme('colors.text-button');
  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.text-placeholder');
  40. border: $switch-border solid theme('colors.text-placeholder');
  41. }
  42. &::after {
  43. position: absolute;
  44. top: 50%;
  45. transform: translate(
  46. calc(var(--w-direction-factor) * $switch-border),
  47. -50%
  48. );
  49. height: $switch-height;
  50. width: $switch-height;
  51. border: $switch-border solid theme('colors.surface-page');
  52. border-radius: theme('borderRadius.full');
  53. background-color: theme('colors.surface-page');
  54. }
  55. }
  56. [type='checkbox']:checked + &__toggle::before {
  57. background: theme('colors.text-button-outline-default');
  58. border-color: theme('colors.text-button-outline-default');
  59. @media (forced-colors: active) {
  60. background: SelectedItem;
  61. border-color: SelectedItem;
  62. }
  63. }
  64. [type='checkbox']:checked + &__toggle::after {
  65. transform: translate(
  66. calc(
  67. var(--w-direction-factor) * (#{$switch-width + $switch-border} - 100%)
  68. ),
  69. -50%
  70. );
  71. }
  72. [type='checkbox']:disabled + &__toggle {
  73. cursor: not-allowed;
  74. filter: grayscale(100%);
  75. opacity: 0.3;
  76. }
  77. [type='checkbox']:focus-visible + &__toggle {
  78. outline: theme('colors.focus') solid $switch-outline;
  79. }
  80. [type='checkbox'] {
  81. position: absolute;
  82. opacity: 0;
  83. pointer-events: none;
  84. width: 100%;
  85. height: 100%;
  86. }
  87. }