1
1
/* eslint-disable @typescript-eslint/no-explicit-any */
2
2
/**
3
- * This file contains an runtime version of `styled` component. Responsibilities of the component are:
3
+ * This file contains a runtime version of `styled` component. Responsibilities of the component are:
4
4
* - returns ReactElement based on HTML tag used with `styled` or custom React Component
5
5
* - injects classNames for the returned component
6
6
* - injects CSS variables used to define dynamic styles based on props
7
7
*/
8
8
import validAttr from '@emotion/is-prop-valid' ;
9
- import React from 'react' ;
9
+ import { createElement , forwardRef } from 'react' ;
10
+ import type React from 'react' ;
10
11
11
12
import { cx } from '@linaria/core' ;
12
13
import type { CSSProperties } from '@linaria/core' ;
@@ -106,8 +107,26 @@ interface IProps {
106
107
style ?: Record < string , string > ;
107
108
}
108
109
110
+ // React <19
111
+ declare global {
112
+ // eslint-disable-next-line @typescript-eslint/no-namespace
113
+ namespace JSX {
114
+ interface IntrinsicElements { }
115
+ }
116
+ }
117
+
118
+ // React >=19
119
+ declare module 'react' {
120
+ // eslint-disable-next-line @typescript-eslint/no-namespace
121
+ namespace JSX {
122
+ interface IntrinsicElements { }
123
+ }
124
+ }
125
+
109
126
let idx = 0 ;
110
127
128
+ type IntrinsicElements = React . JSX . IntrinsicElements & JSX . IntrinsicElements ;
129
+
111
130
// Components with props are not allowed
112
131
function styled (
113
132
componentWithStyle : ( ) => any
@@ -128,7 +147,7 @@ function styled<
128
147
> (
129
148
componentWithoutStyle : TConstructor & Component < TProps >
130
149
) : ComponentStyledTagWithoutInterpolation < TConstructor > ;
131
- function styled < TName extends keyof React . JSX . IntrinsicElements > (
150
+ function styled < TName extends keyof IntrinsicElements > (
132
151
tag : TName
133
152
) : HtmlStyledTag < TName > ;
134
153
function styled (
@@ -211,13 +230,13 @@ function styled(tag: any): any {
211
230
// Otherwise the styles from the underlying component will be ignored
212
231
filteredProps . as = component ;
213
232
214
- return React . createElement ( tag , filteredProps ) ;
233
+ return createElement ( tag , filteredProps ) ;
215
234
}
216
- return React . createElement ( component , filteredProps ) ;
235
+ return createElement ( component , filteredProps ) ;
217
236
} ;
218
237
219
- const Result = React . forwardRef
220
- ? React . forwardRef ( render )
238
+ const Result = forwardRef
239
+ ? forwardRef ( render )
221
240
: // React.forwardRef won't available on older React versions and in Preact
222
241
// Fallback to a innerRef prop in that case
223
242
( props : any ) => {
@@ -244,7 +263,7 @@ export type StyledComponent<T> = WYWEvalMeta &
244
263
245
264
type StaticPlaceholder = string | number | CSSProperties | WYWEvalMeta ;
246
265
247
- export type HtmlStyledTag < TName extends keyof React . JSX . IntrinsicElements > = <
266
+ export type HtmlStyledTag < TName extends keyof IntrinsicElements > = <
248
267
TAdditionalProps = Record < never , unknown > ,
249
268
> (
250
269
strings : TemplateStringsArray ,
@@ -253,10 +272,10 @@ export type HtmlStyledTag<TName extends keyof React.JSX.IntrinsicElements> = <
253
272
| ( (
254
273
// Without Omit here TS tries to infer TAdditionalProps
255
274
// from a component passed for interpolation
256
- props : React . JSX . IntrinsicElements [ TName ] & Omit < TAdditionalProps , never >
275
+ props : IntrinsicElements [ TName ] & Omit < TAdditionalProps , never >
257
276
) => string | number )
258
277
>
259
- ) => StyledComponent < React . JSX . IntrinsicElements [ TName ] & TAdditionalProps > ;
278
+ ) => StyledComponent < IntrinsicElements [ TName ] & TAdditionalProps > ;
260
279
261
280
type ComponentStyledTagWithoutInterpolation < TOrigCmp > = (
262
281
strings : TemplateStringsArray ,
@@ -278,14 +297,14 @@ type ComponentStyledTagWithInterpolation<TTrgProps, TOrigCmp> = <OwnProps = {}>(
278
297
: StyledComponent < OwnProps & TTrgProps > ;
279
298
280
299
export type StyledJSXIntrinsics = {
281
- readonly [ P in keyof React . JSX . IntrinsicElements ] : HtmlStyledTag < P > ;
300
+ readonly [ P in keyof IntrinsicElements ] : HtmlStyledTag < P > ;
282
301
} ;
283
302
284
303
export type Styled = typeof styled & StyledJSXIntrinsics ;
285
304
286
305
export default ( process . env . NODE_ENV !== 'production'
287
306
? new Proxy ( styled , {
288
- get ( o , prop : keyof React . JSX . IntrinsicElements ) {
307
+ get ( o , prop : keyof IntrinsicElements ) {
289
308
return o ( prop ) ;
290
309
} ,
291
310
} )
0 commit comments