Skip to content

Conversation

@donaldharvey
Copy link

I've reimplemented named arguments using the same syntax as James' earlier implementation - i.e. .mixin(@arg: value, @arg2: value).

Named arguments are particularly useful when working with CSS frameworks. For instance, if you had a mixin like this:

.grid(@columns:12) {
    .col(@cols: 1, @offset-left: 0, @offset-right: 0, @vertical-margin: 0, @horizontal-margin: 0, @vertical-padding: 0, @horizontal-padding: 0, @float: left) {
        width: (@cols * 100% / @columns) - (@horizontal-padding * 2) - (@horizontal-margin * 2);
        padding: @vertical-padding @horizontal-padding;
        margin: @vertical-margin @horizontal-margin @vertical-margin @horizontal-margin;
        margin-left: @offset-left * 100% / @columns + @horizontal-margin;
        margin-right: @offset-right * 100% / @columns + @horizontal-margin;
        float: @float;
    }
}

You can then either use it like this, as usual:

.wrapper {
    .grid(12);
}

.sidebar {
    .grid .col(6);
}

Or set specific parameters like this:

.wrapper {
    .grid(12);
}

.sidebar {
    .grid .col(
        @cols: 6,
        @offset-left: 1,
        @horizontal-margin: 2%
    );
}

I'm not yet convinced that this is the best syntax: it's certainly consistent with mixin definitions, but it feels a little bit strange if you're passing variables in with the same name as the keyword argument (e.g. .mixin(@width: @width)). Feedback and/or suggestions on an alternative syntax from LESS users would be appreciated!

The implementation places constraints on keyword arguments similar to those of Python: errors are thrown when positional arguments are placed after keyword arguments, when multiple values are received for a parameter, and when unexpected keyword arguments are received.

Finally, I'm not really sure how well named arguments work with combined with some of the recent additions to mixins (mainly patterns and @arguments); the current behaviour works, but might be unintuitive. Again, feedback from users would be helpful!

@donaldharvey
Copy link
Author

@cloudhead, what are your thoughts on this? Is there anything preventing this from being pulled?

@joeldrapper
Copy link

+1 this would make mixins so much more powerful! I quite like the syntax you've used as it's the same as you use to specify defaults if you were creating a mixin.

@donaldharvey
Copy link
Author

Closing this as James' updated branch, which does this, was just merged in.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants