-
Notifications
You must be signed in to change notification settings - Fork 8
/
_mq.scss
445 lines (352 loc) · 11.7 KB
/
_mq.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
$mediaQuery: ();
$mq-ems: false !default;
$mq-em-base: 16px !default;
$mq-debug: false !default;
@import '_functions';
@function mediaType($mediaType: false, $hasAnd: true){
$media: '';
$mediaType: if($mediaType == 'not', 'not all', $mediaType);
@if ($mediaType){
$media: if($hasAnd, $mediaType + ' and ', $mediaType);
}
@return $media;
}
@function isRatio($string){
@if (type-of($string) == 'string' and $string != 'plus'){
@if (str-index($string, "/")){
@return true;
}
}
@return false;
}
@function isMedia($string){
@return type-of($string) == 'string' and $string != 'plus' and not isRatio($string);
}
@function calculateMQ($range, $breakpoint_1: null, $breakpoint_2: null, $mediaType: false, $debug: $mq-debug){
$swap-values: false;
$mediaTypeString: '' + $mediaType + '';
$invalidMediaType:
($mediaType != false) and (
(type-of($mediaType) == 'number') or
(str-index($mediaTypeString, '/') != null)
);
@if ($invalidMediaType == true){
@error '"'+$mediaType+'" is not a valid media type';
}
@if ($range == null) {
@return $mediaType;
}
@if ($breakpoint_2 != null){
@if (isRatio($breakpoint_1)){
$A1: get-start($breakpoint_1);
$A2: get-end($breakpoint_1);
$B1: get-start($breakpoint_2);
$B2: get-end($breakpoint_2);
@if ($debug) {
@debug range_numbers "('A1/A2', 'B1/B2')" (A1: $A1, A2: $A2, B1: $B1, B2: $B2);
}
$swap-values: $A1 / $A2 < $B1 / $B2;
} @else {
$swap-values: $breakpoint_1 < $breakpoint_2;
}
//Swaps the breakpoint_s around
@if ($swap-values){
$temp: $breakpoint_1;
$breakpoint_1: $breakpoint_2;
$breakpoint_2: $temp;
}
} @else {
$breakpoint_2: 0;
}
$mediaString: '';
@if ($range == 'orientation'){
@if ($breakpoint_1 != 'landscape' and $breakpoint_1 != 'portrait'){
@error '"orientation" range only accepts the values "landscape" and "portrait". Currently providing the value "#{$breakpoint_1}".';
}
}
@if ($debug){
@debug calculateMQ (range: $range, breakpoint_1: $breakpoint_1, breakpoint_2: $breakpoint_2, mediaType: $mediaType);
}
@if (str-index($range, 'inside') != null or str-index($range, 'outside') != null){
@if ($breakpoint_2 == 0) {
@error '"#{$range}" range requires a second value. Currently only providing "#{$breakpoint_1}" as a value.';
}
} @else {
@if ($breakpoint_2 != 0) {
@error '"#{$range}" range can only take one value. Currently providing both "#{$breakpoint_1}" and "#{$breakpoint_2}" as values.';
}
}
@if (str-index($range, 'ratio') == null){
//if a standard media query
@if (type-of($breakpoint_1) != number and $range != 'orientation'){
@if ($breakpoint_2 != 0) {
@error '"#{$range}" range requires the breakpoint_s ("#{$breakpoint_1}" and "#{$breakpoint_2}") to be pixel values';
} @else {
@error '"#{$range}" range requires the breakpoint_ "#{$breakpoint_1}" to be a pixel value';
}
}
} @else {
//else if it's a ratio...
@if ((($breakpoint_2 != 0) and (type-of($breakpoint_2) != string))){
@error '"#{$range}" range requires the breakpoint_s ("#{$breakpoint_1}" and "#{$breakpoint_2}") to both be strings in this format "4 / 3" (width ratio / height ratio).';
} @else if (type-of($breakpoint_1) != string) {
@error '"#{$range}" range requires the breakpoint_ "#{$breakpoint_1}" to be a string in this format "4 / 3" (width ratio / height ratio).';
}
}
$breakpoint_1_plus: $breakpoint_1 + 1;
$breakpoint_2_plus: $breakpoint_2 + 1;
$media: if($mediaType, $mediaType, '');
@if ($mq-ems) {
$breakpoint_1: _mq-em($breakpoint_1);
$breakpoint_2: _mq-em($breakpoint_2);
$breakpoint_1_plus: _mq-em($breakpoint_1_plus);
$breakpoint_2_plus: _mq-em($breakpoint_2_plus);
}
$min-width: '(min-width: #{$breakpoint_1_plus})';
$max-width: '(max-width: #{$breakpoint_1})';
$inside-width: '(max-width: #{$breakpoint_1}) and (min-width: #{$breakpoint_2_plus})';
$outside-width: '(max-width: #{$breakpoint_2}), '+$media+'(min-width: #{$breakpoint_1_plus})';
$mediaString: map-get((
//*1 value given*/
min : $min-width,
max : $max-width,
min-width : $min-width,
max-width : $max-width,
min-height : '(min-height: #{$breakpoint_1_plus})',
max-height : '(max-height: #{$breakpoint_1})',
ratio : '(aspect-ratio: #{$breakpoint_1})',
min-ratio : '(min-aspect-ratio: #{$breakpoint_1})',
max-ratio : '(max-aspect-ratio: #{$breakpoint_1})',
device-ratio : '(device-aspect-ratio: #{$breakpoint_1})',
min-device-ratio : '(min-device-aspect-ratio: #{$breakpoint_1})',
max-device-ratio : '(max-device-aspect-ratio: #{$breakpoint_1})',
orientation : '(orientation: #{$breakpoint_1})',
//*2 values given*/
inside : $inside-width,
outside : $outside-width,
inside-width : $inside-width,
outside-width : $outside-width,
inside-height : '(max-height: #{$breakpoint_1}) and (min-height: #{$breakpoint_2_plus})',
outside-height : '(max-height: #{$breakpoint_2}), '+$media+'(min-height: #{$breakpoint_1_plus})',
inside-ratio : '(max-aspect-ratio: #{$breakpoint_1}) and (min-aspect-ratio: #{$breakpoint_2})',
outside-ratio : '(max-aspect-ratio: #{$breakpoint_2}), '+$media+'(min-aspect-ratio: #{$breakpoint_1})',
inside-device-ratio : '(max-device-aspect-ratio: #{$breakpoint_1}) and (min-device-aspect-ratio: #{$breakpoint_2})',
outside-device-ratio : '(max-device-aspect-ratio: #{$breakpoint_2}), '+$media+'(min-device-aspect-ratio: #{$breakpoint_1})',
), $range);
@return $mediaString;
}
@function isBasicQuery($range) {
$first_is_string: type-of(nth($range,1)) == string;
$longer_than_1: length($range) > 1;
$not_plus_statment: true;
@if ($longer_than_1) {
$not_plus_statment: nth($range,2) != 'plus';
}
@return $first_is_string and $longer_than_1 and $not_plus_statment;
}
@function get_values($mqSet, $debug: $mq-debug){
@if ($mq-debug) {
@debug mqSet (length: length($mqSet), mqSet: $mqSet);
}
$range: nth($mqSet,1);
$breakpoint_1: null;
$breakpoint_2: null;
$media: false;
@if (length($mqSet) == 1){
$first: nth($mqSet, 1);
@if (type-of($first) != 'string'){
@error '"#{$first}" is not a valid media type';
}
$range: null;
$media: $first;
}
@if (length($mqSet) > 1){
$breakpoint_1: nth($mqSet, 2);
}
@if (length($mqSet) == 3){
$third: nth($mqSet, 3);
@if (type-of($third) == 'number' or isRatio($third)){
$breakpoint_2: $third;
} @else {
$media: $third;
}
}
@if (length($mqSet) == 4){
$third: nth($mqSet, 3);
$fourth: nth($mqSet, 4);
@if ($fourth == false and isMedia($third)){
$media: $third;
} @else {
$breakpoint_2: $third;
$media: $fourth;
}
@if ($media == " and ") {
$media: '';
}
}
$finalValues: (
range: $range,
breakpoint_1: $breakpoint_1,
breakpoint_2: $breakpoint_2,
media: mediaType($media, $range != null),
);
@if ($debug) {
@debug get_values_result $finalValues;
}
@return $finalValues;
}
@function get_media($mqSet){
@return map-get( get_values($mqSet), media);
}
@function calculateBasicQuery($mqSet, $isAndStatement: false, $debug: $mq-debug){
$values: get_values($mqSet, $debug);
$range: map-get($values, range);
$breakpoint_1: map-get($values, breakpoint_1);
$breakpoint_2: map-get($values, breakpoint_2);
$media: map-get($values, media);
@if ($range == null) {
//if only a media type is given return nothing because it is added elsewhere
@return '';
}
@if ((str-index($range, 'outside') != null) and $isAndStatement){
$error: 'All "outside" range types are incompatible with "plus" statements: (' + $mqSet + ')';
@error $error;
}
@return calculateMQ($range, $breakpoint_1, $breakpoint_2, $media, $debug: $debug);
}
@function joinQueries($range, $queryString: (), $debug: $mq-debug){
$isAndStatement: false;
$contains_media_only_statement: false;
$media_statement_at_start: true;
@if (length($range) > 1) {
@for $i from 1 through length($range) {
$statement: nth($range,$i);
@if ($debug) {
@debug joinQueries_statement $statement;
}
$mediaNotFirst: $i != 1 and isMedia($statement);
@if ($statement == 'plus') {
$isAndStatement: true;
@if ($i == 2 and length(nth($range,1)) == 1) {
$contains_media_only_statement: true;
}
}
@if ($mediaNotFirst){
$media_statement_at_start: false;
}
}
} @else if (type-of(nth($range,1)) == 'string') {
$contains_media_only_statement: true;
}
@if ($isAndStatement and not $media_statement_at_start) {
@error 'Media type definitions must be specified at the start of plus statements: (#{$range})';
}
//Uses this functionality if using a single level MQ variable
//eg:
//$MQ-example--single: inside, 1000px, 500px;
//@include mq($MQ-example--single){}
@if (isBasicQuery($range)){
@return get_media($range) + calculateBasicQuery($range, $isAndStatement, $debug: $debug);
//Uses this functionality if using a multi level MQ variable
//eg:
//$MQ-example--multiple:
// (inside, 1000px, 500px),
// (min, 1200px)
//;
//@include mq($MQ-example--multiple){}
} @else {
@each $mqSet in $range {
@if ($mqSet != 'plus') {
@if (isBasicQuery($mqSet)){
$newQuery: calculateBasicQuery($mqSet, $isAndStatement, $debug: $debug);
$media: get_media($mqSet);
@if ($isAndStatement) {
@if ($queryString == ()){
$queryString: $media + $newQuery;
} @else if ($newQuery != '') {
$queryString: $queryString + ' and ' + $newQuery;
}
} @else {
$queryString: append($queryString, $media + $newQuery, 'comma');
}
} @else {
$is_media_only_statment: $contains_media_only_statement and type-of($mqSet) == 'string';
@if ($isAndStatement) {
@if ($is_media_only_statment) {
$queryString: mediaType($mqSet, false);
} @else {
$queryString: joinQueries($mqSet, $queryString, $debug);
}
@if (length($queryString) > 1){
$error: 'mq-scss does not support "or" statements inside "plus" statements: ' + $queryString;
@error $error;
}
} @else {
@if ($is_media_only_statment) {
$queryString: $mqSet;
} @else {
$queryString: append($queryString, joinQueries($mqSet, $debug: $debug), 'comma');
}
}
}
}
}
@return $queryString;
}
}
@mixin mq($range, $breakpoint_1: null, $breakpoint_2: null, $mediaType: false, $debug: $mq-debug) {
@if ($debug) {
@debug $range;
}
@if (length($range) > 1) {
$finalOutput: joinQueries($range, $debug: $debug);
@if ($debug) {
@debug '!!!!! FINAL OUTPUT: @media ' + $finalOutput;
}
@media #{$finalOutput} {
@content;
}
//Uses this functionality if placing MQ data inline
//eg:
//@include mq(inside, 1000px, 500px, 'screen');
} @else {
//standardises the values to prevent mediaType from getting confused with breakpoint_2
$mediaOnly: $breakpoint_1 == null and $breakpoint_2 == null and $mediaType == false;
@if ($debug) {
@debug inline_mq_values (range: $range, breakpoint_1: $breakpoint_1, breakpoint_2: $breakpoint_2, mediaType: $mediaType, mediaOnly: $mediaOnly);
}
$values: get_values(if($mediaOnly, $range, ($range $breakpoint_1 $breakpoint_2 $mediaType)), $debug: $debug);
$newRange: map-get($values, range);
$newWidth1: map-get($values, breakpoint_1);
$newWidth2: map-get($values, breakpoint_2);
$newMedia: map-get($values, media);
@if ($mediaOnly) {
@if ($debug) {
@debug '!!!!! FINAL OUTPUT: @media ' + $newMedia;
}
@media #{ $newMedia } {
@content;
}
} @else {
$finalMQ: $newMedia + calculateMQ($newRange, $newWidth1, $newWidth2, $debug: $debug);
@if ($debug) {
@debug '!!!!! FINAL OUTPUT: @media ' + $finalMQ ;
}
@media #{ $finalMQ } {
@content;
}
}
}
@if ($debug){
//spacer for seperating results
@debug '';
}
}
@mixin retina($density: 2) {
@media
only screen and (min-device-pixel-ratio: $density),
only screen and (min-resolution: $density * 96ppi),
only screen and (min-resolution: $density * 1dppx) {
@content;
}
}