如何设置react + vite项目的主色调?
如何设置react + vite项目的主色调?
eeeeeeeeeeeeeeeeeeee
🎯 在 React + Vite 项目中设置主色调的方法
在 Vite 项目中,你可以通过以下几种方式为项目设置主色调:
✅ 方法一:使用 CSS 变量(推荐)
这种方式简单易维护,适合小型项目或自定义主题。
🔥 步骤
- 在
src/index.css(或src/App.css)中定义主色调变量:
/* src/index.css */
:root {
--primary-color: #1976d2; /* 主色调 */
--secondary-color: #ff4081; /* 次要色调 */
--background-color: #f5f5f5;
--text-color: #333;
}
body {
background-color: var(--background-color);
color: var(--text-color);
}
- 在组件中使用:
import React from 'react';
import './index.css'; // 引入全局样式
const Home: React.FC = () => {
return (
<div style={{ padding: '20px' }}>
<h1 style={{ color: 'var(--primary-color)' }}>主色调标题</h1>
<p style={{ color: 'var(--secondary-color)' }}>次要色调文本</p>
<button style={{ backgroundColor: 'var(--primary-color)', color: '#fff' }}>
按钮
</button>
</div>
);
};
export default Home;
✅ 优点
- 简单易用。
- 全局生效,维护方便。
- 只需在 CSS 文件中修改变量值即可更新整个项目的主色调。
✅ 方法二:使用 MUI 的主题自定义
如果你的项目使用了 MUI,可以通过自定义 MUI 的 ThemeProvider 来设置主色调。
🔥 步骤
- 安装 MUI 主题包(如未安装):
npm install @mui/material @emotion/react @emotion/styled
- 创建主题配置:
// src/theme.ts
import { createTheme } from '@mui/material/styles';
const theme = createTheme({
palette: {
primary: {
main: '#1976d2', // 主色调
},
secondary: {
main: '#ff4081', // 次要色调
},
background: {
default: '#f5f5f5',
},
text: {
primary: '#333',
},
},
});
export default theme;
- 在项目入口处引入主题:
// src/main.tsx
import React from 'react';
import ReactDOM from 'react-dom/client';
import { ThemeProvider } from '@mui/material/styles';
import CssBaseline from '@mui/material/CssBaseline';
import App from './App.tsx';
import theme from './theme'; // 引入自定义主题
ReactDOM.createRoot(document.getElementById('root')!).render(
<React.StrictMode>
<ThemeProvider theme={theme}>
<CssBaseline /> {/* 全局重置样式 */}
<App />
</ThemeProvider>
</React.StrictMode>
);
- 在组件中使用:
import React from 'react';
import { Button, Typography, Box } from '@mui/material';
const Home: React.FC = () => {
return (
<Box sx={{ p: 4 }}>
<Typography variant="h4" color="primary">
主色调标题
</Typography>
<Typography variant="body1" color="secondary">
次要色调文本
</Typography>
<Button variant="contained" color="primary">
按钮
</Button>
</Box>
);
};
export default Home;
✅ 优点
- MUI 内置主题系统,支持深色模式、颜色模式切换。
- 样式和组件高度集成,适合中大型项目。
✅ 方法三:使用 Tailwind CSS 配置主色调
如果你使用了 Tailwind CSS,可以通过配置 tailwind.config.js 自定义主色调。
🔥 步骤
- 安装 Tailwind:
npm install -D tailwindcss postcss autoprefixer
npx tailwindcss init -p
- 修改
tailwind.config.js:
// tailwind.config.js
module.exports = {
content: ["./index.html", "./src/**/*.{js,ts,jsx,tsx}"],
theme: {
extend: {
colors: {
primary: '#1976d2', // 主色调
secondary: '#ff4081', // 次要色调
background: '#f5f5f5',
text: '#333'
}
}
},
plugins: [],
};
- 在项目中使用:
import React from 'react';
const Home: React.FC = () => {
return (
<div className="p-4 bg-background">
<h1 className="text-primary text-3xl">主色调标题</h1>
<p className="text-secondary">次要色调文本</p>
<button className="bg-primary text-white px-4 py-2 rounded">
按钮
</button>
</div>
);
};
export default Home;
✅ 优点
- Tailwind 的原子化 CSS 提高开发效率。
- 可以在配置中集中管理颜色,修改时只需改一处即可全局生效。
✅ 方法四:在 Vite 环境变量中设置主色调
你也可以将主色调写在 .env 文件中,方便在多个环境中管理。
🔥 步骤
- 在根目录下创建
.env文件:
VITE_PRIMARY_COLOR=#1976d2
VITE_SECONDARY_COLOR=#ff4081
- 在
vite.config.ts中读取环境变量:
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
export default defineConfig({
plugins: [react()],
define: {
'process.env': process.env
}
});
- 在组件中使用:
import React from 'react';
const Home: React.FC = () => {
const primaryColor = import.meta.env.VITE_PRIMARY_COLOR;
const secondaryColor = import.meta.env.VITE_SECONDARY_COLOR;
return (
<div style={{ padding: '20px' }}>
<h1 style={{ color: primaryColor }}>主色调标题</h1>
<p style={{ color: secondaryColor }}>次要色调文本</p>
<button style={{ backgroundColor: primaryColor, color: '#fff' }}>
按钮
</button>
</div>
);
};
export default Home;
✅ 优点
- 环境变量方式可以轻松切换不同环境的主色调。
- 适合多环境配置,如开发、测试、生产环境使用不同的颜色。
🚀 推荐方案
- ✅ 小型项目:使用 CSS 变量(方法一),简单易用。
- ✅ 中大型项目:使用 MUI 主题配置(方法二),组件样式集成,易扩展。
- ✅ 使用 Tailwind:在 Tailwind 配置中集中管理颜色(方法三),保持一致性。
- ✅ 多环境项目:使用
.env环境变量(方法四),适合切换多环境主色调。
👉 推荐使用 MUI 主题配置(方法二)或 Tailwind(方法三),这两种方式更易维护且适合大型项目!
浙公网安备 33010602011771号