Created
April 1, 2015 19:43
-
-
Save asbjornenge/e761ddf44247e5c7b943 to your computer and use it in GitHub Desktop.
React ScopeSyle InjectStyle
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react' | |
import { InjectStyle } from 'react-scopestyle' | |
import Component from './component' | |
React.render( | |
<div className="app"> | |
<InjectStyle /> | |
<Component /> | |
</div> | |
, document.body) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react' | |
import ScopeStyle from 'react-scopestyle' | |
let css = "div { color:pink; }" | |
export default class MyScopeStyleComponent extends React.Component { | |
render() { | |
return ( | |
<ScopeStyle style={css}> | |
<div>yolo</div> | |
</ScopeStyle> | |
) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react' | |
import uid from 'uid' | |
import Emitter from 'nanoemitter' | |
let emitter = Emitter() | |
let style = "" | |
function mergeStyle(style) { | |
// magic merge | |
emitter.trigger('style', style) | |
} | |
export default class ScopeStyle extends React.Component { | |
constructor(props) { | |
super(props) | |
this.state = { uid : uid() } | |
} | |
render() { | |
return ( | |
<span className={this.state.uid}> | |
{this.props.children} | |
</span> | |
) | |
} | |
scopeStyle() { | |
// magically scope under .uid | |
return this.props.style | |
} | |
componentDidMount() { | |
mergeStyle(this.scopeStyle()) | |
} | |
} | |
export class InjectStyle extends React.Component { | |
constructor(props) { | |
super(props) | |
this.state = { style : "" } | |
} | |
render() { | |
return ( | |
<style dangerouslySetInnerHTML={{__html: this.state.style}} /> | |
) | |
} | |
shouldComponentUpdate(nextProps, nextState) { | |
// magic diffing | |
return true | |
} | |
appendStyle(style) { | |
this.setState({style:style}) | |
} | |
componentDidMount() { | |
emitter.on('style', this.appendStyle.bind(this)) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Alternative to react-scopestyle that collect styles and injects at the top level (or wherever). Still suffers from some performance issues, however minor.
I'm leaning towards that none of this should happen at runtime. I think we would be better off with just using a basic Style component (below), perhaps with some scoping, and do all these optimizations, merging etc. in a build step.
A build step could potentially pluck out all style tags, optimize and stick it in head or somewhere.
We should also think about how this will work with reusable/published components. It is probably a lot easier having people opt-in to a simple style tag than some complicated system.