]> git.ipfire.org Git - thirdparty/foundation/foundation-sites.git/blob - scss/util/_breakpoint.scss
fix: updated sass map syntax
[thirdparty/foundation/foundation-sites.git] / scss / util / _breakpoint.scss
1 // Foundation for Sites
2 // https://get.foundation
3 // Licensed under MIT Open Source
4
5 ////
6 /// @group breakpoints
7 ////
8
9 /// Patch to fix issue #12080
10 $-zf-size: null;
11
12 /// A list of named breakpoints. You can use these with the `breakpoint()` mixin to quickly create media queries.
13 /// @type Map
14 $breakpoints: (
15 "small": 0,
16 "medium": 640px,
17 "large": 1024px,
18 "xlarge": 1200px,
19 "xxlarge": 1440px,
20 ) !default;
21
22 /// A list of named HiDPI breakpoints. You can use these with the `breakpoint()` mixin to quickly create media queries for resolutions.
23 /// Values must represent the device pixels / web pixels ration and be unitless or in DPPX.
24 /// @type Map
25 $breakpoints-hidpi: (
26 "hidpi-1": 1,
27 "hidpi-1-5": 1.5,
28 "hidpi-2": 2,
29 "retina": 2,
30 "hidpi-3": 3
31 ) !default;
32
33 /// The largest named breakpoint in which to include print as a media type
34 /// @type Keyword
35 $print-breakpoint: large !default;
36
37 $-zf-zero-breakpoint: small !default;
38
39 $-zf-breakpoints-keys: map-to-list($breakpoints, 'keys');
40
41 @if nth(map-values($breakpoints), 1) != 0 {
42 @error 'The first key in the $breakpoints map must have a value of "0".';
43 }
44 @else {
45 $-zf-zero-breakpoint: nth(map-keys($breakpoints), 1);
46 }
47
48 /// All of the names in this list will be output as classes in your CSS, like `.small-12`, `.medium-6`, and so on. Each value in this list must also be in the `$breakpoints` map.
49 /// @type List
50 $breakpoint-classes: (small medium large) !default;
51
52 /// Generates a media query string matching the input value. Refer to the documentation for the `breakpoint()` mixin to see what the possible inputs are.
53 ///
54 /// @param {Keyword|Number} $val [small] - Breakpoint name, or px, rem, or em value to process.
55 @function breakpoint($val: $-zf-zero-breakpoint) {
56 // Web standard Pixels per inch. (1ddpx / $std-web-dpi) = 1dpi
57 // See https://www.w3.org/TR/css-values-3/#absolute-lengths
58 $std-web-dpi: 96;
59
60 // Size or keyword
61 $bp: nth($val, 1);
62 // Value of the following breakpoint
63 $bp-next: null;
64 // Value for max-width media queries
65 $bp-min: null;
66 // Value for min-width media queries
67 $bp-max: null;
68 // Direction of media query (up, down, or only)
69 $dir: if(length($val) > 1, nth($val, 2), up);
70 // If named, name of the breakpoint
71 $name: null;
72 // If the breakpoint is a HiDPI breakpoint
73 $hidpi: false;
74
75 // Orientation media queries have a unique syntax
76 @if $bp == 'landscape' or $bp == 'portrait' {
77 @return '(orientation: #{$bp})';
78 }
79
80 // If a breakpoint name is given, get its value from the $breakpoints/$breakpoints-hidpi map.
81 @if type-of($bp) == 'string' {
82 @if map-has-key($breakpoints, $bp) {
83 $name: $bp;
84 $bp: map-get($breakpoints, $name);
85 $bp-next: -zf-map-next($breakpoints, $name);
86 }
87 @else if map-has-key($breakpoints-hidpi, $bp) {
88 $name: $bp;
89 $bp: map-get($breakpoints-hidpi, $name);
90 $bp-next: -zf-map-next-number($breakpoints-hidpi, $bp);
91 $hidpi: true;
92 }
93 @else {
94 $bp: 0;
95 @warn 'breakpoint(): "#{$val}" is not defined in your `$breakpoints` or `$breakpoints-hidpi` setting.';
96 }
97 }
98
99 @if not $name and $dir == 'only' {
100 @warn 'breakpoint(): Only named media queries can have an `only` range.';
101 @return null;
102 }
103
104 // Only 'only' and 'up' have a min limit.
105 @if $dir == 'only' or $dir == 'up' {
106 $bp-min: if($hidpi, strip-unit($bp), -zf-bp-to-em($bp));
107 }
108 // Only 'only' and 'down' have a max limit.
109 @if $dir == 'only' or $dir == 'down' {
110 // If the breakpoint is a value, use it as max limit.
111 @if not $name {
112 $bp-max: if($hidpi, strip-unit($bp), -zf-bp-to-em($bp));
113 }
114 // If the breakpoint is named, the max limit is the following breakpoint - 1px.
115 @else if $bp-next {
116 // Max value is 0.2px under the next breakpoint (0.02 / 16 = 0.00125).
117 // Use a precision under 1px to support browser zoom, but not to low to avoid rounding.
118 // See https://github.com/foundation/foundation-sites/issues/11313
119 $bp-max: if($hidpi, $bp-next - divide(1, $std-web-dpi), -zf-bp-to-em($bp-next) - 0.00125);
120 }
121 }
122
123 // Generate the media query string from min and max limits.
124 @if $hidpi {
125 // Generate values in DPI instead of DPPX for an IE9-11/Opera mini compatibility.
126 // See https://caniuse.com/#feat=css-media-resolution
127 $bp-min-dpi: if($bp-min, $bp-min * $std-web-dpi * 1dpi, $bp-min);
128 $bp-max-dpi: if($bp-max, $bp-max * $std-web-dpi * 1dpi, $bp-max);
129 @return zf-str-join(
130 -zf-bp-join($bp-min, $bp-max, '-webkit-min-device-pixel-ratio', '-webkit-max-device-pixel-ratio'),
131 -zf-bp-join($bp-min-dpi, $bp-max-dpi, 'min-resolution', 'max-resolution'),
132 ', ');
133 }
134 @else {
135 @return -zf-bp-join($bp-min, $bp-max);
136 }
137 }
138
139 /// Wraps a media query around the content you put inside the mixin. This mixin accepts a number of values:
140 /// - If a string is passed, the mixin will look for it in the `$breakpoints` and `$breakpoints-hidpi` maps, and use a media query there.
141 /// - If a pixel value is passed, it will be converted to an em value using `$global-font-size` as the base.
142 /// - If a rem value is passed, the unit will be changed to em.
143 /// - If an em value is passed, the value will be used as-is.
144 ///
145 /// If multiple values are passed, the mixin will generate a media query for each of them as described above.
146 /// Since the content is duplicated for each breakpoint, this mixin should only be used with properties that
147 /// change across breakpoints.
148 ///
149 /// @param {Keyword|Number} $values... - Breakpoint name or px/rem/em value to process.
150 ///
151 /// @output If the breakpoint is "0px and larger", outputs the content as-is. Otherwise, outputs the content wrapped in a media query.
152 @mixin breakpoint($values...) {
153 @for $i from 1 through length($values) {
154 $value: nth($values, $i);
155 $str: breakpoint($value);
156 $bp: index($-zf-breakpoints-keys, nth($value, 1));
157 $pbp: index($-zf-breakpoints-keys, $print-breakpoint);
158 // Direction of media query (up, down, or only)
159 $dir: if(length($value) > 1, nth($value, 2), up);
160
161 $old-zf-size: null;
162
163 // Make breakpoint size available as a variable
164 @if global-variable-exists(-zf-size) {
165 $old-zf-size: $-zf-size;
166 }
167 $-zf-size: nth($value, 1) !global; // get the first value to account for `only` and `down` keywords
168
169 // If $str is still an empty string, no media query is needed
170 @if $str == '' {
171 @content;
172 }
173
174 // Otherwise, wrap the content in a media query
175 @else {
176 // For named breakpoints less than or equal to $print-breakpoint, add print to the media types
177 // generate print if the breakpoint affects the print-breakpoint (or smaller).
178 // This means the current condition only needs to be extended so 'down' always generates print.
179 @if $bp != null and ($bp <= $pbp or $dir == down) {
180 @media print, screen and #{$str} {
181 @content;
182 }
183 }
184 @else {
185 @media screen and #{$str} {
186 @content;
187 }
188 }
189 }
190
191 $-zf-size: $old-zf-size !global;
192 }
193 }
194
195 /// Converts the breakpoints map to a URL-encoded string, like this: `key1=value1&key2=value2`. The value is then dropped into the CSS for a special `<meta>` tag, which is read by the Foundation JavaScript. This is how we transfer values from Sass to JavaScript, so they can be defined in one place.
196 /// @access private
197 ///
198 /// @param {Map} $map - Map to convert.
199 ///
200 /// @returns {String} A string containing the map's contents.
201 @function -zf-bp-serialize($map) {
202 $str: '';
203 @each $key, $value in $map {
204 $str: $str + $key + '=' + -zf-bp-to-em($value) + '&';
205 }
206 $str: str-slice($str, 1, -2);
207
208 @return $str;
209 }
210
211 /// Find the next key in a map.
212 /// @access private
213 ///
214 /// @param {Map} $map - Map to traverse.
215 /// @param {Mixed} $key - Key to use as a starting point.
216 ///
217 /// @returns {Mixed} The value for the key after `$key`, if `$key` was found. If `$key` was not found, or `$key` was the last value in the map, returns `null`.
218 @function -zf-map-next($map, $key) {
219
220 // Store the keys of the map as a list
221 $values: map-keys($map);
222
223 $i: 0;
224
225 // If the Key Exists, Get the index of the key within the map and add 1 to it for the next breakpoint in the map
226 @if (map-has-key($map, $key)) {
227 $i: index($values, $key) + 1;
228 }
229
230 // If the key doesn't exist, or it's the last key in the map, return null
231 @if ($i > length($map) or $i == 0) {
232 @return null;
233 }
234 // Otherwise, return the value
235 @else {
236 @return map-get($map, nth($values, $i));
237 }
238
239 }
240
241 /// Find the next number in a map.
242 /// @access private
243 ///
244 /// @param {Map} $map - Map to traverse.
245 /// @param {Mixed} $number - Number to use as a starting point.
246 ///
247 /// @returns {Mixed} The number following `$number`, if `$number` was found. If `$number` was not found, or `$number` was the biggest number in the map, returns `null`.
248 @function -zf-map-next-number($map, $number) {
249
250 $next-number: null;
251
252 @each $k, $v in $map {
253 @if type-of($v) == 'number' and $v > $number and ($next-number == null or $v < $next-number) {
254 $next-number: $v;
255 }
256 }
257
258 @return $next-number;
259 }
260
261 /// Return a list of our named breakpoints less than $key. Useful for dealing with
262 /// responsive gutters for the grid.
263 /// @access private
264 ///
265 /// @param {String} $key - Key to use as last breakpoint.
266 ///
267 /// @returns {Array} The list of breakpoints up to and. If $key is auto, returns breakpoints above the zero
268 @function -zf-breakpoints-less-than($key) {
269 $list: ();
270 $found-key: false;
271
272 @each $name in $-zf-breakpoints-keys {
273 @if ($name == $key) {
274 $found-key: true;
275 }
276 @if not $found-key {
277 $list: append($list, $name);
278 }
279 }
280 @return $list;
281 }
282
283 /// Return a list of our named breakpoints less than $key. Useful for dealing with
284 /// responsive gutters for the grid.
285 /// @access private
286 ///
287 /// @param {String} $breakpoint - a named or non-named breakpoint.
288 ///
289 /// @returns {Array} The list of breakpoints up to and. If $key is auto, returns breakpoints above the zero
290 @function -zf-closest-named-breakpoint($breakpoint) {
291 $last: $-zf-zero-breakpoint;
292 $found: false;
293
294 $value: unitless-calc($breakpoint, 1px);
295 @each $key, $val in $breakpoints {
296 @if not $found {
297 @if unitless-calc($val) > $value {
298 $found: true;
299 } @else {
300 $last: $key;
301 }
302 }
303 }
304
305 @return $last;
306 }
307
308 /// Get a value for a breakpoint from a responsive config map or single value.
309 /// - If the config is a single value, return it regardless of `$value`.
310 /// - If the config is a map and has the key `$value`, the exact breakpoint value is returned.
311 /// - If the config is a map and does *not* have the breakpoint, the value matching the next lowest breakpoint in the config map is returned.
312 /// @access private
313 ///
314 /// @param {Number|Map} $map - Responsive config map or single value.
315 /// @param {Keyword} $value - Breakpoint name to use.
316 ///
317 /// @return {Mixed} The corresponding breakpoint value.
318 @function -zf-get-bp-val($map, $value) {
319 // If the given map is a single value, return it
320 @if type-of($map) == 'number' {
321 @return $map;
322 }
323
324
325 // Check if the breakpoint name exists globally
326 @if not map-has-key($breakpoints, $value) {
327 @if type-of($value) == 'number' {
328 $value: -zf-closest-named-breakpoint($value);
329 } @else {
330 @return null;
331 }
332 }
333 // Check if the breakpoint name exists in the local config map
334 @else if map-has-key($map, $value) {
335 // If it does, just return the value
336 @return map-get($map, $value);
337 }
338 // Otherwise, find the next lowest breakpoint and return that value
339 @else {
340 $anchor: null;
341 $found: false;
342
343 @each $key, $val in $breakpoints {
344 @if not $found {
345 @if map-has-key($map, $key) {
346 $anchor: $key;
347 }
348 @if $key == $value {
349 $found: true;
350 }
351 }
352 }
353
354 @return map-get($map, $anchor);
355 }
356 }
357
358 /// Return the best breakpoint to use according to the calling context. It returns in order:
359 /// 1. the given `$value` argument if it is not null.
360 /// 2. the global breakpoint context `$-zf-size` if it is not null (like if called inside then `breakpoint()` mixin)
361 /// 3. the given `$default` argument.
362 /// @access private
363 ///
364 /// @param {Keyword} $value [null] - Breakpoint to use in priority if non-null.
365 /// @param {Keyword} $default [null] - Breakpoint to use by default if no other value can be used.
366 ///
367 /// @return {Keyword} The resolved breakpoint.
368 @function -zf-current-breakpoint($value: null, $default: null) {
369 @if ($value != null) {
370 @return $value;
371 }
372 @else if (variable-exists(-zf-size) and type-of($-zf-size) != 'number') and $-zf-size != null {
373 @return $-zf-size;
374 }
375 @else {
376 @return $default;
377 }
378 }
379
380 /// Return media query string from the given min and/or max limits.
381 /// If a limit is equal to `null` or `0`, it is ignored.
382 /// @access private
383 ///
384 /// @param {Number} $min [0] - Min media query limit.
385 /// @param {Number} $max [0] - Max media query limit.
386 /// @param {String} $min-name ['min-width'] - Name of the min media query limit.
387 /// @param {String} $delimiter ['max-width'] - Name of the max media query limit.
388 ///
389 /// @returns {String} Media Query string.
390 @function -zf-bp-join(
391 $min: 0,
392 $max: 0,
393 $min-name: 'min-width',
394 $max-name: 'max-width'
395 ) {
396 @return zf-str-join(
397 if($min and $min > 0, '(#{$min-name}: #{$min})', null),
398 if($max and $max > 0, '(#{$max-name}: #{$max})', null),
399 ' and ');
400 }
401
402 $small-up: '';
403 $small-only: '';
404
405 @if map-has-key($breakpoints, small) {
406 $small-up: screen;
407 $small-only: unquote('screen and #{breakpoint(small only)}');
408 }
409
410 $medium-up: '';
411 $medium-only: '';
412
413 @if map-has-key($breakpoints, medium) {
414 $medium-up: unquote('screen and #{breakpoint(medium)}');
415 $medium-only: unquote('screen and #{breakpoint(medium only)}');
416 }
417
418 $large-up: '';
419 $large-only: '';
420
421 @if map-has-key($breakpoints, large) {
422 $large-up: unquote('screen and #{breakpoint(large)}');
423 $large-only: unquote('screen and #{breakpoint(large only)}');
424 }
425
426 $xlarge-up: '';
427 $xlarge-only: '';
428
429 @if map-has-key($breakpoints, xlarge) {
430 $xlarge-up: unquote('screen and #{breakpoint(xlarge)}');
431 $xlarge-only: unquote('screen and #{breakpoint(xlarge only)}');
432 }
433
434 $xxlarge-up: '';
435
436 @if map-has-key($breakpoints, xxlarge) {
437 $xxlarge-up: unquote('screen and #{breakpoint(xxlarge)}');
438 }