Less border-radius shorthand mixin, disable variable -


i trying write mixin border-radius outputs when values, set variable, >= 0. set base value in variable 3px, if enter -1 or no example, border-radius mixin not create properties in final stylesheet. can work if want have same value every corner. can't workout how working if want use shorthand i.e 3px 3px 0 0. think issue 3px being changed variable , 0 in both scenarios. code @ moment is

.border-radius(@r) when not (@r = no), (@r = 0) {     -webkit-border-radius: @r;        -moz-border-radius: @r;             border-radius: @r; } .border-radius(@r) when (@r = no), (@r = 0) {}  @baseborderradius: 3px; .class1 { .border-radius(@baseborderradius); } // outputs fine: 3px 3px 3px 3px .class2 { .border-radius(@baseborderradius @baseborderradius 0 0); } // outputs fine 3px 3px 0 0  @baseborderradius: no; // if change variable try , disable/not run mixin .class1 { .border-radius(@baseborderradius); } // want , doesn't run mixin .class2 { .border-radius(@baseborderradius @baseborderradius 0 0); } // problem! outputs: no no 0 0 

so need way disable/not run mixin if contains value or word defined global variable. doing theme variables file where, based on branding, companies might want rounded corners or not , prefer not have loads of 0 values unnecessarily included in final stylesheet.

i appreciate this, if find out want isn't possible within less. thank you

you try this, multi-parametric mixins ... , check guards each parameter separately, wrote mixin in 2 steps guards separately

  • for values nonnumeric entries (in case 'no') isnumber() and
  • for value = 0

here less code (note use od and in guards):

.border-r-not-0 (@a, @b, @c, @d) when not (@a = 0), not (@b = 0), not (@c = 0), not (@d = 0){       -webkit-border-radius: @a @b @c @d;        -moz-border-radius: @a @b @c @d;             border-radius: @a @b @c @d; } .border-radius(@a, @b, @c, @d) when (isnumber(@a)) , (isnumber(@b)) , (isnumber(@c)) , (isnumber(@d)){     .border-r-not-0(@a, @b, @c, @d); }  .border-radius(@r) when (isnumber(@r)) , not (@r = 0) {     -webkit-border-radius: @r;        -moz-border-radius: @r;             border-radius: @r; } 

now

@baseborderradius: 3px; .class1 { .border-radius(@baseborderradius); } .class2 { .border-radius(@baseborderradius, @baseborderradius, 0, 0); } 

the css output is:

.class1 {   -webkit-border-radius: 3px;   -moz-border-radius: 3px;   border-radius: 3px; } .class2 {   -webkit-border-radius: 3px 3px 0 0;   -moz-border-radius: 3px 3px 0 0;   border-radius: 3px 3px 0 0; } 

and there no output if

@baseborderradius: no; 

because not pass isnumber() test,

or if

@baseborderradius: 0; 

because arguments equal 0.

note: doing more complex stuff, using slash / parameters, have define different mixin, takes additional atributes, hope can idea.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -