Comments (12)
Just re-posting to gain an answer. Would something like this be better placed on stack overflow?
from include-media.
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:
.hidden--for-small
: 400px to 1199px.hidden--from-small
: 400px upwards.hidden--upto-small
: anything below and including 399px.hidden--for-large
: 1200px upwards?.hidden--from-large
: 1200px upwards?.hidden--upto-large
: below and including 1199px
Is this correct?
from include-media.
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 $breakpoints
is ordered by breakpoint value, ascending.
from include-media.
Thanks @eduardoboucas, this looks spot on. Will have a look at this implementation and get back to you. 👍
from include-media.
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.
Play with this gist on SassMeister.
<script src="http://cdn.sassmeister.com/js/embed.js" async></script>from include-media.
@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);
}
}
from include-media.
👍 Perfect.
from include-media.
👍 closing.
from include-media.
I was working on this exact issue earlier today. I was going for something more along the lines of the bootstrap responsive classes (hidden-sm
, visible-sm-block
, etc).
It's funny how nearly identical @eduardoboucas' solution is to mine. Here it is in case anyone is interested:
$breakpoints: (
'xs': 480px,
'sm': 768px,
'md': 992px,
'lg': 1200px,
'xl': 1600px
);
$breakpoints-count: length($breakpoints);
@for $i from 1 through $breakpoints-count {
$bp: nth(map-keys($breakpoints), $i);
$next-bp: if($i < $breakpoints-count, nth(map-keys($breakpoints), $i + 1), null);
$display-modes: inline inline-block block; // TODO: support other types?
.hide-#{$bp} {
@if $i == 1 {
@include media('<#{$next-bp}') {
display: none;
}
} @else if $i == $breakpoints-count {
@include media('>=#{$bp}') {
display: none;
}
} @else {
@include media('>=#{$bp}', '<#{$next-bp}') {
display: none;
}
}
}
@each $display-mode in $display-modes {
.show-#{$bp}-#{$display-mode} {
display: none;
@if $i == 1 {
@include media('<#{$next-bp}') {
display: $display-mode;
}
} @else if $i == $breakpoints-count {
@include media('>=#{$bp}') {
display: $display-mode;
}
} @else {
@include media('>=#{$bp}', '<#{$next-bp}') {
display: $display-mode;
}
}
}
}
}
from include-media.
@puglyfe looks great, thanks for sharing!
from include-media.
@aaronstezycki @eduardoboucas, any chance of making this an official include-media plugin and posting a link on the repo 'Plugin' section?
from include-media.
@patrickcate Here it is: https://github.com/eduardoboucas/include-media-hidden-classes
from include-media.
Related Issues (20)
- [Documentation] Website down? https://include-media.com/ HOT 5
- Compiling error in IDE's with media(slice($conditions, 2)...) - possible fix HOT 4
- Migrate from @import to @use HOT 3
- Undefined mixin media HOT 1
- cannot import via `@use` HOT 13
- I made a library using this project, can I introduce it as a plugin? HOT 3
- include-media page on npm has a dodgy homepage field and link HOT 1
- How to use AND ? HOT 2
- Remove broken link HOT 1
- Define a default media-expression HOT 1
- Writing style HOT 2
- styled-components HOT 1
- DEPRECATION WARNING: Using / for division is deprecated and will be removed in Dart Sass 2.0.0. HOT 7
- Help needed- setup HOT 7
- Release 2.0 on npm HOT 19
- Error on compiling HOT 8
- Container Queries? HOT 10
- Compability with current sass version? HOT 3
- `print` support HOT 1
- Using media with variables HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from include-media.