Skip to content

Commit

Permalink
✨ feat: 为 ThemeProvider 补齐主题切换的能力
Browse files Browse the repository at this point in the history
  • Loading branch information
arvinxx committed Jan 9, 2023
1 parent 9b9e340 commit 52b68d5
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 59 deletions.
50 changes: 0 additions & 50 deletions src/containers/AppContainer/AppContainer.tsx

This file was deleted.

1 change: 0 additions & 1 deletion src/containers/AppContainer/index.ts

This file was deleted.

21 changes: 21 additions & 0 deletions src/containers/AppContainer/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { App } from 'antd';
import { memo, ReactElement } from 'react';

import { ThemeProvider, ThemeProviderProps } from '../ThemeProvider';

export interface AppContainerProps<T, S = Record<string, string>> extends ThemeProviderProps<T, S> {
className?: string;
}

export const AppContainer: <T, S>(props: AppContainerProps<T, S>) => ReactElement | null = memo(
({
children,

className,
...props
}) => (
<ThemeProvider {...props}>
<App className={className}>{children}</App>
</ThemeProvider>
),
);
32 changes: 25 additions & 7 deletions src/containers/ThemeProvider/ThemeProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,39 @@
import { ReactElement } from 'react';

import AntdProvider from './AntdProvider';
import ThemeSwitcher from './ThemeSwitcher';
import TokenContainer from './TokenContainer';
import { ThemeProviderProps } from './type';

export const ThemeProvider: <T, S>(props: ThemeProviderProps<T, S>) => ReactElement | null = ({
export const ThemeProvider: <T = any, S = any>(
props: ThemeProviderProps<T, S>,
) => ReactElement | null = ({
children,

customToken,
customStylish,
theme: themeProp,

theme,
getStaticInstance,
prefixCls,

appearance,
defaultAppearance,
onAppearanceChange,
themeMode,
}) => {
return (
<AntdProvider prefixCls={prefixCls} theme={themeProp}>
<TokenContainer customToken={customToken} customStylish={customStylish}>
{children}
</TokenContainer>
</AntdProvider>
<ThemeSwitcher
themeMode={themeMode}
defaultAppearance={defaultAppearance}
appearance={appearance}
onAppearanceChange={onAppearanceChange}
>
<AntdProvider prefixCls={prefixCls} theme={theme} getStaticInstance={getStaticInstance}>
<TokenContainer customToken={customToken} customStylish={customStylish}>
{children}
</TokenContainer>
</AntdProvider>
</ThemeSwitcher>
);
};
20 changes: 19 additions & 1 deletion src/containers/ThemeProvider/type.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { GetCustomStylish, GetCustomToken, ThemeAppearance } from '@/types';
import { GetCustomStylish, GetCustomToken, ThemeAppearance, ThemeMode } from '@/types';
import { ThemeConfig } from 'antd/es/config-provider/context';
import { MessageInstance } from 'antd/es/message/interface';
import { ModalStaticFunctions } from 'antd/es/modal/confirm';
Expand All @@ -11,6 +11,7 @@ export interface GetAntdTheme {

export interface ThemeProviderProps<T, S = Record<string, string>> {
children: ReactNode;
// --------------------- 自定义主题 --------------------- //
/**
* 自定义 Token
*/
Expand All @@ -19,16 +20,33 @@ export interface ThemeProviderProps<T, S = Record<string, string>> {
* 自定义 Stylish
*/
customStylish?: S | GetCustomStylish<S>;
// --------------------- antd 主题 --------------------- //
/**
* 直接传入 antd 主题,或者传入一个函数,根据当前的主题模式返回对应的主题
*/
theme?: ThemeConfig | GetAntdTheme;
prefixCls?: string;

/**
* 从 ThemeProvider 中获取静态方法的实例对象
* @param instances
*/
getStaticInstance?: (instances: StaticInstance) => void;

// --------------------- 主题切换 --------------------- //
/**
* 应用的展示外观主题,只存在亮色和暗色两种
* @default light
*/
appearance?: ThemeAppearance;
defaultAppearance?: ThemeAppearance;
onAppearanceChange?: (mode: ThemeAppearance) => void;
/**
* 主题的展示模式,有三种配置:跟随系统、亮色、暗色
* 默认不开启自动模式,需要手动进行配置
* @default light
*/
themeMode?: ThemeMode;
}

export interface StaticInstance {
Expand Down

0 comments on commit 52b68d5

Please sign in to comment.