Skip to content

Commit fcb0e28

Browse files
authored
feat: rm style tokenKey (#213)
* feat: rm style tokenKey * test: fix test cases * chore: fix tsc * feat: remove wrapSSR * docs: update examples
1 parent 296d730 commit fcb0e28

20 files changed

+223
-476
lines changed

docs/demo/auto-clear.md

Lines changed: 0 additions & 8 deletions
This file was deleted.

docs/examples/auto-clear.tsx

Lines changed: 0 additions & 37 deletions
This file was deleted.

docs/examples/components/Button.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ const Button = ({ className, type, ...restProps }: ButtonProps) => {
111111
const ghostCls = `${prefixCls}-ghost`;
112112

113113
// 全局注册,内部会做缓存优化
114-
const wrapSSR = useStyleRegister(
114+
useStyleRegister(
115115
{ theme, token, hashId, path: [prefixCls] },
116116
() => [
117117
genDefaultButtonStyle(defaultCls, token),
@@ -128,11 +128,11 @@ const Button = ({ className, type, ...restProps }: ButtonProps) => {
128128
} as any
129129
)[type as any] || defaultCls;
130130

131-
return wrapSSR(
131+
return (
132132
<button
133133
className={classNames(prefixCls, typeCls, hashId, className, cssVarKey)}
134134
{...restProps}
135-
/>,
135+
/>
136136
);
137137
};
138138

docs/examples/components/Spin.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@ const Spin = ({ className, ...restProps }: SpinProps) => {
4141
const [theme, token, hashId] = useToken();
4242

4343
// 全局注册,内部会做缓存优化
44-
const wrapSSR = useStyleRegister(
44+
useStyleRegister(
4545
{ theme, token, hashId, path: [prefixCls] },
4646
() => [genSpinStyle(prefixCls, token)],
4747
);
4848

49-
return wrapSSR(
50-
<div className={classNames(prefixCls, hashId, className)} {...restProps} />,
49+
return (
50+
<div className={classNames(prefixCls, hashId, className)} {...restProps} />
5151
);
5252
};
5353

docs/examples/components/theme.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,9 @@ export function useToken(): [
7979
[defaultDesignToken, rootDesignToken],
8080
{
8181
salt: typeof hashed === 'string' ? hashed : '',
82-
cssVar: cssVar && {
82+
cssVar: {
8383
prefix: 'rc',
84-
key: cssVar.key,
84+
key: cssVar?.key || 'css-var-root',
8585
unitless: {
8686
lineHeight: true,
8787
},

docs/examples/css-var.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export default function App() {
2323
{show && (
2424
<div>
2525
<DesignTokenContext.Provider
26-
value={{ cssVar: { key: 'default' }, hashed: true }}
26+
value={{ cssVar: { key: 'default' }, hashed: false }}
2727
>
2828
<Button>Default</Button>
2929
<Button type="primary">Primary</Button>
@@ -36,7 +36,7 @@ export default function App() {
3636
value={{
3737
token: { primaryColor: 'green' },
3838
cssVar: { key: 'default2' },
39-
hashed: true,
39+
hashed: false,
4040
}}
4141
>
4242
<Button>Default</Button>

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
"@ctrl/tinycolor": "^3.4.0",
5454
"@rc-component/father-plugin": "^1.0.1",
5555
"@testing-library/jest-dom": "^5.16.3",
56-
"@testing-library/react": "^13.0.0",
56+
"@testing-library/react": "^14.0.0",
5757
"@types/classnames": "^2.2.9",
5858
"@types/enzyme": "^3.10.11",
5959
"@types/react": "^18.0.0",

src/StyleContext.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ export function createCache() {
5353
export type HashPriority = 'low' | 'high';
5454

5555
export interface StyleContextProps {
56-
autoClear?: boolean;
5756
/** @private Test only. Not work in production. */
5857
mock?: 'server' | 'client';
5958
/**

src/hooks/useCacheToken.tsx

Lines changed: 38 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ export interface Option<DerivativeToken, DesignToken> {
5555
/**
5656
* Transform token to css variables.
5757
*/
58-
cssVar?: {
58+
cssVar: {
5959
/** Prefix for css variables */
6060
prefix?: string;
6161
/** Tokens that should not be appended with unit */
@@ -65,7 +65,7 @@ export interface Option<DerivativeToken, DesignToken> {
6565
/** Tokens that preserves origin value */
6666
preserve?: Record<string, boolean>;
6767
/** Key for current theme. Useful for customizing and should be unique */
68-
key?: string;
68+
key: string;
6969
};
7070
}
7171

@@ -86,7 +86,7 @@ function removeStyleTags(key: string, instanceId: string) {
8686
}
8787
}
8888

89-
const TOKEN_THRESHOLD = 0;
89+
const TOKEN_THRESHOLD = -1;
9090

9191
// Remove will check current keys first
9292
function cleanTokenStyle(tokenKey: string, instanceId: string) {
@@ -136,9 +136,9 @@ export const getComputedToken = <
136136
export const TOKEN_PREFIX = 'token';
137137

138138
type TokenCacheValue<DerivativeToken> = [
139-
token: DerivativeToken & { _tokenKey: string; _themeKey: string },
139+
token: DerivativeToken,
140140
hashId: string,
141-
realToken: DerivativeToken & { _tokenKey: string },
141+
realToken: DerivativeToken,
142142
cssVarStr: string,
143143
cssVarKey: string,
144144
];
@@ -156,7 +156,7 @@ export default function useCacheToken<
156156
>(
157157
theme: Theme<any, any>,
158158
tokens: Partial<DesignToken>[],
159-
option: Option<DerivativeToken, DesignToken> = {},
159+
option: Option<DerivativeToken, DesignToken>,
160160
): TokenCacheValue<DerivativeToken> {
161161
const {
162162
cache: { instanceId },
@@ -182,68 +182,53 @@ export default function useCacheToken<
182182
TOKEN_PREFIX,
183183
[salt, theme.id, tokenStr, overrideTokenStr, cssVarStr],
184184
() => {
185-
let mergedDerivativeToken = compute
185+
const mergedDerivativeToken = compute
186186
? compute(mergedToken, override, theme)
187187
: getComputedToken(mergedToken, override, theme, formatToken);
188188

189189
// Replace token value with css variables
190190
const actualToken = { ...mergedDerivativeToken };
191-
let cssVarsStr = '';
192-
if (!!cssVar) {
193-
[mergedDerivativeToken, cssVarsStr] = transformToken(
194-
mergedDerivativeToken,
195-
cssVar.key!,
196-
{
197-
prefix: cssVar.prefix,
198-
ignore: cssVar.ignore,
199-
unitless: cssVar.unitless,
200-
preserve: cssVar.preserve,
201-
},
202-
);
203-
}
191+
const [tokenWithCssVar, cssVarsStr] = transformToken(
192+
mergedDerivativeToken,
193+
cssVar.key,
194+
{
195+
prefix: cssVar.prefix,
196+
ignore: cssVar.ignore,
197+
unitless: cssVar.unitless,
198+
preserve: cssVar.preserve,
199+
},
200+
) as [any, string];
204201

205202
// Optimize for `useStyleRegister` performance
206-
const tokenKey = token2key(mergedDerivativeToken, salt);
207-
mergedDerivativeToken._tokenKey = tokenKey;
208-
actualToken._tokenKey = token2key(actualToken, salt);
203+
const mergedSalt = `${salt}_${cssVar.prefix || ''}`;
204+
actualToken._tokenKey = token2key(actualToken, mergedSalt);
209205

210-
const themeKey = cssVar?.key ?? tokenKey;
211-
mergedDerivativeToken._themeKey = themeKey;
206+
const themeKey = cssVar.key;
212207
recordCleanToken(themeKey);
213208

214-
const hashId = `${hashPrefix}-${hash(tokenKey)}`;
215-
mergedDerivativeToken._hashId = hashId; // Not used
209+
const hashId = `${hashPrefix}-${hash(mergedSalt)}`;
216210

217-
return [
218-
mergedDerivativeToken,
219-
hashId,
220-
actualToken,
221-
cssVarsStr,
222-
cssVar?.key || '',
223-
];
211+
return [tokenWithCssVar, hashId, actualToken, cssVarsStr, cssVar.key];
224212
},
225-
(cache) => {
213+
([, , , , themeKey]) => {
226214
// Remove token will remove all related style
227-
cleanTokenStyle(cache[0]._themeKey, instanceId);
215+
cleanTokenStyle(themeKey, instanceId);
228216
},
229-
([token, , , cssVarsStr]) => {
230-
if (cssVar && cssVarsStr) {
231-
const style = updateCSS(
232-
cssVarsStr,
233-
hash(`css-variables-${token._themeKey}`),
234-
{
235-
mark: ATTR_MARK,
236-
prepend: 'queue',
237-
attachTo: container,
238-
priority: -999,
239-
},
240-
);
241-
242-
(style as any)[CSS_IN_JS_INSTANCE] = instanceId;
243-
244-
// Used for `useCacheToken` to remove on batch when token removed
245-
style.setAttribute(ATTR_TOKEN, token._themeKey);
217+
([, , , cssVarsStr, themeKey]) => {
218+
if (!cssVarsStr) {
219+
return;
246220
}
221+
const style = updateCSS(cssVarsStr, hash(`css-var-${themeKey}`), {
222+
mark: ATTR_MARK,
223+
prepend: 'queue',
224+
attachTo: container,
225+
priority: -999,
226+
});
227+
228+
(style as any)[CSS_IN_JS_INSTANCE] = instanceId;
229+
230+
// Used for `useCacheToken` to remove on batch when token removed
231+
style.setAttribute(ATTR_TOKEN, themeKey);
247232
},
248233
);
249234

0 commit comments

Comments
 (0)