Add option to select system theme

This commit is contained in:
schroda
2024-09-04 23:46:04 +02:00
parent e909fc64fd
commit ba2ce37468
6 changed files with 83 additions and 24 deletions

View File

@@ -817,6 +817,8 @@
} }
}, },
"appearance": { "appearance": {
"device_theme": "Device theme",
"theme": "Theme",
"title": "Appearance" "title": "Appearance"
}, },
"backup": { "backup": {
@@ -912,7 +914,7 @@
} }
}, },
"label": { "label": {
"dark_theme": "Dark theme", "theme": "Theme",
"hide_library_entries": "Hide entries already in library", "hide_library_entries": "Hide entries already in library",
"language_description": "Feel free to translate the project on", "language_description": "Feel free to translate the project on",
"manga_item_width": "Manga item width", "manga_item_width": "Manga item width",

View File

@@ -7,7 +7,7 @@
*/ */
import { Direction, StyledEngineProvider, ThemeProvider } from '@mui/material/styles'; import { Direction, StyledEngineProvider, ThemeProvider } from '@mui/material/styles';
import React, { useMemo, useRef } from 'react'; import React, { useLayoutEffect, useMemo, useRef, useState } from 'react';
import { BrowserRouter as Router } from 'react-router-dom'; import { BrowserRouter as Router } from 'react-router-dom';
import { QueryParamProvider } from 'use-query-params'; import { QueryParamProvider } from 'use-query-params';
import { ReactRouter6Adapter } from 'use-query-params/adapters/react-router-6'; import { ReactRouter6Adapter } from 'use-query-params/adapters/react-router-6';
@@ -18,10 +18,11 @@ import { prefixer } from 'stylis';
import rtlPlugin from 'stylis-plugin-rtl'; import rtlPlugin from 'stylis-plugin-rtl';
import { createTheme } from '@/theme'; import { createTheme } from '@/theme';
import { useLocalStorage } from '@/util/useStorage.tsx'; import { useLocalStorage } from '@/util/useStorage.tsx';
import { DarkTheme } from '@/components/context/DarkTheme'; import { ThemeMode, ThemeModeContext } from '@/components/context/ThemeModeContext.tsx';
import { NavBarContextProvider } from '@/components/navbar/NavBarContextProvider'; import { NavBarContextProvider } from '@/components/navbar/NavBarContextProvider';
import { LibraryOptionsContextProvider } from '@/components/library/LibraryOptionsProvider'; import { LibraryOptionsContextProvider } from '@/components/library/LibraryOptionsProvider';
import { ActiveDevice, DEFAULT_DEVICE, setActiveDevice } from '@/util/device.ts'; import { ActiveDevice, DEFAULT_DEVICE, setActiveDevice } from '@/util/device.ts';
import { MediaQuery } from '@/lib/ui/MediaQuery.tsx';
interface Props { interface Props {
children: React.ReactNode; children: React.ReactNode;
@@ -48,15 +49,22 @@ export const AppContext: React.FC<Props> = ({ children }) => {
directionRef.current = currentDirection; directionRef.current = currentDirection;
} }
const [darkTheme, setDarkTheme] = useLocalStorage<boolean>('darkTheme', true); const [systemThemeMode, setSystemThemeMode] = useState<ThemeMode>(MediaQuery.getSystemThemeMode());
useLayoutEffect(() => {
const unsubscribe = MediaQuery.listenToSystemThemeChange(setSystemThemeMode);
return () => unsubscribe();
}, []);
const [themeMode, setThemeMode] = useLocalStorage<ThemeMode>('themeMode', ThemeMode.SYSTEM);
const [activeDevice, setActiveDeviceContext] = useLocalStorage('activeDevice', DEFAULT_DEVICE); const [activeDevice, setActiveDeviceContext] = useLocalStorage('activeDevice', DEFAULT_DEVICE);
const darkThemeContext = useMemo( const darkThemeContext = useMemo(
() => ({ () => ({
darkTheme, themeMode,
setDarkTheme, setThemeMode,
}), }),
[darkTheme], [themeMode],
); );
const activeDeviceContext = useMemo( const activeDeviceContext = useMemo(
@@ -64,7 +72,10 @@ export const AppContext: React.FC<Props> = ({ children }) => {
[activeDevice], [activeDevice],
); );
const theme = useMemo(() => createTheme(darkTheme, currentDirection), [darkTheme, currentDirection]); const theme = useMemo(
() => createTheme(themeMode, currentDirection),
[themeMode, currentDirection, systemThemeMode],
);
setActiveDevice(activeDevice); setActiveDevice(activeDevice);
@@ -73,7 +84,7 @@ export const AppContext: React.FC<Props> = ({ children }) => {
<StyledEngineProvider injectFirst> <StyledEngineProvider injectFirst>
<CacheProvider value={directionToCache[currentDirection]}> <CacheProvider value={directionToCache[currentDirection]}>
<ThemeProvider theme={theme}> <ThemeProvider theme={theme}>
<DarkTheme.Provider value={darkThemeContext}> <ThemeModeContext.Provider value={darkThemeContext}>
<QueryParamProvider adapter={ReactRouter6Adapter}> <QueryParamProvider adapter={ReactRouter6Adapter}>
<LibraryOptionsContextProvider> <LibraryOptionsContextProvider>
<NavBarContextProvider> <NavBarContextProvider>
@@ -83,7 +94,7 @@ export const AppContext: React.FC<Props> = ({ children }) => {
</NavBarContextProvider> </NavBarContextProvider>
</LibraryOptionsContextProvider> </LibraryOptionsContextProvider>
</QueryParamProvider> </QueryParamProvider>
</DarkTheme.Provider> </ThemeModeContext.Provider>
</ThemeProvider> </ThemeProvider>
</CacheProvider> </CacheProvider>
</StyledEngineProvider> </StyledEngineProvider>

View File

@@ -8,12 +8,18 @@
import React from 'react'; import React from 'react';
export enum ThemeMode {
SYSTEM = 'system',
DARK = 'dark',
LIGHT = 'light',
}
type ContextType = { type ContextType = {
darkTheme: boolean; themeMode: ThemeMode;
setDarkTheme: React.Dispatch<React.SetStateAction<boolean>>; setThemeMode: React.Dispatch<React.SetStateAction<ThemeMode>>;
}; };
export const DarkTheme = React.createContext<ContextType>({ export const ThemeModeContext = React.createContext<ContextType>({
darkTheme: true, themeMode: ThemeMode.SYSTEM,
setDarkTheme: (): void => {}, setThemeMode: (): void => {},
}); });

View File

@@ -9,6 +9,7 @@
import useMediaQuery from '@mui/material/useMediaQuery'; import useMediaQuery from '@mui/material/useMediaQuery';
import { Breakpoint } from '@mui/material/styles'; import { Breakpoint } from '@mui/material/styles';
import { getCurrentTheme } from '@/theme.ts'; import { getCurrentTheme } from '@/theme.ts';
import { ThemeMode } from '@/components/context/ThemeModeContext.tsx';
export class MediaQuery { export class MediaQuery {
static useIsTouchDevice(): boolean { static useIsTouchDevice(): boolean {
@@ -22,4 +23,21 @@ export class MediaQuery {
static useIsMobileWidth(): boolean { static useIsMobileWidth(): boolean {
return this.useIsBelowWidth('sm'); return this.useIsBelowWidth('sm');
} }
static getSystemThemeMode(): Exclude<ThemeMode, 'system'> {
const prefersDarkMode = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches;
return prefersDarkMode ? ThemeMode.DARK : ThemeMode.LIGHT;
}
static listenToSystemThemeChange(onChange: (themeMode: Exclude<ThemeMode, 'system'>) => void): () => void {
const handleSystemThemeModeChange = (e: MediaQueryListEvent) => {
onChange(e.matches ? ThemeMode.DARK : ThemeMode.LIGHT);
};
const matchSystemThemeMode = window.matchMedia('(prefers-color-scheme: dark)');
matchSystemThemeMode.addEventListener('change', handleSystemThemeModeChange);
return () => matchSystemThemeMode.removeEventListener('change', handleSystemThemeModeChange);
}
} }

View File

@@ -11,13 +11,15 @@ import { useContext, useEffect } from 'react';
import List from '@mui/material/List'; import List from '@mui/material/List';
import ListItem from '@mui/material/ListItem'; import ListItem from '@mui/material/ListItem';
import ListItemText from '@mui/material/ListItemText'; import ListItemText from '@mui/material/ListItemText';
import Switch from '@mui/material/Switch'; import MenuItem from '@mui/material/MenuItem';
import ListSubheader from '@mui/material/ListSubheader';
import { NavBarContext } from '@/components/context/NavbarContext.tsx'; import { NavBarContext } from '@/components/context/NavbarContext.tsx';
import { DarkTheme } from '@/components/context/DarkTheme'; import { ThemeMode, ThemeModeContext } from '@/components/context/ThemeModeContext.tsx';
import { Select } from '@/components/atoms/Select.tsx';
export const Appearance = () => { export const Appearance = () => {
const { t } = useTranslation(); const { t } = useTranslation();
const { darkTheme, setDarkTheme } = useContext(DarkTheme); const { themeMode, setThemeMode } = useContext(ThemeModeContext);
const { setTitle, setAction } = useContext(NavBarContext); const { setTitle, setAction } = useContext(NavBarContext);
useEffect(() => { useEffect(() => {
@@ -31,10 +33,26 @@ export const Appearance = () => {
}, [t]); }, [t]);
return ( return (
<List> <List
subheader={
<ListSubheader component="div" id="appearance-theme">
{t('settings.appearance.theme')}
</ListSubheader>
}
>
<ListItem> <ListItem>
<ListItemText primary={t('settings.label.dark_theme')} /> <ListItemText primary={t('settings.appearance.device_theme')} />
<Switch edge="end" checked={darkTheme} onChange={() => setDarkTheme(!darkTheme)} /> <Select<ThemeMode> value={themeMode} onChange={(e) => setThemeMode(e.target.value as ThemeMode)}>
<MenuItem key={ThemeMode.SYSTEM} value={ThemeMode.SYSTEM}>
System
</MenuItem>
<MenuItem key={ThemeMode.DARK} value={ThemeMode.DARK}>
Dark
</MenuItem>
<MenuItem key={ThemeMode.LIGHT} value={ThemeMode.LIGHT}>
Light
</MenuItem>
</Select>
</ListItem> </ListItem>
</List> </List>
); );

View File

@@ -15,23 +15,27 @@ import {
responsiveFontSizes, responsiveFontSizes,
Theme, Theme,
} from '@mui/material/styles'; } from '@mui/material/styles';
import { ThemeMode } from '@/components/context/ThemeModeContext.tsx';
import { MediaQuery } from '@/lib/ui/MediaQuery.tsx';
export const SCROLLBAR_WIDTH = 14; export const SCROLLBAR_WIDTH = 14;
let theme: Theme; let theme: Theme;
export const getCurrentTheme = () => theme; export const getCurrentTheme = () => theme;
export const createTheme = ( export const createTheme = (
dark?: boolean, themeMode: ThemeMode,
direction: Direction = 'ltr', direction: Direction = 'ltr',
color: string = '#5b74ef', color: string = '#5b74ef',
pureBlackMode: boolean = false, pureBlackMode: boolean = false,
) => { ) => {
const isDarkMode = dark || pureBlackMode; const systemMode = MediaQuery.getSystemThemeMode();
const mode = themeMode === ThemeMode.SYSTEM ? systemMode : themeMode;
const isDarkMode = mode === ThemeMode.DARK;
const baseTheme = createMuiTheme({ const baseTheme = createMuiTheme({
direction, direction,
palette: { palette: {
mode: isDarkMode ? 'dark' : 'light', mode,
primary: { primary: {
main: color, main: color,
}, },