_switch.scss 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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(calc(#{$switch-width} + #{$switch-border} - 100%), -50%);
  51. }
  52. [type=checkbox]:disabled + &__toggle {
  53. cursor: not-allowed;
  54. filter: grayscale(100%);
  55. opacity: 0.3;
  56. }
  57. [type=checkbox]:disabled + &__toggle::after {
  58. opacity: 0.5;
  59. box-shadow: none;
  60. }
  61. [type=checkbox]:focus + &__toggle {
  62. outline: $color-focus-outline solid $switch-outline;
  63. -moz-outline-radius: $switch-outline-radius;
  64. }
  65. [type=checkbox] {
  66. position: absolute;
  67. opacity: 0;
  68. pointer-events: none;
  69. }
  70. // Colour changes for when displaying on teal background
  71. &--teal-background {
  72. $background: #03b0b1;
  73. .switch__toggle {
  74. &::before {
  75. background-color: #b9b9b9;
  76. border: $switch-border solid #b9b9b9;
  77. opacity: 0.6;
  78. }
  79. &::after {
  80. background-color: $color-white;
  81. }
  82. }
  83. [type=checkbox]:checked + .switch__toggle::before {
  84. // Override the white-background styling
  85. background-color: $background;
  86. border-color: $background;
  87. opacity: 1;
  88. }
  89. }
  90. }