Skip to content

Commit

Permalink
Simplify theme computation logic.
Browse files Browse the repository at this point in the history
  • Loading branch information
MinhxNguyen7 committed Feb 1, 2024
1 parent 75be871 commit 6263803
Showing 1 changed file with 17 additions and 36 deletions.
53 changes: 17 additions & 36 deletions apps/antalmanac/src/stores/SettingsStore.ts
Original file line number Diff line number Diff line change
@@ -1,67 +1,48 @@
import { create } from 'zustand';
import analyticsEnum, { logAnalytics } from '$lib/analytics';

export type ThemeSetting = 'light' | 'dark' | 'system';

export interface ThemeStore {
/**
* The 'raw' theme, based on the user's selected setting
*/
themeSetting: 'light' | 'dark' | 'system';
themeSetting: ThemeSetting;
/**
* The 'derived' theme, based on user settings and device preferences
*/
appTheme: 'light' | 'dark';
isDark: boolean;

setAppTheme: (themeSetting: 'light' | 'dark' | 'system') => void;
setAppTheme: (themeSetting: ThemeSetting) => void;
}

function computeTheme(themeSetting: string) {
if (themeSetting === 'system') {
return window.matchMedia('(prefers-color-scheme: dark)').matches ? true : false;
} else {
if (themeSetting === 'dark') {
return true;
}
return false;
}
function themeShouldBeDark(themeSetting: ThemeSetting) {
if (themeSetting == 'system') return window.matchMedia('(prefers-color-scheme: dark)').matches;
return themeSetting == 'dark';
}

export const useThemeStore = create<ThemeStore>((set) => {
const themeSetting = typeof Storage !== 'undefined' ? window.localStorage.getItem('theme') ?? 'system' : 'system';

const appTheme =
themeSetting !== 'system'
? themeSetting
: window.matchMedia('(prefers-color-scheme: dark)').matches
? 'dark'
: 'light';

const isDark = computeTheme(themeSetting);
const storedThemeSetting: ThemeSetting = (window.localStorage?.getItem('theme') ?? 'system') as ThemeSetting;
const isDark = themeShouldBeDark(storedThemeSetting);

return {
themeSetting: themeSetting as 'light' | 'dark' | 'system',
appTheme: appTheme as 'light' | 'dark',
isDark,
setAppTheme: (themeSetting) => {
if (typeof Storage !== 'undefined') {
window.localStorage.setItem('theme', themeSetting);
}
themeSetting: storedThemeSetting,
appTheme: isDark ? 'dark' : 'light',
isDark: isDark,

const appTheme =
themeSetting !== 'system'
? themeSetting
: window.matchMedia('(prefers-color-scheme: dark)').matches
? 'dark'
: 'light';
setAppTheme: (themeSetting) => {
window.localStorage?.setItem('theme', themeSetting);

const isDark = computeTheme(themeSetting);
const isDark = themeShouldBeDark(themeSetting);
const appTheme = isDark ? 'dark' : 'light';

set({ appTheme, themeSetting, isDark });

logAnalytics({
category: analyticsEnum.nav.title,
action: analyticsEnum.nav.actions.CHANGE_THEME,
label: appTheme,
label: themeSetting,
});
},
};
Expand Down

0 comments on commit 6263803

Please sign in to comment.