5 // _ _ __ ___| |_ _ __| | ___ _ __ ___ ___ __| |_ __ _
6 // | | '_ \ / __| | | | |/ _` |/ _ \ | '_ ` _ \ / _ \/ _` | |/ _` |
7 // | | | | | (__| | |_| | (_| | __/ | | | | | | __/ (_| | | (_| |
8 // |_|_| |_|\___|_|\__,_|\__,_|\___| |_| |_| |_|\___|\__,_|_|\__,_|
10 // Simple, elegant and maintainable media queries in Sass
13 // http://include-media.com
15 // Authors: Eduardo Boucas (@eduardoboucas)
16 // Hugo Giraudel (@hugogiraudel)
18 // This project is licensed under the terms of the MIT license
22 /// include-media library public configuration
23 /// @author Eduardo Boucas
29 /// Creates a list of global breakpoints
31 /// @example scss - Creates a single breakpoint with the label `phone`
32 /// $breakpoints: ('phone': 320px);
42 /// Creates a list of static expressions or media types
44 /// @example scss - Creates a single media type (screen)
45 /// $media-expressions: ('screen': 'screen');
47 /// @example scss - Creates a static expression with logical disjunction (OR operator)
48 /// $media-expressions: (
49 /// 'retina2x': '(-webkit-min-device-pixel-ratio: 2), (min-resolution: 192dpi)'
56 'handheld': 'handheld',
57 'landscape': '(orientation: landscape)',
58 'portrait': '(orientation: portrait)',
59 'retina2x': '(-webkit-min-device-pixel-ratio: 2), (min-resolution: 192dpi)',
60 'retina3x': '(-webkit-min-device-pixel-ratio: 3), (min-resolution: 350dpi)'
65 /// Defines a number to be added or subtracted from each unit when declaring breakpoints with exclusive intervals
67 /// @example scss - Interval for pixels is defined as `1` by default
68 /// @include media('>128px') {}
71 /// @media (min-width: 129px) {}
73 /// @example scss - Interval for ems is defined as `0.01` by default
74 /// @include media('>20em') {}
77 /// @media (min-width: 20.01em) {}
79 /// @example scss - Interval for rems is defined as `0.1` by default, to be used with `font-size: 62.5%;`
80 /// @include media('>2.0rem') {}
83 /// @media (min-width: 2.1rem) {}
92 /// Defines whether support for media queries is available, useful for creating separate stylesheets
93 /// for browsers that don't support media queries.
95 /// @example scss - Disables support for media queries
96 /// $im-media-support: false;
97 /// @include media('>=tablet') {
108 $im-media-support: true !default;
111 /// Selects which breakpoint to emulate when support for media queries is disabled. Media queries that start at or
112 /// intercept the breakpoint will be displayed, any others will be ignored.
114 /// @example scss - This media query will show because it intercepts the static breakpoint
115 /// $im-media-support: false;
116 /// $im-no-media-breakpoint: 'desktop';
117 /// @include media('>=tablet') {
128 /// @example scss - This media query will NOT show because it does not intercept the desktop breakpoint
129 /// $im-media-support: false;
130 /// $im-no-media-breakpoint: 'tablet';
131 /// @include media('>=desktop') {
139 $im-no-media-breakpoint: 'desktop' !default;
142 /// Selects which media expressions are allowed in an expression for it to be used when media queries
143 /// are not supported.
145 /// @example scss - This media query will show because it intercepts the static breakpoint and contains only accepted media expressions
146 /// $im-media-support: false;
147 /// $im-no-media-breakpoint: 'desktop';
148 /// $im-no-media-expressions: ('screen');
149 /// @include media('>=tablet', 'screen') {
160 /// @example scss - This media query will NOT show because it intercepts the static breakpoint but contains a media expression that is not accepted
161 /// $im-media-support: false;
162 /// $im-no-media-breakpoint: 'desktop';
163 /// $im-no-media-expressions: ('screen');
164 /// @include media('>=tablet', 'retina2x') {
172 $im-no-media-expressions: ('screen', 'portrait', 'landscape') !default;
175 /// Cross-engine logging engine
176 /// @author Hugo Giraudel
182 /// Log a message either with `@error` if supported
183 /// else with `@warn`, using `feature-exists('at-error')`
184 /// to detect support.
186 /// @param {String} $message - Message to log
188 @function log($message) {
189 @if feature-exists('at-error') {
201 /// Wrapper mixin for the log function so it can be used with a more friendly
202 /// API than `@if log('..') {}` or `$_: log('..')`. Basically, use the function
203 /// within functions because it is not possible to include a mixin in a function
204 /// and use the mixin everywhere else because it's much more elegant.
206 /// @param {String} $message - Message to log
208 @mixin log($message) {
214 /// Function with no `@return` called next to `@warn` in Sass 3.3
215 /// to trigger a compiling error and stop the process.
220 /// Determines whether a list of conditions is intercepted by the static breakpoint.
222 /// @param {Arglist} $conditions - Media query conditions
224 /// @return {Boolean} - Returns true if the conditions are intercepted by the static breakpoint
226 @function im-intercepts-static-breakpoint($conditions...) {
227 $no-media-breakpoint-value: map-get($breakpoints, $im-no-media-breakpoint);
229 @if not $no-media-breakpoint-value {
230 @if log('`#{$im-no-media-breakpoint}` is not a valid breakpoint.') {}
233 @each $condition in $conditions {
234 @if not map-has-key($media-expressions, $condition) {
235 $operator: get-expression-operator($condition);
236 $prefix: get-expression-prefix($operator);
237 $value: get-expression-value($condition, $operator);
239 // scss-lint:disable SpaceAroundOperator
240 @if ($prefix == 'max' and $value <= $no-media-breakpoint-value) or
241 ($prefix == 'min' and $value > $no-media-breakpoint-value) {
244 } @else if not index($im-no-media-expressions, $condition) {
254 /// @author Hugo Giraudel
260 /// Get operator of an expression
262 /// @param {String} $expression - Expression to extract operator from
264 /// @return {String} - Any of `>=`, `>`, `<=`, `<`, `≥`, `≤`
266 @function get-expression-operator($expression) {
267 @each $operator in ('>=', '>', '<=', '<', '≥', '≤') {
268 @if str-index($expression, $operator) {
273 // It is not possible to include a mixin inside a function, so we have to
274 // rely on the `log(..)` function rather than the `log(..)` mixin. Because
275 // functions cannot be called anywhere in Sass, we need to hack the call in
276 // a dummy variable, such as `$_`. If anybody ever raise a scoping issue with
277 // Sass 3.3, change this line in `@if log(..) {}` instead.
278 $_: log('No operator found in `#{$expression}`.');
283 /// Get dimension of an expression, based on a found operator
285 /// @param {String} $expression - Expression to extract dimension from
286 /// @param {String} $operator - Operator from `$expression`
288 /// @return {String} - `width` or `height` (or potentially anything else)
290 @function get-expression-dimension($expression, $operator) {
291 $operator-index: str-index($expression, $operator);
292 $parsed-dimension: str-slice($expression, 0, $operator-index - 1);
295 @if str-length($parsed-dimension) > 0 {
296 $dimension: $parsed-dimension;
304 /// Get dimension prefix based on an operator
306 /// @param {String} $operator - Operator
308 /// @return {String} - `min` or `max`
310 @function get-expression-prefix($operator) {
311 @return if(index(('<', '<=', '≤'), $operator), 'max', 'min');
316 /// Get value of an expression, based on a found operator
318 /// @param {String} $expression - Expression to extract value from
319 /// @param {String} $operator - Operator from `$expression`
321 /// @return {Number} - A numeric value
323 @function get-expression-value($expression, $operator) {
324 $operator-index: str-index($expression, $operator);
325 $value: str-slice($expression, $operator-index + str-length($operator));
327 @if map-has-key($breakpoints, $value) {
328 $value: map-get($breakpoints, $value);
330 $value: to-number($value);
333 $interval: map-get($unit-intervals, unit($value));
336 // It is not possible to include a mixin inside a function, so we have to
337 // rely on the `log(..)` function rather than the `log(..)` mixin. Because
338 // functions cannot be called anywhere in Sass, we need to hack the call in
339 // a dummy variable, such as `$_`. If anybody ever raise a scoping issue with
340 // Sass 3.3, change this line in `@if log(..) {}` instead.
341 $_: log('Unknown unit `#{unit($value)}`.');
344 @if $operator == '>' {
345 $value: $value + $interval;
346 } @else if $operator == '<' {
347 $value: $value - $interval;
355 /// Parse an expression to return a valid media-query expression
357 /// @param {String} $expression - Expression to parse
359 /// @return {String} - Valid media query
361 @function parse-expression($expression) {
362 // If it is part of $media-expressions, it has no operator
363 // then there is no need to go any further, just return the value
364 @if map-has-key($media-expressions, $expression) {
365 @return map-get($media-expressions, $expression);
368 $operator: get-expression-operator($expression);
369 $dimension: get-expression-dimension($expression, $operator);
370 $prefix: get-expression-prefix($operator);
371 $value: get-expression-value($expression, $operator);
373 @return '(#{$prefix}-#{$dimension}: #{$value})';
377 /// Slice `$list` between `$start` and `$end` indexes
381 /// @param {List} $list - List to slice
382 /// @param {Number} $start [1] - Start index
383 /// @param {Number} $end [length($list)] - End index
385 /// @return {List} Sliced list
387 @function slice($list, $start: 1, $end: length($list)) {
388 @if length($list) < 1 or $start > $end {
394 @for $i from $start through $end {
395 $result: append($result, nth($list, $i));
402 /// String to number converter
403 /// @author Hugo Giraudel
409 /// Casts a string into a number
411 /// @param {String | Number} $value - Value to be parsed
415 @function to-number($value) {
416 @if type-of($value) == 'number' {
418 } @else if type-of($value) != 'string' {
419 $_: log('Value for `to-number` should be a number or a string.');
424 $minus: str-slice($value, 1, 1) == '-';
425 $numbers: ('0': 0, '1': 1, '2': 2, '3': 3, '4': 4, '5': 5, '6': 6, '7': 7, '8': 8, '9': 9);
427 @for $i from if($minus, 2, 1) through str-length($value) {
428 $character: str-slice($value, $i, $i);
430 @if not (index(map-keys($numbers), $character) or $character == '.') {
431 @return to-length(if($minus, -$result, $result), str-slice($value, $i))
434 @if $character == '.' {
436 } @else if $digits == 0 {
437 $result: $result * 10 + map-get($numbers, $character);
439 $digits: $digits * 10;
440 $result: $result + map-get($numbers, $character) / $digits;
444 @return if($minus, -$result, $result);
449 /// Add `$unit` to `$value`
451 /// @param {Number} $value - Value to add unit to
452 /// @param {String} $unit - String representation of the unit
454 /// @return {Number} - `$value` expressed in `$unit`
456 @function to-length($value, $unit) {
457 $units: ('px': 1px, 'cm': 1cm, 'mm': 1mm, '%': 1%, 'ch': 1ch, 'pc': 1pc, 'in': 1in, 'em': 1em, 'rem': 1rem, 'pt': 1pt, 'ex': 1ex, 'vw': 1vw, 'vh': 1vh, 'vmin': 1vmin, 'vmax': 1vmax);
459 @if not index(map-keys($units), $unit) {
460 $_: log('Invalid unit `#{$unit}`.');
463 @return $value * map-get($units, $unit);
467 /// This mixin aims at redefining the configuration just for the scope of
468 /// the call. It is helpful when having a component needing an extended
469 /// configuration such as custom breakpoints (referred to as tweakpoints)
472 /// @author Hugo Giraudel
474 /// @param {Map} $tweakpoints [()] - Map of tweakpoints to be merged with `$breakpoints`
475 /// @param {Map} $tweak-media-expressions [()] - Map of tweaked media expressions to be merged with `$media-expression`
477 /// @example scss - Extend the global breakpoints with a tweakpoint
478 /// @include media-context(('custom': 678px)) {
480 /// @include media('>phone', '<=custom') {
486 /// @example scss - Extend the global media expressions with a custom one
487 /// @include media-context($tweak-media-expressions: ('all': 'all')) {
489 /// @include media('all', '>phone') {
495 /// @example scss - Extend both configuration maps
496 /// @include media-context(('custom': 678px), ('all': 'all')) {
498 /// @include media('all', '>phone', '<=custom') {
504 @mixin media-context($tweakpoints: (), $tweak-media-expressions: ()) {
505 // Save global configuration
506 $global-breakpoints: $breakpoints;
507 $global-media-expressions: $media-expressions;
509 // Update global configuration
510 $breakpoints: map-merge($breakpoints, $tweakpoints) !global;
511 $media-expressions: map-merge($media-expressions, $tweak-media-expressions) !global;
515 // Restore global configuration
516 $breakpoints: $global-breakpoints !global;
517 $media-expressions: $global-media-expressions !global;
521 /// include-media public exposed API
522 /// @author Eduardo Boucas
528 /// Generates a media query based on a list of conditions
530 /// @param {Arglist} $conditions - Media query conditions
532 /// @example scss - With a single set breakpoint
533 /// @include media('>phone') { }
535 /// @example scss - With two set breakpoints
536 /// @include media('>phone', '<=tablet') { }
538 /// @example scss - With custom values
539 /// @include media('>=358px', '<850px') { }
541 /// @example scss - With set breakpoints with custom values
542 /// @include media('>desktop', '<=1350px') { }
544 /// @example scss - With a static expression
545 /// @include media('retina2x') { }
547 /// @example scss - Mixing everything
548 /// @include media('>=350px', '<tablet', 'retina3x') { }
550 @mixin media($conditions...) {
551 // scss-lint:disable SpaceAroundOperator
552 @if ($im-media-support and length($conditions) == 0) or
553 (not $im-media-support and im-intercepts-static-breakpoint($conditions...)) {
555 } @else if ($im-media-support and length($conditions) > 0) {
556 @media #{unquote(parse-expression(nth($conditions, 1)))} {
558 @include media(slice($conditions, 2)...) {