1
- import * as React from 'react'
2
- import {
3
- getDefaultShouldForwardProp ,
4
- composeShouldForwardProps
5
- /*
6
- type StyledOptions,
7
- type CreateStyled,
8
- type PrivateStyledComponent,
9
- type StyledElementType
10
- */
11
- } from './utils'
12
- import { withEmotionCache , ThemeContext } from '@emotion/react'
13
- import isDevelopment from '#is-development'
14
1
import isBrowser from '#is-browser'
2
+ import isDevelopment from '#is-development'
3
+ import { Theme , ThemeContext , withEmotionCache } from '@emotion/react'
4
+ import { Interpolation , serializeStyles } from '@emotion/serialize'
5
+ import { useInsertionEffectAlwaysWithSyncFallback } from '@emotion/use-insertion-effect-with-fallbacks'
15
6
import {
7
+ EmotionCache ,
16
8
getRegisteredStyles ,
17
9
insertStyles ,
18
- registerStyles
10
+ registerStyles ,
11
+ SerializedStyles
19
12
} from '@emotion/utils'
20
- import { serializeStyles } from '@emotion/serialize'
21
- import { useInsertionEffectAlwaysWithSyncFallback } from '@emotion/use-insertion-effect-with-fallbacks'
13
+ import * as React from 'react'
14
+ import {
15
+ CreateStyled ,
16
+ ElementType ,
17
+ StyledComponent ,
18
+ StyledOptions
19
+ } from './types'
20
+ import { composeShouldForwardProps , getDefaultShouldForwardProp } from './utils'
21
+ export type {
22
+ ComponentSelector ,
23
+ ArrayInterpolation ,
24
+ CSSObject ,
25
+ FunctionInterpolation ,
26
+ Interpolation
27
+ } from '@emotion/serialize'
22
28
23
29
const ILLEGAL_ESCAPE_SEQUENCE_ERROR = `You have illegal escape sequence in your template literal, most likely inside content's property value.
24
30
Because you write your CSS inside a JavaScript string you actually have to do double escaping, so for example "content: '\\00d7';" should become "content: '\\\\00d7';".
25
31
You can read more about this here:
26
32
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#ES2018_revision_of_illegal_escape_sequences`
27
33
28
- const Insertion = ( { cache, serialized, isStringTag } ) => {
34
+ const Insertion = ( {
35
+ cache,
36
+ serialized,
37
+ isStringTag
38
+ } : {
39
+ cache : EmotionCache
40
+ serialized : SerializedStyles
41
+ isStringTag : boolean
42
+ } ) => {
29
43
registerStyles ( cache , serialized , isStringTag )
30
44
31
45
const rules = useInsertionEffectAlwaysWithSyncFallback ( ( ) =>
@@ -52,10 +66,7 @@ const Insertion = ({ cache, serialized, isStringTag }) => {
52
66
return null
53
67
}
54
68
55
- let createStyled /*: CreateStyled */ = (
56
- tag /*: any */ ,
57
- options /* ?: StyledOptions */
58
- ) => {
69
+ const createStyled = ( tag : ElementType , options ?: StyledOptions ) => {
59
70
if ( isDevelopment ) {
60
71
if ( tag === undefined ) {
61
72
throw new Error (
@@ -66,8 +77,8 @@ let createStyled /*: CreateStyled */ = (
66
77
const isReal = tag . __emotion_real === tag
67
78
const baseTag = ( isReal && tag . __emotion_base ) || tag
68
79
69
- let identifierName
70
- let targetClassName
80
+ let identifierName : string | undefined
81
+ let targetClassName : string | undefined
71
82
if ( options !== undefined ) {
72
83
identifierName = options . label
73
84
targetClassName = options . target
@@ -78,9 +89,11 @@ let createStyled /*: CreateStyled */ = (
78
89
shouldForwardProp || getDefaultShouldForwardProp ( baseTag )
79
90
const shouldUseAs = ! defaultShouldForwardProp ( 'as' )
80
91
81
- /* return function<Props>(): PrivateStyledComponent<Props> { */
82
92
return function ( ) {
83
- let args = arguments
93
+ // eslint-disable-next-line prefer-rest-params
94
+ let args = arguments as any as Array <
95
+ TemplateStringsArray | Interpolation < Theme >
96
+ >
84
97
let styles =
85
98
isReal && tag . __emotion_styles !== undefined
86
99
? tag . __emotion_styles . slice ( 0 )
@@ -89,29 +102,35 @@ let createStyled /*: CreateStyled */ = (
89
102
if ( identifierName !== undefined ) {
90
103
styles . push ( `label:${ identifierName } ;` )
91
104
}
92
- if ( args [ 0 ] == null || args [ 0 ] . raw === undefined ) {
105
+ if (
106
+ args [ 0 ] == null ||
107
+ ( args [ 0 ] as TemplateStringsArray ) . raw === undefined
108
+ ) {
109
+ // eslint-disable-next-line prefer-spread
93
110
styles . push . apply ( styles , args )
94
111
} else {
95
- if ( isDevelopment && args [ 0 ] [ 0 ] === undefined ) {
112
+ const templateStringsArr = args [ 0 ] as TemplateStringsArray
113
+ if ( isDevelopment && templateStringsArr [ 0 ] === undefined ) {
96
114
console . error ( ILLEGAL_ESCAPE_SEQUENCE_ERROR )
97
115
}
98
- styles . push ( args [ 0 ] [ 0 ] )
116
+ styles . push ( templateStringsArr [ 0 ] )
99
117
let len = args . length
100
118
let i = 1
101
119
for ( ; i < len ; i ++ ) {
102
- if ( isDevelopment && args [ 0 ] [ i ] === undefined ) {
120
+ if ( isDevelopment && templateStringsArr [ i ] === undefined ) {
103
121
console . error ( ILLEGAL_ESCAPE_SEQUENCE_ERROR )
104
122
}
105
- styles . push ( args [ i ] , args [ 0 ] [ i ] )
123
+ styles . push ( args [ i ] , templateStringsArr [ i ] )
106
124
}
107
125
}
108
126
109
- const Styled /*: PrivateStyledComponent<Props> */ = withEmotionCache (
110
- ( props , cache , ref ) => {
111
- const FinalTag = ( shouldUseAs && props . as ) || baseTag
127
+ const Styled : ElementType = withEmotionCache (
128
+ ( props : Record < string , unknown > , cache , ref ) => {
129
+ const FinalTag = ( ( shouldUseAs && props . as ) ||
130
+ baseTag ) as React . ElementType
112
131
113
132
let className = ''
114
- let classInterpolations = [ ]
133
+ let classInterpolations : Interpolation < Theme > [ ] = [ ]
115
134
let mergedProps = props
116
135
if ( props . theme == null ) {
117
136
mergedProps = { }
@@ -146,7 +165,7 @@ let createStyled /*: CreateStyled */ = (
146
165
? getDefaultShouldForwardProp ( FinalTag )
147
166
: defaultShouldForwardProp
148
167
149
- let newProps = { }
168
+ let newProps : Record < string , unknown > = { }
150
169
151
170
for ( let key in props ) {
152
171
if ( shouldUseAs && key === 'as' ) continue
@@ -171,7 +190,7 @@ let createStyled /*: CreateStyled */ = (
171
190
</ >
172
191
)
173
192
}
174
- )
193
+ ) as StyledComponent < any , any , any >
175
194
176
195
Styled . displayName =
177
196
identifierName !== undefined
@@ -196,20 +215,20 @@ let createStyled /*: CreateStyled */ = (
196
215
return `.${ targetClassName } `
197
216
}
198
217
} )
199
-
200
- Styled . withComponent = (
201
- nextTag /*: StyledElementType<Props> */ ,
202
- nextOptions /* ?: StyledOptions */
218
+ ; ( Styled as any ) . withComponent = (
219
+ nextTag : ElementType ,
220
+ nextOptions : StyledOptions
203
221
) => {
204
- return createStyled ( nextTag , {
222
+ const newStyled = createStyled ( nextTag , {
205
223
...options ,
206
224
...nextOptions ,
207
225
shouldForwardProp : composeShouldForwardProps ( Styled , nextOptions , true )
208
- } ) ( ...styles )
226
+ } )
227
+ return ( newStyled as any ) ( ...styles )
209
228
}
210
229
211
230
return Styled
212
231
}
213
232
}
214
233
215
- export default createStyled
234
+ export default createStyled as CreateStyled
0 commit comments