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

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

View File

@@ -1,5 +1,5 @@
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 Icon from '@/Components/Icon/Icon'
import { usePremiumModal } from '@/Hooks/usePremiumModal'
@@ -37,10 +37,6 @@ const ThemesMenuButton: FunctionComponent<Props> = ({ application, item }) => {
if (themeIsLayerableOrNotActive) {
application.mutator.toggleTheme(item.component).catch(console.error)
if (!isThemeLayerable) {
application.setPreference(PrefKey.DarkMode, false)
}
}
} else {
premiumModal.activate(`${item.name} theme`)