Skip to content

Commit

Permalink
feat: add basic static class decorator
Browse files Browse the repository at this point in the history
  • Loading branch information
streamich committed Mar 14, 2018
1 parent 9b12511 commit 51b09b3
Showing 1 changed file with 40 additions and 0 deletions.
40 changes: 40 additions & 0 deletions addon/decorator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'use strict';

var cloneElement = require('react').cloneElement;

exports.addon = function (renderer) {
var transformStatic = function (fn, styles, block) {
block = block || fn.displayName || fn.name;

var render_ = fn.prototype.render;
var className = '';

fn.prototype.render = function() {
var element = render_.call(this);

if (element) {
if (!className) {
className = renderer.rule(styles, block);
}

if (process.env.NODE_ENV === 'production') {
element.props.className = (element.props.className || '') + className;
} else {
element = cloneElement(element, {
className: (element.props.className || '') + className,
});
}
}

return element;
};
};

var decorator = function (a, b) {
return function (Klass) {
transformStatic(Klass, a, b);
};
};

return decorator;
};

0 comments on commit 51b09b3

Please sign in to comment.