Skip to content

Commit

Permalink
Refactor dark mode logic (#875)
Browse files Browse the repository at this point in the history
  • Loading branch information
MinhxNguyen7 authored Feb 2, 2024
1 parent 46c93f3 commit cc71fdc
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 37 deletions.
1 change: 0 additions & 1 deletion apps/antalmanac/src/lib/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import React from 'react';

import { PETERPORTAL_GRAPHQL_ENDPOINT } from './api/endpoints';
import { openSnackbar } from '$actions/AppStoreActions';
import { useThemeStore } from '$stores/SettingsStore';

export async function queryGraphQL<PromiseReturnType>(queryString: string): Promise<PromiseReturnType | null> {
const query = JSON.stringify({
Expand Down
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 cc71fdc

Please sign in to comment.