Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: css var prefix follow prefixCls by default #47481

Merged
merged 2 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
58 changes: 58 additions & 0 deletions components/config-provider/__tests__/theme.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,64 @@ describe('ConfigProvider.Theme', () => {
});
});

it('prefix follow prefixCls by default', () => {
const { container } = render(
<>
<ConfigProvider prefixCls="foo" theme={{ cssVar: { key: 'foo' }, hashed: true }}>
<Button className="button-foo">Button</Button>
</ConfigProvider>
<ConfigProvider prefixCls="bar">
<ConfigProvider theme={{ cssVar: { key: 'bar' }, hashed: true }}>
<Button className="button-bar">Button</Button>
</ConfigProvider>
</ConfigProvider>
<ConfigProvider prefixCls="apple">
<ConfigProvider prefixCls="banana" theme={{ cssVar: { key: 'banana' }, hashed: true }}>
<Button className="button-banana">Button</Button>
</ConfigProvider>
</ConfigProvider>
<ConfigProvider
prefixCls="apple"
theme={{ cssVar: { key: 'apple', prefix: 'cat' }, hashed: true }}
>
<Button className="button-cat">Button</Button>
</ConfigProvider>
</>,
);

const fooBtn = container.querySelector('.button-foo')!;
expect(fooBtn).toHaveClass('foo');
expect(fooBtn).toHaveStyle({
'--foo-color-text': 'rgba(0, 0, 0, 0.88)',
boxShadow: 'var(--foo-button-default-shadow)',
'border-radius': 'var(--foo-border-radius)',
});

const barBtn = container.querySelector('.button-bar')!;
expect(barBtn).toHaveClass('bar');
expect(barBtn).toHaveStyle({
'--bar-color-text': 'rgba(0, 0, 0, 0.88)',
boxShadow: 'var(--bar-button-default-shadow)',
'border-radius': 'var(--bar-border-radius)',
});

const bananaBtn = container.querySelector('.button-banana')!;
expect(bananaBtn).toHaveClass('banana');
expect(bananaBtn).toHaveStyle({
'--banana-color-text': 'rgba(0, 0, 0, 0.88)',
boxShadow: 'var(--banana-button-default-shadow)',
'border-radius': 'var(--banana-border-radius)',
});

const catBtn = container.querySelector('.button-cat')!;
expect(catBtn).toHaveClass('apple');
expect(catBtn).toHaveStyle({
'--cat-color-text': 'rgba(0, 0, 0, 0.88)',
boxShadow: 'var(--cat-button-default-shadow)',
'border-radius': 'var(--cat-border-radius)',
});
});

it('component token should work', () => {
const { container } = render(
<ConfigProvider
Expand Down
5 changes: 4 additions & 1 deletion components/config-provider/hooks/useTheme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import { devUseWarning } from '../../_util/warning';
export default function useTheme(
theme?: ThemeConfig,
parentTheme?: ThemeConfig,
config?: {
prefixCls?: string;
},
): ThemeConfig | undefined {
const warning = devUseWarning('ConfigProvider');

Expand Down Expand Up @@ -57,7 +60,7 @@ export default function useTheme(

const cssVarKey = `css-var-${themeKey.replace(/:/g, '')}`;
const mergedCssVar = (themeConfig.cssVar ?? parentThemeConfig.cssVar) && {
prefix: 'ant', // Default to ant
prefix: config?.prefixCls, // Same as prefixCls by default
...(typeof parentThemeConfig.cssVar === 'object' ? parentThemeConfig.cssVar : {}),
...(typeof themeConfig.cssVar === 'object' ? themeConfig.cssVar : {}),
key: (typeof themeConfig.cssVar === 'object' && themeConfig.cssVar?.key) || cssVarKey,
Expand Down
2 changes: 1 addition & 1 deletion components/config-provider/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ const ProviderChildren: React.FC<ProviderChildrenProps> = (props) => {

useStyle(iconPrefixCls, csp);

const mergedTheme = useTheme(theme, parentContext.theme);
const mergedTheme = useTheme(theme, parentContext.theme, { prefixCls: getPrefixCls('') });

if (process.env.NODE_ENV !== 'production') {
existThemeConfig = existThemeConfig || !!mergedTheme;
Expand Down
2 changes: 1 addition & 1 deletion docs/react/css-variables.en-US.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,5 @@ With CSS variable mode, the method of modifying the theme is the same as before,

| Properties | Description | Type | Default | Version |
| --- | --- | --- | --- | --- |
| prefix | Prefix of CSS Variables | string | `ant` | 5.12.0 |
| prefix | Prefix of CSS Variables, same as `prefixCls` of ConfigProvider by default | string | `ant` | 5.12.0 |
| key | The unique key of theme. Automatically set by `useId` in React 18, but need to be set manually in React 17 or 16 | string | `useId` in React 18 | 5.12.0 |
2 changes: 1 addition & 1 deletion docs/react/css-variables.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,5 @@ hash 是 Ant Design 5.0 以来的特性之一,其功能是为每一份主题

| 属性 | 说明 | 类型 | 默认值 | 版本 |
| --- | --- | --- | --- | --- |
| prefix | CSS 变量的前缀 | string | `ant` | 5.12.0 |
| prefix | CSS 变量的前缀,默认与 ConfigProvider 上配置的 `prefixCls` 相同。 | string | `ant` | 5.12.0 |
| key | 当前主题的唯一识别 key. 在 React 18 中会默认用 `useId` 填充,小于 React 18 的版本需要手动填充。 | string | `useId` in React 18 | 5.12.0 |