Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: Using mixins to create new mixins #1659

Closed
kmcaloon opened this issue Feb 28, 2015 · 4 comments
Closed

Feature Request: Using mixins to create new mixins #1659

kmcaloon opened this issue Feb 28, 2015 · 4 comments

Comments

@kmcaloon
Copy link

Could not find any information on this and it doesn't seem to be supported. Maybe there is a better solution that achieves the same functionality, but it might be useful to be able to use mixins to create new mixins whose names and attributes are defined by the parent.

Here is a for instance.

In order to use helper styles in my projects, I usually implement a mixin which allows me to either extend them when outside of a media query, and simply include the styles when inside. Here is an example:

@mixin equal-height($extend: true) {
    @if $extend {
        @extend .equal-height
    }
    @else {
        position: relative;

        .content {
            position: absolute;
            top: 0;
            left: 0;
            width: 100%;
            height: 100%;
        }

        &:before {
            content: '';
            display: block;
            padding-top: 100%;
        }
    }
}
.equal-height {@include equal-height($extend: false);}

Now what would be nice if for every time I need to make a new helper style, I could use a helper mixin which creates a new mixin with the styles I need. Here is what it could look like:

@mixin helper($name, $styles) {
    @mixin $name($extend: true) {
        @if $extend {
            @extend .$name;
        }
        @else {
            $styles
        }
    }
    .$name {@include $name($extend: false);}
}

So using this new method, this is how I could create a new equal-height mixin:

@mixin helper(equal-height, 
          position: relative;

          .content {
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
       }
          &:before {
        content: '';
        display: block;
        padding-top: 100%;
        }
) ;

This would then create a new helper mixin called "equal-height" achieving the desired functionality.

Is this functionality something that could be useful to others -- or is it already possible and I am just missing something?

@anEffingChamp
Copy link

I am not entirely sure what you are trying to accomplish, but mixins are
available to any object once they are declared.

@mixin foo {
// declarations
}

@mixin bar {
    @include foo;
// more declarations
}

bar will include foo, and you can override undesirable styles from foo if
necessary.

@davidkpiano
Copy link

@kmcaloon I'd keep track of issue #1582 - if/when that feature is introduced into Sass, it would give you an easy way to do what you're trying to accomplish.

@nex3
Copy link
Contributor

nex3 commented Mar 6, 2015

Dynamic mixin declarations add a huge amount of complexity both for users and for the implementation, with relatively little benefit to show for it. Once we have #1289, it will be pretty easy to do something like this more explicitly by storing styles in maps and defining an extend-or-include mixin.

@nex3 nex3 closed this as completed Mar 6, 2015
@lolmaus
Copy link

lolmaus commented Mar 7, 2015

If you really-really need dynamic mixins, you can peek into how Compass 1 was achieving that with ERB: https://github.com/Compass/compass/blob/354d6fffa43f79c020bf565f3ba42b75a577a34d/lib/compass/sprite_importer/content.erb#L45

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
5 participants