From 2093f1538bba13df31d34bc58fda80eea2aa8077 Mon Sep 17 00:00:00 2001 From: Jan Keromnes Date: Tue, 13 Apr 2021 12:56:16 +0000 Subject: [PATCH] [dashboard] Implement 'System' color theme option --- .../dashboard/src/settings/Preferences.tsx | 22 +++++++++++++------ 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/components/dashboard/src/settings/Preferences.tsx b/components/dashboard/src/settings/Preferences.tsx index aec06e938939a0..c7be381596db2b 100644 --- a/components/dashboard/src/settings/Preferences.tsx +++ b/components/dashboard/src/settings/Preferences.tsx @@ -13,6 +13,8 @@ import vscode from '../images/vscode.svg'; import { PageWithSubMenu } from "../components/PageWithSubMenu"; import settingsMenu from "./settings-menu"; +type ColorTheme = 'system' | 'dark' | 'light'; + export default function Preferences() { const { user } = useContext(UserContext); const [ defaultIde, setDefaultIde ] = useState(user?.additionalData?.ideSettings?.defaultIde || 'theia'); @@ -28,11 +30,16 @@ export default function Preferences() { await getGitpodService().server.updateLoggedInUser({ additionalData }); setDefaultIde(value); } - const [ isDarkTheme, setIsDarkTheme ] = useState(document.documentElement.classList.contains('dark')); - const actuallySetIsDarkTheme = (is: boolean) => { - document.documentElement.classList.toggle('dark', is); - localStorage.theme = is ? 'dark' : 'light'; - setIsDarkTheme(is); + const [ theme, setTheme ] = useState(localStorage.theme || 'system'); + const actuallySetTheme = (theme: ColorTheme) => { + if (theme === 'light' || theme === 'dark') { + localStorage.theme = theme; + } else { + localStorage.removeItem('theme'); + } + const isDark = localStorage.theme === 'dark' || (!('theme' in localStorage) && window.matchMedia('(prefers-color-scheme: dark)').matches); + document.documentElement.classList.toggle('dark', isDark); + setTheme(theme); } return
@@ -54,8 +61,9 @@ export default function Preferences() {

Color Scheme

Light or dark?

- - + + +
;