LESS allows parametric mixins, such as:
.transition(@property, @duration){
transition: @property @duration;
-moz-transition: @property @duration; /* Firefox 4 */
-webkit-transition: @property @duration; /* Safari and Chrome */
-o-transition: @property @duration; /* Opera */
}
However, this doesn't always work with properties such as transitions. If you are trying to have multiple transitions and attempt to call the mixin multiple times, the last mixin overrides all previously defined transitions. That's because the proper CSS3 syntax for defining multiple transitions is:
... {
transition: @property1 @duration1, @property2 @duration2, ...;
}
The only way that I can think of to define multiple transitions as mixins is to overload the mixin:
.transition(@property, @duration){...}
.transition(@property, @duration, @prop2, @dur2){...}
.transition(@property, @duration, @prop2, @dur2, @prop3, @dur3){...}
Is there a more robust and concise way of defining the transition mixin to take in a variable number of arguments and construct the appropriate transition CSS?
Context: Sometimes I'd like to transition on multiple properties; for example, a :hover
might trigger transitions on background color, box-shadow, text-color, etc...
Current as of LESS 1.4, the documentation (http://lesscss.org/features/#mixins-parametric-feature-mixins-with-multiple-parameters) suggests the proper way to handle this:
Concretely, mixin:
usage:
Note that multiple properties are separated by commas and the trailing semi-colon causes the comma separated list to be treated as a single parameter in the mixin.
It would be nicer to define the mixin with a
rest...
parameter and be able to extract each element of the arbitrary-length arguments for separate handling, but the use-case I'm thinking of is adding vendor prefixes to transform transitions (so i could call it simply with.transition(opacity .2s, transform .3s)
and have the-webkit-transform
bit added automatically) and perhaps this is better handled by a different utility anyway (gulp-autoprefixer, for example).See my answer here: Multiple properties are getting treated as separate arguments in mixins
Summary: use this mixin for variable number of arguments:
In LESS, you can separate arguments using commas OR semi-colons. For single values that include commas, you can terminate that single value with a semi-colon in order to send the list as a single value, like this:
For multiple values, just use this syntax:
Easy peasy!
Advantages of using this method: This method would become more useful when there is a need to perform any extra operation on the values (say like adding
unit
asdeg
,px
or performing any extra math operations etc) or dynamically adding the vendor prefixes for the@property
also. For example there are times when you might want to pass onlytransform
as an input property to the mixin but want to add-webkit-transform
for the-webkit-transition
and-moz-transform
for the-moz-transition
etc.In this method, we make use of the
...
feature which allows us to pass variable number of arguments to a mixin, loop over each argument that is passed,extract
the name of the property along with the additional parameters (like duration, degree of rotation etc) and then use the merge feature that is provided by Less to concatenate the values specified for the property.+:
concatenates the property values with a comma and was introduced in Less v1.5.0+_:
concatenates the property values with a space and was introduced in Less v1.7.0.The above code when compiled would produce the below output:
Related Answer:
UPDATE for LESS 1.3.3+
Output is the same, but note the difference in how the properties can be passed in the newer versions of LESS by using the semicolon instead of doing an escaped string:
The semicolon forces the commas to be evaluated as list separators rather than parameter separators.
One Solution for LESS pre 1.3.3
We build the correct property arguments as a string for the
transition
, then use the escaped value (~
) operator to translate that into the proprietary syntax needed. By using string interpolation (@{variableName}
) we can even embed variables into the process, but the actual input needs to be in the form of an escaped string.LESS Code
CSS Output
Note: no
.class1
is output because the guard expression insures that something is input (though it does not guard against improper input).This should work, I think:
...
- is a valid less syntax, not something to be replaced.