Skip to content
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

Closed
aaronstezycki opened this issue Dec 14, 2015 · 12 comments
Closed

Loop through breakpoints and generate media queries. #109

aaronstezycki opened this issue Dec 14, 2015 · 12 comments
Labels

Comments

@aaronstezycki
Copy link

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.

.hidden{
    @include hidden;
}

@each $breakpoint in $all-breakpoints {
    $breakpoint-name: nth($breakpoint, 1);
    $upto-breakpoint: 'Unsure what Sass is needed here';


    @include media('>=#{$breakpoint-name}', '<#{$upto-breakpoint}'){
        .hidden--for-#{$breakpoint-name}{
            @include hidden();
        }
    }

    @include media('>=#{$breakpoint-name}'){
        .hidden--from-#{$breakpoint-name}{
            @include hidden();
        }
    }

    @include media('<#{$breakpoint-name}'){
        .hidden--upto-#{$breakpoint-name}{
            @include hidden();
        }
    }
}

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!

@aaronstezycki
Copy link
Author

Just re-posting to gain an answer. Would something like this be better placed on stack overflow?

@eduardoboucas
Copy link
Owner

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?

@eduardoboucas
Copy link
Owner

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.

Open in SassMeister

@aaronstezycki
Copy link
Author

Thanks @eduardoboucas, this looks spot on. Will have a look at this implementation and get back to you. 👍

@aaronstezycki
Copy link
Author

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>

@eduardoboucas
Copy link
Owner

@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);
    }    
  }

@aaronstezycki
Copy link
Author

👍 Perfect.

@eduardoboucas
Copy link
Owner

👍 closing.

@puglyfe
Copy link

puglyfe commented Oct 7, 2016

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;
        }
      }
    }
  }
}

view on codepen

@eduardoboucas
Copy link
Owner

@puglyfe looks great, thanks for sharing!

@patrickcate
Copy link

@aaronstezycki @eduardoboucas, any chance of making this an official include-media plugin and posting a link on the repo 'Plugin' section?

@eduardoboucas
Copy link
Owner

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants