Mixin return values
Can I set a return value as a color? As in:
.myselector {
border: 1px solid .custommixin(@colorValue);
}
Just wondering in response to another user's issue regarding color functions: https://github.com/cloudhead/less.js/pull/488
Oh I see, no. But you can do..
.myselector () {
@return: 2px;
}
.class {
.myselector;
border: @return solid red;
}
pretty ugly though.
Ah, of course. It might be nice to have a returned value for custom color functions (and other custom value functions) when mixin guards are in place. @return could be a reserved variable for LESS. As you say, it's a bit ugly to just have a mixin do nothing but variable assignment when it's called.
Would be nicer to see the first example I posted supported at some point. The rationale being that you have spin, lighten, darken, etc functioning as built-in mixins, so the logic for return values is already there in LESS, it would just be opening up the door for custom mixins.
Not I think that programming said functionality is easy mind you, just that it would empower people to extend the logic a bit.
yes, true, but I also like how it currently is, where the 'functions' are all javascript..
Couldn't variables accept arguments the same way mixins do? My usecase would be for instance:
@boxGlow(@spread: 5px, @color: #f00): inset 0 0 @spread @color;
.myselector {
box-shadow: 1px 1px 1px #000, @boxGlow(2px, #00f);
}
It's also fairly analogous with JavaScript where functions live in regular variables.
@cloudhead: If I spent some time implementing that feature (I haven't looked at the Less source yet, so I can't promise anything), would you be willing to add it if the code is good enough?
Adding a return capability to mixins would preserve the mapping of mixins to JavaScript functions, while still facilitating @jacobrask and @matthewdl's use case. I've documented this a bit more on issue 637.
duplicate of #538
woops, this is #538
This would be a very, very useful solution.
I'm trying to implement dynamically resizing images (like [UIImage resizable...] on iOS) and I would like to use a function which will return an URL string, not property/value pair.
Curently I'm using @cloudhead's solution.
Just to tie this in a nice bow, this should be included when the options.json support is added. Issue: #850
Mixins are mixins, we must not conflate their usage with functions. If having the ability to define custom functions in the style sheet is something we want though, that can be a separate discussion.
Mixins are mixins, we must not conflate their usage with functions.
The problem is that there's no way to forbid using mixins as functions. It is possible to use one as the other and they (and me :) get to use that ability. So de facto it's only a matter of syntactic sugar and verbosity:
.sum(@a, @b) {
@-: (@a + @b);
}
usage {
width: @-;.sum(20px, 33px);
}
Using same "function" twice in the "same scope":
// short (since 1.6.2):
usage {
& {width: @-;.sum(55px, 77px)}
& {height: @-;.sum(11px, 99px)}
}
// oldschool (since 1.4.x):
usage {
.-() {width: @-;.sum(20px, 33px)}
.-() {height: @-;.sum(40px, 42px)}
.-();
}
Is there a reason to invent some new "Less function declaration/definition" syntax (different from a mixin definition syntax) only to not allow to use width: .sum(20px, 33px); directly?
What the what did you just do there. That's some next-level Less-hackery going on there, lol.
@lukeapage I am sorry I can't understand the state of this issue. You've put a reference to this issue on several others but this is set as closed. Is there a plan to allow to use mixins as lambdas or are you suggesting us to take advantage of the global variable setting workaround? In other issue I've understand you were planning to remove that on some future.
@seven-phases-max your approach is awesome but there is no way to pass to the sum mixin the result of calling sum two times, am I wrong?
Talking on a somewhat more universal language, this: foo(bar(1, 2), bar(3, 4))
@axelcostaspena Well, everything is possible using mixins, it's just a matter of verbosity (obviously 5 or 6 lines of code do not look like a practical replacement for a oneliner).
less-plugin-functions
Wow. Nice! Really, the only thing I'd improve syntax-wise if this is moved to core, is turning the outer block into @functions and probably dropping the leading period on the mixin declarations for consistency with how these functions are used at call sites (namely; also without the leading period, as real Less functions).
E.g.
@functions {
foo(@x) {
return: @x * 2;
}
}
This still leaves the issue of having functions/mixins assignable to variables and passable as parameters, so that they can be used as lambda delegates. But this right here is already a very nice way to segregate 'computation' mixins from 'render' mixins.
Wow. Nice! Really, the only thing I'd improve syntax-wise if this is moved to core, is turning the outer block into @functions and probably dropping the leading period on the mixin declarations for consistency with how these functions are used at call sites
Yeah, we should respect this from @cloudhead :
Mixins are mixins, we must not conflate their usage with functions.
Defining a function as ".foo" by putting it in ".function" and then calling it with "foo" doesn't really make sense as a syntax.
@rjgotten The one suggestion I would make to your syntax is to not make functions plural, even if there is more than one function in the function block. Seems more Less/CSS consistent.
@function {
foo(@x) {
return: @x * 2;
}
}
Also, as sugar:
@function foo(@x) {
return: @x * 2;
}