“Not a map for `map-get`” error after adding new elements to a mapping

前端 未结 1 562
Happy的楠姐
Happy的楠姐 2021-01-20 11:57

How can I merge a mapping into an existing mapping in sass?

I am currently using sass maps in a _config partial to define my breakpoints, for example:

1条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-20 12:26

    The function you're looking for is map-merge(), not join(). The join() function is for joining lists together and is causing your mapping to get converted to a list of lists.

    $fix-mqs: false;
    $breakpoints: (
        small: 35rem,
        medium: 55rem,
        large: 75rem,
        xlarge: 90rem
    );
    
    // map-merge here, not join
    $breakpoints: map-merge($breakpoints, (element-breakpoint-1: 100rem, element-breakpoint-2: 110rem));
    
    @mixin breakpoint($width) {
        @if $fix-mqs {
            @if $fix-mqs >= map-get($breakpoints, $width) {
                @content;
            }
        }
        @else {
            @media screen and (min-width: map-get($breakpoints, $width)) {
                @content;
            }
        }
    }
    
    @include breakpoint(element-breakpoint-2) {
      .foo {
        color: red;
      }
    }
    

    Output:

    @media screen and (min-width: 110rem) {
      .foo {
        color: red;
      }
    }
    

    0 讨论(0)
提交回复
热议问题