From d058a81be449afc49c5f843eb6054df738e59e90 Mon Sep 17 00:00:00 2001 From: AkiraFukushima Date: Tue, 12 Mar 2024 23:02:18 +0900 Subject: [PATCH] refs #4887 Switch dark/light mode --- locales/en/translation.json | 3 ++ renderer/components/Settings.tsx | 42 ++++++++++++++++++- renderer/components/layouts/account.tsx | 15 ++++++- renderer/components/timelines/status/Card.tsx | 2 +- .../components/timelines/status/Status.tsx | 14 +++---- renderer/pages/_document.tsx | 16 +++++++ renderer/tailwind.config.js | 1 + 7 files changed, 83 insertions(+), 10 deletions(-) create mode 100644 renderer/pages/_document.tsx diff --git a/locales/en/translation.json b/locales/en/translation.json index 4f686787..269b2bef 100644 --- a/locales/en/translation.json +++ b/locales/en/translation.json @@ -167,6 +167,9 @@ "title": "Settings", "language": "Language", "font_size": "Font size", + "mode": "Color mode", + "dark_mode": "Dark mode", + "light_mode": "Light mode", "theme": "Color theme" }, "thirdparty": { diff --git a/renderer/components/Settings.tsx b/renderer/components/Settings.tsx index 69190110..bad685bf 100644 --- a/renderer/components/Settings.tsx +++ b/renderer/components/Settings.tsx @@ -1,5 +1,5 @@ import { localeType } from '@/provider/i18n' -import { Dialog, DialogBody, DialogHeader, Input, Option, Select, Typography } from '@material-tailwind/react' +import { Dialog, DialogBody, DialogHeader, Input, Option, Radio, Select, Typography } from '@material-tailwind/react' import { ChangeEvent, useEffect, useState } from 'react' import { FormattedMessage } from 'react-intl' @@ -55,6 +55,7 @@ export default function Settings(props: Props) { const [language, setLanguage] = useState('en') const [fontSize, setFontSize] = useState(16) const [theme, setTheme] = useState('theme-blue') + const [isDark, setIsDark] = useState(false) useEffect(() => { if (typeof localStorage !== 'undefined') { @@ -64,6 +65,12 @@ export default function Settings(props: Props) { } else { setLanguage('en') } + const dark = localStorage.getItem('color-mode') + if (dark === 'dark') { + setIsDark(true) + } else { + setIsDark(false) + } } }, []) @@ -91,6 +98,18 @@ export default function Settings(props: Props) { props.reloadSettings() } + const modeChanged = (isDark: boolean) => { + setIsDark(isDark) + if (typeof localStorage !== 'undefined') { + if (isDark) { + localStorage.setItem('color-mode', 'dark') + } else { + localStorage.setItem('color-mode', 'light') + } + } + props.reloadSettings() + } + return ( @@ -124,6 +143,27 @@ export default function Settings(props: Props) { +
+
+ + + +
+
+ } + onClick={() => modeChanged(true)} + defaultChecked={isDark} + /> + } + onClick={() => modeChanged(false)} + defaultChecked={!isDark} + /> +
+
diff --git a/renderer/components/layouts/account.tsx b/renderer/components/layouts/account.tsx index 073658e3..ebe0f791 100644 --- a/renderer/components/layouts/account.tsx +++ b/renderer/components/layouts/account.tsx @@ -34,6 +34,7 @@ export default function Layout({ children }: LayoutProps) { const [style, setStyle] = useState({}) const [openPopover, setOpenPopover] = useState(false) const [theme, setTheme] = useState('theme-blue') + const [isDark, setIsDark] = useState(false) const { switchLang } = useContext(Context) const router = useRouter() @@ -69,6 +70,11 @@ export default function Layout({ children }: LayoutProps) { } }, []) + useEffect(() => { + console.log('isDark', isDark) + document.body.className = isDark ? 'dark' : 'light' + }, [isDark]) + const closeNewModal = async () => { const acct = await db.accounts.toArray() setAccounts(acct) @@ -121,12 +127,19 @@ export default function Layout({ children }: LayoutProps) { if (t && t.length > 0) { setTheme(t) } + const dark = localStorage.getItem('color-mode') + console.log(dark) + if (dark && dark === 'dark') { + setIsDark(true) + } else { + setIsDark(false) + } } } return (
-
+