switch.scss 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. $switch-height: calc(#{$input-height} * .8) !default;
  2. $switch-height-sm: calc(#{$input-height-sm} * .8) !default;
  3. $switch-height-lg: calc(#{$input-height-lg} * .8) !default;
  4. $switch-border-radius: $switch-height !default;
  5. $switch-bg: $custom-control-indicator-bg !default;
  6. $switch-checked-bg: map-get($theme-colors, 'danger') !default;
  7. $switch-disabled-bg: $custom-control-indicator-disabled-bg !default;
  8. $switch-disabled-color: $custom-control-description-disabled-color !default;
  9. $switch-thumb-bg: $white !default;
  10. $switch-thumb-border-radius: 50% !default;
  11. $switch-thumb-padding: 2px !default;
  12. $switch-focus-box-shadow: 0 0 0 $input-btn-focus-width rgba(map-get($theme-colors, 'primary'), .25);
  13. $switch-transition: .2s all !default;
  14. .switch {
  15. font-size: $font-size-base;
  16. position: relative;
  17. input {
  18. position: absolute;
  19. height: 1px;
  20. width: 1px;
  21. background: none;
  22. border: 0;
  23. clip: rect(0 0 0 0);
  24. clip-path: inset(50%);
  25. overflow: hidden;
  26. padding: 0;
  27. + label {
  28. position: relative;
  29. min-width: calc(#{$switch-height} * 2);
  30. border-radius: $switch-border-radius;
  31. height: $switch-height;
  32. line-height: $switch-height;
  33. display: inline-block;
  34. cursor: pointer;
  35. outline: none;
  36. user-select: none;
  37. vertical-align: middle;
  38. text-indent: calc(calc(#{$switch-height} * 2) + .5rem);
  39. }
  40. + label::before,
  41. + label::after {
  42. content: '';
  43. position: absolute;
  44. top: 0;
  45. left: 0;
  46. width: calc(#{$switch-height} * 2);
  47. bottom: 0;
  48. display: block;
  49. }
  50. + label::before {
  51. right: 0;
  52. background-color: $switch-bg;
  53. border-radius: $switch-border-radius;
  54. transition: $switch-transition;
  55. }
  56. + label::after {
  57. top: $switch-thumb-padding;
  58. left: $switch-thumb-padding;
  59. width: calc(#{$switch-height} - calc(#{$switch-thumb-padding} * 2));
  60. height: calc(#{$switch-height} - calc(#{$switch-thumb-padding} * 2));
  61. border-radius: $switch-thumb-border-radius;
  62. background-color: $switch-thumb-bg;
  63. transition: $switch-transition;
  64. }
  65. &:checked + label::before {
  66. background-color: $switch-checked-bg;
  67. }
  68. &:checked + label::after {
  69. margin-left: $switch-height;
  70. }
  71. &:focus + label::before {
  72. outline: none;
  73. box-shadow: $switch-focus-box-shadow;
  74. }
  75. &:disabled + label {
  76. color: $switch-disabled-color;
  77. cursor: not-allowed;
  78. }
  79. &:disabled + label::before {
  80. background-color: $switch-disabled-bg;
  81. }
  82. }
  83. // Small variation
  84. &.switch-sm {
  85. font-size: $font-size-sm;
  86. input {
  87. + label {
  88. min-width: calc(#{$switch-height-sm} * 2);
  89. height: $switch-height-sm;
  90. line-height: $switch-height-sm;
  91. text-indent: calc(calc(#{$switch-height-sm} * 2) + .5rem);
  92. }
  93. + label::before {
  94. width: calc(#{$switch-height-sm} * 2);
  95. }
  96. + label::after {
  97. width: calc(#{$switch-height-sm} - calc(#{$switch-thumb-padding} * 2));
  98. height: calc(#{$switch-height-sm} - calc(#{$switch-thumb-padding} * 2));
  99. }
  100. &:checked + label::after {
  101. margin-left: $switch-height-sm;
  102. }
  103. }
  104. }
  105. // Large variation
  106. &.switch-lg {
  107. font-size: $font-size-lg;
  108. input {
  109. + label {
  110. min-width: calc(#{$switch-height-lg} * 2);
  111. height: $switch-height-lg;
  112. line-height: $switch-height-lg;
  113. text-indent: calc(calc(#{$switch-height-lg} * 2) + .5rem);
  114. }
  115. + label::before {
  116. width: calc(#{$switch-height-lg} * 2);
  117. }
  118. + label::after {
  119. width: calc(#{$switch-height-lg} - calc(#{$switch-thumb-padding} * 2));
  120. height: calc(#{$switch-height-lg} - calc(#{$switch-thumb-padding} * 2));
  121. }
  122. &:checked + label::after {
  123. margin-left: $switch-height-lg;
  124. }
  125. }
  126. }
  127. + .switch {
  128. margin-left: 1rem;
  129. }
  130. }