Skip to content

Commit

Permalink
✨ feat: 支持 useTheme 获取默认的主题变量
Browse files Browse the repository at this point in the history
BREAKING CHANGE: useTheme 的默认返回值发生变动,默认返回 antd token 值
  • Loading branch information
arvinxx committed Jan 7, 2023
1 parent 3ab3f4b commit a465bf4
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/hooks/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
export * from './useAntdStylish';
export * from './useAntdTheme';
export * from './useAntdToken';
export * from './useAutoThemeMode';
export * from './useTheme';
export * from './useThemeMode';
11 changes: 11 additions & 0 deletions src/hooks/useAntdStylish.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { useAntdToken } from '@/hooks/useAntdToken';
import { AntdStylish } from '@/types';
import { useMemo } from 'react';

export const useAntdStylish = (): AntdStylish => {
const token = useAntdToken();

return useMemo(() => {
return {};
}, [token]);
};
10 changes: 10 additions & 0 deletions src/hooks/useAntdTheme.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { AntdTheme } from '@/types';
import { useAntdStylish } from './useAntdStylish';
import { useAntdToken } from './useAntdToken';

export const useAntdTheme = (): AntdTheme => {
const token = useAntdToken();
const antdStylish = useAntdStylish();

return { stylish: antdStylish, ...token };
};
13 changes: 12 additions & 1 deletion src/hooks/useTheme.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
import { useAntdTheme } from '@/hooks/useAntdTheme';
import { Theme } from '@/types';
import { useTheme as _useTheme } from '@emotion/react';

export const useTheme = () => _useTheme() as Theme;
export const useTheme = (): Theme => {
const antdTheme = useAntdTheme();
const defaultTheme = _useTheme();

// 如果是个空值,说明没有套 Provider,返回 antdTheme 的默认值
if (Object.keys(defaultTheme).length === 0) {
return antdTheme;
}

return defaultTheme as Theme;
};

0 comments on commit a465bf4

Please sign in to comment.