主题信息已集成到 Redux 状态管理中,可以在任何组件中访问当前主题类型和主题配置。
src/
├── states/
│ └── themeSlice.ts # 主题状态管理
├── themes/
│ ├── lightTheme.ts # 浅色主题配置
│ ├── darkTheme.ts # 深色主题配置
│ └── index.ts # 主题导出
└── app.tsx # 主题应用入口
localStorage
'light'
| 'dark'
)setTheme
action 设置特定主题toggleTheme
action 在两个主题间切换import { useAppSelector } from '@/states/store';
function MyComponent() {
// 获取当前主题类型
const themeType = useAppSelector((state) => state.theme.themeType);
// 根据主题类型动态加载图标
const iconPath =
themeType === 'dark' ? '/icons/dark/report.svg' : '/icons/light/report.svg';
return <img src={iconPath} alt="report" />;
}
import { useAppSelector } from '@/states/store';
function MyComponent() {
// 获取完整的主题配置
const currentTheme = useAppSelector((state) => state.theme.currentTheme);
return (
<div
style={{
backgroundColor: currentTheme.token.colorBgContainer,
color: currentTheme.token.colorText,
borderColor: currentTheme.token.colorPrimary,
}}
>
内容区域
</div>
);
}
import { useAppSelector } from '@/states/store';
function MyComponent() {
const { themeType, currentTheme } = useAppSelector((state) => state.theme);
const isDark = themeType === 'dark';
return (
<div>
<p>当前主题: {isDark ? '深色' : '浅色'}</p>
<div style={{ color: currentTheme.token.colorPrimary }}>主题色文字</div>
</div>
);
}
import { useAppDispatch } from '@/states/store';
import { setTheme } from '@/states/themeSlice';
function ThemeSelector() {
const dispatch = useAppDispatch();
return (
<div>
<button onClick={() => dispatch(setTheme('light'))}>浅色主题</button>
<button onClick={() => dispatch(setTheme('dark'))}>深色主题</button>
</div>
);
}
import { useAppDispatch } from '@/states/store';
import { toggleTheme } from '@/states/themeSlice';
function ThemeToggle() {
const dispatch = useAppDispatch();
return <button onClick={() => dispatch(toggleTheme())}>切换主题</button>;
}
import { useAppSelector } from '@/states/store';
function ReportIcon() {
const themeType = useAppSelector((state) => state.theme.themeType);
// 根据主题类型选择对应的图标路径
const iconBasePath = `src/assets/Icons/base/module-patient/theme-${themeType}`;
return (
<img src={`${iconBasePath}/1x/report.svg`} alt="report" className="icon" />
);
}
import { useAppSelector } from '@/states/store';
function Card({ children }) {
const { themeType, currentTheme } = useAppSelector((state) => state.theme);
return (
<div
className={`card card-${themeType}`}
style={{
backgroundColor: currentTheme.token.colorBgContainer,
borderColor: currentTheme.token.colorPrimary,
color: currentTheme.token.colorText,
}}
>
{children}
</div>
);
}
import { useAppSelector } from '@/states/store';
function ThemedLogo() {
const themeType = useAppSelector((state) => state.theme.themeType);
return themeType === 'dark' ? <DarkLogo /> : <LightLogo />;
}
currentTheme.token.colorPrimary; // 主色调
currentTheme.token.colorBgContainer; // 容器背景色
currentTheme.token.colorBgLayout; // 全局背景色
currentTheme.token.colorText; // 文字颜色
currentTheme.token.buttonBgHover; // 按钮悬停背景色
colorPrimary
: #1DA57A
colorBgContainer
: #FFFFFF
colorText
: #000000
colorBgLayout
: #FAFAFA
colorPrimary
: #1DA57A
colorBgContainer
: #1F1F1F
colorText
: #E0E0E0
colorBgLayout
: #121212
// 主题类型
type ThemeType = 'light' | 'dark';
// 主题配置类型
type ThemeConfig = typeof lightTheme;
// Redux 状态中的主题状态
interface ThemeState {
themeType: ThemeType;
currentTheme: ThemeConfig;
}
themeType
即可currentTheme.token
中的值lightTheme
或 darkTheme
,始终从 Redux 获取ConfigProvider
的组件:root
,可在 Tailwind CSS 中使用在浏览器控制台中查看当前主题状态:
// 在开发环境中,store 已暴露到 window
window.store.getState().theme;