forked from sass-mq/sass-mq
-
Notifications
You must be signed in to change notification settings - Fork 1
/
_mq.scss
423 lines (388 loc) · 11.5 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
@use 'sass:math';
@use 'sass:map';
@use 'sass:list';
/// Breakpoint list
///
/// Name your breakpoints in a way that creates a ubiquitous language
/// across team members. It will improve communication between
/// stakeholders, designers, developers, and testers.
///
/// @type Map
/// @link https://github.com/mcaskill/sass-mq#seeing-the-currently-active-breakpoint Full documentation and examples
$breakpoints: (
mobile: 320px,
tablet: 740px,
desktop: 980px,
wide: 1300px,
) !default;
/// Show breakpoints in the top right corner
///
/// If you want to display the currently active breakpoint in the top
/// right corner of your site during development, add the breakpoints
/// to this list, ordered by length. For example: (mobile, tablet, desktop).
///
/// @example scss
/// @use 'path/to/mq' with ($show-breakpoints: ('mobile', 'tablet', 'desktop'));
///
///
/// @type map
$show-breakpoints: () !default;
/// Customize the media range feature (for example: `@media (min-width…)`
/// or `@media (min-height:…)`)
/// By default sass-mq uses an `width` range feature.
///
/// If you want to overried the range feature, you can use this option.
/// @example scss
/// @use 'path/to/mq' with ($range-feature: height);
///
/// @type String
/// @link https://github.com/mcaskill/sass-mq#changing-range-feature Full documentation and example
$range-feature: width !default;
/// Customize the media type (for example: `@media screen` or `@media print`)
/// By default sass-mq uses an "all" media type (`@media all and …`)
///
/// If you want to overried the media type, you can use this option.
/// @example scss
/// @use 'path/to/mq' with ($media-type: 'screen');
///
/// @type String
/// @link https://github.com/mcaskill/sass-mq#changing-media-type Full documentation and example
$media-type: all !default;
/// Convert pixels to ems
///
/// @param {Number} $px - value to convert
///
/// @example scss
/// $font-size-in-ems: px2em(16px);
/// p { font-size: px2em(16px); }
///
/// @returns {Number}
@function px2em($px) {
@if math.is-unitless($px) {
@warn "Assuming #{$px} to be in pixels, attempting to convert it into pixels.";
@return px2em($px * 1px);
}
// if $px is compatible with em units, then return value unchanged
@if math.compatible($px, 1em) {
@return $px;
}
@return math.div($px, 16px) * 1em;
}
/// Get a breakpoint's length
///
/// @param {String} $name - Name of the breakpoint. One of $breakpoints
///
/// @example scss
/// $tablet-width: get-breakpoint-length(tablet);
/// @media (min-width: get-breakpoint-length(tablet)) {}
///
/// @requires {Variable} $breakpoints
///
/// @returns {Number} Value in pixels
@function get-breakpoint-length($name, $breakpoints: $breakpoints) {
@if map.has-key($breakpoints, $name) {
@return map.get($breakpoints, $name);
} @else {
@warn "Breakpoint #{$name} wasn't found in $breakpoints.";
@return null;
}
}
/// Joins all elements of `$list` with `$glue`.
///
/// @ignore Documentation: https://github.com/at-import/SassyLists/blob/master/stylesheets/functions/_to-string.scss
///
/// @param {List} $list - list to cast
/// @param {String} $glue [' and '] - value to use as a join string
///
/// @example scss
/// stringify(a b c)
/// // a and b and c
///
/// @example scss
/// stringify(a b c, ', ')
/// // a, b, c
///
/// @returns {String}
@function stringify($list, $glue: ' and ') {
$result: '';
@each $item in $list {
$result: $result + if(length($item) > 1, stringify($item, $glue), $item);
@if $item != nth($list, -1) {
$result: $result + $glue;
}
}
@return quote($result);
}
/// Media Query function
///
/// Computes a media query based on a list of conditions.
///
/// @param {String | Boolean} $from [false] - One of $breakpoints
/// @param {String | Boolean} $until [false] - One of $breakpoints
/// @param {String | Boolean} $and [false] - Additional media query parameters
/// @param {String | Boolean} $or [false] - Alternative media query parameters
/// @param {String} $range-feature [$range-feature] - Media range feature: width, height…
/// @param {String} $media-type [$media-type] - Media type: screen, print…
///
/// @ignore Undocumented API, for advanced use only:
/// @ignore @param {Map} $breakpoints [$breakpoints]
///
/// @requires {Variable} $range-feature
/// @requires {Variable} $media-type
/// @requires {Variable} $breakpoints
/// @requires {function} stringify
///
/// @example scss
/// $mq-lap-and-up: mq($from: mobile);
///
/// $mq-palm: mq($until: tablet);
///
/// $mq-lap: mq(mobile, tablet);
///
/// $mq-portable: mq($from: tablet, $and: '(orientation: landscape)');
///
/// $mq-desk-small: mq(950px) {
///
/// $mq-portable-screen: mq(tablet, $media-type: screen) {
///
/// // Advanced use:
/// $my-breakpoints: (L: 900px, XL: 1200px);
/// $mq-custom: mq(L, $breakpoints: $my-breakpoints);
@function mq(
$from: false,
$until: false,
$and: false,
$or: false,
$range-feature: $range-feature,
$media-type: $media-type,
$breakpoints: $breakpoints
) {
$min-value: 0;
$max-value: 0;
$media-query: ();
// From: this breakpoint (inclusive)
@if $from {
@if type-of($from) == number {
$min-value: px2em($from);
} @else {
$min-value: px2em(get-breakpoint-length($from, $breakpoints));
}
}
// Until: that breakpoint (exclusive)
@if $until {
@if type-of($until) == number {
$max-value: px2em($until);
} @else {
$max-value: px2em(get-breakpoint-length($until, $breakpoints)) - 0.01em;
}
}
@if $range-feature {
@if $min-value != 0 {
$media-query: append($media-query, '(min-#{$range-feature}: #{$min-value})');
}
@if $max-value != 0 {
$media-query: append($media-query, '(max-#{$range-feature}: #{$max-value})');
}
}
@if $and {
$media-query: append($media-query, '#{$and}');
}
$media-query: stringify($media-query, ' and ');
// Prevent unnecessary media query prefix 'all and '
@if ($media-type != 'all' and $media-query != '') {
$media-query: '#{$media-type} and #{$media-query}';
}
@else if $media-query == '' {
$media-query: $media-type;
}
@if $or {
$media-query: append($media-query, '#{$or}');
$media-query: stringify($media-query, ', ');
}
$media-query: unquote("#{$media-query}");
@return $media-query;
}
/// Media Query mixin
///
/// Generates a media query block, based on a list of conditions, around a set
/// of nested CSS statements.
///
/// @param {String | Boolean} $from [false] - One of $breakpoints
/// @param {String | Boolean} $until [false] - One of $breakpoints
/// @param {String | Boolean} $and [false] - Additional media query parameters
/// @param {String | Boolean} $or [false] - Alternative media query parameters
/// @param {String} $range-feature [$range-feature] - Media range feature: width, height…
/// @param {String} $media-type [$media-type] - Media type: screen, print…
///
/// @ignore Undocumented API, for advanced use only:
/// @ignore @param {Map} $breakpoints [$breakpoints]
///
/// @content styling rules, wrapped into a @media query
///
/// @requires {Variable} $range-feature
/// @requires {Variable} $media-type
/// @requires {Variable} $breakpoints
/// @requires {function} mq
///
/// @link https://github.com/mcaskill/sass-mq#responsive-mode Full documentation and examples
///
/// @example scss
/// @use 'path/to/mq' as *;
/// .element {
/// @include mq($from: mobile) {
/// color: red;
/// }
/// @include mq($until: tablet) {
/// color: blue;
/// }
/// @include mq(mobile, tablet) {
/// color: green;
/// }
/// @include mq($from: tablet, $and: '(orientation: landscape)') {
/// color: teal;
/// }
/// @include mq(950px) {
/// color: hotpink;
/// }
/// @include mq(tablet, $media-type: screen) {
/// color: hotpink;
/// }
/// // Advanced use:
/// $my-breakpoints: (L: 900px, XL: 1200px);
/// @include mq(L, $breakpoints: $my-breakpoints) {
/// color: hotpink;
/// }
/// }
@mixin mq(
$from: false,
$until: false,
$and: false,
$or: false,
$range-feature: $range-feature,
$media-type: $media-type,
$breakpoints: $breakpoints
) {
$media-query: mq(
$from,
$until,
$and,
$or,
$range-feature,
$media-type,
$breakpoints
);
@media #{$media-query} {
@content;
}
}
/// Quick sort
///
/// @author Sam Richards
/// @access private
/// @param {List} $list - List to sort
/// @returns {List} Sorted List
@function _quick-sort($list) {
$less: ();
$equal: ();
$large: ();
@if length($list) > 1 {
$seed: list.nth($list, math.ceil(math.div(length($list), 2)));
@each $item in $list {
@if ($item == $seed) {
$equal: list.append($equal, $item);
} @else if ($item < $seed) {
$less: list.append($less, $item);
} @else if ($item > $seed) {
$large: list.append($large, $item);
}
}
@return join(join(_quick-sort($less), $equal), _quick-sort($large));
}
@return $list;
}
/// Sort a map by values (works with numbers only)
///
/// @access private
/// @param {Map} $map - Map to sort
/// @returns {Map} Map sorted by value
@function _map-sort-by-value($map) {
$map-sorted: ();
$map-keys: map.keys($map);
$map-values: map.values($map);
$map-values-sorted: _quick-sort($map-values);
// Reorder key/value pairs based on key values
@each $value in $map-values-sorted {
$index: index($map-values, $value);
$key: list.nth($map-keys, $index);
$map-sorted: map.merge(
$map-sorted,
(
$key: $value,
)
);
// Unset the value in $map-values to prevent the loop
// from finding the same index twice
$map-values: list.set-nth($map-values, $index, 0);
}
@return $map-sorted;
}
/// Add a breakpoint
///
/// @param {String} $name - Name of the breakpoint
/// @param {Number} $length - Length of the breakpoint
///
/// @requires {Variable} $breakpoints
///
/// @example scss
/// @include add-breakpoint(tvscreen, 1920px);
/// @include mq(tvscreen) {}
@mixin add-breakpoint($name, $length) {
$new-breakpoint: (
$name: $length,
);
$breakpoints: map.merge($breakpoints, $new-breakpoint) !global;
$breakpoints: _map-sort-by-value($breakpoints) !global;
}
/// Show the active breakpoint in the top right corner of the viewport
/// @link https://github.com/mcaskill/sass-mq#seeing-the-currently-active-breakpoint
///
/// @param {List} $show-breakpoints [$show-breakpoints] - List of breakpoints to show in the top right corner
/// @param {Map} $breakpoints [$breakpoints] - Breakpoint names and sizes
///
/// @requires {Variable} $breakpoints
/// @requires {Variable} $show-breakpoints
///
/// @example scss
/// // Show breakpoints using global settings
/// @include show-breakpoints;
///
/// // Show breakpoints using custom settings
/// @include show-breakpoints((L, XL), (S: 300px, L: 800px, XL: 1200px));
@mixin show-breakpoints(
$show-breakpoints: $show-breakpoints,
$breakpoints: $breakpoints
) {
body:before {
background-color: #fcf8e3;
border-bottom: 1px solid #fbeed5;
border-left: 1px solid #fbeed5;
color: #c09853;
font: small-caption;
padding: 3px 6px;
pointer-events: none;
position: fixed;
right: 0;
top: 0;
z-index: 100;
// Loop through the breakpoints that should be shown
@each $show-breakpoint in $show-breakpoints {
$length: get-breakpoint-length($show-breakpoint, $breakpoints);
@include mq($show-breakpoint, $breakpoints: $breakpoints) {
content: '#{$show-breakpoint} ≥ #{$length} (#{px2em($length)})';
}
}
}
}
@if list.length($show-breakpoints) > 0 {
@include show-breakpoints;
}