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

Is it possible to lookup for variable that doesn't exist? #3444

Closed
theexplay opened this issue Oct 11, 2019 · 5 comments
Closed

Is it possible to lookup for variable that doesn't exist? #3444

theexplay opened this issue Oct 11, 2019 · 5 comments

Comments

@theexplay
Copy link

theexplay commented Oct 11, 2019

Hello!
Is it possible to lookup for variables that doesn't exist? I have a list of generated variables for every icon glyph. Some icons consist from two 2 glyphs (need for ::before + ::after for 2 colors icon). I want to pass base icon name to mixin and get correct output, but i get an error of looking @@icon2, and it doesn't matter if i use when or if. Also tried to use mixin guards, but no changes.
Some code:

// generated list of icons
@icon__glyph_about_20__1: '\E00C';
@icon__glyph_about_32__1: '\E00D';
@icon__glyph_about_32__2: '\E00E';

// common mixin for icons
.nbl-icon__use(@iconName) {
  @icon1: ~'icon__glyph_@{iconName}__1'; // always exists
  @icon2: ~'icon__glyph_@{iconName}__2'; // sometimes doesn't exist
  
  &::before {
    content: @@icon1;
  }
  
  & when not (@@icon2 = undefined) {
    &::after {
      content: @@icon2;
    }
  }
}

.test {
  .nbl-icon__use(about_20);
}

Example of error: Message: variable @icon__glyph_about_20__2 is undefined in file ...
ezgif-3-2bd786268161

P.S. Unfortunately i can't generate list of vars with null's.
P.S.S. sry for my eng, hope u understand me

@matthew-dean
Copy link
Member

matthew-dean commented Oct 11, 2019

@theexplay The short answer is that no, there's no current way to do this in exactly the way you mention. One workaround, though, would be to specify all the var names in a file loaded before the optional definitions, and set those values to something you would check later. In other words:

// default values
@icon__glyph_about_20__1: false;
@icon__glyph_about_20__2: false;
@icon__glyph_about_32__1: false;
@icon__glyph_about_32__2: false;

// generated values
@icon__glyph_about_20__1: '\E00C';
@icon__glyph_about_32__1: '\E00D';
@icon__glyph_about_32__2: '\E00E';

// common mixin for icons
.nbl-icon__use(@iconName) {
  @icon1: ~'icon__glyph_@{iconName}__1'; // always exists
  @icon2: ~'icon__glyph_@{iconName}__2'; // sometimes doesn't exist
  
  &::before {
    content: @@icon1;
  }
  
  & when not (@@icon2 = false) {
    &::after {
      content: @@icon2;
    }
  }
}

.test {
  .nbl-icon__use(about_20);
}

@matthew-dean
Copy link
Member

@theexplay

By the way, you can shorten the & when statement to:

  &::after when (@@icon2) {
    content: @@icon2;
  }

@theexplay
Copy link
Author

theexplay commented Oct 11, 2019

would be to specify all the var names in a file loaded before the optional definitions

I'll try to do as you suggested, but maybe this will also be a rather difficult option for me. Since I use data from the system design, I don’t really want to add another “middleware”.

Thank you so much for such a quick reply!

@seven-phases-max
Copy link
Member

Just in case (and unless I'm missing a corresponding change in v3.x) the example above is a bit misleading since Less has non-mainstream bool treating: anything but true is false
Thus for @icon__glyph_about_*__*: both false and '\E00C' are false.
A working example would need something like:

@foo: none; // or anything you like
... when not (@foo = none)

or

@foo: boo; // anything but string
... when (isstring(@foo))

etc.


ref: #1400

@matthew-dean
Copy link
Member

matthew-dean commented Oct 26, 2019

@seven-phases-max

Oh, shoot. That's a good catch. This is one of those cases where doing a lot of Less refactoring has tricked my brain towards what Less will do vs. what Less currently does do.

It should be:

  & when not (@@icon2 = false) {
    &::after {
      content: @@icon2;
    }
  }

You're right that Less currently expects something close to === equality, although not consistently.

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

No branches or pull requests

3 participants