_switch.scss 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. @use 'sass:math';
  2. $switch-width: 40px;
  3. $switch-height: 20px;
  4. $switch-border: 2px;
  5. $switch-outline: 3px;
  6. $switch-border-radius: math.div(($switch-height + $switch-border * 2), 2);
  7. $switch-outline-radius: $switch-border-radius + $switch-outline;
  8. // All the greys in Wagtail are really dark or really bright
  9. $switch-color-middle-grey: #777;
  10. .switch {
  11. display: inline-flex;
  12. align-items: center;
  13. margin: 5px 0;
  14. // Disable forms styling that's applied to the <label> tag
  15. width: unset;
  16. float: unset;
  17. &__toggle {
  18. position: relative;
  19. cursor: pointer;
  20. &::before,
  21. &::after {
  22. content: '';
  23. transition: all 100ms cubic-bezier(0.4, 0, 0.2, 1);
  24. display: block;
  25. }
  26. &::before {
  27. height: $switch-height;
  28. width: $switch-width;
  29. border-radius: $switch-border-radius;
  30. background: $switch-color-middle-grey;
  31. border: $switch-border solid $switch-color-middle-grey;
  32. }
  33. &::after {
  34. box-sizing: border-box;
  35. position: absolute;
  36. top: 50%;
  37. transform: translate($switch-border, -50%);
  38. height: $switch-height;
  39. width: $switch-height;
  40. border: $switch-border solid $color-white;
  41. border-radius: 50%;
  42. background-color: $color-white;
  43. }
  44. }
  45. [type='checkbox']:checked + &__toggle::before {
  46. background: $color-teal;
  47. border-color: $color-teal;
  48. }
  49. [type='checkbox']:checked + &__toggle::after {
  50. transform: translate(
  51. calc(#{$switch-width} + #{$switch-border} - 100%),
  52. -50%
  53. );
  54. }
  55. [type='checkbox']:disabled + &__toggle {
  56. cursor: not-allowed;
  57. filter: grayscale(100%);
  58. opacity: 0.3;
  59. }
  60. [type='checkbox']:disabled + &__toggle::after {
  61. opacity: 0.5;
  62. box-shadow: none;
  63. }
  64. [type='checkbox']:focus + &__toggle {
  65. outline: $color-focus-outline solid $switch-outline;
  66. }
  67. [type='checkbox'] {
  68. position: absolute;
  69. opacity: 0;
  70. pointer-events: none;
  71. }
  72. // Colour changes for when displaying on teal background
  73. &--teal-background {
  74. $background: #03b0b1;
  75. .switch__toggle {
  76. &::before {
  77. background-color: #b9b9b9;
  78. border: $switch-border solid #b9b9b9;
  79. opacity: 0.6;
  80. }
  81. &::after {
  82. background-color: $color-white;
  83. }
  84. }
  85. [type='checkbox']:checked + .switch__toggle::before {
  86. // Override the white-background styling
  87. background-color: $background;
  88. border-color: $background;
  89. opacity: 1;
  90. }
  91. }
  92. }