mirror of
https://github.com/iib0011/omni-tools.git
synced 2025-09-20 14:39:34 +02:00
Merge pull request #162 from y1hao/dark
feat: allow following OS system theme
This commit is contained in:
@@ -1,9 +1,9 @@
|
|||||||
import { BrowserRouter, useRoutes } from 'react-router-dom';
|
import { BrowserRouter, useRoutes } from 'react-router-dom';
|
||||||
import routesConfig from '../config/routesConfig';
|
import routesConfig from '../config/routesConfig';
|
||||||
import Navbar from './Navbar';
|
import Navbar from './Navbar';
|
||||||
import { Suspense, useMemo, useState } from 'react';
|
import { Suspense, useState, useEffect } from 'react';
|
||||||
import Loading from './Loading';
|
import Loading from './Loading';
|
||||||
import { CssBaseline, ThemeProvider } from '@mui/material';
|
import { CssBaseline, Theme, ThemeProvider } from '@mui/material';
|
||||||
import { CustomSnackBarProvider } from '../contexts/CustomSnackBarContext';
|
import { CustomSnackBarProvider } from '../contexts/CustomSnackBarContext';
|
||||||
import { SnackbarProvider } from 'notistack';
|
import { SnackbarProvider } from 'notistack';
|
||||||
import { tools } from '../tools';
|
import { tools } from '../tools';
|
||||||
@@ -11,6 +11,8 @@ import './index.css';
|
|||||||
import { darkTheme, lightTheme } from '../config/muiConfig';
|
import { darkTheme, lightTheme } from '../config/muiConfig';
|
||||||
import ScrollToTopButton from './ScrollToTopButton';
|
import ScrollToTopButton from './ScrollToTopButton';
|
||||||
|
|
||||||
|
export type Mode = 'dark' | 'light' | 'system';
|
||||||
|
|
||||||
const AppRoutes = () => {
|
const AppRoutes = () => {
|
||||||
const updatedRoutesConfig = [...routesConfig];
|
const updatedRoutesConfig = [...routesConfig];
|
||||||
tools.forEach((tool) => {
|
tools.forEach((tool) => {
|
||||||
@@ -20,10 +22,26 @@ const AppRoutes = () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
function App() {
|
function App() {
|
||||||
const [darkMode, setDarkMode] = useState<boolean>(() => {
|
const [mode, setMode] = useState<Mode>(
|
||||||
return localStorage.getItem('theme') === 'dark';
|
() => (localStorage.getItem('theme') || 'system') as Mode
|
||||||
});
|
);
|
||||||
const theme = useMemo(() => (darkMode ? darkTheme : lightTheme), [darkMode]);
|
const [theme, setTheme] = useState<Theme>(() => getTheme(mode));
|
||||||
|
useEffect(() => setTheme(getTheme(mode)), [mode]);
|
||||||
|
|
||||||
|
// Make sure to update the theme when the mode changes
|
||||||
|
useEffect(() => {
|
||||||
|
const systemDarkModeQuery = window.matchMedia(
|
||||||
|
'(prefers-color-scheme: dark)'
|
||||||
|
);
|
||||||
|
const handleThemeChange = (e: MediaQueryListEvent) => {
|
||||||
|
setTheme(e.matches ? darkTheme : lightTheme);
|
||||||
|
};
|
||||||
|
systemDarkModeQuery.addEventListener('change', handleThemeChange);
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
systemDarkModeQuery.removeEventListener('change', handleThemeChange);
|
||||||
|
};
|
||||||
|
}, []);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<ThemeProvider theme={theme}>
|
<ThemeProvider theme={theme}>
|
||||||
@@ -38,9 +56,10 @@ function App() {
|
|||||||
<CustomSnackBarProvider>
|
<CustomSnackBarProvider>
|
||||||
<BrowserRouter>
|
<BrowserRouter>
|
||||||
<Navbar
|
<Navbar
|
||||||
onSwitchTheme={() => {
|
mode={mode}
|
||||||
setDarkMode((prevState) => !prevState);
|
onChangeMode={() => {
|
||||||
localStorage.setItem('theme', darkMode ? 'light' : 'dark');
|
setMode((prev) => nextMode(prev));
|
||||||
|
localStorage.setItem('theme', nextMode(mode));
|
||||||
}}
|
}}
|
||||||
/>
|
/>
|
||||||
<Suspense fallback={<Loading />}>
|
<Suspense fallback={<Loading />}>
|
||||||
@@ -54,4 +73,21 @@ function App() {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function getTheme(mode: Mode): Theme {
|
||||||
|
switch (mode) {
|
||||||
|
case 'dark':
|
||||||
|
return darkTheme;
|
||||||
|
case 'light':
|
||||||
|
return lightTheme;
|
||||||
|
default:
|
||||||
|
return window.matchMedia('(prefers-color-scheme: dark)').matches
|
||||||
|
? darkTheme
|
||||||
|
: lightTheme;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function nextMode(mode: Mode): Mode {
|
||||||
|
return mode === 'light' ? 'dark' : mode === 'dark' ? 'system' : 'light';
|
||||||
|
}
|
||||||
|
|
||||||
export default App;
|
export default App;
|
||||||
|
@@ -17,13 +17,17 @@ import {
|
|||||||
import useMediaQuery from '@mui/material/useMediaQuery';
|
import useMediaQuery from '@mui/material/useMediaQuery';
|
||||||
import { useTheme } from '@mui/material/styles';
|
import { useTheme } from '@mui/material/styles';
|
||||||
import { Icon } from '@iconify/react';
|
import { Icon } from '@iconify/react';
|
||||||
import DarkModeIcon from '@mui/icons-material/DarkMode';
|
import { Mode } from 'components/App';
|
||||||
|
|
||||||
interface NavbarProps {
|
interface NavbarProps {
|
||||||
onSwitchTheme: () => void;
|
mode: Mode;
|
||||||
|
onChangeMode: () => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
const Navbar: React.FC<NavbarProps> = ({ onSwitchTheme }) => {
|
const Navbar: React.FC<NavbarProps> = ({
|
||||||
|
mode,
|
||||||
|
onChangeMode: onChangeMode
|
||||||
|
}) => {
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const theme = useTheme();
|
const theme = useTheme();
|
||||||
const isMobile = useMediaQuery(theme.breakpoints.down('md'));
|
const isMobile = useMediaQuery(theme.breakpoints.down('md'));
|
||||||
@@ -37,7 +41,19 @@ const Navbar: React.FC<NavbarProps> = ({ onSwitchTheme }) => {
|
|||||||
];
|
];
|
||||||
|
|
||||||
const buttons: ReactNode[] = [
|
const buttons: ReactNode[] = [
|
||||||
<DarkModeIcon onClick={onSwitchTheme} style={{ cursor: 'pointer' }} />,
|
<Icon
|
||||||
|
key={mode}
|
||||||
|
onClick={onChangeMode}
|
||||||
|
style={{ cursor: 'pointer' }}
|
||||||
|
fontSize={30}
|
||||||
|
icon={
|
||||||
|
mode === 'dark'
|
||||||
|
? 'ic:round-dark-mode'
|
||||||
|
: mode === 'light'
|
||||||
|
? 'ic:round-light-mode'
|
||||||
|
: 'ic:round-contrast'
|
||||||
|
}
|
||||||
|
/>,
|
||||||
<Icon
|
<Icon
|
||||||
onClick={() => window.open('https://discord.gg/SDbbn3hT4b', '_blank')}
|
onClick={() => window.open('https://discord.gg/SDbbn3hT4b', '_blank')}
|
||||||
style={{ cursor: 'pointer' }}
|
style={{ cursor: 'pointer' }}
|
||||||
|
Reference in New Issue
Block a user