LESS CSS how to modify parent property in mixin

2k views Asked by At

I'm looking for a less syntax to do something like that:

.button-default{
  background-color: rgb(100,200,250);
  :hover{
    .button-hover-effect-mixin();
  }
}
.button-warning{
  background-color: rgb(250,100,0);
  :hover{
    .button-hover-effect-mixin();
  }
}

.button-hover-effect-mixin(){
    background-color: darken(PARENT.background-color, 50%);
}

I know how to do that with a parameter or a global variable but the hover-effect schould not always be designed to change the background-color.

1

There are 1 answers

2
seven-phases-max On BEST ANSWER

If you mean the .button-default and .button-warning are those "PARENT"s for the .button-hover-effect-mixin then your friends are variables:

.button-default {
    @background-color: rgb(100, 200, 250);
    background-color: @background-color;
    &:hover {
        .button-hover-effect-mixin();
    }
}

.button-warning {
    @background-color: rgb(250, 100, 0);
    background-color: @background-color;
    &:hover {
        .button-hover-effect-mixin();
    }
}

.button-hover-effect-mixin() {
    background-color: darken(@background-color, 50%);
}

You also can make this variable to be a parameter of .button-hover-effect-mixin. Additionally don't miss & near :hover selector (without & it expands to .button-default :hover and this probably is not what you need, see Nesting).


And... if this goes in right direction and those colors are the only difference between the buttons I would rewrite the whole snippet to something like this:

.button(default, rgb(100, 200, 250));
.button(warning, rgb(250, 100, 0));

.button(@name, @color) {
    .button-@{name} {
        background-color: @color;
        &:hover {
            background-color: darken(@color, 50%);
        }
    }
}