Skip to content

Commit 5fdff89

Browse files
authored
feat: cssVar support preserve token (#161)
* feat: cssVar support preserve token * feat: use computed tokenKey for hash * test: add test case
1 parent defcde1 commit 5fdff89

File tree

5 files changed

+32
-13
lines changed

5 files changed

+32
-13
lines changed

src/hooks/useCSSVarRegister.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ import { uniqueHash } from './useStyleRegister';
1414

1515
export const CSS_VAR_PREFIX = 'cssVar';
1616

17-
type CSSVarCacheValue<T> = [
18-
cssVarToken: TokenWithCSSVar<T>,
17+
type CSSVarCacheValue<V, T extends Record<string, V> = Record<string, V>> = [
18+
cssVarToken: TokenWithCSSVar<V, T>,
1919
cssVarStr: string,
2020
styleId: string,
2121
cssVarKey: string,
@@ -42,12 +42,12 @@ const useCSSVarRegister = <V, T extends Record<string, V>>(
4242

4343
const stylePath = [...config.path, key, scope, tokenKey];
4444

45-
const cache = useGlobalCache<CSSVarCacheValue<T>>(
45+
const cache = useGlobalCache<CSSVarCacheValue<V, T>>(
4646
CSS_VAR_PREFIX,
4747
stylePath,
4848
() => {
4949
const originToken = fn();
50-
const [mergedToken, cssVarsStr] = transformToken(originToken, key, {
50+
const [mergedToken, cssVarsStr] = transformToken<V, T>(originToken, key, {
5151
prefix,
5252
unitless,
5353
ignore,

src/hooks/useCacheToken.tsx

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ export interface Option<DerivativeToken, DesignToken> {
6262
unitless?: Record<string, boolean>;
6363
/** Tokens that should not be transformed to css variables */
6464
ignore?: Record<string, boolean>;
65+
/** Tokens that preserves origin value */
66+
preserve?: Record<string, boolean>;
6567
/** Key for current theme. Useful for customizing and should be unique */
6668
key?: string;
6769
};
@@ -195,6 +197,7 @@ export default function useCacheToken<
195197
prefix: cssVar.prefix,
196198
ignore: cssVar.ignore,
197199
unitless: cssVar.unitless,
200+
preserve: cssVar.preserve,
198201
},
199202
);
200203
}
@@ -208,9 +211,7 @@ export default function useCacheToken<
208211
mergedDerivativeToken._themeKey = themeKey;
209212
recordCleanToken(themeKey);
210213

211-
const hashId = cssVar
212-
? `${hashPrefix}-${hash(`${salt}${cssVar.prefix ?? ''}`)}`
213-
: `${hashPrefix}-${hash(tokenKey)}`;
214+
const hashId = `${hashPrefix}-${hash(tokenKey)}`;
214215
mergedDerivativeToken._hashId = hashId; // Not used
215216

216217
return [

src/util/css-variables.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,11 @@ export const serializeCSSVar = <T extends Record<string, any>>(
2323
.join('')}}`;
2424
};
2525

26-
export type TokenWithCSSVar<T> = {
27-
[key in keyof T]?: string;
26+
export type TokenWithCSSVar<
27+
V,
28+
T extends Record<string, V> = Record<string, V>,
29+
> = {
30+
[key in keyof T]?: string | V;
2831
};
2932

3033
export const transformToken = <
@@ -41,13 +44,18 @@ export const transformToken = <
4144
unitless?: {
4245
[key in keyof T]?: boolean;
4346
};
47+
preserve?: {
48+
[key in keyof T]?: boolean;
49+
};
4450
scope?: string;
4551
},
46-
): [TokenWithCSSVar<T>, string] => {
52+
): [TokenWithCSSVar<V, T>, string] => {
4753
const cssVars: Record<string, string> = {};
48-
const result: TokenWithCSSVar<T> = {};
54+
const result: TokenWithCSSVar<V, T> = {};
4955
Object.entries(token).forEach(([key, value]) => {
50-
if (
56+
if (config?.preserve?.[key]) {
57+
result[key as keyof T] = value;
58+
} else if (
5159
(typeof value === 'string' || typeof value === 'number') &&
5260
!config?.ignore?.[key]
5361
) {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
22

3-
exports[`CSS Variables > support ssr 1`] = `"<style data-rc-order=\\"prependQueue\\" data-rc-priority=\\"-999\\" data-token-hash=\\"apple\\" data-css-hash=\\"8y225z\\">.apple{--rc-primary-color:#1890ff;--rc-text-color:#333333;--rc-border-radius:2px;--rc-border-color:black;--rc-border-width:1px;--rc-line-height:1.5;--rc-primary-color-disabled:rgba(24, 144, 255, 0.5);}</style><style data-rc-order=\\"prependQueue\\" data-rc-priority=\\"-999\\" data-token-hash=\\"apple\\" data-css-hash=\\"j9jql8\\">.apple.box{--rc-box-box-color:#5c21ff;}</style><style data-rc-order=\\"prependQueue\\" data-rc-priority=\\"0\\" data-token-hash=\\"302na3\\" data-css-hash=\\"nq18cu\\">:where(.css-dev-only-do-not-override-xcmczj).box{line-height:var(--rc-line-height);border:var(--rc-border-width) solid var(--rc-border-color);color:var(--rc-box-box-color);background-color:var(--rc-primary-color);}</style><style data-ant-cssinjs-cache-path=\\"data-ant-cssinjs-cache-path\\">.data-ant-cssinjs-cache-path{content:\\"302na3|Box:nq18cu\\";}</style>"`;
3+
exports[`CSS Variables > support ssr 1`] = `"<style data-rc-order=\\"prependQueue\\" data-rc-priority=\\"-999\\" data-token-hash=\\"apple\\" data-css-hash=\\"1iuujbx\\">.apple{--rc-primary-color:#1890ff;--rc-text-color:#333333;--rc-border-radius:2px;--rc-border-color:black;--rc-border-width:1px;--rc-line-height:1.5;--rc-primary-color-disabled:rgba(24, 144, 255, 0.5);}</style><style data-rc-order=\\"prependQueue\\" data-rc-priority=\\"-999\\" data-token-hash=\\"apple\\" data-css-hash=\\"fjuhit\\">.apple.box{--rc-box-box-color:#5c21ff;}</style><style data-rc-order=\\"prependQueue\\" data-rc-priority=\\"0\\" data-token-hash=\\"lt4t7l\\" data-css-hash=\\"1pik734\\">:where(.css-dev-only-do-not-override-tkc50g).box{line-height:var(--rc-line-height);border:var(--rc-border-width) solid var(--rc-border-color);color:var(--rc-box-box-color);background-color:var(--rc-primary-color);content:\\"800\\";}</style><style data-ant-cssinjs-cache-path=\\"data-ant-cssinjs-cache-path\\">.data-ant-cssinjs-cache-path{content:\\"lt4t7l|Box:1pik734\\";}</style>"`;

tests/css-variables.spec.tsx

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ export interface DesignToken {
2525

2626
lineHeight: number;
2727
lineHeightBase: number;
28+
29+
smallScreen: number;
2830
}
2931

3032
export interface DerivativeToken extends DesignToken {
@@ -41,6 +43,8 @@ const defaultDesignToken: DesignToken = {
4143

4244
lineHeight: 1.5,
4345
lineHeightBase: 1.5,
46+
47+
smallScreen: 800,
4448
};
4549

4650
// 模拟推导过程
@@ -119,6 +123,9 @@ function useToken(): [DerivativeToken, string, string, DerivativeToken] {
119123
ignore: {
120124
lineHeightBase: true,
121125
},
126+
preserve: {
127+
smallScreen: true,
128+
},
122129
},
123130
});
124131
return [token, hashed ? hashId : '', cssVar?.key || '', realToken];
@@ -168,6 +175,7 @@ const useStyle = () => {
168175
}`,
169176
color: mergedToken.boxColor,
170177
backgroundColor: mergedToken.primaryColor,
178+
content: `"${mergedToken.smallScreen}"`,
171179
},
172180
};
173181
},
@@ -210,11 +218,13 @@ describe('CSS Variables', () => {
210218
expect(styles[0].textContent).toContain('.apple{');
211219
expect(styles[0].textContent).toContain('--rc-line-height:1.5;');
212220
expect(styles[0].textContent).not.toContain('--rc-line-height-base:1.5;');
221+
expect(styles[0].textContent).not.toContain('--rc-small-screen:800;');
213222
expect(styles[1].textContent).toContain('--rc-box-box-color:#5c21ff');
214223
expect(styles[1].textContent).toContain('.apple.box{');
215224
expect(styles[2].textContent).toContain(
216225
'line-height:var(--rc-line-height);',
217226
);
227+
expect(styles[2].textContent).toContain('content:"800"');
218228
expect(box).toHaveClass('apple');
219229
expect(box).toHaveStyle({
220230
'--rc-line-height': '1.5',

0 commit comments

Comments
 (0)