@@ -4,7 +4,8 @@ import type { ElementType } from 'react'
44import {
55 getDefaultShouldForwardProp ,
66 type StyledOptions ,
7- type CreateStyled
7+ type CreateStyled ,
8+ type PrivateStyledComponent
89} from './utils'
910import { withEmotionCache , ThemeContext } from '@emotion/core'
1011import { getRegisteredStyles , insertStyles } from '@emotion/utils'
@@ -17,12 +18,6 @@ https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_liter
1718
1819let isBrowser = typeof document !== 'undefined'
1920
20- type StyledComponent = (
21- props : *
22- ) => React . Node & {
23- withComponent ( nextTag : ElementType , nextOptions ?: StyledOptions ) : *
24- }
25-
2621let createStyled : CreateStyled = ( tag : any , options ? : StyledOptions ) => {
2722 if ( process . env . NODE_ENV !== 'production' ) {
2823 if ( tag === undefined ) {
@@ -55,7 +50,7 @@ let createStyled: CreateStyled = (tag: any, options?: StyledOptions) => {
5550 shouldForwardProp || getDefaultShouldForwardProp ( baseTag )
5651 const shouldUseAs = ! defaultShouldForwardProp ( 'as' )
5752
58- return function ( ) : StyledComponent {
53+ return function < P > (): PrivateStyledComponent < P > {
5954 let args = arguments
6055 let styles =
6156 isReal && tag . __emotion_styles !== undefined
@@ -82,104 +77,107 @@ let createStyled: CreateStyled = (tag: any, options?: StyledOptions) => {
8277 }
8378 }
8479
85- const Styled : any = withEmotionCache ( ( props , context , ref ) => {
86- return (
87- < ThemeContext . Consumer >
88- { theme => {
89- const finalTag = ( shouldUseAs && props . as ) || baseTag
90-
91- let className = ''
92- let classInterpolations = [ ]
93- let mergedProps = props
94- if ( props . theme == null ) {
95- mergedProps = { }
96- for ( let key in props ) {
97- mergedProps [ key ] = props [ key ]
80+ // $FlowFixMe: we need to cast StatelessFunctionalComponent to our PrivateStyledComponent class
81+ const Styled : PrivateStyledComponent < P > = withEmotionCache(
82+ (props, context, ref) => {
83+ return (
84+ < ThemeContext . Consumer >
85+ { theme => {
86+ const finalTag = ( shouldUseAs && props . as ) || baseTag
87+
88+ let className = ''
89+ let classInterpolations = [ ]
90+ let mergedProps = props
91+ if ( props . theme == null ) {
92+ mergedProps = { }
93+ for ( let key in props ) {
94+ mergedProps [ key ] = props [ key ]
95+ }
96+ mergedProps . theme = theme
97+ }
98+
99+ if ( typeof props . className === 'string' ) {
100+ className = getRegisteredStyles (
101+ context . registered ,
102+ classInterpolations ,
103+ props . className
104+ )
105+ } else if ( props . className != null ) {
106+ className = `${ props . className } `
98107 }
99- mergedProps . theme = theme
100- }
101108
102- if ( typeof props . className === 'string' ) {
103- className = getRegisteredStyles (
109+ const serialized = serializeStyles (
110+ styles . concat ( classInterpolations ) ,
104111 context . registered ,
105- classInterpolations ,
106- props . className
112+ mergedProps
107113 )
108- } else if ( props . className != null ) {
109- className = `${ props . className } `
110- }
111-
112- const serialized = serializeStyles (
113- styles . concat ( classInterpolations ) ,
114- context . registered ,
115- mergedProps
116- )
117- const rules = insertStyles (
118- context ,
119- serialized ,
120- typeof finalTag === 'string'
121- )
122- className += `${ context . key } -${ serialized . name } `
123- if ( targetClassName !== undefined ) {
124- className += ` ${ targetClassName } `
125- }
126-
127- const finalShouldForwardProp =
128- shouldUseAs && shouldForwardProp === undefined
129- ? getDefaultShouldForwardProp ( finalTag )
130- : defaultShouldForwardProp
131-
132- let newProps = { }
133-
134- for ( let key in props ) {
135- if ( shouldUseAs && key === 'as' ) continue
136-
137- if (
138- // $FlowFixMe
139- finalShouldForwardProp ( key )
140- ) {
141- newProps [ key ] = props [ key ]
114+ const rules = insertStyles (
115+ context ,
116+ serialized ,
117+ typeof finalTag === 'string'
118+ )
119+ className += `${ context . key } -${ serialized . name } `
120+ if ( targetClassName !== undefined ) {
121+ className += ` ${ targetClassName } `
142122 }
143- }
144123
145- newProps . className = className
124+ const finalShouldForwardProp =
125+ shouldUseAs && shouldForwardProp === undefined
126+ ? getDefaultShouldForwardProp ( finalTag )
127+ : defaultShouldForwardProp
146128
147- newProps . ref = ref || props . innerRef
148- if ( process . env . NODE_ENV !== 'production' && props . innerRef ) {
149- console . error (
150- '`innerRef` is deprecated and will be removed in a future major version of Emotion, please use the `ref` prop instead' +
151- ( identifierName === undefined
152- ? ''
153- : ` in the usage of \`${ identifierName } \`` )
154- )
155- }
156-
157- const ele = React . createElement ( finalTag , newProps )
158- if ( ! isBrowser && rules !== undefined ) {
159- let serializedNames = serialized . name
160- let next = serialized . next
161- while ( next !== undefined ) {
162- serializedNames += ' ' + next . name
163- next = next . next
129+ let newProps = { }
130+
131+ for ( let key in props ) {
132+ if ( shouldUseAs && key === 'as' ) continue
133+
134+ if (
135+ // $FlowFixMe
136+ finalShouldForwardProp ( key )
137+ ) {
138+ newProps [ key ] = props [ key ]
139+ }
140+ }
141+
142+ newProps . className = className
143+
144+ newProps . ref = ref || props . innerRef
145+ if ( process . env . NODE_ENV !== 'production' && props . innerRef ) {
146+ console . error (
147+ '`innerRef` is deprecated and will be removed in a future major version of Emotion, please use the `ref` prop instead' +
148+ ( identifierName === undefined
149+ ? ''
150+ : ` in the usage of \`${ identifierName } \`` )
151+ )
164152 }
165- return (
166- < React . Fragment >
167- < style
168- { ...{
169- [ `data-emotion-${ context . key } ` ] : serializedNames ,
170- dangerouslySetInnerHTML : { __html : rules } ,
171- nonce : context . sheet . nonce
172- } }
173- />
174- { ele }
175- </ React . Fragment >
176- )
177- }
178- return ele
179- } }
180- </ ThemeContext . Consumer >
181- )
182- } )
153+
154+ const ele = React . createElement ( finalTag , newProps )
155+ if ( ! isBrowser && rules !== undefined ) {
156+ let serializedNames = serialized . name
157+ let next = serialized . next
158+ while ( next !== undefined ) {
159+ serializedNames += ' ' + next . name
160+ next = next . next
161+ }
162+ return (
163+ < React . Fragment >
164+ < style
165+ { ...{
166+ [ `data-emotion-${ context . key } ` ] : serializedNames ,
167+ dangerouslySetInnerHTML : { __html : rules } ,
168+ nonce : context . sheet . nonce
169+ } }
170+ />
171+ { ele }
172+ </ React . Fragment >
173+ )
174+ }
175+ return ele
176+ } }
177+ </ ThemeContext . Consumer >
178+ )
179+ }
180+ )
183181
184182 Styled.displayName =
185183 identifierName !== undefined
@@ -204,7 +202,7 @@ let createStyled: CreateStyled = (tag: any, options?: StyledOptions) => {
204202 ) {
205203 return 'NO_COMPONENT_SELECTOR'
206204 }
207- // $FlowFixMe
205+ // $FlowFixMe: coerce undefined to string
208206 return `.${ targetClassName } `
209207 }
210208 } )
@@ -220,6 +218,7 @@ let createStyled: CreateStyled = (tag: any, options?: StyledOptions) => {
220218 : options
221219 ) ( ...styles )
222220 }
221+
223222 return Styled
224223 }
225224}
0 commit comments