Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor dark mode logic #875

Merged
merged 2 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading