Skip to content

Commit

Permalink
Styled components support (#2)
Browse files Browse the repository at this point in the history
* createFactory is considered legacy. Tis encouraged to use createElement directly

* Add styled-components support + tests

* Re-add
  • Loading branch information
rpastorelle authored Jun 6, 2018
1 parent 90ac350 commit 347a291
Show file tree
Hide file tree
Showing 7 changed files with 165 additions and 56 deletions.
43 changes: 16 additions & 27 deletions lib/Component.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
var React = require('react');
var ReactDOMServer = require('react-dom/server');
var decache = require('decache');
var styledComponents = require('styled-components');

var Component = function Component(opts) {
this.opts = opts;
this.path = null;
this.component = null;
this.factory = null;
};

Component.prototype.getPath = function getPath(cb) {
Expand Down Expand Up @@ -54,36 +54,33 @@ Component.prototype.getComponent = function getComponent(cb) {
}.bind(this));
};

Component.prototype.getFactory = function getFactory(cb) {
if (this.factory) {
return cb(null, this.factory);
}

Component.prototype.renderToString = function renderToString(props, cb) {
this.getComponent(function(err, component) {
if (err) return cb(err);

var sheet = new styledComponents.ServerStyleSheet();

try {
this.factory = React.createFactory(component);
} catch(err) {
cb(err);
var markup = ReactDOMServer.renderToString(
sheet.collectStyles(React.createElement(component, props))
);
} catch (err) {
return cb(err);
}

cb(null, this.factory);
// Add style tags
markup = sheet.getStyleTags() + markup;

cb(null, markup);
}.bind(this));
};

Component.prototype._render = function _render(props, toStaticMarkup, cb) {
this.getFactory(function(err, factory) {
Component.prototype.renderToStaticMarkup = function renderToStaticMarkup(props, cb) {
this.getComponent(function(err, component) {
if (err) return cb(err);

var render = (
toStaticMarkup
? ReactDOMServer.renderToStaticMarkup
: ReactDOMServer.renderToString
).bind(ReactDOMServer);

try {
var markup = render(factory(props));
var markup = ReactDOMServer.renderToStaticMarkup(React.createElement(component, props));
} catch (err) {
return cb(err);
}
Expand All @@ -92,12 +89,4 @@ Component.prototype._render = function _render(props, toStaticMarkup, cb) {
}.bind(this));
};

Component.prototype.renderToString = function renderToString(props, cb) {
this._render(props, false, cb);
};

Component.prototype.renderToStaticMarkup = function renderToStaticMarkup(props, cb) {
this._render(props, true, cb);
};

module.exports = Component;
2 changes: 1 addition & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,4 @@ var reactRender = function reactRender(opts, cb) {

reactRender._components = components;

module.exports = reactRender;
module.exports = reactRender;
139 changes: 113 additions & 26 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
},
"dependencies": {
"decache": "^4.1.0",
"lodash": "^3.10.0"
"lodash": "^3.10.0",
"styled-components": "^3.3.2"
},
"peerDependencies": {
"react": ">= 16.3.0",
Expand Down
Loading

0 comments on commit 347a291

Please sign in to comment.