Skip to content

Commit

Permalink
✨ feat: 支持基于主题模式的风格定制
Browse files Browse the repository at this point in the history
  • Loading branch information
arvinxx committed Jan 7, 2023
1 parent 6ce3b7d commit 3f5d2cb
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@
"@emotion/css": "^11",
"@emotion/react": "^11",
"@emotion/serialize": "^1",
"@emotion/styled": "^11"
"@emotion/styled": "^11",
"use-merge-value": "^1"
},
"devDependencies": {
"@ant-design/icons": "^4",
Expand Down
5 changes: 1 addition & 4 deletions src/containers/AppContainer/AppContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@ import { memo, ReactElement, ReactNode } from 'react';

import { ThemeAppearance, ThemeMode } from '@/types';

import { type AntdProviderProps } from './AntdProvider';
import ThemeContent, { ThemeContentProps } from './ThemeContent';
import ThemeSwitcher from './ThemeSwitcher';

export interface AppContainerProps<T, S = Record<string, string>>
extends AntdProviderProps,
ThemeContentProps<T, S> {
export interface AppContainerProps<T, S = Record<string, string>> extends ThemeContentProps<T, S> {
/**
* 应用的展示外观主题,只存在亮色和暗色两种
* @default light
Expand Down
28 changes: 20 additions & 8 deletions src/containers/AppContainer/ThemeContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ export type GetCustomStylish<S> = (theme: {
appearance: ThemeAppearance;
}) => S;

export interface ThemeContentProps<T, S = Record<string, string>> extends AntdProviderProps {
type GetAntdTheme = (appearance: ThemeAppearance) => ThemeConfig | undefined;
export interface ThemeContentProps<T, S = Record<string, string>>
extends Omit<AntdProviderProps, 'theme'> {
children: ReactNode;
/**
* 自定义 Token
Expand All @@ -27,6 +29,10 @@ export interface ThemeContentProps<T, S = Record<string, string>> extends AntdPr
* 自定义 Stylish
*/
customStylish?: S | GetCustomStylish<S>;
/**
* 直接传入 antd 主题,或者传入一个函数,根据当前的主题模式返回对应的主题
*/
theme?: ThemeConfig | GetAntdTheme;
}

const ThemeContent: <T, S>(props: ThemeContentProps<T, S>) => ReactElement | null = ({
Expand Down Expand Up @@ -61,20 +67,26 @@ const ThemeContent: <T, S>(props: ThemeContentProps<T, S>) => ReactElement | nul
const antdTheme = useMemo<ThemeConfig>(() => {
const baseAlgorithm = isDarkMode ? theme.darkAlgorithm : theme.defaultAlgorithm;

if (!themeProp) {
let antdTheme = themeProp as ThemeConfig | undefined;

if (typeof themeProp === 'function') {
antdTheme = themeProp(appearance);
}

if (!antdTheme) {
return { algorithm: baseAlgorithm };
}

// 如果有 themeProp 说明是外部传入的 theme,需要对算法做一个合并处理,因此先把 themeProp 的算法规整为一个数组
const algoProp = !themeProp.algorithm
const algoProp = !antdTheme.algorithm
? []
: themeProp.algorithm instanceof Array
? themeProp.algorithm
: [themeProp.algorithm];
: antdTheme.algorithm instanceof Array
? antdTheme.algorithm
: [antdTheme.algorithm];

return {
...themeProp,
algorithm: !themeProp.algorithm ? baseAlgorithm : [baseAlgorithm, ...algoProp],
...antdTheme,
algorithm: !antdTheme.algorithm ? baseAlgorithm : [baseAlgorithm, ...algoProp],
};
}, [themeProp, isDarkMode]);

Expand Down

0 comments on commit 3f5d2cb

Please sign in to comment.