-
Notifications
You must be signed in to change notification settings - Fork 28
/
index.scss
698 lines (594 loc) · 20.5 KB
/
index.scss
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
////
/// @author Andrey Mikhaylov (lolmaus) <https://github.com/lolmaus>
/// @group api
////
@use "sass:list";
@use "sass:map";
@use "sass:math";
@use "sass:meta";
@use "sass:string";
/// The default slices map.
/// @type Map
/// @access public
$slices: (
xxs: 0px,
xs: 200px,
s: 400px,
m: 600px,
l: 800px,
xl: 1000px,
xxl: 1200px,
xxxl: 1400px,
) !default;
/// A list of media types and features to be prepended to every media query generated by breakpoint-slicer.
/// Can assume one of the following values:
/// * `null` (default) — nothing is prepended
/// * a single media query, e. g. `monochrome and (orientation: portrait)`
/// * a comma-separated list of media queries, e. g. `screen, print`
/// @type null | list
/// @access public
/// @example scss
/// $media: null;
///
/// .foo {
/// @include from(s) {
/// color: red;
/// }
/// }
///
/// // produces:
///
/// @media (min-width: 400px) {
/// .foo {
/// color: red;
/// }
/// }
/// @example scss
/// $media: monochrome and (orientation: portrait);
///
/// .foo {
/// @include from(s) {
/// color: red;
/// }
/// }
///
/// // produces:
///
/// @media monochrome and (orientation: portrait) and (min-width: 400px) {
/// .foo {
/// color: red;
/// }
/// }
/// @example scss
/// $media: screen, print;
///
/// .foo {
/// @include from(s) {
/// color: red;
/// }
/// }
///
/// // produces:
///
/// @media screen and (min-width: 400px), print and (min-width: 400px) {
/// .foo {
/// color: red;
/// }
/// }
$media: null !default;
// // https://github.com/boltdesignsystem/bolt/blob/v3.0.1/packages/core-v3.x/styles/02-tools/tools-list-remove/_tools-list-remove.scss
// @function list-remove($list, $index) {
// $separator: list.separator($list);
// $newList: ();
// @for $i from 1 through list.length($list) {
// @if $i != $index {
// $newList: list.append($newList, list.nth($list, $i), $separator);
// }
// }
// @return $newList;
// }
// // @function list-swap($list, $index-a, $index-b) {
// // @if math.abs($index-a) > list.length($list) or
// // math.abs($index-b) > list.length($list) {
// // @return $list;
// // }
// // $temp: list.nth($list, $index-a);
// // $list: list.set-nth($list, $index-a, list.nth($list, $index-b));
// // $list: list.set-nth($list, $index-b, $temp);
// // @return $list;
// // }
// // https://github.com/boltdesignsystem/bolt/blob/v3.0.1/packages/core-v3.x/styles/02-tools/tools-list-sort/_tools-list-sort.scss
// @function list-sort-numeric($list) {
// $separator: list.separator($list);
// $sortedlist: ();
// @while list.length($list) > 0 {
// $value: list.nth($list, 1);
// @each $item in $list {
// @if meta.type-of($item) == "number" and meta.type-of($value) == "number" {
// @if $item < $value {
// $value: $item;
// }
// } @else {
// @error 'These values are not sortable: ' + $item + ' and ' + $value;
// }
// }
// $sortedlist: list.append($sortedlist, $value, $separator);
// $list: list-remove($list, index($list, $value));
// }
// @return $sortedlist;
// }
// // https://github.com/boltdesignsystem/bolt/blob/v3.0.1/packages/core-v3.x/styles/02-tools/tools-map-sort/_tools-map-sort.scss
// @function map-sort-keys($map) {
// $keys: list-sort-numeric(map.keys($map));
// $sorted-map: ();
// @each $key in $keys {
// $sorted-map: map.merge($sorted-map, ($key: map.get($map, $key)));
// }
// @return $sorted-map;
// }
/// Sorts a Sass map by its values. Values must be numeric.
/// @access private
/// @arg {Map} $map - a map to sort
/// @return {Map} sorted map
/// @example sass
/// map-sort-values((a: 200, b: 0))
/// // => (b: 0, a: 200)
/// @link https://github.com/iamandrewluca/map-sort-by-values/blob/8ad9fb6b4fa3eee8e8f79ddb305c6b3f8f4ad2c3/_index.scss
/// @author Andrew Luca <https://github.com/iamandrewluca>
@function map-sort-values($map) {
// Transform map to zipped list
$keys: ();
$values: ();
@each $key, $val in $map {
$keys: list.append($keys, $key);
$values: list.append($values, $val);
}
$list: list.zip($keys, $values);
// Sort zipped list and create sorted map
$sortedMap: ();
@while list.length($list) > 0 {
// Find smallest pair
$smallestPair: list.nth($list, 1);
@each $pair in $list {
$value: list.nth($pair, 2);
$smallestValue: list.nth($smallestPair, 2);
@if $value < $smallestValue {
$smallestPair: $pair;
}
}
// Add smallest pair to sorted map
$key: list.nth($smallestPair, 1);
$value: list.nth($smallestPair, 2);
$sortedMap: map.merge($sortedMap, ($key: $value));
// Remove from list smallest pair
$newList: ();
$smallestPairIndex: list.index($list, $smallestPair);
@for $i from 1 through list.length($list) {
@if $i != $smallestPairIndex {
$newList: list.append($newList, list.nth($list, $i), "space");
}
}
$list: $newList;
}
@return $sortedMap;
}
/// Checks whether given value is a number with px unit.
/// @access private
/// @arg {Any} $value - a value to check
/// @return {Bool} whether the value is a number with px unit
/// @example sass
/// is-number-px(100px) // => true
/// is-number-px(100em) // => false
/// is-number-px(100) // => false
/// is-number-px('text') // => false
@function is-number-px($value) {
@return meta.type-of($value) == 'number' and math.unit($value) == 'px';
}
/// Returns the index of given slice.
/// @access private
/// @arg {String} $slice - a slice to look up
/// @arg {Map} $slices [globally defined $slices] - a map of slices to work with
/// @return {Number} index of the slice (1-indexed)
/// @example sass
/// index-of-slice(xl) // => 6
/// index-of-slice(large, $slices: (small: 0px, large: 600px)) // => 2
@function index-of-slice($slice, $slices: $slices) {
$slices-sorted: map-sort-values($slices);
$keys: map.keys($slices-sorted);
$index: list.index($keys, $slice);
@return $index;
}
/// Checks whether given slice is the first one in the map.
/// @access private
/// @arg {String} $slice - a slice to look up
/// @arg {Map} $slices [globally defined $slices] - a map of slices to work with
/// @return {Bool} whether the slice is the first
/// @example sass
/// is-first-slice(xxs) // => true
/// is-first-slice(s) // => false
/// is-first-slice(small, $slices: (small: 0px, large: 600px)) // => true
@function is-first-slice($slice, $slices: $slices) {
$index: index-of-slice($slice, $slices);
@return $index == 1;
}
/// Checks whether given slice is the last one in the map.
/// @access private
/// @arg {String} $slice - a slice to look up
/// @arg {Map} $slices [globally defined $slices] - a map of slices to work with
/// @return {Bool} whether the slice is the last
/// @example sass
/// is-last-slice(xxl) // => false
/// is-last-slice(xxxl) // => true
/// is-last-slice(small, $slices: (small: 0px, large: 600px)) // => false
@function is-last-slice($slice, $slices: $slices) {
$index: index-of-slice($slice, $slices);
$length: list.length(map.keys($slices));
@return $index == $length;
}
/// Returns the name of the next slice
/// @access private
/// @arg {String} $slice - a slice to look up
/// @arg {Map} $slices [globally defined $slices] - a map of slices to work with
/// @return {string | null} the name of the next slice, or null if given slice was the last
/// @example sass
/// next-slice(s) // => m
/// next-slice(xxxl) // => null
/// next-slice(small, $slices: (small: 0px, large: 600px)) // => large
@function next-slice($slice, $slices: $slices) {
$slices-sorted: map-sort-values($slices);
$keys: map.keys($slices-sorted);
$index: list.index($keys, $slice);
@if $index == null {
@error "Slice not found", $slice;
}
@if ($index >= list.length($keys)) {
@return null;
}
@return list.nth($keys, $index + 1);
}
/// Returns the starting (normally left) breakpoint of given slice
/// @access private
/// @arg {String} $slice - a slice to look up
/// @arg {Map} $slices [globally defined $slices] - a map of slices to work with
/// @return {number | null} the breakpoint, or null if the slice is the first one
/// @example sass
/// start-bp-of-slice(xxs) // => null
/// start-bp-of-slice(xs) // => 200px
/// start-bp-of-slice(small, $slices: (small: 0px, large: 600px)) // => null
/// start-bp-of-slice(large, $slices: (small: 0px, large: 600px)) // => 600px
@function start-bp-of-slice($slice, $slices: $slices) {
@if is-first-slice($slice, $slices) {
@return null;
}
$bp: map.get($slices, $slice);
@return $bp;
}
/// Returns the ending (normally right) breakpoint of given slice,
/// which is the left breakpoint of the next slice minus 1px,
/// or null if the slice is the last one.
/// @access private
/// @arg {String} $slice - a slice to look up
/// @arg {Map} $slices [globally defined $slices] - a map of slices to work with
/// @return {number | null} the breakpoint, or null if the slice is the first one
/// @example sass
/// end-bp-of-slice(l) // => 999px
/// end-bp-of-slice(xxxl) // => null
/// end-bp-of-slice(small, $slices: (small: 0px, large: 600px)) // => 599px
/// start-bp-of-slice(large, $slices: (small: 0px, large: 600px)) // => null
@function end-bp-of-slice($slice, $slices: $slices) {
@if is-last-slice($slice, $slices) {
@return null;
}
$next-slice: next-slice($slice, $slices);
$bp: map.get($slices, $next-slice);
@return $bp - 1;
}
/// Generates a media query for a given slice or a range of two slices
/// @access private
/// @arg {String | Null} $slice-start - a slice whose left breakpoint will be used
/// @arg {String | Null} $slice-end - a slice whose right breakpoint will be used
/// @arg {Map} $slices [globally defined $slices] - a map of slices to work with
/// @arg {Map | null} $media [globally defined $media] - a media query to prend the result with
/// @return {string} resulting media query
/// @example sass
/// query-for-slice(l) // => (min-width: 800px) and (max-width: 999px)
/// query-for-slice(null, l) // => (max-width: 999px)
/// query-for-slice(l, null) // => (min-width: 800px)
/// query-for-slice(xxxl) // => (min-width: 1400px)
/// query-for-slice(xxxl, null) // => (min-width: 1400px)
/// query-for-slice(xxs) // => (max-width: 199px)
/// query-for-slice(null, xxs) // => (max-width: 199px)
/// query-for-slice(s l) // => (min-width: 400px) and (max-width: 999px)
/// query-for-slice(small, $slices: (small: 0px, large: 600px)) // => (max-width: 999px)
/// query-for-slice(l, $media: screen) // => screen and (min-width: 800px) and (max-width: 999px)
@function query-for-slice($slice-start, $slice-end: "__undefined__", $slices: $slices, $media: $media) {
$result: null;
@if ($slice-end == "__undefined__") {
$slice-end: $slice-start;
}
$start: $slice-start and start-bp-of-slice($slice-start, $slices);
$end: $slice-end and end-bp-of-slice($slice-end, $slices);
@if $start == null and $end == null {
@error "This query spans the whole range of viewports and is redundant.";
}
@else if $start != null and not is-number-px($start) {
@error "Expected left breakpoint of slice #{$slice-start} (#{$start}) to be a number with unit px.";
}
@else if $end != null and not is-number-px($end) {
@error "Expected right breakpoint of slice #{$slice-end} (#{$end}) to be a number with unit px.";
}
@else if $start != null and $end != null {
@if ($start >= $end) {
@error "Expected left breakpoint of slice #{$slice-start} (#{$start}) to be smaller than right breakpoint of slice #{$slice-end} (#{$end}). Try reversing the order of slices.";
}
$result: "(min-width: #{$start}) and (max-width: #{$end})";
}
@else if $start == null {
$result: "(max-width: #{$end})";
}
@else if $end == null {
$result: "(min-width: #{$start})";
}
@else {
@error "Neither left nor right breakpoint found for slices: #{$slice-start} #{$slice-end}";
}
@if ($media) {
$result: "#{$media} and #{$result}";
}
@return $result;
}
@function _query-for-slices($query, $slices: $slices, $media: $media) {
$result: "";
@if list.separator($query) == "space" {
@if list.length($query) > 2 or list.length($query) == 0 {
@error "Inavlid usage of query-for-slices: it accepts a comma-separated list of items where each item is a number or a space-separated list of two numbers";
}
@return query-for-slice($query..., $slices: $slices, $media: $media)
}
@each $slice-or-slices in $query {
@if string.length($result) > 0 {
$result: "#{$result}, ";
}
@if list.length($slice-or-slices) == 0 or list.length($slice-or-slices) > 2 {
@error "Inavlid usage of query-for-slices: it accepts a comma-separated list of items where each item is a number or a space-separated list of two numbers";
}
$result-item: query-for-slice($slice-or-slices..., $slices: $slices, $media: $media);
$result: "#{$result}#{$result-item}";
}
@return $result;
}
/// Generates a media query for a given comma-separated list of slices and ranges of two slices
/// @access private
/// @arg {Map} $query - a comma-separated list of slices and ranges of two slices
/// @arg {Map} $slices [globally defined $slices] - a map of slices to work with
/// @arg {Map | null} $media [globally defined $media] - a media query to prend the result with
/// @return {string} resulting media query
/// @example sass
/// query-for-slices(l) // => (min-width: 800px) and (max-width: 999px)
/// query-for-slices((null l)) // => (max-width: 999px)
/// query-for-slices((null l), xxxl) // => (max-width: 999px), (min-width: 1400px)
/// query-for-slices((s l), xxxl) // => (min-width: 400px) and (max-width: 999px), (min-width: 1400px)
/// query-for-slices(small, $slices: (small: 0px, large: 600px)) // => (max-width: 999px)
/// query-for-slices(l, $media: screen) // => screen and (min-width: 800px) and (max-width: 999px)
@function query-for-slices($query, $slices: $slices, $media: $media) {
@if $media and list.separator($media) == "comma" and list.length($media) > 1 {
$result: "";
@each $medium in $media {
@if string.length($result) > 0 {
$result: "#{$result}, ";
}
$result-item: _query-for-slices($query, $slices: $slices, $media: $medium);
$result: "#{$result}#{$result-item}";
}
@return $result;
}
@else {
@return _query-for-slices($query, $slices: $slices, $media: $media);
}
}
/// Wraps CSS code with a media query confined by a given slice
/// @access public
/// @arg {String} $slice - name of slice
/// @arg {Map} $slices [globally defined $slices] - a map of slices to work with
/// @arg {Map | null} $media [globally defined $media] - a media query to prend the result with
/// @content Any CSS to wrap with a media query. Note: requires a selector either inside or outside of the mixin invocation.
/// @output CSS wrapped in a media query
/// @example scss
/// .foo {
/// @include at(s) {
/// color: red;
/// }
/// }
///
/// // produces:
///
/// @media (min-width: 400px) and (max-width: 599px) {
/// .foo {
/// color: red;
/// }
/// }
/// @example scss
/// .foo {
/// @include at(medium, $slices: (small: 0px, medium: 300px, large: 600px), $media: (screen, print)) {
/// color: red;
/// }
/// }
///
/// // produces:
///
/// @media screen and (min-width: 300px) and (max-width: 599px), print and (min-width: 300px) and (max-width: 599px) {
/// .foo {
/// color: red;
/// }
/// }
@mixin at($slice, $slices: $slices, $media: $media) {
$media: query-for-slices($slice, $slices: $slices, $media: $media);
@media #{$media} {
@content;
}
}
/// Wraps CSS code with a media query confined by a given slice and larger
/// @access public
/// @arg {String} $slice - name of slice
/// @arg {Map} $slices [globally defined $slices] - a map of slices to work with
/// @arg {Map | null} $media [globally defined $media] - a media query to prend the result with
/// @content Any CSS to wrap with a media query. Note: requires a selector either inside or outside of the mixin invocation.
/// @output CSS wrapped in a media query
/// @example scss
/// .foo {
/// @include from(s) {
/// color: red;
/// }
/// }
///
/// // produces:
///
/// @media (min-width: 400px) {
/// .foo {
/// color: red;
/// }
/// }
/// @example scss
/// .foo {
/// @include from(medium, $slices: (small: 0px, medium: 300px, large: 600px), $media: (screen, print)) {
/// color: red;
/// }
/// }
///
/// // produces:
///
/// @media screen and (min-width: 300px), print and (min-width: 300px) {
/// .foo {
/// color: red;
/// }
/// }
@mixin from($slice, $slices: $slices, $media: $media) {
$media: query-for-slices(($slice null), $slices: $slices, $media: $media);
@media #{$media} {
@content;
}
}
/// Wraps CSS code with a media query confined by a given slice and smaller
/// @access public
/// @arg {String} $slice - name of slice
/// @arg {Map} $slices [globally defined $slices] - a map of slices to work with
/// @arg {Map | null} $media [globally defined $media] - a media query to prend the result with
/// @content Any CSS to wrap with a media query. Note: requires a selector either inside or outside of the mixin invocation.
/// @output CSS wrapped in a media query
/// @example scss
/// .foo {
/// @include to(s) {
/// color: red;
/// }
/// }
///
/// // produces:
///
/// @media (max-width: 599px) {
/// .foo {
/// color: red;
/// }
/// }
/// @example scss
/// .foo {
/// @include to(medium, $slices: (small: 0px, medium: 300px, large: 600px), $media: (screen, print)) {
/// color: red;
/// }
/// }
///
/// // produces:
///
/// @media screen and (max-width: 599px), print and (max-width: 599px) {
/// .foo {
/// color: red;
/// }
/// }
@mixin to($slice, $slices: $slices, $media: $media) {
$media: query-for-slices((null $slice), $slices: $slices, $media: $media);
@media #{$media} {
@content;
}
}
/// Wraps CSS code with a media query confined within two slices
/// @access public
/// @arg {String} $slice-start - name of starting slice
/// @arg {String} $slice-end - name of ending slice, inclusive
/// @arg {Map} $slices [globally defined $slices] - a map of slices to work with
/// @arg {Map | null} $media [globally defined $media] - a media query to prend the result with
/// @content Any CSS to wrap with a media query. Note: requires a selector either inside or outside of the mixin invocation.
/// @output CSS wrapped in a media query
/// @example scss
/// .foo {
/// @include between(s, l) {
/// color: red;
/// }
/// }
///
/// // produces:
///
/// @media (min-width: 400px) and (max-width: 999px) {
/// .foo {
/// color: red;
/// }
/// }
/// @example scss
/// .foo {
/// @include between(small, medium, $slices: (small: 0px, medium: 300px, large: 600px), $media: (screen, print)) {
/// color: red;
/// }
/// }
///
/// // produces:
///
/// @media screen and (max-width: 599px), print and (max-width: 599px) {
/// .foo {
/// color: red;
/// }
/// }
@mixin between($slice-start, $slice-end, $slices: $slices, $media: $media) {
$media: query-for-slices(($slice-start $slice-end), $slices: $slices, $media: $media);
@media #{$media} {
@content;
}
}
/// Wraps CSS code with a compound media query, using flexible syntax
/// @access public
/// @arg {Map} $query - a comma-separated list of slices and ranges of two slices
/// @arg {Map} $slices [globally defined $slices] - a map of slices to work with
/// @arg {Map | null} $media [globally defined $media] - a media query to prend the result with
/// @content Any CSS to wrap with a media query. Note: requires a selector either inside or outside of the mixin invocation.
/// @output CSS wrapped in a media query
/// @example scss
/// .foo {
/// @include query((s l), xxxl) {
/// color: red;
/// }
/// }
///
/// // produces:
///
/// @media (min-width: 400px) and (max-width: 999px), (min-width: 1400px) {
/// .foo {
/// color: red;
/// }
/// }
/// @example scss
/// .foo {
/// @include query(small, $slices: (small: 0px, large: 600px), $media: (screen, print)) {
/// color: red;
/// }
/// }
///
/// // produces:
///
/// @media screen and (max-width: 599px), print and (max-width: 599px) {
/// .foo {
/// color: red;
/// }
/// }
@mixin query($query, $slices: $slices, $media: $media) {
$media: query-for-slices($query, $slices: $slices, $media: $media);
@media #{$media} {
@content;
}
}