Skip to content

Commit ed1d26d

Browse files
authored
feat: only cssvar mode (#31)
* feat: only cssvar mode * chore: update * chore: cov
1 parent 4747423 commit ed1d26d

File tree

3 files changed

+32
-51
lines changed

3 files changed

+32
-51
lines changed

src/interface/index.ts

-2
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,3 @@ export type {
77
ComponentTokenKey,
88
GlobalToken,
99
} from './components';
10-
11-
export type UseComponentStyleResult = [(node: React.ReactNode) => React.ReactElement, string];

src/util/genStyleUtils.tsx src/util/genStyleUtils.ts

+27-46
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react';
1+
import type React from 'react';
22

33
import type { CSSInterpolation, CSSObject, TokenType } from '@ant-design/cssinjs';
44

@@ -9,7 +9,6 @@ import type {
99
GlobalTokenWithComponent,
1010
TokenMap,
1111
TokenMapKey,
12-
UseComponentStyleResult,
1312
} from '../interface';
1413

1514
import type AbstractCalculator from './calc/calculator';
@@ -197,10 +196,10 @@ function genStyleUtils<
197196
const useCSSVar = genCSSVarRegister(componentName, getDefaultToken, mergedOptions);
198197

199198
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);
202201

203-
return [wrapCSSVar, hashId, cssVarCls] as const;
202+
return [hashId, cssVarCls] as const;
204203
};
205204
}
206205

@@ -218,9 +217,11 @@ function genStyleUtils<
218217
prefixToken: (key: string) => string;
219218
},
220219
) {
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();
222224

223-
const CSSVarRegister: React.FC<Readonly<CSSVarRegisterProps>> = ({ rootCls, cssVar = {} }) => {
224225
const { realToken } = useToken();
225226
useCSSVarRegister(
226227
{
@@ -246,34 +247,18 @@ function genStyleUtils<
246247
deprecatedTokens: options?.deprecatedTokens,
247248
},
248249
);
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+
}
253256
return componentToken;
254257
},
255258
);
256-
return null;
257-
};
258-
259-
const useCSSVar = (rootCls: string) => {
260-
const { cssVar } = useToken();
261259

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;
274261
};
275-
276-
return useCSSVar;
277262
}
278263

279264
function genComponentStyleHook<C extends TokenMapKey<CompTokenMap>>(
@@ -312,25 +297,23 @@ function genStyleUtils<
312297
};
313298

314299
// Return new style hook
315-
return (prefixCls: string, rootCls: string = prefixCls): UseComponentStyleResult => {
300+
return (prefixCls: string, rootCls: string = prefixCls): string => {
316301
const { theme, realToken, hashId, token, cssVar } = useToken();
317302

318303
const { rootPrefixCls, iconPrefixCls } = usePrefix();
319304
const csp = useCSP();
320305

321-
const type = cssVar ? 'css' : 'js';
306+
const type = 'css';
322307

323308
// Use unique memo to share the result across all instances
324309
const calc = useUniqueMemo(() => {
325310
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+
});
334317

335318
return genCalc(type, unitlessCssVar);
336319
}, [type, component, cssVar?.prefix]);
@@ -359,7 +342,7 @@ function genStyleUtils<
359342
);
360343
}
361344

362-
const wrapSSR = useStyleRegister(
345+
useStyleRegister(
363346
{ ...sharedConfig, path: [concatComponent, prefixCls, iconPrefixCls] },
364347
() => {
365348
if (options.injectStyle === false) {
@@ -382,7 +365,7 @@ function genStyleUtils<
382365
{ deprecatedTokens: options.deprecatedTokens },
383366
);
384367

385-
if (cssVar && defaultComponentToken && typeof defaultComponentToken === 'object') {
368+
if (defaultComponentToken && typeof defaultComponentToken === 'object') {
386369
Object.keys(defaultComponentToken).forEach((key) => {
387370
defaultComponentToken[key] = `var(${token2CSSVar(
388371
key,
@@ -398,12 +381,10 @@ function genStyleUtils<
398381
iconCls: `.${iconPrefixCls}`,
399382
antCls: `.${rootPrefixCls}`,
400383
calc,
401-
// @ts-ignore
402384
max,
403-
// @ts-ignore
404385
min,
405386
},
406-
cssVar ? defaultComponentToken : componentToken,
387+
defaultComponentToken,
407388
);
408389

409390
const styleInterpolation = styleFn(mergedToken, {
@@ -421,7 +402,7 @@ function genStyleUtils<
421402
},
422403
);
423404

424-
return [wrapSSR, hashId];
405+
return hashId;
425406
};
426407
}
427408

tests/genStyleUtils.test.tsx

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React from 'react';
22
import { render, renderHook } from '@testing-library/react';
33

44
import { genStyleUtils } from '../src';
5-
import type { CSSVarRegisterProps, SubStyleComponentProps } from '../src/util/genStyleUtils';
5+
import type { CSSVarRegisterProps, SubStyleComponentProps } from '@/util/genStyleUtils';
66
import { createCache, StyleProvider } from '@ant-design/cssinjs';
77

88
interface TestCompTokenMap {
@@ -46,7 +46,9 @@ describe('genStyleUtils', () => {
4646
it('should generate style hooks', () => {
4747
const component = 'TestComponent';
4848
const styleFn = jest.fn();
49-
const getDefaultToken = jest.fn();
49+
const getDefaultToken = {
50+
mockCompToken: 'mock'
51+
};
5052
const hooks = genStyleHooks(component, styleFn, getDefaultToken);
5153

5254
expect(hooks).toBeInstanceOf(Function);
@@ -55,7 +57,7 @@ describe('genStyleUtils', () => {
5557
result: { current },
5658
} = renderHook(() => hooks('test-prefix'));
5759
expect(current).toBeInstanceOf(Array);
58-
expect(current).toHaveLength(3);
60+
expect(current).toHaveLength(2);
5961
});
6062
});
6163

0 commit comments

Comments
 (0)