Skip to content

Commit 491a7d8

Browse files
zombieJclaude
andcommitted
feat: support function type for extraCssVarPrefixCls to dynamically resolve prefixes
Allow extraCssVarPrefixCls to accept a function that receives { prefixCls, rootCls } and returns a string array. This enables dynamic generation of CSS variable scope names based on runtime values. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent d68e958 commit 491a7d8

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed

src/util/genStyleUtils.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,15 +166,22 @@ function genStyleUtils<
166166
/**
167167
* Extra prefixCls to inject CSS variables.
168168
* 为额外的 prefixCls 注入 CSS 变量(不注入样式)。
169+
* 支持传入 function 类型,动态生成 prefixCls 数组。
169170
*
170171
* @example
171172
* ```typescript
172173
* {
173174
* extraCssVarPrefixCls: ['my-comp-compact', 'my-comp-large']
174175
* }
176+
* // 或者
177+
* {
178+
* extraCssVarPrefixCls: ({ prefixCls, rootCls }) => [`${prefixCls}-container`]
179+
* }
175180
* ```
176181
*/
177-
extraCssVarPrefixCls?: string[];
182+
extraCssVarPrefixCls?:
183+
| string[]
184+
| ((info: { prefixCls: string; rootCls: string }) => string[]);
178185
},
179186
) {
180187
const componentName = Array.isArray(component) ? component[0] : component;
@@ -212,8 +219,16 @@ function genStyleUtils<
212219

213220
return (prefixCls: string, rootCls: string = prefixCls) => {
214221
const hashId = useStyle(prefixCls, rootCls);
222+
223+
// 支持 function 类型,动态解析 extraCssVarPrefixCls
224+
const extraPrefixCls = options?.extraCssVarPrefixCls;
225+
const resolvedExtraPrefixCls =
226+
typeof extraPrefixCls === 'function'
227+
? extraPrefixCls({ prefixCls, rootCls })
228+
: extraPrefixCls;
229+
215230
const cssVarCls = useCSSVar(
216-
options?.extraCssVarPrefixCls?.length ? [rootCls, ...options.extraCssVarPrefixCls] : rootCls,
231+
resolvedExtraPrefixCls?.length ? [rootCls, ...resolvedExtraPrefixCls] : rootCls,
217232
);
218233

219234
return [hashId, cssVarCls] as const;

tests/extraCssVarPrefixCls.test.tsx

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,45 @@ describe('extraCssVarPrefixCls', () => {
9393
expect(totalStyle).toContain('.custom-a');
9494
expect(totalStyle).toContain('.custom-b');
9595
});
96+
97+
it('should support function type for extraCssVarPrefixCls', () => {
98+
const hooks = genStyleHooks(
99+
'TestComponent',
100+
(token) => ({
101+
[`${token.componentCls}`]: {
102+
color: token.colorPrimary,
103+
fontSize: token.fontSize,
104+
},
105+
}),
106+
() => ({
107+
colorPrimary: '#ff0000',
108+
fontSize: 16,
109+
}),
110+
{
111+
extraCssVarPrefixCls: ({ prefixCls, rootCls }) => [
112+
`${prefixCls}-container`,
113+
`${rootCls}-wrapper`,
114+
],
115+
},
116+
);
117+
118+
const TestComponent = () => {
119+
const [hashId, cssVarCls] = hooks('custom-list', 'custom');
120+
const className = [hashId, cssVarCls].filter(Boolean).join(' ');
121+
return <div className={className}>{hashId}</div>;
122+
};
123+
124+
render(
125+
<StyleProvider cache={createCache()}>
126+
<TestComponent />
127+
</StyleProvider>,
128+
);
129+
130+
const totalStyle = Array.from(document.querySelectorAll('style'))
131+
.map((el) => el.textContent)
132+
.join('\n');
133+
134+
expect(totalStyle).toContain('.custom-list-container');
135+
expect(totalStyle).toContain('.custom-wrapper');
136+
});
96137
});

0 commit comments

Comments
 (0)