sasssass-maps

How can an empty Map be created in Sass?


The following code will create an empty map in the variable $test, but is there a proper way to accomplish the this? This seems too hackish to be the best way to get an empty map.

$test: map-remove((default:null), 'default');

@if type-of($test) != map {
    @warn "This is not a map: #{inspect($test)}";
}
@else {
    @warn "This is a map: #{inspect($test)}";
}

WARNING: This is a map: ()
         on line 7 of app/styles/functions/map.scss

Solution

  • There is no better native way to do that, but you could make it shorter.

    $map: map-remove((x:x), x);
    
    .foo {
      output: type-of($map); // map
    }
    

    Another solution would be to use a custom function.

    @function empty-map($x: x) {
      @return map-remove(($x:$x), $x);
    }
    
    $map2: empty-map();
    $map3: empty-map();
    
    .foo {
      output: type-of($map2); // map
      output: type-of($map3); // map
    }
    

    SassMeister demo