Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Map property lookup by variable #3362

Closed
dmgawel opened this issue Feb 7, 2019 · 1 comment
Closed

Map property lookup by variable #3362

dmgawel opened this issue Feb 7, 2019 · 1 comment

Comments

@dmgawel
Copy link

dmgawel commented Feb 7, 2019

Thanks to the lookup syntax I can access ruleset/mixin property by its name (@sizes[mobile]). Additionally I can access ruleset/mixin variable using @@variable syntax. What I'd like to do is lookup a property with variable interpolation, like that:

@sizes: {
  mobile: 320px;
  tablet: 768px;
  desktop: 1024px;
}

@lookup-size: mobile;

.test {
  width: @sizes[@{lookup-size}];
}

Which should result in:

.test {
  width: 320px;
}

It would be very useful for iterating over properties. Here is my complete real-life use case:

// Defining breakpoints
@breakpoints: {
  xs: 0;
  sm: 36rem;
  md: 48rem;
  lg: 75rem;
  xl: 85rem;
};

// Generating helper classes based on breakpoints
each(@breakpoints, {
  .wrap-@{key} {
    max-width: @value;
  }
});

// Mixin
.respond(@size; @rules) {
  @media screen and (min-width: @breakpoints[@{size}]) {
    @rules();
  }
}

// Using mixin
.test {
  .respond(xl, {
    color: blue;
  });
}
@matthew-dean
Copy link
Member

@dmgawel You can do that with variable property syntax e.g.

@sizes: {
  mobile: 320px;
  tablet: 768px;
  desktop: 1024px;
}

@lookup-size: mobile;

.test {
  width: @sizes[$@lookup-size];
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants