fix: dark mode toggling

This commit is contained in:
Aman Harwara
2022-10-06 18:53:27 +05:30
parent f9ee197f04
commit 1377846f3f
3 changed files with 11 additions and 8 deletions

View File

@@ -233,7 +233,6 @@ export class ThemeManager extends AbstractService {
if (theme && !theme.active) { if (theme && !theme.active) {
this.application.mutator.toggleTheme(theme).catch(console.error) this.application.mutator.toggleTheme(theme).catch(console.error)
} }
void this.application.setPreference(PrefKey.DarkMode, false)
} }
} }
@@ -324,6 +323,8 @@ export class ThemeManager extends AbstractService {
} }
} }
document.getElementsByTagName('head')[0].appendChild(link) document.getElementsByTagName('head')[0].appendChild(link)
void this.application.setPreference(PrefKey.DarkMode, false)
} }
private getBackgroundColor() { private getBackgroundColor() {

View File

@@ -48,17 +48,23 @@ const QuickSettingsMenu: FunctionComponent<MenuProps> = ({ application, quickSet
const [themes, setThemes] = useState<ThemeItem[]>([]) const [themes, setThemes] = useState<ThemeItem[]>([])
const [toggleableComponents, setToggleableComponents] = useState<SNComponent[]>([]) const [toggleableComponents, setToggleableComponents] = useState<SNComponent[]>([])
const [isDarkModeOn, setDarkModeOn] = useState( const [isDarkModeOn, setDarkModeOn] = useState(() =>
application.getPreference(PrefKey.DarkMode, PrefDefaults[PrefKey.DarkMode]), application.getPreference(PrefKey.DarkMode, PrefDefaults[PrefKey.DarkMode]),
) )
const defaultThemeOn = const defaultThemeOn =
!themes.map((item) => item?.component).find((theme) => theme?.active && !theme.isLayerable()) && !isDarkModeOn !themes.map((item) => item?.component).find((theme) => theme?.active && !theme.isLayerable()) && !isDarkModeOn
useEffect(() => { useEffect(() => {
application.addSingleEventObserver(ApplicationEvent.PreferencesChanged, async () => { const removeObserver = application.addEventObserver(async (event) => {
if (event !== ApplicationEvent.PreferencesChanged) {
return
}
const isDarkModeOn = application.getPreference(PrefKey.DarkMode, PrefDefaults[PrefKey.DarkMode]) const isDarkModeOn = application.getPreference(PrefKey.DarkMode, PrefDefaults[PrefKey.DarkMode])
setDarkModeOn(isDarkModeOn) setDarkModeOn(isDarkModeOn)
}) })
return removeObserver
}, [application]) }, [application])
const prefsButtonRef = useRef<HTMLButtonElement>(null) const prefsButtonRef = useRef<HTMLButtonElement>(null)

View File

@@ -1,5 +1,5 @@
import { WebApplication } from '@/Application/Application' import { WebApplication } from '@/Application/Application'
import { FeatureIdentifier, FeatureStatus, PrefKey } from '@standardnotes/snjs' import { FeatureIdentifier, FeatureStatus } from '@standardnotes/snjs'
import { FunctionComponent, MouseEventHandler, useCallback, useMemo } from 'react' import { FunctionComponent, MouseEventHandler, useCallback, useMemo } from 'react'
import Icon from '@/Components/Icon/Icon' import Icon from '@/Components/Icon/Icon'
import { usePremiumModal } from '@/Hooks/usePremiumModal' import { usePremiumModal } from '@/Hooks/usePremiumModal'
@@ -37,10 +37,6 @@ const ThemesMenuButton: FunctionComponent<Props> = ({ application, item }) => {
if (themeIsLayerableOrNotActive) { if (themeIsLayerableOrNotActive) {
application.mutator.toggleTheme(item.component).catch(console.error) application.mutator.toggleTheme(item.component).catch(console.error)
if (!isThemeLayerable) {
application.setPreference(PrefKey.DarkMode, false)
}
} }
} else { } else {
premiumModal.activate(`${item.name} theme`) premiumModal.activate(`${item.name} theme`)