diff --git a/src/containers/ThemeProvider/ThemeProvider.tsx b/src/containers/ThemeProvider/ThemeProvider.tsx index c8c33e63..66a4c1cb 100644 --- a/src/containers/ThemeProvider/ThemeProvider.tsx +++ b/src/containers/ThemeProvider/ThemeProvider.tsx @@ -36,7 +36,11 @@ export const ThemeProvider: ( theme={theme} getStaticInstance={getStaticInstance} > - + {children} diff --git a/src/containers/ThemeProvider/TokenContainer.tsx b/src/containers/ThemeProvider/TokenContainer.tsx index 1f9b3c22..e3c2d1ff 100644 --- a/src/containers/ThemeProvider/TokenContainer.tsx +++ b/src/containers/ThemeProvider/TokenContainer.tsx @@ -10,13 +10,14 @@ import { Theme } from '@/types'; type TokenContainerProps> = Pick< ThemeProviderProps, - 'children' | 'customToken' | 'customStylish' + 'children' | 'customToken' | 'customStylish' | 'prefixCls' >; const TokenContainer: (props: TokenContainerProps) => ReactElement | null = ({ children, customToken: customTokenOrFn, customStylish: stylishOrGetStylish, + prefixCls = 'ant', }) => { const themeState = useThemeMode(); const { appearance, isDarkMode } = themeState; @@ -53,6 +54,7 @@ const TokenContainer: (props: TokenContainerProps) => ReactElement | ...customToken, stylish, ...themeState, + prefixCls, }; return {children}; diff --git a/src/context/EmotionContext.ts b/src/context/EmotionContext.ts index 159dcea3..2ba3aaff 100644 --- a/src/context/EmotionContext.ts +++ b/src/context/EmotionContext.ts @@ -2,9 +2,4 @@ import { createContext } from 'react'; import { emotion, type Emotion } from '@/functions'; -export interface CommonStyleUtils { - cx: Emotion['cx']; - css: Emotion['css']; -} - export const EmotionContext = createContext(emotion); diff --git a/src/functions/createStyles.ts b/src/functions/createStyles.ts index 36181137..db19b71c 100644 --- a/src/functions/createStyles.ts +++ b/src/functions/createStyles.ts @@ -1,8 +1,8 @@ import { useMemo } from 'react'; -import { CommonStyleUtils } from '@/context'; import { useEmotion, useTheme } from '@/hooks'; -import { +import type { + CommonStyleUtils, FullStylish, FullToken, ReturnStyleToUse, @@ -18,6 +18,7 @@ export interface CreateStylesTheme extends CommonStyleUtils { stylish: FullStylish; appearance: ThemeAppearance; isDarkMode: boolean; + prefixCls: string; } /** @@ -25,8 +26,9 @@ export interface CreateStylesTheme extends CommonStyleUtils { */ export interface ReturnStyles { styles: ReturnStyleToUse; - theme: Theme; + theme: Omit; cx: Emotion['cx']; + prefixCls: string; } // 获取样式 @@ -56,14 +58,17 @@ export const createStyles = const styles = useMemo(() => { let tempStyles: ReturnStyleToUse; + // 函数场景 if (styleOrGetStyleFn instanceof Function) { - const { stylish, appearance, isDarkMode, ...token } = theme; + const { stylish, appearance, isDarkMode, prefixCls, ...token } = theme; tempStyles = styleOrGetStyleFn( - { token, stylish, appearance, cx, css, isDarkMode }, + { token, stylish, appearance, cx, css, isDarkMode, prefixCls }, props!, ) as any; - } else { + } + // 没有函数时直接就是 object 或者 string + else { tempStyles = styleOrGetStyleFn as any; } @@ -82,5 +87,8 @@ export const createStyles = return tempStyles; }, [styleOrGetStyleFn, props, theme]); - return useMemo(() => ({ styles, cx, theme }), [styles, theme]); + return useMemo(() => { + const { prefixCls, ...res } = theme; + return { styles, cx, theme: res, prefixCls }; + }, [styles, theme]); }; diff --git a/src/hooks/useTheme.ts b/src/hooks/useTheme.ts index 3a4483e2..4fc5e5e4 100644 --- a/src/hooks/useTheme.ts +++ b/src/hooks/useTheme.ts @@ -10,7 +10,10 @@ export const useTheme = (): Theme => { const themeState = useThemeMode(); const defaultTheme = _useTheme(); - const initTheme = useMemo(() => ({ ...antdTheme, ...themeState }), [antdTheme, themeState]); + const initTheme = useMemo( + () => ({ ...antdTheme, ...themeState, prefixCls: 'ant' }), + [antdTheme, themeState], + ); // 如果是个空值,说明没有套 Provider,返回 antdTheme 的默认值 if (Object.keys(defaultTheme).length === 0) { diff --git a/src/types/theme.ts b/src/types/theme.ts index ec951358..798c73d0 100644 --- a/src/types/theme.ts +++ b/src/types/theme.ts @@ -1,9 +1,13 @@ import { ThemeConfig } from 'antd/es/config-provider/context'; import { AliasToken } from 'antd/es/theme/interface'; -import { CommonStyleUtils } from '@/context'; +import { Emotion } from '@/functions'; import { ThemeAppearance, ThemeMode } from './appearance'; +export interface CommonStyleUtils { + cx: Emotion['cx']; + css: Emotion['css']; +} export interface ThemeContextState { appearance: ThemeAppearance; themeMode: ThemeMode; @@ -54,4 +58,6 @@ export interface FullToken extends AntdToken, CustomToken {} export interface Theme extends FullToken, ThemeContextState { stylish: FullStylish; + + prefixCls: string; }