1
1
import * as React from 'react'
2
2
import {
3
+ EmotionCache ,
3
4
getRegisteredStyles ,
4
5
insertStyles ,
5
- registerStyles
6
+ registerStyles ,
7
+ SerializedStyles
6
8
} from '@emotion/utils'
7
- import { serializeStyles } from '@emotion/serialize'
9
+ import { CSSInterpolation , serializeStyles } from '@emotion/serialize'
8
10
import isDevelopment from '#is-development'
9
11
import { withEmotionCache } from './context'
10
- import { ThemeContext } from './theming'
12
+ import { Theme , ThemeContext } from './theming'
11
13
import { useInsertionEffectAlwaysWithSyncFallback } from '@emotion/use-insertion-effect-with-fallbacks'
12
14
import isBrowser from '#is-browser'
13
15
14
- /*
15
- type ClassNameArg =
16
+ export interface ArrayClassNamesArg extends Array < ClassNamesArg > { }
17
+
18
+ export type ClassNamesArg =
19
+ | undefined
20
+ | null
16
21
| string
17
22
| boolean
18
- | { [key: string]: boolean }
19
- | Array<ClassNameArg>
20
- | null
21
- | void
22
- */
23
+ | { [ className : string ] : boolean | null | undefined }
24
+ | ArrayClassNamesArg
23
25
24
- let classnames = ( args /*: Array<ClassNameArg> */ ) /* : string */ => {
26
+ let classnames = ( args : ArrayClassNamesArg ) : string => {
25
27
let len = args . length
26
28
let i = 0
27
29
let cls = ''
@@ -69,11 +71,11 @@ let classnames = (args /*: Array<ClassNameArg> */) /*: string */ => {
69
71
return cls
70
72
}
71
73
function merge (
72
- registered /*: Object */ ,
73
- css /*: (...args: Array<any>) => string */ ,
74
- className /* : string */
74
+ registered : EmotionCache [ 'registered' ] ,
75
+ css : ClassNamesContent [ 'css' ] ,
76
+ className : string
75
77
) {
76
- const registeredStyles = [ ]
78
+ const registeredStyles : string [ ] = [ ]
77
79
78
80
const rawClassName = getRegisteredStyles (
79
81
registered ,
@@ -87,7 +89,13 @@ function merge(
87
89
return rawClassName + css ( registeredStyles )
88
90
}
89
91
90
- const Insertion = ( { cache, serializedArr } ) => {
92
+ const Insertion = ( {
93
+ cache,
94
+ serializedArr
95
+ } : {
96
+ cache : EmotionCache
97
+ serializedArr : SerializedStyles [ ]
98
+ } ) => {
91
99
let rules = useInsertionEffectAlwaysWithSyncFallback ( ( ) => {
92
100
let rules = ''
93
101
for ( let i = 0 ; i < serializedArr . length ; i ++ ) {
@@ -101,14 +109,14 @@ const Insertion = ({ cache, serializedArr }) => {
101
109
}
102
110
} )
103
111
104
- if ( ! isBrowser && rules . length !== 0 ) {
112
+ if ( ! isBrowser && rules ! . length !== 0 ) {
105
113
return (
106
114
< style
107
115
{ ...{
108
116
[ `data-emotion` ] : `${ cache . key } ${ serializedArr
109
117
. map ( serialized => serialized . name )
110
118
. join ( ' ' ) } `,
111
- dangerouslySetInnerHTML : { __html : rules } ,
119
+ dangerouslySetInnerHTML : { __html : rules ! } ,
112
120
nonce : cache . sheet . nonce
113
121
} }
114
122
/>
@@ -117,21 +125,23 @@ const Insertion = ({ cache, serializedArr }) => {
117
125
return null
118
126
}
119
127
120
- /*
121
- type Props = {
122
- children: ({
123
- css: (...args: any) => string,
124
- cx: (...args: Array<ClassNameArg>) => string,
125
- theme: Object
126
- }) => React.Node
127
- } */
128
+ export interface ClassNamesContent {
129
+ css ( template : TemplateStringsArray , ...args : Array < CSSInterpolation > ) : string
130
+ css ( ...args : Array < CSSInterpolation > ) : string
131
+ cx ( ...args : Array < ClassNamesArg > ) : string
132
+ theme : Theme
133
+ }
134
+
135
+ export interface ClassNamesProps {
136
+ children ( content : ClassNamesContent ) : React . ReactNode
137
+ }
128
138
129
- export const ClassNames /*: React.AbstractComponent<Props> */ =
130
- /* #__PURE__ */ withEmotionCache ( ( props , cache ) => {
139
+ export const ClassNames = /* #__PURE__ */ withEmotionCache < ClassNamesProps > (
140
+ ( props , cache ) => {
131
141
let hasRendered = false
132
- let serializedArr = [ ]
142
+ let serializedArr : SerializedStyles [ ] = [ ]
133
143
134
- let css = ( ...args /*: Array<any> */ ) => {
144
+ let css : ClassNamesContent [ 'css' ] = ( ...args ) => {
135
145
if ( hasRendered && isDevelopment ) {
136
146
throw new Error ( 'css can only be used during render' )
137
147
}
@@ -142,7 +152,7 @@ export const ClassNames /*: React.AbstractComponent<Props>*/ =
142
152
registerStyles ( cache , serialized , false )
143
153
return `${ cache . key } -${ serialized . name } `
144
154
}
145
- let cx = ( ...args /* : Array<ClassNameArg>*/ ) => {
155
+ let cx = ( ...args : Array < ClassNamesArg > ) => {
146
156
if ( hasRendered && isDevelopment ) {
147
157
throw new Error ( 'cx can only be used during render' )
148
158
}
@@ -162,7 +172,8 @@ export const ClassNames /*: React.AbstractComponent<Props>*/ =
162
172
{ ele }
163
173
</ >
164
174
)
165
- } )
175
+ }
176
+ )
166
177
167
178
if ( isDevelopment ) {
168
179
ClassNames . displayName = 'EmotionClassNames'
0 commit comments