Less custom function/mixin

user1325696 picture user1325696 · Jun 3, 2014 · Viewed 7.5k times · Source

For example I have next less code

form.someForm
{
    .form-group
    {
        > div
        {
            @media @wideMobile
            {
                width: calc(~"100%-144px");            
            }
        }

        > label
        {
            @media @wideMobile
            {
                width: 138px;            
            }
        }

    }
}  

I want to write something like

form.someForm
{
    .setWidths(138px,144px); 
}

To get the same result. How can I do that?

Answer

seven-phases-max picture seven-phases-max · Jun 3, 2014

I guess all those examples at the docs could emanate a few ideas:

form.someForm {
    .setWidths(138px, 144px);
}

// the mixin:

.setWidths(@labelWidth, @divMargin) {
    .form-group {
        > div {
            @media @wideMobile {
                width: calc(100% ~'-' @divMargin);
            }
        }

        > label {
            @media @wideMobile {
                width: @labelWidth;
            }
        }
    }
}

Or even shorter (if the descendants share same media query):

.setWidths(@labelWidth, @divMargin) {
    .form-group {
        @media @wideMobile {
            > div   {width: calc(100% ~'-' @divMargin)}
            > label {width: @labelWidth}
        }
    }
}