_switch.scss 2.8 KB

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