Dark mode toggle in settings

This commit is contained in:
Ben Grant 2021-04-12 12:09:10 +10:00
parent 5fa0370b86
commit 01a9b55b6a
5 changed files with 23 additions and 3 deletions

1
.gitignore vendored
View file

@ -1 +1,2 @@
/graphics /graphics
.DS_Store

View file

@ -1,4 +1,4 @@
import { useState, Suspense, lazy } from 'react'; import { useState, useEffect, Suspense, lazy } from 'react';
import { import {
BrowserRouter, BrowserRouter,
Switch, Switch,
@ -8,16 +8,22 @@ import { ThemeProvider, Global } from '@emotion/react';
import { Settings, Loading } from 'components'; import { Settings, Loading } from 'components';
import { useSettingsStore } from 'stores';
import theme from 'theme'; import theme from 'theme';
const Home = lazy(() => import('pages/Home/Home')); const Home = lazy(() => import('pages/Home/Home'));
const Event = lazy(() => import('pages/Event/Event')); const Event = lazy(() => import('pages/Event/Event'));
const App = () => { const App = () => {
const colortheme = useSettingsStore(state => state.theme);
const darkQuery = window.matchMedia('(prefers-color-scheme: dark)'); const darkQuery = window.matchMedia('(prefers-color-scheme: dark)');
const [isDark, setIsDark] = useState(darkQuery.matches); const [isDark, setIsDark] = useState(darkQuery.matches);
darkQuery.addListener(e => setIsDark(e.matches)); darkQuery.addListener(e => colortheme === 'System' && setIsDark(e.matches));
useEffect(() => {
setIsDark(colortheme === 'System' ? darkQuery.matches : colortheme === 'Dark');
}, [colortheme]);
return ( return (
<BrowserRouter> <BrowserRouter>

View file

@ -49,6 +49,15 @@ const Settings = () => {
value={store.timeFormat} value={store.timeFormat}
onChange={value => store.setTimeFormat(value)} onChange={value => store.setTimeFormat(value)}
/> />
<ToggleField
label="Theme"
name="theme"
id="theme"
options={['System', 'Light', 'Dark']}
value={store.theme}
onChange={value => store.setTheme(value)}
/>
</Modal> </Modal>
</> </>
); );

View file

@ -50,7 +50,9 @@ export const Modal = styled.div`
top: 70px; top: 70px;
right: 12px; right: 12px;
background-color: ${props => props.theme.background}; background-color: ${props => props.theme.background};
border: 1px solid ${props => props.theme.primaryBackground}; ${props => props.theme.mode === 'dark' && `
border: 1px solid ${props.theme.primaryBackground};
`}
z-index: 150; z-index: 150;
padding: 10px 18px; padding: 10px 18px;
border-radius: 3px; border-radius: 3px;

View file

@ -5,9 +5,11 @@ export const useSettingsStore = create(persist(
set => ({ set => ({
weekStart: 0, weekStart: 0,
timeFormat: '12h', timeFormat: '12h',
theme: 'System',
setWeekStart: weekStart => set({ weekStart }), setWeekStart: weekStart => set({ weekStart }),
setTimeFormat: timeFormat => set({ timeFormat }), setTimeFormat: timeFormat => set({ timeFormat }),
setTheme: theme => set({ theme }),
}), }),
{ name: 'crabfit-settings' }, { name: 'crabfit-settings' },
)); ));