Skip to content

Commit

Permalink
fix: dark mode toggling
Browse files Browse the repository at this point in the history
  • Loading branch information
amanharwara committed Oct 6, 2022
1 parent f9ee197 commit 1377846
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
3 changes: 2 additions & 1 deletion packages/ui-services/src/Theme/ThemeManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,6 @@ export class ThemeManager extends AbstractService {
if (theme && !theme.active) {
this.application.mutator.toggleTheme(theme).catch(console.error)
}
void this.application.setPreference(PrefKey.DarkMode, false)
}
}

Expand Down Expand Up @@ -324,6 +323,8 @@ export class ThemeManager extends AbstractService {
}
}
document.getElementsByTagName('head')[0].appendChild(link)

void this.application.setPreference(PrefKey.DarkMode, false)
}

private getBackgroundColor() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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`)
Expand Down

0 comments on commit 1377846

Please sign in to comment.