Skip to content

Instantly share code, notes, and snippets.

@imerkle
Last active July 7, 2017 20:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save imerkle/5e89f5fd115ee587d8b4d8647b7832a6 to your computer and use it in GitHub Desktop.
Save imerkle/5e89f5fd115ee587d8b4d8647b7832a6 to your computer and use it in GitHub Desktop.
SO react no-inline checkpoint file
import React from 'react';
import PropTypes from 'prop-types';
import { injectStyle } from 'styletron-utils';
function removeInline(reactEl,styletron){
let newChildrens;
if(reactEl.props.children){
if(reactEl.props.children instanceof Array){
newChildrens = [];
Object.keys(reactEl.props.children).forEach(function(key) {
const childReactEl = reactEl.props.children[key];
newChildrens.push(removeInline(childReactEl,styletron));
});
}else if(reactEl.props.children instanceof Object){
let childReactEl = reactEl.props.children;
newChildrens = removeInline(childReactEl,styletron);
}else{
newChildrens = reactEl.props.children;
}
}
const {style, className, ...others} = reactEl.props;
let newProps = {style: null};
if(style){
let newClassName = injectStyle(styletron, style);
newProps.className = [newClassName,(className|| "")].join(' ');
}
return React.cloneElement(reactEl,newProps,newChildrens);
}
const noInline = (Component) => {
const StyledElement = (props, { styletron }) => {
return removeInline(Component,styletron);
};
StyledElement.contextTypes = {
styletron: PropTypes.object.isRequired,
};
return StyledElement;
};
export default noInline;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment