Skip to content

Commit a62f198

Browse files
author
AMJones
committed
Improves media query mixins and functions.
1 parent f355b4f commit a62f198

File tree

5 files changed

+81
-43
lines changed

5 files changed

+81
-43
lines changed

scss/_base.scss

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,6 @@
77

88
// Mixins
99
@import "mixins/media-query";
10-
@import "mixins/vendor-prefixes";
1110
@import "mixins/rounding";
1211
@import "base-mixins";
12+
@import "mixins/bs4-shim";

scss/mixins/_bs4-shim.scss

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
@import "media-query";
2+
3+
@mixin media-breakpoint-up($bp) {
4+
@include media("screen", ">=#{$bp}") {
5+
@content;
6+
}
7+
}
8+
9+
@mixin media-breakpoint-down($bp) {
10+
@include media("screen", "<=#{$bp}") {
11+
@content;
12+
}
13+
}
14+
15+
// Note: This mixin is for legacy bootstrap code that calls it. Vendor prefixes aren't needed for opacity.
16+
@mixin opacity($o) {
17+
opacity: $o;
18+
}
19+
20+
@mixin user-select($value) {
21+
user-select: $value;
22+
}

scss/mixins/_media-query.scss

Lines changed: 49 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1+
@import "../variables/root-variables";
2+
@import "../functions/functions";
3+
@import "../functions/map";
4+
15

26
@function get-expression-operator($expression) {
37
@each $operator in ('>=', '<=', '>', '<', '', '', '=') {
48
@if str-index($expression, $operator) {
59
@return $operator;
610
}
711
}
12+
13+
@return '';
814
}
915

1016
@function get-expression-value($expression, $operator) {
@@ -13,60 +19,69 @@
1319
@return $value;
1420
}
1521

22+
@function get-width-string($operator, $min: 0, $max: 0) {
23+
@if $operator == '' { $operator: '>='; }
24+
@if $operator == '' { $operator: '<='; }
25+
@if $operator == '=' {
26+
@if $max > 0 {
27+
@return ' (min-width: ' + $min + ') and (max-width: ' + $max + ')';
28+
} @else {
29+
@return ' (min-width: ' + $min + ')';
30+
}
31+
} @else if $operator == '<' {
32+
@return ' (max-width: ' + ($min - 1) + ')';
33+
} @else if $operator == '>' {
34+
@return ' (min-width: ' + ($max + 1) + ')';
35+
} @else if $operator == '<=' {
36+
@return ' (max-width: ' + $max + ')';
37+
} @else if $operator == '>=' {
38+
@return ' (min-width: ' + $min + ')';
39+
}
40+
41+
@return '';
42+
}
43+
1644
@function get-condition-string($viewport,$operator) {
1745
@each $class, $min in $grid-breakpoints {
1846
@if($class == $viewport) {
1947
$max: map-get-next($grid-breakpoints,$class, 0);
20-
@if $operator == '' { $operator: '>='; }
21-
@if $operator == '' { $operator: '<='; }
22-
@if $operator == '=' {
23-
@if $max > 0 {
24-
@return ' (min-width: ' + $min + ') and (max-width: ' + $max + ')';
25-
} @else {
26-
@return ' (min-width: ' + $min + ')';
27-
}
28-
} @else if $operator == '<' {
29-
@return ' (max-width: ' + ($min - 1) + ')';
30-
} @else if $operator == '>' {
31-
@return ' (min-width: ' + ($max + 1) + ')';
32-
} @else if $operator == '<=' {
33-
@return ' (max-width: ' + $max + ')';
34-
} @else if $operator == '>=' {
35-
@return ' (min-width: ' + $min + ')';
36-
}
48+
@return get-width-string($operator,$min,$max);
3749
}
3850
}
3951

4052
@return '';
4153
}
4254

43-
// Used with @include media("<sm",">xs") etc.
44-
@mixin media($conditions...) {
45-
$condition-string: '';
55+
@function _media($conditions...) {
56+
$conditions-string: "";
4657
@each $condition in $conditions {
47-
$operator: get-expression-operator($condition);
48-
$value: get-expression-value($condition, $operator);
49-
$condition-string: $condition-string + get-condition-string($value,$operator);
50-
}
51-
@media screen and #{$condition-string} {
52-
@content
58+
// Basic String Conditions
59+
@if map-has-key($condition-map, $condition) {
60+
$conditions-string: $conditions-string + map-get($condition-map,$condition);
61+
} @else {
62+
$operator: get-expression-operator($condition);
63+
$value: get-expression-value($condition,$operator);
64+
// Breakpoint Based Conditions
65+
@if map-has-key($grid-breakpoints, $value) {
66+
$conditions-string: $conditions-string + get-condition-string($value,$operator);
67+
} @else {
68+
$conditions-string: $conditions-string + get-width-string($operator,$value,$value);
69+
}
70+
}
5371
}
54-
}
5572

56-
@mixin media-breakpoint-up($bp) {
57-
@include media(">=#{$bp}") {
58-
@content;
59-
}
73+
@return $conditions-string;
6074
}
6175

62-
@mixin media-breakpoint-down($bp) {
63-
@include media("<=#{$bp}") {
76+
// Used with @include media("<sm",">xs") etc.
77+
@mixin media($conditions...) {
78+
@media #{_media($conditions)} {
6479
@content;
6580
}
6681
}
6782

6883
@mixin touch() {
69-
html.touch {
84+
@media #{_media("screen", "touch")} {
7085
@content;
7186
}
7287
}

scss/mixins/_vendor-prefixes.scss

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +0,0 @@
1-
// Note: This mixin is for legacy bootstrap code that calls it. Vendor prefixes aren't needed for opacity.
2-
@mixin opacity($o) {
3-
opacity: $o;
4-
}
5-
6-
@mixin user-select($value) {
7-
user-select: $value;
8-
}

scss/variables/_root-variables.scss

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,15 @@ $container-max-widths: (
7474
xl: 1140px
7575
) !default;
7676

77+
$condition-map: (
78+
"screen": "screen",
79+
"print" : "print",
80+
"coarse": "(pointer: fine)",
81+
"touch" : "(pointer: coarse), (-moz-touch-enabled)",
82+
"fine" : "(pointer: fine), not all and (-moz-touch-enabled)",
83+
"hidpi" : "(-webkit-min-device-pixel-ratio: 1.5), (min-resolution: 144dpi)"
84+
);
85+
7786
// endregion //////////////////////////////////////////////////////////// End Breakpoint & Grid
7887

7988
// region /////////////////////////////////////////////////////////////// Utility Variables

0 commit comments

Comments
 (0)