Giter VIP home page Giter VIP logo

Comments (12)

aaronstezycki avatar aaronstezycki commented on May 18, 2024

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

from include-media.

eduardoboucas avatar eduardoboucas commented on May 18, 2024

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.

eduardoboucas avatar eduardoboucas commented on May 18, 2024

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

from include-media.

aaronstezycki avatar aaronstezycki commented on May 18, 2024

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

from include-media.

aaronstezycki avatar aaronstezycki commented on May 18, 2024

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.

eduardoboucas avatar eduardoboucas commented on May 18, 2024

@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.

aaronstezycki avatar aaronstezycki commented on May 18, 2024

👍 Perfect.

from include-media.

eduardoboucas avatar eduardoboucas commented on May 18, 2024

👍 closing.

from include-media.

puglyfe avatar puglyfe commented on May 18, 2024

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

from include-media.

eduardoboucas avatar eduardoboucas commented on May 18, 2024

@puglyfe looks great, thanks for sharing!

from include-media.

patrickcate avatar patrickcate commented on May 18, 2024

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

from include-media.

eduardoboucas avatar eduardoboucas commented on May 18, 2024

@patrickcate Here it is: https://github.com/eduardoboucas/include-media-hidden-classes

from include-media.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.