Skip to content

Instantly share code, notes, and snippets.

@asbjornenge
Created April 1, 2015 19:43
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 asbjornenge/e761ddf44247e5c7b943 to your computer and use it in GitHub Desktop.
Save asbjornenge/e761ddf44247e5c7b943 to your computer and use it in GitHub Desktop.
React ScopeSyle InjectStyle
import React from 'react'
import { InjectStyle } from 'react-scopestyle'
import Component from './component'
React.render(
<div className="app">
<InjectStyle />
<Component />
</div>
, document.body)
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>
)
}
}
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))
}
}
@asbjornenge
Copy link
Author

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.

import React from 'react'

export default class Style extends React.Component {
    render() {
        return <style dangerouslySetInnerHTML={{__html: this.props.style}} />
    }
}
import React from 'react'
import Style  from 'above'

let css = ".MyComp { color : pink; }"

export default class MyComp extends React.Component {
    render() {
        return (
            <div className="MyComp">
                <Style style={css} />
                <div>yolo</div>
            </div>
        )
    }
}

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment