样式
项目中主要用到了以下几种方式进行样式开发:
- Ant Design 样式
- Tailwind CSS
- Styled Components
Ant Design样式
项目的基础骨架是基于Ant Design设计的, 其相关目录在src/theme/
├── src
│ ├── theme
│ ├── antd
│ ├── components # 复写的Ant Design 组件
│ ├── theme.ts # Ant Design 主题配置
│ ├── index.tsx # Ant Design 的 ConfigProvider
│ ├── hooks
│ ├── use-responsive.ts
│ ├── use-theme-token.ts # useThemeToken钩子
定制主题
主题相关配置定义在 src/thems/antd/theme.ts
文件中
src/thems/antd/theme.ts
import { ThemeConfig } from 'antd';
import { ThemeColorPresets } from '#/enum';
/**
* Antd theme editor: https://ant.design/theme-editor-cn
*/
const customThemeTokenConfig: ThemeConfig['token'] = {
colorSuccess: '#22c55e',
colorWarning: '#ff7849',
colorError: '#ff5630',
colorInfo: '#00b8d9',
// 线性化
wireframe: false,
borderRadiusSM: 2,
borderRadius: 4,
borderRadiusLG: 8,
};
const customComponentConfig: ThemeConfig['components'] = {
Breadcrumb: {
fontSize: 12,
separatorMargin: 4,
},
Menu: {
fontSize: 14,
colorFillAlter: 'transparent',
itemColor: 'rgb(145, 158, 171)',
},
};
const colorPrimarys: {
[k in ThemeColorPresets]: string;
} = {
default: '#00a76f',
cyan: '#078DEE',
purple: '#7635DC',
blue: '#2065D1',
orange: '#FDA92D',
red: '#FF3030',
};
const themeModeToken: Record<'dark' | 'light', ThemeConfig> = {
dark: {
token: {
colorBgLayout: '#161c24',
colorBgContainer: '#212b36',
colorBgElevated: '#161c24',
},
components: {
Modal: {
headerBg: '#212b36',
contentBg: '#212b36',
footerBg: '#212b36',
},
Notification: {},
},
},
light: {},
};
export { customThemeTokenConfig, customComponentConfig, colorPrimarys, themeModeToken };
useThemeToken
其中useThemeToken
会在项目中大量使用
export function useThemeToken() {
const { token } = theme.useToken();
return useMemo(() => token, [token]);
}
用于获取Ant Design 的 theme token, 比如:
import { NavLink } from 'react-router-dom';
import { useThemeToken } from '@/theme/hooks';
import { Iconify } from '../icon';
interface Props {
size?: number | string;
}
function Logo({ size = 50 }: Props) {
const { colorPrimary } = useThemeToken();
return (
<NavLink to="/">
<Iconify icon="solar:code-square-bold" color={colorPrimary} size={size} />
</NavLink>
);
}
export default Logo;