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

Using a variable in a mixin call #2885

Closed
vrbvillor opened this issue Apr 28, 2016 · 6 comments
Closed

Using a variable in a mixin call #2885

vrbvillor opened this issue Apr 28, 2016 · 6 comments

Comments

@vrbvillor
Copy link

Such as

.lt(){
    left:0;
    top:0;
}

when I use a variable

@n : lt;
.@{n}();

The second line of the second passage of code, I want to call the .lt(). How to realize it? THANKS.

@seven-phases-max
Copy link
Member

seven-phases-max commented Apr 28, 2016

Currently it's impossible to call a mixin indirectly (i.e. by name as variable value).
However depending on your specific use-case there're several nearly equal methods to achieve indirect calls.


(The feature itself was proposed among other variations in #965 (comment). But since #965 itself is closed because its initial idea (DRs) is implement I suppose this one needs a dedicated ticket. So I'm not closing this one yet (but eventually it's most likely to be replaced by one with more detailed info).

@matthew-dean
Copy link
Member

matthew-dean commented May 4, 2016

This would be a short-term fix.

@lt: {
    left:0;
    top:0;
};
@n : lt;
@x : @@n;
@x();

And if you have multiple matching .lt mixins, you can "wrap" your mixin in a detached ruleset to get the same effect (e.g.):

@lt: { .lt() };
.lt() {
    left:0;
    top:0;
}
.lt() {
    color: red;
}
rules {
    @n : lt;
    @x : @@n;
    @x();
}

// this will output
rules {
  left: 0;
  top: 0;
  color: red;
}

Unfortunately, there's an existing issue that prevents you from calling @@n() directly, but it's a short workaround.

@matthew-dean matthew-dean changed the title How can I use a variable to call a mixin? Using a variable in a mixin call May 4, 2016
@seven-phases-max
Copy link
Member

seven-phases-max commented May 4, 2016

That's all too verbose. Old-school methods may result in a more compact workaround (though I'm intentionally not providing any in this case since it would be pretty useless w/o knowing the actual use-case/context (i.e. why exactly n is a variable). Otherwise it's not clear how it would be different from just :

.lt() {
    left: 0;
    top:  0;
}

.n() {.lt}
.n();

// or:

@n: {.lt}
@n();

@vrbvillor
Copy link
Author

vrbvillor commented May 31, 2016

@matthew-dean @seven-phases-max Thanks to you all, I'll try them. and by the way, the script below can't compile succesfully, what's wrong with it ?

.clouds(){
    0% {
        .l(-20%);
        .opacity(.8);
    }
    50%{
        .l(50%);
        .opacity(.7);
    }
    100%{
        .l(120%);
        .opacity(.7);
    }
}

.keyframes(@name,@mixin){
    @keyframes @name{
        @mixin();
    }
    @-webkit-keyframes @name{
        @mixin();
    }
}
.keyframes(cloud,.clouds());

It says "Syntax error expected ')' got '.' in (filename) on line 92, column 18:

91 }
92 .keyframes(cloud,.clouds());
93

How to use a mixin as a parameter?? Thanks...

@vrbvillor
Copy link
Author

I've resolved myself........

@seven-phases-max
Copy link
Member

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