UNPKG

5.88 kBSCSSView Raw
1// Foundation for Sites by ZURB
2// foundation.zurb.com
3// Licensed under MIT Open Source
4
5////
6/// @group switch
7////
8
9/// Background color of a switch.
10/// @type Color
11$switch-background: $medium-gray !default;
12
13/// Background active color of a switch.
14/// @type Color
15$switch-background-active: $primary-color !default;
16
17/// Height of a switch, with no class applied.
18/// @type Number
19$switch-height: 2rem !default;
20
21/// Height of a switch with .tiny class.
22/// @type Number
23$switch-height-tiny: 1.5rem !default;
24
25/// Height of a switch with .small class.
26/// @type Number
27$switch-height-small: 1.75rem !default;
28
29/// Height of a switch with .large class.
30/// @type Number
31$switch-height-large: 2.5rem !default;
32
33/// Border radius of the switch
34/// @type Number
35$switch-radius: $global-radius !default;
36
37/// border around a modal.
38/// @type Number
39$switch-margin: $global-margin !default;
40
41/// Background color for the switch container and paddle.
42/// @type Color
43$switch-paddle-background: $white !default;
44
45/// Spacing between a switch paddle and the edge of the body.
46/// @type Number
47$switch-paddle-offset: 0.25rem !default;
48
49/// border radius of the switch paddle
50/// @type Number
51$switch-paddle-radius: $global-radius !default;
52
53/// switch transition.
54/// @type Number
55$switch-paddle-transition: all 0.25s ease-out !default;
56
57// make them variables
58// ask about accessibility on label
59// change class name for text
60
61/// Adds styles for a switch container. Apply this to a container class.
62@mixin switch-container {
63 position: relative;
64 margin-bottom: $switch-margin;
65 outline: 0;
66
67 // These properties cascade down to the switch text
68 font-size: rem-calc(14);
69 font-weight: bold;
70 color: $white;
71
72 user-select: none;
73}
74
75/// Adds styles for a switch input. Apply this to an `<input>` within a switch.
76@mixin switch-input {
77 position: absolute;
78 margin-bottom: 0;
79 opacity: 0;
80}
81
82/// Adds styles for the background and paddle of a switch. Apply this to a `<label>` within a switch.
83@mixin switch-paddle {
84 $switch-width: $switch-height * 2;
85 $paddle-height: $switch-height - ($switch-paddle-offset * 2);
86 $paddle-width: $switch-height - ($switch-paddle-offset * 2);
87 $paddle-active-offest: $switch-width - $paddle-width - $switch-paddle-offset;
88
89 position: relative;
90 display: block;
91 width: $switch-width;
92 height: $switch-height;
93
94 border-radius: $switch-radius;
95 background: $switch-background;
96 transition: $switch-paddle-transition;
97
98 // Resetting these <label> presets so type styles cascade down
99 font-weight: inherit;
100 color: inherit;
101
102 cursor: pointer;
103
104 // Needed to override specificity
105 input + & {
106 margin: 0;
107 }
108
109 // The paddle itself
110 &::after {
111 position: absolute;
112 top: $switch-paddle-offset;
113 #{$global-left}: $switch-paddle-offset;
114
115 display: block;
116 width: $paddle-width;
117 height: $paddle-height;
118
119 transform: translate3d(0, 0, 0);
120 border-radius: $switch-paddle-radius;
121 background: $switch-paddle-background;
122 transition: $switch-paddle-transition;
123 content: '';
124 }
125
126 // Change the visual style when the switch is active
127 input:checked ~ & {
128 background: $switch-background-active;
129
130 &::after {
131 #{$global-left}: $paddle-active-offest;
132 }
133 }
134
135 input:focus ~ & {
136 @include disable-mouse-outline;
137 }
138}
139
140/// Adds base styles for active/inactive text inside a switch. Apply this to text elements inside the switch `<label>`.
141@mixin switch-text {
142 position: absolute;
143 top: 50%;
144 transform: translateY(-50%);
145}
146
147/// Adds styles for the active state text within a switch.
148@mixin switch-text-active {
149 #{$global-left}: 8%;
150 display: none;
151
152 input:checked + label > & {
153 display: block;
154 }
155}
156
157/// Adds styles for the inactive state text within a switch.
158@mixin switch-text-inactive {
159 #{$global-right}: 15%;
160
161 input:checked + label > & {
162 display: none;
163 }
164}
165
166/// Changes the size of a switch by modifying the size of the body and paddle. Apply this to a switch container.
167/// @param {Number} $font-size [1rem] - Font size of label text within the switch.
168/// @param {Number} $switch-height [2rem] - Height of the switch body.
169/// @param {Number} $paddle-offset [0.25rem] - Spacing between the switch paddle and the edge of the switch body.
170@mixin switch-size(
171 $font-size: 1rem,
172 $switch-height: 2rem,
173 $paddle-offset: 0.25rem
174) {
175
176 $switch-width: $switch-height * 2;
177 $paddle-width: $switch-height - ($paddle-offset * 2);
178 $paddle-height: $switch-height - ($paddle-offset * 2);
179 $paddle-active-offest: $switch-width - $paddle-width - $paddle-offset;
180
181 height: $switch-height;
182
183 .switch-paddle {
184 width: $switch-width;
185 height: $switch-height;
186 font-size: $font-size;
187 }
188
189 .switch-paddle::after {
190 top: $paddle-offset;
191 #{$global-left}: $paddle-offset;
192 width: $paddle-width;
193 height: $paddle-height;
194 }
195
196 input:checked ~ .switch-paddle::after {
197 #{$global-left}: $paddle-active-offest;
198 }
199}
200
201@mixin foundation-switch {
202 // Container class
203 .switch {
204 height: $switch-height;
205 @include switch-container;
206 }
207
208 // <input> element
209 .switch-input {
210 @include switch-input;
211 }
212
213 // <label> element
214 .switch-paddle {
215 @include switch-paddle;
216 }
217
218 // Base label text styles
219 %switch-text {
220 @include switch-text;
221 }
222
223 // Active label text styles
224 .switch-active {
225 @extend %switch-text;
226 @include switch-text-active;
227 }
228
229 // Inactive label text styles
230 .switch-inactive {
231 @extend %switch-text;
232 @include switch-text-inactive;
233 }
234
235 // Switch sizes
236 .switch.tiny {
237 @include switch-size(rem-calc(10), $switch-height-tiny, $switch-paddle-offset);
238 }
239
240 .switch.small {
241 @include switch-size(rem-calc(12), $switch-height-small, $switch-paddle-offset);
242 }
243
244 .switch.large {
245 @include switch-size(rem-calc(16), $switch-height-large, $switch-paddle-offset);
246 }
247}