UNPKG

1.13 kBJavaScriptView Raw
1/**
2 * Module dependencies
3 */
4
5var postcss = require('postcss');
6var nested = require('postcss-nested');
7var DOM = require('./dom');
8var slice = Array.prototype.slice;
9
10module.exports = function(mod, from) {
11 var compiler = postcss([validate, nested]);
12 return function(props) {
13 var out = mod.render(DOM, $get, props, null, genYield(props));
14 var str = Array.isArray(out) ? out.join('') : out;
15 return compiler.process(str, {from: from}).css;
16 };
17};
18
19function validate(css) {
20 (css.nodes || []).forEach(function(node) {
21 if (!~node.selector.indexOf('&')) return;
22 throw new Error('properties cannot be declared in the root. check ' + node.source.input.from);
23 });
24}
25
26function $get(path, parent, fallback) {
27 for (var i = 0; i < path.length; i++) {
28 if (!parent) return undefined;
29 parent = parent[path[i]];
30 }
31 return parent;
32}
33
34function genYield(props) {
35 props = props || {};
36 return function $yield(name) {
37 if (!name) return props.children;
38 var prop = props[name];
39 if (typeof prop === 'function') return prop.apply(null, slice.call(arguments, 1));
40 return prop || '';
41 };
42}