Skip to content

Commit

Permalink
Added mediaOnly mixin (#28)
Browse files Browse the repository at this point in the history
* Added mediaOnly mixin

* Fixed CI errors

* Fixed CI errors.

* Fixed little bug.

* Added case for default only

* Enhancement to not allow greater-than queries for breakpoints that don't exist.

* Removed unit px
  • Loading branch information
luca-rath authored and alexander-schranz committed Aug 30, 2018
1 parent 47cdde0 commit 2876ae6
Showing 1 changed file with 58 additions and 2 deletions.
60 changes: 58 additions & 2 deletions scss/tools/_media.scss
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ $media-breakpoints: $breakpoints !default;
}
}

// Media only automatically create a "only screen and(min-width: $minvalue) and (max-width: $maxvalue)" media query.
@mixin mediaOnly($minvalue, $maxvalue) {
@media only screen and (min-width: $minvalue) and (max-width: $maxvalue) {
@content;
}
}

// Media mixin for easier readable media queries.
//
// Usage:
Expand All @@ -30,6 +37,10 @@ $media-breakpoints: $breakpoints !default;
// @include media('<tablet') {
// color: red;
// }
//
// @include media('laptop') {
// color: blue;
// }
// }
//
// Output:
Expand All @@ -39,12 +50,24 @@ $media-breakpoints: $breakpoints !default;
// color: red;
// }
// }
//
// @media only screen and (min-width: 992px) and (max-width: 1199px) {
// .example {
// color: blue;
// }
// }
@mixin media($condition) {
$function: str-slice($condition, 1, 1);
$currentBreakpoint: str-slice($condition, 2);

@if $function == '>' {
@include mediaMin(map-get($media-breakpoints, $currentBreakpoint) + 1px) {
$value: map-get($media-breakpoints, $currentBreakpoint);

@if not $value {
@error 'Following condition is not allowed: #{$condition}';
}

@include mediaMin($value + 1) {
@content;
}
} @else if $function == '<' {
Expand All @@ -64,7 +87,40 @@ $media-breakpoints: $breakpoints !default;
@content;
}
} @else {
@error 'Following condition is not allowed: #{$condition}';
@if $condition == 'default' {
$maxBreakpoint: 0;

@each $name, $breakpoint in $media-breakpoints {
@if $breakpoint > $maxBreakpoint {
$maxBreakpoint: $breakpoint;
}
}

@if $maxBreakpoint > 0 {
$maxBreakpoint: $maxBreakpoint + 1;
}

@include mediaMin($maxBreakpoint) {
@content;
}
} @else {
$maxvalue: map-get($media-breakpoints, $condition);
$minvalue: 0;

@if not $maxvalue {
@error 'Following condition is not allowed: #{$condition}';
}

@each $name, $breakpoint in $media-breakpoints {
@if $breakpoint > $minvalue and $breakpoint < $maxvalue {
$minvalue: $breakpoint + 1;
}
}

@include mediaOnly($minvalue, $maxvalue) {
@content;
}
}
}
}

Expand Down

0 comments on commit 2876ae6

Please sign in to comment.