_switches.scss 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. // Foundation by ZURB
  2. // foundation.zurb.com
  3. // Licensed under MIT Open Source
  4. @import 'global';
  5. //
  6. // @name
  7. // @dependencies _global.scss
  8. //
  9. //
  10. // @variables
  11. //
  12. $include-html-form-classes: $include-html-classes !default;
  13. // Controlling background color for the switch container
  14. $switch-bg: $gainsboro !default;
  15. // We use these to control the switch heights for our default classes
  16. $switch-height-tny: 1.5rem !default;
  17. $switch-height-sml: 1.75rem !default;
  18. $switch-height-med: 2rem !default;
  19. $switch-height-lrg: 2.5rem !default;
  20. $switch-bottom-margin: 1.5rem !default;
  21. // We use these to style the switch-paddle
  22. $switch-paddle-bg: $white !default;
  23. $switch-paddle-transition-speed: .15s !default;
  24. $switch-paddle-transition-ease: ease-out !default;
  25. $switch-active-color: $primary-color !default;
  26. //
  27. // @mixins
  28. //
  29. // We use this mixin to create the base styles for our switch element.
  30. //
  31. // $transition-speed - Time in ms for switch to toggle. Default: $switch-paddle-transition-speed.
  32. // $transition-ease - Easing function to use for animation (i.e. ease-out). Default: $switch-paddle-transition-ease.
  33. @mixin switch-base(
  34. $transition-speed:$switch-paddle-transition-speed,
  35. $transition-ease:$switch-paddle-transition-ease) {
  36. border: none;
  37. margin-bottom: $switch-bottom-margin;
  38. outline: 0;
  39. padding: 0;
  40. position: relative;
  41. -webkit-user-select: none;
  42. -moz-user-select: none;
  43. -ms-user-select: none;
  44. user-select: none;
  45. // Default label styles for type and transition
  46. label {
  47. background: $switch-bg;
  48. color: transparent;
  49. cursor: pointer;
  50. display: block;
  51. margin-bottom: ($switch-height-med / 2);
  52. position: relative;
  53. text-indent: 100%;
  54. width: $switch-height-med * 2; height: $switch-height-med;
  55. // Transition for the switch label to follow paddle
  56. @include single-transition(left, $transition-speed, $transition-ease);
  57. }
  58. // So that we don't need to recreate the form with any JS, we use the
  59. // existing checkbox or radio button, but we cleverly position and hide it.
  60. input {
  61. left: 10px;
  62. opacity: 0;
  63. padding:0;
  64. position: absolute;
  65. top: 9px;
  66. & + label { margin-left: 0; margin-right: 0; }
  67. }
  68. // The paddle for the switch is created from an after psuedoclass
  69. // content element. This is sized and positioned, and reacts to
  70. // the state of the input.
  71. label:after {
  72. background: $switch-paddle-bg;
  73. content: "";
  74. display: block;
  75. height: $switch-height-med - .5rem;
  76. left: .25rem;
  77. position: absolute;
  78. top: .25rem;
  79. width: $switch-height-med - .5rem;
  80. -webkit-transition: left $transition-speed $transition-ease;
  81. -moz-transition: left $transition-speed $transition-ease;
  82. -o-transition: translate3d(0,0,0);
  83. transition: left $transition-speed $transition-ease;
  84. -webkit-transform: translate3d(0,0,0);
  85. -moz-transform: translate3d(0,0,0);
  86. -ms-transform: translate3d(0,0,0);
  87. -o-transform: translate3d(0,0,0);
  88. transform: translate3d(0,0,0);
  89. }
  90. input:checked + label {
  91. background: $switch-active-color;
  92. }
  93. input:checked + label:after {
  94. left: $switch-height-med + .25rem;
  95. }
  96. }
  97. // We use this mixin to create the size styles for switches.
  98. //
  99. // $height - Height (in px) of the switch. Default: $switch-height-med.
  100. // $font-size - Font size of text in switch. Default: $switch-font-size-med.
  101. // $line-height - Line height of switch. Default: 2.3rem.
  102. @mixin switch-size($height: $switch-height-med) {
  103. label {
  104. height: $height;
  105. width: $height * 2;
  106. }
  107. label:after {
  108. height: $height - .5rem;
  109. width: $height - .5rem;
  110. }
  111. input:checked + label:after {
  112. left: $height + .25rem;
  113. }
  114. }
  115. // We use this mixin to add color and other fanciness to the switches.
  116. //
  117. // $paddle-bg - Background of switch paddle. Default: $switch-paddle-bg.
  118. // $active-color - Background color of positive side of switch. Default: $switch-positive-color.
  119. // $negative-color - Background color of negative side of switch. Default: $switch-negative-color.
  120. // $radius - Radius to apply to switch. Default: false.
  121. // $base-style - Apply base styles? Default: true.
  122. @mixin switch-style(
  123. $paddle-bg:$switch-paddle-bg,
  124. $active-color:$switch-active-color,
  125. $radius:false,
  126. $base-style:true) {
  127. @if $base-style {
  128. label {
  129. color: transparent;
  130. background: $switch-bg;
  131. }
  132. label:after {
  133. background: $paddle-bg;
  134. }
  135. input:checked + label {
  136. background: $active-color;
  137. }
  138. }
  139. // Setting up the radius for switches
  140. @if $radius == true {
  141. label {
  142. border-radius: 2rem;
  143. }
  144. label:after {
  145. border-radius: 2rem;
  146. }
  147. }
  148. @else if $radius {
  149. label {
  150. border-radius: $radius;
  151. }
  152. label:after {
  153. border-radius: $radius;
  154. }
  155. }
  156. }
  157. // We use this to quickly create switches with a single mixin
  158. //
  159. // $transition-speed - Time in ms for switch to toggle. Default: $switch-paddle-transition-speed.
  160. // $transition-ease - Easing function to use for animation (i.e. ease-out). Default: $switch-paddle-transition-ease.
  161. // $height - Height (in px) of the switch. Default: $switch-height-med.
  162. // $paddle-bg - Background of switch paddle. Default: $switch-paddle-bg.
  163. // $active-color - Background color of an active switch. Default: $switch-active-color.
  164. // $radius - Radius to apply to switch. Default: false.
  165. // $base-style - Apply base styles? Default: true.
  166. @mixin switch(
  167. $transition-speed: $switch-paddle-transition-speed,
  168. $transition-ease: $switch-paddle-transition-ease,
  169. $height: $switch-height-med,
  170. $paddle-bg: $switch-paddle-bg,
  171. $active-color: $switch-active-color,
  172. $radius:false,
  173. $base-style:true) {
  174. @include switch-base($transition-speed, $transition-ease);
  175. @include switch-size($height);
  176. @include switch-style($paddle-bg, $active-color, $radius, $base-style);
  177. }
  178. @include exports("switch") {
  179. @if $include-html-form-classes {
  180. .switch {
  181. @include switch;
  182. // Large radio switches
  183. &.large { @include switch-size($switch-height-lrg); }
  184. // Small radio switches
  185. &.small { @include switch-size($switch-height-sml); }
  186. // Tiny radio switches
  187. &.tiny { @include switch-size($switch-height-tny); }
  188. // Add a radius to the switch
  189. &.radius {
  190. label { @include radius(4px); }
  191. label:after { @include radius(3px); }
  192. }
  193. // Make the switch completely round, like a pill
  194. &.round { @include radius(1000px);
  195. label { @include radius(2rem); }
  196. label:after { @include radius(2rem); }
  197. }
  198. }
  199. }
  200. }