Skip to content

Commit

Permalink
🐛 fix: 修正 prefixCls 在嵌套 ThemeProvider 中没有继承的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
arvinxx committed Jul 2, 2023
1 parent f5eeb9c commit 4e5488a
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 2 deletions.
6 changes: 6 additions & 0 deletions docs/api/theme-provider.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,12 @@ export default () => {

:::

## 嵌套 ThemeProvider

在某些场景下,我们会需要在一个 ThemeProvider 中嵌套另一个 ThemeProvider,这时候需要注意的是,内层的 ThemeProvider 会覆盖外层的 ThemeProvider。

<code src="../demos/ThemeProvider/nested-prefixCls.tsx"></code>

## styled 集成

antd-style 通过 `styled` 方法提供了 styled-components 的 ThemeProvider 兼容能力,进而使用 antd-style 的 ThemeProvider 为 styled-components 或 emotion/styled 库提供主题消费。
Expand Down
34 changes: 34 additions & 0 deletions docs/demos/ThemeProvider/nested-prefixCls.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* debug: true
* iframe: true
*/
import { Button } from 'antd';
import { ThemeProvider, createStyles } from 'antd-style';

const useStyles = createStyles(({ css, prefixCls }) => ({
button: css`
.${prefixCls}-btn {
background: pink;
}
`,
}));

const Demo = () => {
const { styles } = useStyles();
return (
<div className={styles.button}>
<Button>按钮</Button>
</div>
);
};

const App = () => {
return (
<ThemeProvider appearance={'dark'} prefixCls={'a'}>
<ThemeProvider appearance={'light'}>
<Demo />
</ThemeProvider>
</ThemeProvider>
);
};
export default App;
6 changes: 4 additions & 2 deletions src/factories/createThemeProvider/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export const createThemeProvider = (

theme,
getStaticInstance,
prefixCls,
prefixCls: outPrefixCls,
staticInstanceConfig,

appearance,
Expand All @@ -74,10 +74,12 @@ export const createThemeProvider = (
? createStyledThemeProvider(styled)
: DefaultStyledThemeProvider || DEFAULT_THEME_PROVIDER;

const prefixCls = outPrefixCls || defaultPrefixCls;

return (
<StyleEngineContext.Provider
value={{
prefixCls: prefixCls || defaultPrefixCls,
prefixCls: prefixCls,
StyledThemeContext: styled?.ThemeContext || StyledThemeContext || DEFAULT_THEME_CONTEXT,
CustomThemeContext,
}}
Expand Down

0 comments on commit 4e5488a

Please sign in to comment.