Skip to content

Commit 4888842

Browse files
fix(ConfigProvider): use correct cssVar key for icon styles (ant-design#56504)
Co-authored-by: lijianan <574980606@qq.com>
1 parent 1463722 commit 4888842

2 files changed

Lines changed: 40 additions & 2 deletions

File tree

components/config-provider/__tests__/cssinjs.test.tsx

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,33 @@ describe('ConfigProvider.DynamicTheme', () => {
9696
).toBeTruthy();
9797
});
9898

99+
it('icon styles should use cssVar key from theme config', () => {
100+
render(
101+
<ConfigProvider theme={{ cssVar: { key: 'custom-css-var' } }}>
102+
<SmileOutlined />
103+
</ConfigProvider>,
104+
);
105+
106+
const dynamicStyles = Array.from(document.querySelectorAll('style[data-css-hash]'));
107+
108+
// Should have styles with the custom cssVar key
109+
expect(
110+
dynamicStyles.some((style) => {
111+
const { innerHTML } = style;
112+
return innerHTML.includes('.custom-css-var');
113+
}),
114+
).toBeTruthy();
115+
116+
// Should NOT have styles with the default css-var-root key
117+
// This ensures icon styles registered inside ConfigProvider use the correct context
118+
expect(
119+
dynamicStyles.some((style) => {
120+
const { innerHTML } = style;
121+
return innerHTML.includes('.css-var-root');
122+
}),
123+
).toBeFalsy();
124+
});
125+
99126
// eslint-disable-next-line jest/no-disabled-tests
100127
it.skip('layer should affect icon', () => {
101128
render(

components/config-provider/index.tsx

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,18 @@ import type { SizeType } from './SizeContext';
9191
import SizeContext, { SizeContextProvider } from './SizeContext';
9292
import useStyle from './style';
9393

94+
/**
95+
* This component registers icon styles inside the DesignTokenContext.Provider
96+
* so that CSS variables use the correct cssVar key from the theme config.
97+
*/
98+
const IconStyle: React.FC<{ iconPrefixCls: string; csp?: CSPConfig }> = ({
99+
iconPrefixCls,
100+
csp,
101+
}) => {
102+
useStyle(iconPrefixCls, csp);
103+
return null;
104+
};
105+
94106
export type { Variant };
95107

96108
export { Variants };
@@ -440,8 +452,6 @@ const ProviderChildren: React.FC<ProviderChildrenProps> = (props) => {
440452
const iconPrefixCls = customIconPrefixCls || parentContext.iconPrefixCls || defaultIconPrefixCls;
441453
const csp = customCsp || parentContext.csp;
442454

443-
useStyle(iconPrefixCls, csp);
444-
445455
const mergedTheme = useTheme(theme, parentContext.theme, { prefixCls: getPrefixCls('') });
446456

447457
if (process.env.NODE_ENV !== 'production') {
@@ -591,6 +601,7 @@ const ProviderChildren: React.FC<ProviderChildrenProps> = (props) => {
591601

592602
let childNode = (
593603
<>
604+
<IconStyle iconPrefixCls={iconPrefixCls} csp={csp} />
594605
<PropWarning dropdownMatchSelectWidth={dropdownMatchSelectWidth} />
595606
{children}
596607
</>

0 commit comments

Comments
 (0)