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

property name #1

Closed
TrySound opened this issue Feb 10, 2015 · 25 comments
Closed

property name #1

TrySound opened this issue Feb 10, 2015 · 25 comments

Comments

@TrySound
Copy link
Member

Does argument work for property name?

@define-mixin item $prop {
    &-12 {
        $prop: 100%;
    }
    &-11 {
        $prop: 91.66666667%;
    }
    &-10 {
        $prop: 83.33333333%;
    }
    &-9 {
        $prop: 75%;
    }
    &-8 {
        $prop: 66.66666667%;
    }
    &-7 {
        $prop: 58.33333333%;
    }
    &-6 {
        $prop: 50%;
    }
    &-5 {
        $prop: 41.66666667%;
    }
    &-4 {
        $prop: 33.33333333%;
    }
    &-3 {
        $prop: 25%;
    }
    &-2 {
        $prop: 16.66666667%;
    }
    &-1 {
        $prop: 8.33333333%;
    }
}
@TrySound TrySound changed the title property var property name Feb 10, 2015
@ai
Copy link
Member

ai commented Feb 10, 2015

Not now. There is limitation in PostCSS parser. But I plan to fix it in next parser release: postcss/postcss#185

Right now you can create JS mixin:

var postcss = require('postcss');

item: function (mixin, prop) {
    for ( var i = 0; i < 12; i++ ) {
        var rule = postcss.rule({ selector: '&-' + i });
        var rule.append({ prop: prop, value: (i * 8.33333333) + '%' });
        mixin.insertBefore(rule);
    }
};

@TrySound
Copy link
Member Author

Cool, thanks!
But is there any way to get postcss instance inside of item method?
For example with gulp:

var gulp = require('gulp'). postcss = require('gulp-postcss');
gulp.src('src.css')
  .pipe(postcss([
    require('postcss-mixin')({
      mixins: {
        item: function () {
          //
        }
      }
    })
  ]))

@ai
Copy link
Member

ai commented Feb 10, 2015

Nope :(.

Why you need PostCSS instance there?

@TrySound
Copy link
Member Author

To make mixin I need require and gulp-postcss and postcss. I think this is excess.

@ai
Copy link
Member

ai commented Feb 10, 2015

Just add postcss to your package.json.

@TrySound
Copy link
Member Author

Okey. I just like to optimize anything like in browser.

@TrySound
Copy link
Member Author

Maybe you will pass instance in this?

@ai
Copy link
Member

ai commented Feb 10, 2015

require() is a better way :)

@TrySound
Copy link
Member Author

Maybe in the next release? It would be more elegant for gulp.

@ai
Copy link
Member

ai commented Feb 10, 2015

Why one line of var postcss = require('postcss') is not elegant?

Problem is very simple:

  1. Mixin function can receive different number of argument. So we need to store all system arguments in first places.
  2. But most of mixin users doesn’t need to create nodes with postcss.rule() shortcut. Tere is a many other way to change CSS AST.

So if I add postcss to functions argument I move parameters from mixin as third and next arguments.

@TrySound
Copy link
Member Author

mixin.insertBefore doesn't work

TypeError: Cannot read property 'type' of undefined
    .pipe(gulp_postcss([
        require('postcss-mixins')({
            mixins: {
                item: function (mixin, prop) {
                    var rule, i, val;
                    for(i = 1; i <= 12; i++ ) {
                        rule = postcss.rule({
                            selector: '&-' + i
                        });
                        val = i / 12 * 100;
                        rule.append({
                            prop: prop,
                            value: val.toFixed(val % 1 === 0 ? 0 : 4) + '%'
                        });
                        mixin.insertBefore(rule);
                    }
                }
            }
        }),
        require('postcss-custom-properties')(),
        require('postcss-nested'),
        require('postcss-calc')()
    ]))

@ai
Copy link
Member

ai commented Feb 11, 2015

Please, post a full stack trace. I can’t help by this small message :).

@ai
Copy link
Member

ai commented Feb 11, 2015

BTW, you can clean Gruntfile if you will use mixinsDir options:

.pipe(gulp_postcss([
        require('postcss-mixins')({
            mixinsDir: __dirname + '/postcss/mixins/';
        }),
        require('postcss-custom-properties')(),
        require('postcss-nested'),
        require('postcss-calc')()
    ]))

and put JS-mixin to postcss/mixins/item.js:

var postcss = require('postcss');

module.exports = function (mixin, prop) {
    var rule, i, val;
    for(i = 1; i <= 12; i++ ) {
        rule = postcss.rule({ selector: '&-' + i });
        val = i / 12 * 100;
        rule.append({
            prop: prop,
            value: val.toFixed(val % 1 === 0 ? 0 : 4) + '%'
        });
        mixin.insertBefore(rule);
    }
}

@ai
Copy link
Member

ai commented Feb 11, 2015

I found a problem. Use mixin.parent.insertBefore(mixin, rule); instead of mixin.insertBefore(rule);.

@TrySound
Copy link
Member Author

Thank you very much, Andrey!
For now problem in postcss-import (breaks indentation), and I use gulp-cssimport

My result

P.S. Don't forget to update documentation. Mixins is a good thing.

@ai
Copy link
Member

ai commented Feb 12, 2015

@TrySound what docs update you ask?

@ai
Copy link
Member

ai commented Feb 12, 2015

Like this? 5e9fe94

@ai ai closed this as completed Feb 12, 2015
@ai ai reopened this Feb 12, 2015
@TrySound
Copy link
Member Author

No, like this mixin.parent.insertBefore(mixin, rule); :)

@ai
Copy link
Member

ai commented Feb 12, 2015

@TrySound here is all docs https://github.com/postcss/postcss/blob/master/API.md

What are you ask?

@TrySound
Copy link
Member Author

In this repo:

Function Mixin

require('postcss-mixins')({
    mixins: {
        icons: function (mixin, dir) {
            fs.readdirSync('/images/' + dir).forEach(function (file) {
                var icon = file.replace(/\.svg$/, '');
                var rule = postcss.rule('.icon.icon-' + icon);
                rule.append({
                    prop:  'background',
                    value: 'url(' + dir + '/' + file ')'
                });
                mixin.insertBefore(rule);
            });
        }
    }
});

@ai
Copy link
Member

ai commented Feb 12, 2015

Я тебя совсем не понимаю. Объясни с самого начала, что ты хочешь.

@TrySound
Copy link
Member Author

В документации к миксинам в разделе Function Mixin исправить добавление правила.

@ai
Copy link
Member

ai commented Feb 12, 2015

А, я исправил вот так 8cccd2a

@TrySound
Copy link
Member Author

Thanks)

@ai
Copy link
Member

ai commented May 5, 2015

Close this issue, because we have same in PostCSS about variables

@ai ai closed this as completed May 5, 2015
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

No branches or pull requests

2 participants