diff --git a/src/app/organisms/settings/Settings.jsx b/src/app/organisms/settings/Settings.jsx index e31af8f..9a3db0f 100644 --- a/src/app/organisms/settings/Settings.jsx +++ b/src/app/organisms/settings/Settings.jsx @@ -5,7 +5,7 @@ import './Settings.scss'; import initMatrix from '../../../client/initMatrix'; import cons from '../../../client/state/cons'; import settings from '../../../client/state/settings'; -import { toggleMarkdown, toggleMembershipEvents, toggleNickAvatarEvents } from '../../../client/action/settings'; +import { toggleSystemTheme, toggleMarkdown, toggleMembershipEvents, toggleNickAvatarEvents } from '../../../client/action/settings'; import logout from '../../../client/action/logout'; import Text from '../../atoms/text/Text'; @@ -49,20 +49,34 @@ function AppearanceSection() { return (
settings.setTheme(index)} + title="Follow system theme" + options={( + { toggleSystemTheme(); updateState({}); }} /> )} + content={Use light or dark mode based on the system's settings.} /> + {(() => { + if (!settings.useSystemTheme) { + return settings.setTheme(index)} + /> + )} + /> + } + })()} { + this.useSystemTheme = !this.useSystemTheme; + setSettings('useSystemTheme', this.useSystemTheme); + const appBody = document.getElementById('appBody'); + + if (this.useSystemTheme) { + appBody.classList.add('system-theme'); + this.themes.forEach((themeName) => { + if (themeName === '') return; + appBody.classList.remove(themeName); + }); + } else { + appBody.classList.remove('system-theme'); + this.setTheme(this.themeIndex); + } + + this.emit(cons.events.settings.SYSTEM_THEME_TOGGLED, this.useSystemTheme); + }, [cons.actions.settings.TOGGLE_MARKDOWN]: () => { this.isMarkdown = !this.isMarkdown; setSettings('isMarkdown', this.isMarkdown); diff --git a/src/index.scss b/src/index.scss index 52688e8..e24af32 100644 --- a/src/index.scss +++ b/src/index.scss @@ -202,8 +202,7 @@ --bg-surface-extra-low-transparent: hsla(0, 0%, 91%, 0); } -.dark-theme, -.butter-theme { +@mixin dark-mode() { /* background color | --bg-[background type]: value */ --bg-surface: hsl(208, 8%, 20%); --bg-surface-transparent: hsla(208, 8%, 20%, 0); @@ -290,6 +289,17 @@ --font-secondary: 'Inter', 'Roboto', sans-serif; } +.dark-theme, +.butter-theme { + @include dark-mode(); +} + +@media (prefers-color-scheme: dark) { + .system-theme { + @include dark-mode(); + } +} + .butter-theme { /* background color | --bg-[background type]: value */ --bg-surface: hsl(64, 6%, 14%);