-
Notifications
You must be signed in to change notification settings - Fork 189
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Loop through breakpoints and generate media queries. #109
Comments
Just re-posting to gain an answer. Would something like this be better placed on stack overflow? |
Sorry I missed this. Just to make sure I understand what you need, imagining that you have the following breakpoints map: $breakpoints:(
'small': 400px,
'large': 1200px
); You want the following classes generated:
Is this correct? |
If that's the case, see if this helps: $breakpoints:(
'small': 400px,
'large': 1200px
);
$num-breakpoints: length($breakpoints);
@mixin hidden($type, $breakpoint) {
.hidden--#{$type}-#{$breakpoint} {
display: none;
}
}
@for $i from 1 through $num-breakpoints {
$breakpoint: nth($breakpoints, $i);
$breakpoint-name: nth($breakpoint, 1);
// For
@if $i == $num-breakpoints {
@include media('>=#{$breakpoint-name}') {
@include hidden('for', $breakpoint-name);
}
} @else {
$next-breakpoint: nth($breakpoints, $i + 1);
$next-breakpoint-name: nth($next-breakpoint, 1);
@include media('>=#{$breakpoint-name}', '<#{$next-breakpoint-name}') {
@include hidden('for', $breakpoint-name);
}
}
// From
@include media('>=#{$breakpoint-name}') {
@include hidden('from', $breakpoint-name);
}
// To
@include media('<#{$breakpoint-name}') {
@include hidden('to', $breakpoint-name);
}
} Note that this will only work if |
Thanks @eduardoboucas, this looks spot on. Will have a look at this implementation and get back to you. 👍 |
Hi @eduardoboucas. This works great, this is one unfortunate circumstance that comes as a result of using the above. The hidden for, and from for the last breakpoint are exactly the same, resulting in redundant code. Is there anyway to miss this in the loop so that it just outputs for, and upto? I'll almost certainly use the same technique for other objects in the project. So removing the redundancy is kind of important. <script src="http://cdn.sassmeister.com/js/embed.js" async></script> |
@aaronstezycki Yes, just wrap the From section with an if statement: // From
@if $i != $num-breakpoints {
@include media('>=#{$breakpoint-name}') {
@include hidden('from', $breakpoint-name);
}
} |
👍 Perfect. |
👍 closing. |
I was working on this exact issue earlier today. I was going for something more along the lines of the bootstrap responsive classes ( It's funny how nearly identical @eduardoboucas' solution is to mine. Here it is in case anyone is interested:
|
@puglyfe looks great, thanks for sharing! |
@aaronstezycki @eduardoboucas, any chance of making this an official include-media plugin and posting a link on the repo 'Plugin' section? |
Hi Guys, I'm trying to simplify a bit code I have... and largely this works very well but I'm a bit new to Sass so I'm probably just missing something here.
I have a set of .hidden rules for, from and up to certain breakpoints. The --from and --upto parts work, but getting it to work between breakpoints is slightly more difficult.
To get this to work, I kinda need to populate the $upto-breakpoint var with the next breakpoint so It has minimum and maximum values. Ive tried a couple of different things but nothing has worked. Any help appreciated!
The text was updated successfully, but these errors were encountered: