Skip to content

Commit

Permalink
Track whether the color scheme follows the system setting
Browse files Browse the repository at this point in the history
  • Loading branch information
fwcd committed Oct 7, 2024
1 parent a73d92e commit 274e4a9
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
3 changes: 2 additions & 1 deletion src/constants/LocalStorageKey.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export enum LocalStorageKey {
ColorScheme = 'luna.contexts.colorScheme',
ColorScheme = 'luna.contexts.colorScheme.colorScheme',
ColorSchemeFollowsSystem = 'luna.contexts.colorScheme.followsSystem',
AdminResourcesLayout = 'luna.screens.home.admin.resources.layout',
DisplaysZoom = 'luna.screens.home.displays.zoom',
}
41 changes: 32 additions & 9 deletions src/contexts/ColorSchemeContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,28 +7,31 @@ import {
createContext,
useEffect,
useMemo,
useState,
} from 'react';

interface ColorScheme {
readonly isDark: boolean;
}

function systemDarkQuery(): MediaQueryList {
function systemDarkModeQuery(): MediaQueryList {
return window.matchMedia('(prefers-color-scheme: dark)');
}

function systemColorScheme(): ColorScheme {
const query = systemDarkQuery();
function currentSystemColorScheme(): ColorScheme {
const query = systemDarkModeQuery();
return { isDark: query.matches };
}

export interface ColorSchemeContextValue {
readonly colorScheme: ColorScheme;
readonly followsSystem: boolean;
setColorScheme: Dispatch<SetStateAction<ColorScheme>>;
}

export const ColorSchemeContext = createContext<ColorSchemeContextValue>({
colorScheme: { isDark: false },
followsSystem: true,
setColorScheme() {},
});

Expand All @@ -39,23 +42,43 @@ interface ColorSchemeContextProviderProps {
export function ColorSchemeContextProvider({
children,
}: ColorSchemeContextProviderProps) {
const [systemColorScheme, setSystemColorScheme] = useState<ColorScheme>(
currentSystemColorScheme()
);

useEffect(() => {
systemDarkModeQuery().addEventListener('change', () => {
setSystemColorScheme(currentSystemColorScheme());
});
}, []);

const [colorScheme, setColorScheme] = useLocalStorage<ColorScheme>(
LocalStorageKey.ColorScheme,
systemColorScheme
() => systemColorScheme
);

const [followsSystem, setFollowsSystem] = useLocalStorage<boolean>(
LocalStorageKey.ColorSchemeFollowsSystem,
() => true
);

useEffect(() => {
systemDarkQuery().addEventListener('change', () => {
setColorScheme(systemColorScheme());
});
}, [setColorScheme]);
setFollowsSystem(colorScheme.isDark === systemColorScheme.isDark);
}, [colorScheme, systemColorScheme, setFollowsSystem]);

useEffect(() => {
if (followsSystem) {
setColorScheme(systemColorScheme);
}
}, [followsSystem, systemColorScheme, setColorScheme]);

const value: ColorSchemeContextValue = useMemo(
() => ({
colorScheme,
followsSystem,
setColorScheme,
}),
[colorScheme, setColorScheme]
[colorScheme, followsSystem, setColorScheme]
);

return (
Expand Down

0 comments on commit 274e4a9

Please sign in to comment.