Madara's Ghost
Madara's Ghost

Reputation: 175088

How can you use multiple variable breakpoints for media queries in Stylus?

Current Situation

I have the following Stylus code

$mobile = '(max-width 767px)';
$tablet = '(min-width 768px) and (max-width: 1365px)';
$desktop = '(min-width: 1366px)';

  .sidebar
    width 300px;

    @media $tablet
      display none;

This code works well - the sidebar disappears for the listed screen-sizes.

The Problem

Now, I would like to also make it disappear for the $mobile breakpoint.

Ideally, what I would want is something like this:

$mobile = '(max-width 767px)';
$tablet = '(min-width 768px) and (max-width: 1365px)';
$desktop = '(min-width: 1366px)';

  .sidebar
    width 300px;

    @media $tablet, $mobile
      display none;

And it would output (or something to the effect of)

.sidebar { width: 300px; }
@media (min-width: 768px) and (max-width: 1365px), (max-width: 767px) {
  .sidebar { display: none; }

What I've tried

What I can do but don't want to do

TL;DR

How can I make use of multiple variables for multiple breakpoints in Stylus?

Sass has this feature with @media #{$mobile}, #{$tablet}. I'm looking for something similar for Stylus.

Upvotes: 2

Views: 4749

Answers (3)

Liron Navon
Liron Navon

Reputation: 1097

you will have to lose the parenthesis since stylus forces media queries into parenthesis for inline concatenation (stylus is no longer under active development, so this will probably not be solved) for this to work inline, or you can just do regular concatenation into a new variable and then use that. Here is a playground

$mobile = 'max-width: 767px'
$laptop = 'min-width: 1366px'

@media ({$mobile}) , ({$laptop})
  body
    color: green;

Upvotes: 1

Temani Afif
Temani Afif

Reputation: 274024

Probably not the best solution but you can consider the use of for to achieve this by writing a code like below:

$mobile = '(max-width 767px)';
$tablet = '(min-width 768px) and (max-width: 1365px)';
$desktop ='(min-width: 1366px)'; 

  .sidebar
    width 300px;

    for m in $mobile $tablet
      @media m
        display none;  

And you will have this output:

.sidebar {
    width: 300px;
}

@media (max-width: 767px) {
    .sidebar {
        display: none;
    }
}

@media (min-width: 768px) and (max-width: 1365px) {
    .sidebar {
        display: none;
    }
}

The stylus code is somehow like you want but the output will not be a single media query.

UPDATE

Here is another hacky way to avoid the media query duplication but you will have to duplicate the selector:

$mobile = '(max-width: 767px)';
$tablet = '(min-width: 768px) and (max-width: 1365px)';
$desktop ='(min-width: 1366px)';

  .sidebar
    width 300px;

  unquote("@media " + join(',',$mobile $tablet) + "{")
  .sidebar
    display none;   
  unquote("}")

The above will produce this:

.sidebar {
    width: 300px;
}

@media (max-width: 767px),(min-width: 768px) and (max-width: 1365px) {
    .sidebar {
        display: none;
    }
}

Upvotes: 1

haim770
haim770

Reputation: 49133

You can use the join() function to generate the required literal beforehand:

$media = join(',', $tablet $mobile);

Then use it:

@media $media
    display: none;

Upvotes: 0

Related Questions