1
- import { updateCSS } from '../../../vc-util/Dom/dynamicCSS' ;
1
+ import { removeCSS , updateCSS } from '../../../vc-util/Dom/dynamicCSS' ;
2
2
import { ATTR_MARK , ATTR_TOKEN , CSS_IN_JS_INSTANCE , useStyleInject } from '../StyleContext' ;
3
- import { isClientSide , token2key , toStyleStr } from '../util' ;
3
+ import { isClientSide , toStyleStr } from '../util' ;
4
4
import type { TokenWithCSSVar } from '../util/css-variables' ;
5
5
import { transformToken } from '../util/css-variables' ;
6
6
import type { ExtractStyle } from './useGlobalCache' ;
@@ -14,50 +14,10 @@ export const CSS_VAR_PREFIX = 'cssVar';
14
14
type CSSVarCacheValue < V , T extends Record < string , V > = Record < string , V > > = [
15
15
cssVarToken : TokenWithCSSVar < V , T > ,
16
16
cssVarStr : string ,
17
- tokenKey : string ,
18
17
styleId : string ,
19
18
cssVarKey : string ,
20
19
] ;
21
20
22
- const tokenKeys = new Map < string , number > ( ) ;
23
- function recordCleanToken ( tokenKey : string ) {
24
- tokenKeys . set ( tokenKey , ( tokenKeys . get ( tokenKey ) || 0 ) + 1 ) ;
25
- }
26
-
27
- function removeStyleTags ( key : string , instanceId : string ) {
28
- if ( typeof document !== 'undefined' ) {
29
- const styles = document . querySelectorAll ( `style[${ ATTR_MARK } ="${ key } "]` ) ;
30
-
31
- styles . forEach ( style => {
32
- if ( ( style as any ) [ CSS_IN_JS_INSTANCE ] === instanceId ) {
33
- style . parentNode ?. removeChild ( style ) ;
34
- }
35
- } ) ;
36
- }
37
- }
38
-
39
- const TOKEN_THRESHOLD = 0 ;
40
-
41
- // Remove will check current keys first
42
- function cleanTokenStyle ( tokenKey : string , instanceId : string ) {
43
- tokenKeys . set ( tokenKey , ( tokenKeys . get ( tokenKey ) || 0 ) - 1 ) ;
44
-
45
- const tokenKeyList = Array . from ( tokenKeys . keys ( ) ) ;
46
- const cleanableKeyList = tokenKeyList . filter ( key => {
47
- const count = tokenKeys . get ( key ) || 0 ;
48
-
49
- return count <= 0 ;
50
- } ) ;
51
-
52
- // Should keep tokens under threshold for not to insert style too often
53
- if ( tokenKeyList . length - cleanableKeyList . length > TOKEN_THRESHOLD ) {
54
- cleanableKeyList . forEach ( key => {
55
- removeStyleTags ( key , instanceId ) ;
56
- tokenKeys . delete ( key ) ;
57
- } ) ;
58
- }
59
- }
60
-
61
21
const useCSSVarRegister = < V , T extends Record < string , V > > (
62
22
config : ComputedRef < {
63
23
path : string [ ] ;
@@ -93,25 +53,20 @@ const useCSSVarRegister = <V, T extends Record<string, V>>(
93
53
scope : config . value . scope || '' ,
94
54
} ) ;
95
55
96
- const tokenKey = token2key ( mergedToken , '' ) ;
97
-
98
56
const styleId = uniqueHash ( stylePath . value , cssVarsStr ) ;
99
-
100
- recordCleanToken ( tokenKey ) ;
101
- return [ mergedToken , cssVarsStr , tokenKey , styleId , config . value . key ] ;
57
+ return [ mergedToken , cssVarsStr , styleId , config . value . key ] ;
102
58
} ,
103
- ( [ , , tokenKey ] ) => {
59
+ ( [ , , styleId ] ) => {
104
60
if ( isClientSide ) {
105
- // Remove token will remove all related style
106
- cleanTokenStyle ( tokenKey , styleContext . value ?. cache ?. instanceId ) ;
61
+ removeCSS ( styleId , { mark : ATTR_MARK } ) ;
107
62
}
108
63
} ,
109
- ( [ , cssVarsStr , tokenKey ] ) => {
64
+ ( [ , cssVarsStr , styleId ] ) => {
110
65
if ( ! cssVarsStr ) {
111
66
return ;
112
67
}
113
68
114
- const style = updateCSS ( cssVarsStr , tokenKey , {
69
+ const style = updateCSS ( cssVarsStr , styleId , {
115
70
mark : ATTR_MARK ,
116
71
prepend : 'queue' ,
117
72
attachTo : styleContext . value . container ,
0 commit comments