1
- import React from 'react' ;
1
+ import type React from 'react' ;
2
2
3
3
import type { CSSInterpolation , CSSObject , TokenType } from '@ant-design/cssinjs' ;
4
4
@@ -9,7 +9,6 @@ import type {
9
9
GlobalTokenWithComponent ,
10
10
TokenMap ,
11
11
TokenMapKey ,
12
- UseComponentStyleResult ,
13
12
} from '../interface' ;
14
13
15
14
import type AbstractCalculator from './calc/calculator' ;
@@ -197,10 +196,10 @@ function genStyleUtils<
197
196
const useCSSVar = genCSSVarRegister ( componentName , getDefaultToken , mergedOptions ) ;
198
197
199
198
return ( prefixCls : string , rootCls : string = prefixCls ) => {
200
- const [ , hashId ] = useStyle ( prefixCls , rootCls ) ;
201
- const [ wrapCSSVar , cssVarCls ] = useCSSVar ( rootCls ) ;
199
+ const hashId = useStyle ( prefixCls , rootCls ) ;
200
+ const cssVarCls = useCSSVar ( rootCls ) ;
202
201
203
- return [ wrapCSSVar , hashId , cssVarCls ] as const ;
202
+ return [ hashId , cssVarCls ] as const ;
204
203
} ;
205
204
}
206
205
@@ -218,9 +217,11 @@ function genStyleUtils<
218
217
prefixToken : ( key : string ) => string ;
219
218
} ,
220
219
) {
221
- const { unitless : compUnitless , injectStyle = true , prefixToken, ignore } = options ;
220
+ const { unitless : compUnitless , prefixToken, ignore } = options ;
221
+
222
+ return ( rootCls : string ) => {
223
+ const { cssVar } = useToken ( ) ;
222
224
223
- const CSSVarRegister : React . FC < Readonly < CSSVarRegisterProps > > = ( { rootCls, cssVar = { } } ) => {
224
225
const { realToken } = useToken ( ) ;
225
226
useCSSVarRegister (
226
227
{
@@ -246,34 +247,18 @@ function genStyleUtils<
246
247
deprecatedTokens : options ?. deprecatedTokens ,
247
248
} ,
248
249
) ;
249
- Object . keys ( defaultToken ) . forEach ( ( key ) => {
250
- componentToken [ prefixToken ( key ) ] = componentToken [ key ] ;
251
- delete componentToken [ key ] ;
252
- } ) ;
250
+ if ( defaultToken ) {
251
+ Object . keys ( defaultToken ) . forEach ( ( key ) => {
252
+ componentToken [ prefixToken ( key ) ] = componentToken [ key ] ;
253
+ delete componentToken [ key ] ;
254
+ } ) ;
255
+ }
253
256
return componentToken ;
254
257
} ,
255
258
) ;
256
- return null ;
257
- } ;
258
-
259
- const useCSSVar = ( rootCls : string ) => {
260
- const { cssVar } = useToken ( ) ;
261
259
262
- return [
263
- ( node : React . ReactElement ) : React . ReactElement =>
264
- injectStyle && cssVar ? (
265
- < >
266
- < CSSVarRegister rootCls = { rootCls } cssVar = { cssVar } component = { component } />
267
- { node }
268
- </ >
269
- ) : (
270
- node
271
- ) ,
272
- cssVar ?. key ,
273
- ] as const ;
260
+ return cssVar ?. key ;
274
261
} ;
275
-
276
- return useCSSVar ;
277
262
}
278
263
279
264
function genComponentStyleHook < C extends TokenMapKey < CompTokenMap > > (
@@ -312,25 +297,23 @@ function genStyleUtils<
312
297
} ;
313
298
314
299
// Return new style hook
315
- return ( prefixCls : string , rootCls : string = prefixCls ) : UseComponentStyleResult => {
300
+ return ( prefixCls : string , rootCls : string = prefixCls ) : string => {
316
301
const { theme, realToken, hashId, token, cssVar } = useToken ( ) ;
317
302
318
303
const { rootPrefixCls, iconPrefixCls } = usePrefix ( ) ;
319
304
const csp = useCSP ( ) ;
320
305
321
- const type = cssVar ? 'css' : 'js ';
306
+ const type = 'css' ;
322
307
323
308
// Use unique memo to share the result across all instances
324
309
const calc = useUniqueMemo ( ( ) => {
325
310
const unitlessCssVar = new Set < string > ( ) ;
326
- if ( cssVar ) {
327
- Object . keys ( options . unitless || { } ) . forEach ( ( key ) => {
328
- // Some component proxy the AliasToken (e.g. Image) and some not (e.g. Modal)
329
- // We should both pass in `unitlessCssVar` to make sure the CSSVar can be unitless.
330
- unitlessCssVar . add ( token2CSSVar ( key , cssVar . prefix ) ) ;
331
- unitlessCssVar . add ( token2CSSVar ( key , getCompVarPrefix ( component , cssVar . prefix ) ) ) ;
332
- } ) ;
333
- }
311
+ Object . keys ( options . unitless || { } ) . forEach ( ( key ) => {
312
+ // Some component proxy the AliasToken (e.g. Image) and some not (e.g. Modal)
313
+ // We should both pass in `unitlessCssVar` to make sure the CSSVar can be unitless.
314
+ unitlessCssVar . add ( token2CSSVar ( key , cssVar . prefix ) ) ;
315
+ unitlessCssVar . add ( token2CSSVar ( key , getCompVarPrefix ( component , cssVar . prefix ) ) ) ;
316
+ } ) ;
334
317
335
318
return genCalc ( type , unitlessCssVar ) ;
336
319
} , [ type , component , cssVar ?. prefix ] ) ;
@@ -359,7 +342,7 @@ function genStyleUtils<
359
342
) ;
360
343
}
361
344
362
- const wrapSSR = useStyleRegister (
345
+ useStyleRegister (
363
346
{ ...sharedConfig , path : [ concatComponent , prefixCls , iconPrefixCls ] } ,
364
347
( ) => {
365
348
if ( options . injectStyle === false ) {
@@ -382,7 +365,7 @@ function genStyleUtils<
382
365
{ deprecatedTokens : options . deprecatedTokens } ,
383
366
) ;
384
367
385
- if ( cssVar && defaultComponentToken && typeof defaultComponentToken === 'object' ) {
368
+ if ( defaultComponentToken && typeof defaultComponentToken === 'object' ) {
386
369
Object . keys ( defaultComponentToken ) . forEach ( ( key ) => {
387
370
defaultComponentToken [ key ] = `var(${ token2CSSVar (
388
371
key ,
@@ -398,12 +381,10 @@ function genStyleUtils<
398
381
iconCls : `.${ iconPrefixCls } ` ,
399
382
antCls : `.${ rootPrefixCls } ` ,
400
383
calc,
401
- // @ts -ignore
402
384
max,
403
- // @ts -ignore
404
385
min,
405
386
} ,
406
- cssVar ? defaultComponentToken : componentToken ,
387
+ defaultComponentToken ,
407
388
) ;
408
389
409
390
const styleInterpolation = styleFn ( mergedToken , {
@@ -421,7 +402,7 @@ function genStyleUtils<
421
402
} ,
422
403
) ;
423
404
424
- return [ wrapSSR , hashId ] ;
405
+ return hashId ;
425
406
} ;
426
407
}
427
408
0 commit comments