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 { CreateStyled , ElementType , StyledOptions } from './types'
15
+ import { composeShouldForwardProps , getDefaultShouldForwardProp } from './utils'
16
+ export type {
17
+ ArrayInterpolation ,
18
+ ComponentSelector ,
19
+ CSSObject ,
20
+ FunctionInterpolation ,
21
+ Interpolation
22
+ } from '@emotion/serialize'
22
23
23
24
const ILLEGAL_ESCAPE_SEQUENCE_ERROR = `You have illegal escape sequence in your template literal, most likely inside content's property value.
24
25
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
26
You can read more about this here:
26
27
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals#ES2018_revision_of_illegal_escape_sequences`
27
28
28
- const Insertion = ( { cache, serialized, isStringTag } ) => {
29
+ const Insertion = ( {
30
+ cache,
31
+ serialized,
32
+ isStringTag
33
+ } : {
34
+ cache : EmotionCache
35
+ serialized : SerializedStyles
36
+ isStringTag : boolean
37
+ } ) => {
29
38
registerStyles ( cache , serialized , isStringTag )
30
39
31
40
const rules = useInsertionEffectAlwaysWithSyncFallback ( ( ) =>
@@ -52,10 +61,7 @@ const Insertion = ({ cache, serialized, isStringTag }) => {
52
61
return null
53
62
}
54
63
55
- let createStyled /*: CreateStyled */ = (
56
- tag /*: any */ ,
57
- options /* ?: StyledOptions */
58
- ) => {
64
+ const createStyled = ( tag : ElementType , options ?: StyledOptions ) => {
59
65
if ( isDevelopment ) {
60
66
if ( tag === undefined ) {
61
67
throw new Error (
@@ -66,8 +72,8 @@ let createStyled /*: CreateStyled */ = (
66
72
const isReal = tag . __emotion_real === tag
67
73
const baseTag = ( isReal && tag . __emotion_base ) || tag
68
74
69
- let identifierName
70
- let targetClassName
75
+ let identifierName : string | undefined
76
+ let targetClassName : string | undefined
71
77
if ( options !== undefined ) {
72
78
identifierName = options . label
73
79
targetClassName = options . target
@@ -78,9 +84,11 @@ let createStyled /*: CreateStyled */ = (
78
84
shouldForwardProp || getDefaultShouldForwardProp ( baseTag )
79
85
const shouldUseAs = ! defaultShouldForwardProp ( 'as' )
80
86
81
- /* return function<Props>(): PrivateStyledComponent<Props> { */
82
87
return function ( ) {
83
- let args = arguments
88
+ // eslint-disable-next-line prefer-rest-params
89
+ let args = arguments as any as Array <
90
+ TemplateStringsArray | Interpolation < Theme >
91
+ >
84
92
let styles =
85
93
isReal && tag . __emotion_styles !== undefined
86
94
? tag . __emotion_styles . slice ( 0 )
@@ -89,29 +97,35 @@ let createStyled /*: CreateStyled */ = (
89
97
if ( identifierName !== undefined ) {
90
98
styles . push ( `label:${ identifierName } ;` )
91
99
}
92
- if ( args [ 0 ] == null || args [ 0 ] . raw === undefined ) {
100
+ if (
101
+ args [ 0 ] == null ||
102
+ ( args [ 0 ] as TemplateStringsArray ) . raw === undefined
103
+ ) {
104
+ // eslint-disable-next-line prefer-spread
93
105
styles . push . apply ( styles , args )
94
106
} else {
95
- if ( isDevelopment && args [ 0 ] [ 0 ] === undefined ) {
107
+ const templateStringsArr = args [ 0 ] as TemplateStringsArray
108
+ if ( isDevelopment && templateStringsArr [ 0 ] === undefined ) {
96
109
console . error ( ILLEGAL_ESCAPE_SEQUENCE_ERROR )
97
110
}
98
- styles . push ( args [ 0 ] [ 0 ] )
111
+ styles . push ( templateStringsArr [ 0 ] )
99
112
let len = args . length
100
113
let i = 1
101
114
for ( ; i < len ; i ++ ) {
102
- if ( isDevelopment && args [ 0 ] [ i ] === undefined ) {
115
+ if ( isDevelopment && templateStringsArr [ i ] === undefined ) {
103
116
console . error ( ILLEGAL_ESCAPE_SEQUENCE_ERROR )
104
117
}
105
- styles . push ( args [ i ] , args [ 0 ] [ i ] )
118
+ styles . push ( args [ i ] , templateStringsArr [ i ] )
106
119
}
107
120
}
108
121
109
- const Styled /*: PrivateStyledComponent<Props> */ = withEmotionCache (
110
- ( props , cache , ref ) => {
111
- const FinalTag = ( shouldUseAs && props . as ) || baseTag
122
+ const Styled : ElementType = withEmotionCache (
123
+ ( props : Record < string , unknown > , cache , ref ) => {
124
+ const FinalTag =
125
+ ( shouldUseAs && ( props . as as React . ElementType ) ) || baseTag
112
126
113
127
let className = ''
114
- let classInterpolations = [ ]
128
+ let classInterpolations : Interpolation < Theme > [ ] = [ ]
115
129
let mergedProps = props
116
130
if ( props . theme == null ) {
117
131
mergedProps = { }
@@ -146,7 +160,7 @@ let createStyled /*: CreateStyled */ = (
146
160
? getDefaultShouldForwardProp ( FinalTag )
147
161
: defaultShouldForwardProp
148
162
149
- let newProps = { }
163
+ let newProps : Record < string , unknown > = { }
150
164
151
165
for ( let key in props ) {
152
166
if ( shouldUseAs && key === 'as' ) continue
@@ -196,20 +210,20 @@ let createStyled /*: CreateStyled */ = (
196
210
return `.${ targetClassName } `
197
211
}
198
212
} )
199
-
200
- Styled . withComponent = (
201
- nextTag /*: StyledElementType<Props> */ ,
202
- nextOptions /* ?: StyledOptions */
213
+ ; ( Styled as any ) . withComponent = (
214
+ nextTag : ElementType ,
215
+ nextOptions : StyledOptions
203
216
) => {
204
- return createStyled ( nextTag , {
217
+ const newStyled = createStyled ( nextTag , {
205
218
...options ,
206
219
...nextOptions ,
207
220
shouldForwardProp : composeShouldForwardProps ( Styled , nextOptions , true )
208
- } ) ( ...styles )
221
+ } )
222
+ return ( newStyled as any ) ( ...styles )
209
223
}
210
224
211
225
return Styled
212
226
}
213
227
}
214
228
215
- export default createStyled
229
+ export default createStyled as CreateStyled
0 commit comments