11import { PureComponent , createElement } from 'react'
22import { create as createJss , getDynamicStyles } from 'jss'
33import preset from 'jss-preset-default'
4+
45import filterProps from './utils/filter-props'
6+ import composeClasses from './utils/compose-classes'
7+ import type {
8+ StyledElementAttrsType ,
9+ StyledElementType ,
10+ tagOrStyledElementTypeype ,
11+ StyledElementPropsType
12+ } from './types'
513
614const jssDefault = createJss ( preset ( ) )
715
8- type StyledElementAttrsType = { tag : string , styles : Object }
9- type StyledElementType = Function & StyledElementAttrsType
10- type tagOrStyledElementTypeype = string | StyledElementType
11- type StyledElementPropsType = {
12- classes : Object ,
13- children : ?any ,
14- className : ?string ,
15- }
16-
1716const createStyled = ( jss ? : Function = jssDefault ) => ( baseStyles : Object = { } ) = > {
18- let sheet
19- let dynamicSheet
17+ const sheets = { }
2018 let counter = 0
2119
20+ const mountSheets = ( ) => {
21+ if ( ! sheets . staticSheet ) {
22+ sheets . staticSheet = jss . createStyleSheet ( baseStyles , {
23+ link : true ,
24+ meta : 'StaticBaseSheet' ,
25+ } ) . attach ( )
26+
27+ sheets . dynamicSheet = jss . createStyleSheet ( { } , {
28+ link : true ,
29+ meta : 'DynamicComponentSheet' ,
30+ } ) . attach ( )
31+ }
32+ }
33+
2234 const styled = (
2335 tagOrStyledElement : tagOrStyledElementTypeype ,
2436 ownStyles : Object
@@ -46,62 +58,54 @@ const createStyled = (jss?: Function = jssDefault) => (baseStyles: Object = {})
4658 }
4759
4860 componentWillMount ( ) {
49- if ( ! sheet ) {
50- sheet = jss . createStyleSheet ( baseStyles , {
51- link : true ,
52- meta : 'StaticBaseSheet' ,
53- } ) . attach ( )
54-
55- dynamicSheet = jss . createStyleSheet ( { } , {
56- link : true ,
57- meta : 'DynamicComponentSheet' ,
58- } ) . attach ( )
59- }
61+ mountSheets ( )
6062
61- if ( ! sheet . getRule ( staticTag ) ) {
62- sheet . addRule ( staticTag , elementStyles )
63+ if ( ! sheets . staticSheet . getRule ( staticTag ) ) {
64+ sheets . staticSheet . addRule ( staticTag , elementStyles )
6365 }
6466
65- if ( dynamicStyles && ! dynamicSheet . getRule ( this . tagScoped ) ) {
66- dynamicSheet . addRule ( this . tagScoped , dynamicStyles )
67- dynamicSheet
67+ if ( dynamicStyles && ! sheets . dynamicSheet . getRule ( this . tagScoped ) ) {
68+ sheets . dynamicSheet . addRule ( this . tagScoped , dynamicStyles )
69+ sheets . dynamicSheet
6870 . update ( this . tagScoped , this . props )
6971 . deploy ( )
7072 }
7173 }
7274
7375 componentWillReceiveProps ( nextProps : StyledElementPropsType ) {
7476 if ( dynamicStyles ) {
75- dynamicSheet
77+ sheets . dynamicSheet
7678 . update ( this . tagScoped , nextProps )
7779 . deploy ( )
7880 }
7981 }
8082
8183 componentWillUnmount ( ) {
82- dynamicSheet . deleteRule ( this . tagScoped )
84+ sheets . dynamicSheet . deleteRule ( this . tagScoped )
8385 }
8486
8587 render ( ) {
86- if ( ! sheet ) return null
88+ if ( ! sheets . staticSheet ) return null
8789
8890 const { children, className, ...attrs } = this . props
8991
9092 const props = filterProps ( attrs )
91- const tagClass = [
92- sheet . classes [ staticTag ] ,
93- dynamicSheet . classes [ this . tagScoped ] ,
94- className ,
95- ]
96- . filter ( Boolean )
97- . join ( ' ' )
93+ const tagClass = composeClasses (
94+ sheets . staticSheet . classes [ staticTag ] ,
95+ sheets . dynamicSheet . classes [ this . tagScoped ] ,
96+ className
97+ )
9898
9999 return createElement ( tag , { ...props , className : tagClass } , children )
100100 }
101101 }
102102 }
103103
104- return Object . assign ( styled , { styles : baseStyles } )
104+ return Object . assign ( styled , {
105+ sheets,
106+ mountSheets,
107+ styles : baseStyles
108+ } )
105109}
106110
107111const defaultStyledCreator = createStyled ( )
@@ -113,3 +117,5 @@ export {
113117}
114118
115119export default defaultStyled
120+
121+ export { default as injectStyled } from './injectStyled'
0 commit comments