evaluating the type of something returns false, but it's accepted as true

I am striving to enhance my SASS media query mixin by introducing different variations of input to it.

@media(medium)

@media([small, to-large])

@media([[small, to-large], [small, landscape]])


However, I am encountering an issue with an if statement. I am perplexed as to why it is passing even though it is false. What could I be doing wrong? Any ideas?

@if (type-of($queries) == "list" and type-of(nth($queries, 1) == "list")) {
  // do something
} @else {
  // do something
}

Here is the query that passes even though it's false

.block {
  @include media([small, to-large]) {
    background-color: blue;
  }
}

Check out the full example here:

@mixin media($queries, $only-screen: false) {
  $media-query: "";

  $media-query: if($only-screen, "only screen and ", "");

  @debug type-of($queries) == "list" and type-of(nth($queries, 1)) == 'list';
  @debug type-of(nth($queries, 1));

  @if (type-of($queries) == "list" and type-of(nth($queries, 1) == 'list')) {
    @debug 'hey';
    
    @for $p from 1 through length($queries) {
      $array: nth($queries, $p);

      $media-query: $media-query + composeMediaQuery($array);

      @if ($p < length($queries)) {
        $media-query: $media-query + ", ";
      }
    }
  } @else {
    $media-query: composeMediaQuery($queries);
  }

  @if ($media-query) {
    @media #{$media-query} {
      @content;
    }
  } @else {
    @warn "There is no query please check your input to the @include media()";
  }
}

@function composeMediaQuery($array) {
  $media-query: "";

  @for $i from 1 through length($array) {
    $query: inspect(map-get($media-queries, nth($array, $i)));

    @if ($query) {
      @if ($i == 1) {
        $media-query: $media-query + $query;
      } @else {
        $media-query: $media-query + " and " + $query;
      }
    } @else {
      @warn "Unfortunately, no value could be retrieved from `#{$media-queries}`. "
            + "Please make sure it is defined in `$media-queries` map.";
    }
  }

  @return $media-query;
}

$media-queries: (
  to-small: (
    max-width: 766px
  ),
  small: (
    min-width: 767px
  ),
  to-medium: (
    max-width: 991px
  ),
  medium: (
    min-width: 992px
  ),
  to-large: (
    max-width: 1199px
  ),
  large: (
    min-width: 1200px
  ),
  to-x-large: (
    max-width: 1599px
  ),
  x-large: (
    min-width: 1600px
  ),
  grid-two: (
    min-width: 767px
  ),
  grid-three: (
    min-width: 1010px
  ),
  grid-four: (
    min-width: 1340px
  ),
  docked-cart: (
    min-width: 1700px
  ),
  min-width-docking: (
    min-width: 1680px
  ),
  target-ie: (
    -ms-high-contrast: none
  ),
  landscape: (
    orientation: landscape
  ),
  portrait: (
    orientation: portrait
  )
);

.block {
  @include media([[small, to-large], [landscape, to-medium]]) {
    background-color: blue;
  }
  
  @include media([small, to-large]) {
    background-color: blue;
  }
}

Feel free to visit the CodePen link for checking the compiled output and console log.

Answer №1

It's interesting that I needed to assign a variable to the condition before utilizing it in the @if.

$ifNestedList: type-of($queries) == "list" and type-of(nth($queries, 1)) == 'list';

  @if ($ifNestedList) {
    @for $p from 1 through length($queries) {
      $array: nth($queries, $p);

      $media-query: $media-query + composeMediaQuery($array);

      @if ($p < length($queries)) {
        $media-query: $media-query + ", ";
      }
    }
  } @else {
    $media-query: composeMediaQuery($queries);
  }

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How to create a custom CSS style to make Bootstrap 4 modal slide in from the bottom

Back in the days of Bootstrap 3, this was the trick: .modal.fade .modal-dialog { -webkit-transform: translate3d(0, -25%, 0); transform: translate3d(0, -25%, 0); } Another approach that could have been used: .modal.fade .modal-dialog { transfor ...

What is the best way to ensure an input field and a button are aligned perfectly within the same div tag and of equal height?

During a recent HTML-CSS project, I encountered an issue where I struggled to ensure that two elements within a div tag were the same height. The elements in question were an input field and a button with an icon. Here is a snippet of the relevant HTML cod ...

Tips for displaying just the image name without the entire image file path

When I try to upload an image, the path displayed is "C:\fakepath\promotion1.JPG". This fake path is causing the image not to upload to my project media folder. How can I solve this issue? Instead of the complete path, I only want to capture the ...

What is the process for enabling CSS modules in React after performing the eject action?

I'm currently working on a React project and I'm looking to enable CSS modules. I recently ran the eject command but I am unsure of where to locate the file in order to set modules to "true" as I can't seem to find the "webpack.config.dev.js ...

Displaying only the radio button without any additional elements, the Ionic 2 radio button (<ion-radio>) offers a

Looking for some guidance on implementing a list with radio buttons using the ion-list and ion-radio directives. The code provided should be clear, but please feel free to ask for more details if needed. I'm still new to Ionic and may be making some ...

Can you explain the distinction between "javascript:;" and "javascript:" when used in the href attribute?

Can you explain the distinction between using "javascript:;" and just "javascript:" within an anchor tag's href attribute? ...

Bootstrap side navigation bars are an essential tool for creating

Is there a way to change the hamburger icon in Bootstrap's navbar-toggle to an X button? I want the side nav to slide to the left when clicked, and also collapse when clicking inside the red rectangle. Thank you. https://i.sstatic.net/nQMGs.png Code ...

Having trouble connecting to the webserver? Make sure the web server is up and running, and that incoming HTTP requests are not being blocked by a firewall

While working on my Visual Studio 2013 Asp.Net web code using the Local IIS Web server Version 7 (Windows 7 x64) and Framework 4.0, I encountered an error message stating: "Unable to start debugging on the web server. Unable to connect to the webserver. V ...

Struggling to fix errors within a nested div element?

I'm currently utilizing AngularJS to perform basic form validation. Below is the current code snippet: <form name="myForm" id="form_id" method="post" novalidate> <div class="form-group"> <label for="myField_input" class="c ...

The Django code snippet "if request.method == 'POST':" encounters an error

Greetings, I'm encountering a peculiar issue in my recent Django project. Currently, I am experimenting with the NameForm example provided in the Django Documentation on Working with Forms. On the surface, it appears to be a straightforward exercise. ...

Validation failure in the CSS document - Syntax error

I have searched high and low for a solution to my CSS 3.0 validation issue, but none seem to fit the bill. I am struggling with parse errors in the beginning and despite checking for invisible characters, I can't seem to pinpoint the problem. What co ...

Setting up Stylelint in a Next.js project: A step-by-step guide

I'm looking to incorporate Stylelint into my Next.js app. Can I modify the next.config.js file to include the stylelint-webpack-plugin, in order to receive style errors during compilation? // next.config.js module.exports = { reactStrictMode: true, ...

How can you retrieve the current value of a text field in Selenium using Python when there is no attribute available to store it?

I am facing an issue where the input tag in HTML does not have a specific attribute to show the current text in the input field. For example, like the "value" attribute in Google search that stores the value entered by the user. So, my question is - what a ...

What is the best way to align this image in the center?

I've been struggling with this problem for a while now and can't seem to find a solution. I need to center align this image within the form, but everything I've tried so far has been unsuccessful. It may seem like I'm providing too much ...

Constructing Jquery object with added event listener

Take a look at the code I've provided below: <body> <canvas id="canvas" height="300" width="300" style="border:1px solid" /> </body> <script> function maze(canvas){ this.ctx = canvas[0].getContext("2d"); ...

"Maximizing Bootstrap Functionality with Angular 2: Step-by

I'm experiencing an issue with Angular2, Bootstrap 3.3.7, and Bootstrap Plugins. I have imported all the necessary stylesheets and JavaScript files in the 'index.html' file. Below is the snippet from the index.html: <!-- Bootstrap & ...

Using both italic and regular text in an HTML (or ASP.NET) textbox can add emphasis and enhance readability for

Is there a way to achieve this using JavaScript? For instance, if the initial text is 'Axxxxxx', I want it to be displayed in a textbox with normal style for the first letter and italic style for the rest like 'A*xxxxxx*' If the initia ...

Puppeteer challenge with breaking images

When generating a PDF from HTML, I am facing an issue where the signature image breaks on another page. Is there a way to ensure that if content or images break, they will move to another PDF page? Puppeteer version - 3.3.0 Node version - 12.16.1 Check t ...

When attempting to retrieve the percentage value, Jquery Calculation is producing a NaN error

Can anyone assist with a jQuery issue I am facing? I'm trying to calculate the final price after applying a discount percentage entered in the second field. However, when using the keyUp event, it keeps returning NaN. I've tried using parseInt in ...

Unable to execute project using CodeIgniter

I'm facing an issue with my current project. Unfortunately, I can't share the website due to confidentiality reasons. I apologize for any inconvenience this may cause. The project is built on the CodeIgniter framework, and it runs smoothly on lo ...