UI:完成Antd组件库暗色适配

This commit is contained in:
宇阳
2024-12-09 20:30:58 +08:00
parent 358e9a86b9
commit 1b9bc1adeb
3 changed files with 29 additions and 14 deletions

View File

@@ -2,44 +2,59 @@ import { useEffect, useState } from 'react';
import { useLocation } from 'react-router-dom'; import { useLocation } from 'react-router-dom';
import Loader from './common/Loader'; import Loader from './common/Loader';
import useAuthRedirect from '@/hooks/useAuthRedirect'; import useAuthRedirect from '@/hooks/useAuthRedirect';
import { ConfigProvider } from 'antd'; import { ConfigProvider, theme } from 'antd';
import RouteList from './components/RouteList'; import RouteList from './components/RouteList';
import "@/styles/customAntd.scss" import "@/styles/customAntd.scss";
import { getConfigDataAPI } from '@/api/Project'; import { getConfigDataAPI } from '@/api/Project';
import { useWebStore } from './stores'; import { useWebStore } from './stores';
import { Web } from './types/app/project'; import { Web } from './types/app/project';
function App() { function App() {
useAuthRedirect() useAuthRedirect();
const [loading, setLoading] = useState<boolean>(true); const [loading, setLoading] = useState<boolean>(true);
const [isDarkTheme, setIsDarkTheme] = useState<boolean>(false);
const { pathname } = useLocation(); const { pathname } = useLocation();
useEffect(() => { useEffect(() => {
window.scrollTo(0, 0); window.scrollTo(0, 0);
}, [pathname]); }, [pathname]);
// 获取网站数据并把数据共享给全局方便使用 const setWeb = useWebStore(state => state.setWeb);
const setWeb = useWebStore(state => state.setWeb)
const getWebData = async () => { const getWebData = async () => {
const { data } = await getConfigDataAPI<Web>("web"); const { data } = await getConfigDataAPI<Web>("web");
setWeb(data) setWeb(data);
}; };
useEffect(() => { useEffect(() => {
getWebData() getWebData();
setTimeout(() => setLoading(false), 1000); setTimeout(() => setLoading(false), 1000);
const bodyClassList = document.body.classList;
// 监听类名变化
const observer = new MutationObserver(() => {
setIsDarkTheme(bodyClassList.contains('dark'));
});
observer.observe(document.body, { attributes: true, attributeFilter: ['class'] });
return () => observer.disconnect();
}, []); }, []);
return loading ? ( return loading ? (
<Loader /> <Loader />
) : ( ) : (
// 自定义主题色 // 根据主题切换配置主题
<ConfigProvider <ConfigProvider
theme={{ theme={{
token: { token: {
colorPrimary: '#727cf5', colorPrimary: '#727cf5',
colorBgBase: isDarkTheme ? '#24303F' : '#ffffff',
colorTextBase: isDarkTheme ? '#e0e0e0' : '#000000'
}, },
algorithm: isDarkTheme ? theme.darkAlgorithm : theme.defaultAlgorithm,
}} }}
> >
<RouteList /> <RouteList />
@@ -47,4 +62,4 @@ function App() {
); );
} }
export default App; export default App;

View File

@@ -295,15 +295,15 @@ const VisitorsStatisChat = () => {
<div className="flex w-full max-w-45 justify-end"> <div className="flex w-full max-w-45 justify-end">
<div className="inline-flex items-center rounded-md bg-whiter p-1.5 dark:bg-meta-4"> <div className="inline-flex items-center rounded-md bg-whiter p-1.5 dark:bg-meta-4">
<button className={`rounded py-1 px-3 text-xs font-medium text-black hover:bg-white hover:shadow-card dark:bg-boxdark dark:text-white dark:hover:bg-boxdark ${scope === "day" ? "bg-white shadow-card" : ""}`} onClick={() => handleScopeChange("day")}> <button className={`rounded py-1 px-3 text-xs font-medium text-black hover:bg-white hover:shadow-card dark:bg-meta-4 dark:text-white dark:hover:bg-boxdark ${scope === "day" ? "bg-white dark:!bg-[#4e5969] shadow-card" : ""}`} onClick={() => handleScopeChange("day")}>
</button> </button>
<button className={`rounded py-1 px-3 text-xs font-medium text-black hover:bg-white hover:shadow-card dark:bg-boxdark dark:text-white dark:hover:bg-boxdark ${scope === "month" ? "bg-white shadow-card" : ""}`} onClick={() => handleScopeChange("month")}> <button className={`rounded py-1 px-3 text-xs font-medium text-black hover:bg-white hover:shadow-card dark:bg-meta-4 dark:text-white dark:hover:bg-boxdark ${scope === "month" ? "bg-white dark:!bg-[#4e5969] shadow-card" : ""}`} onClick={() => handleScopeChange("month")}>
</button> </button>
<button className={`rounded py-1 px-3 text-xs font-medium text-black hover:bg-white hover:shadow-card dark:bg-boxdark dark:text-white dark:hover:bg-boxdark ${scope === "year" ? "bg-white shadow-card" : ""}`} onClick={() => handleScopeChange("year")}> <button className={`rounded py-1 px-3 text-xs font-medium text-black hover:bg-white hover:shadow-card dark:bg-meta-4 dark:text-white dark:hover:bg-boxdark ${scope === "year" ? "bg-white dark:!bg-[#4e5969] shadow-card" : ""}`} onClick={() => handleScopeChange("year")}>
</button> </button>
</div> </div>

View File

@@ -39,9 +39,9 @@ module.exports = {
'gray-3': '#FAFAFA', 'gray-3': '#FAFAFA',
whiten: '#F1F5F9', whiten: '#F1F5F9',
whiter: '#F5F7FD', whiter: '#F5F7FD',
boxdark: '#24303F', boxdark: '#334459',
'boxdark-2': '#1A222C', 'boxdark-2': '#1A222C',
strokedark: '#2E3A47', strokedark: '#475f7d',
'form-strokedark': '#3d4d60', 'form-strokedark': '#3d4d60',
'form-input': '#1d2a39', 'form-input': '#1d2a39',
'meta-1': '#DC3545', 'meta-1': '#DC3545',