UNPKG

11 kBSCSSView Raw
1//
2// Copyright IBM Corp. 2016, 2018
3//
4// This source code is licensed under the Apache-2.0 license found in the
5// LICENSE file in the root directory of this source tree.
6//
7
8@import '../../globals/scss/vars';
9@import '../../globals/scss/typography';
10@import '../../globals/scss/helper-mixins';
11@import '../../globals/scss/functions';
12@import '../../globals/scss/vendor/@carbon/elements/scss/import-once/import-once';
13@import '../form/form';
14@import '../../globals/scss/css--reset';
15
16@mixin checkbox {
17 .#{$prefix}--form-item.#{$prefix}--checkbox-wrapper {
18 margin-bottom: $spacing-md;
19
20 &:first-of-type {
21 margin-top: $spacing-xs;
22 }
23
24 &:last-of-type {
25 margin-bottom: 0;
26 }
27 }
28
29 .#{$prefix}--checkbox {
30 @include hidden;
31 }
32
33 .#{$prefix}--checkbox-label {
34 @include reset;
35 @include font-family;
36 @include font-smoothing;
37 @include typescale('zeta');
38 display: flex;
39 align-items: center;
40 cursor: pointer;
41 position: relative;
42 padding-left: $spacing-lg;
43 min-height: rem(16px);
44 }
45
46 .#{$prefix}--checkbox-label::before {
47 box-sizing: border-box;
48 content: '';
49 position: absolute;
50 left: 0;
51 top: calc(50% - 9px);
52 height: rem(18px);
53 width: rem(18px);
54 border: $checkbox-border-width solid $ui-05;
55 background-color: $ui-01;
56 }
57
58 // Checkmark
59 .#{$prefix}--checkbox-label::after {
60 box-sizing: border-box;
61 content: '';
62 width: 9px;
63 height: 5px;
64 background: none;
65 /*rtl:ignore*/
66 border-left: 1px solid $inverse-01;
67 border-bottom: 1px solid $inverse-01;
68 /*rtl:ignore*/
69 transform: scale(0) rotate(-45deg);
70 position: absolute;
71 left: rem(5px);
72 top: 50%;
73 margin-top: rem(-3px);
74 }
75
76 .#{$prefix}--checkbox:checked + .#{$prefix}--checkbox-label::before,
77 .#{$prefix}--checkbox:indeterminate + .#{$prefix}--checkbox-label::before,
78 .#{$prefix}--checkbox-label[data-contained-checkbox-state='true']::before,
79 .#{$prefix}--checkbox-label[data-contained-checkbox-state='mixed']::before {
80 background-color: $brand-01;
81 border-color: $brand-01;
82 }
83
84 .#{$prefix}--checkbox:checked + .#{$prefix}--checkbox-label::after,
85 .#{$prefix}--checkbox-label[data-contained-checkbox-state='true']::after {
86 opacity: 1;
87 /*rtl:ignore*/
88 transform: scale(1) rotate(-45deg);
89 }
90
91 .#{$prefix}--checkbox:not(:checked) + .#{$prefix}--checkbox-label::after {
92 opacity: 0;
93 /*rtl:ignore*/
94 transform: scale(0) rotate(-45deg);
95 }
96
97 .#{$prefix}--checkbox:focus + .#{$prefix}--checkbox-label::before,
98 .#{$prefix}--checkbox-label__focus::before {
99 @include focus-outline('blurred');
100 }
101
102 // Indeterminate dash
103 .#{$prefix}--checkbox:indeterminate + .#{$prefix}--checkbox-label::after,
104 .#{$prefix}--checkbox-label[data-contained-checkbox-state='mixed']::after {
105 transform: scale(1) rotate(0deg);
106 /*rtl:ignore*/
107 border-left: 0 solid $inverse-01;
108 border-bottom: 2px solid $inverse-01;
109 opacity: 1;
110 width: rem(10px);
111 margin-top: rem(-4px);
112 left: rem(4px);
113 }
114
115 //Disabled
116 .#{$prefix}--checkbox:disabled + .#{$prefix}--checkbox-label,
117 .#{$prefix}--checkbox:disabled ~ .#{$prefix}--checkbox-label-text,
118 .#{$prefix}--checkbox-label[data-contained-checkbox-disabled='true'] {
119 opacity: 0.5;
120 cursor: not-allowed;
121 }
122
123 .#{$prefix}--checkbox-appearance {
124 position: absolute;
125 left: 0;
126 top: calc(50% - 9px);
127 display: inline-block;
128 height: rem(18px);
129 width: rem(18px);
130 margin-right: $spacing-xs;
131 background-color: $ui-01;
132 border: $checkbox-border-width solid $ui-05;
133 min-width: rem(18px);
134 }
135
136 // Backwards compatibilty for old checkbox html using .#{$prefix}--checkbox-appearance
137 .#{$prefix}--checkbox:checked + .#{$prefix}--checkbox-label .#{$prefix}--checkbox-appearance {
138 top: rem(-1px);
139 }
140
141 .#{$prefix}--checkbox:checked + .#{$prefix}--checkbox-appearance,
142 .#{$prefix}--checkbox:checked + .#{$prefix}--checkbox-label .#{$prefix}--checkbox-appearance {
143 display: flex;
144 align-items: baseline;
145 background-color: $brand-01;
146 border-color: $brand-01;
147 }
148
149 .#{$prefix}--checkbox:focus + .#{$prefix}--checkbox-label .#{$prefix}--checkbox-appearance,
150 .#{$prefix}--checkbox:focus + .#{$prefix}--checkbox-appearance {
151 @include focus-outline('blurred');
152 }
153
154 .#{$prefix}--checkbox:disabled + .#{$prefix}--checkbox-appearance {
155 opacity: 0.5;
156 cursor: not-allowed;
157 }
158
159 .#{$prefix}--checkbox-checkmark {
160 display: none;
161 fill: $inverse-01;
162 width: 100%;
163 height: 100%;
164 }
165
166 .#{$prefix}--checkbox:checked + .#{$prefix}--checkbox-appearance .#{$prefix}--checkbox-checkmark,
167 .#{$prefix}--checkbox:checked + .#{$prefix}--checkbox-label .#{$prefix}--checkbox-appearance .#{$prefix}--checkbox-checkmark {
168 display: block;
169
170 @-moz-document url-prefix() {
171 & {
172 stroke: $brand-01;
173 }
174 }
175 }
176
177 // Skeleton state
178 .#{$prefix}--checkbox-label.#{$prefix}--skeleton {
179 @include skeleton;
180 width: rem(100px);
181 height: rem(18px);
182
183 &:after,
184 &:before {
185 border: none;
186 }
187 }
188}
189
190@mixin checkbox--x {
191 // Spacing between checkboxes
192 .#{$prefix}--form-item.#{$prefix}--checkbox-wrapper {
193 margin-bottom: rem(8px);
194 }
195
196 // Spacing above collection of checkboxes
197 .#{$prefix}--form-item.#{$prefix}--checkbox-wrapper:first-of-type {
198 margin-top: rem(3px);
199 }
200
201 // Remove spacing above collection of checkboxes if label is present
202 .#{$prefix}--label + .#{$prefix}--form-item.#{$prefix}--checkbox-wrapper {
203 margin-top: 0;
204 }
205
206 // Spacing below collection of checkboxes
207 .#{$prefix}--form-item.#{$prefix}--checkbox-wrapper:last-of-type {
208 margin-bottom: rem(3px);
209 }
210
211 // Visually, we'll hide the checkbox input and create our own styled version
212 // to match the specs.
213 .#{$prefix}--checkbox {
214 @include hidden;
215 }
216
217 // The label corresponds to the content inside of the `label` tag. Since we're
218 // creating our own checkbox style, we'll need to position this in order to
219 // accomodate the spacing needed for the checkbox.
220 .#{$prefix}--checkbox-label {
221 @include reset;
222 @include type-style('body-short-01');
223
224 line-height: 1.5rem;
225 position: relative;
226 display: flex;
227 cursor: pointer;
228 padding-left: rem(26px); //width of checkbox 16px + 10px of padding
229 min-height: rem(24px);
230 user-select: none;
231 }
232
233 // Spacing for presentational checkbox
234 .#{$prefix}--checkbox-label::before {
235 box-sizing: border-box;
236 content: '';
237
238 // According to the spec, we'll want the bounding box for our checkbox to
239 // be 16px. The border size will be what will be updated during the
240 // different checkbox states.
241 width: rem(16px);
242 height: rem(16px);
243 margin: rem(2px);
244
245 // We need to position the pseudo-element absolutely in the space that we've
246 // created with the padding from the label itself. We position only with
247 // `top` since we don't want the checkbox to be centered vertically with the
248 // text overflows.
249 position: absolute;
250 left: 0;
251 top: rem(2px);
252
253 // Checkboxes with a background color look visually off against a parent container.
254 background-color: transparent;
255 border: 1px solid $ui-05;
256 border-radius: 1px;
257 }
258
259 // Create the appearance of the check in the `after` pseudo-element
260 .#{$prefix}--checkbox-label::after {
261 content: '';
262 position: absolute;
263 left: rem(6px);
264 top: rem(8px);
265 width: rem(7px);
266 height: rem(3px);
267 background: none;
268 border-left: 2px solid $ui-02;
269 border-bottom: 2px solid $ui-02;
270 transform: scale(0) rotate(-45deg);
271 transform-origin: bottom right;
272 margin-top: rem(-3px);
273 }
274
275 //----------------------------------------------
276 // Checked
277 // ---------------------------------------------
278
279 // Update properties for checked checkbox
280 .#{$prefix}--checkbox:checked + .#{$prefix}--checkbox-label::before,
281 .#{$prefix}--checkbox:indeterminate + .#{$prefix}--checkbox-label::before,
282 .#{$prefix}--checkbox-label[data-contained-checkbox-state='true']::before,
283 .#{$prefix}--checkbox-label[data-contained-checkbox-state='mixed']::before {
284 background-color: $text-01;
285 border-color: $text-01;
286 border-width: 1px;
287 }
288
289 // Display the check
290 .#{$prefix}--checkbox:checked + .#{$prefix}--checkbox-label::after,
291 .#{$prefix}--checkbox-label[data-contained-checkbox-state='true']::after {
292 transform: scale(1) rotate(-45deg);
293 }
294
295 // Indeterminate symbol
296 .#{$prefix}--checkbox:indeterminate + .#{$prefix}--checkbox-label::after,
297 .#{$prefix}--checkbox-label[data-contained-checkbox-state='mixed']::after {
298 transform: scale(1) rotate(0deg);
299 border-left: 0 solid $inverse-01;
300 border-bottom: 2px solid $inverse-01;
301 width: rem(8px);
302 top: rem(11px);
303 }
304
305 //----------------------------------------------
306 // Focus
307 // ---------------------------------------------
308
309 // Unchecked
310 .#{$prefix}--checkbox:focus + .#{$prefix}--checkbox-label::before,
311 .#{$prefix}--checkbox-label__focus::before,
312 // Checked
313 .#{$prefix}--checkbox:checked:focus + .#{$prefix}--checkbox-label::before,
314 .#{$prefix}--checkbox-label[data-contained-checkbox-state='true'].#{$prefix}--checkbox-label__focus::before,
315 // Indeterminate
316 .#{$prefix}--checkbox:indeterminate:focus + .#{$prefix}--checkbox-label::before,
317 .#{$prefix}--checkbox-label[data-contained-checkbox-state='mixed'].#{$prefix}--checkbox-label__focus::before {
318 // We can't use outline here because of the rounded corners so have to increase the width/height to fake an outline.
319 border-color: $interactive-01;
320 border-width: 3px;
321 width: rem(20px);
322 height: rem(20px);
323 left: -2px;
324 top: 0;
325 }
326
327 //----------------------------------------------
328 // Disabled
329 // ---------------------------------------------
330
331 // Workaround for: https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/11295231
332 [disabled] ~ _ {
333 font-size: inherit;
334 }
335
336 .#{$prefix}--checkbox:disabled + .#{$prefix}--checkbox-label,
337 .#{$prefix}--checkbox-label[data-contained-checkbox-disabled='true'] {
338 cursor: not-allowed;
339 color: $disabled-02;
340 }
341
342 .#{$prefix}--checkbox:disabled + .#{$prefix}--checkbox-label::before,
343 .#{$prefix}--checkbox-label[data-contained-checkbox-disabled='true']::before {
344 border-color: $disabled-02;
345 }
346
347 .#{$prefix}--checkbox:checked:disabled + .#{$prefix}--checkbox-label::before,
348 .#{$prefix}--checkbox:indeterminate:disabled + .#{$prefix}--checkbox-label::before,
349 .#{$prefix}--checkbox-label[data-contained-checkbox-state='true'][data-contained-checkbox-disabled='true']::before,
350 .#{$prefix}--checkbox-label[data-contained-checkbox-state='mixed'][data-contained-checkbox-disabled='true']::before {
351 background-color: $disabled-02;
352 }
353}
354
355@include exports('checkbox') {
356 @if feature-flag-enabled('components-x') {
357 @include checkbox--x;
358 } @else {
359 @include checkbox;
360 }
361}