Some checks failed
Build pull request / Build pull request (pull_request) Failing after 2m37s
Sender names over the terrain step to the state's ink: same OKLCH hue and chroma, darker by day and dusk, lighter at night, until 4.5:1 on the lightest measured terrain patch (capped at OKLCH L 0.9 at night). Sidebar labels get a darker dusk ink on phones. An explicit theme still wins. The unread divider keeps only its green line. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
178 lines
6.1 KiB
TypeScript
178 lines
6.1 KiB
TypeScript
import { lightTheme } from 'folds';
|
|
import { createContext, useContext, useEffect, useMemo, useState } from 'react';
|
|
import { onDarkFontWeight, onLightFontWeight } from '../../config.css';
|
|
import {
|
|
butterTheme,
|
|
darkTheme,
|
|
midnightSkyTheme,
|
|
noondaySkyTheme,
|
|
silverTheme,
|
|
} from '../../colors.css';
|
|
import '../../gamecult.css';
|
|
import '../../sky.css';
|
|
import { defaultThemeId, settingsAtom } from '../state/settings';
|
|
import { useSetting } from '../state/hooks/settings';
|
|
import { sceneThemeId } from '../features/scene/skyTheme';
|
|
import { sceneInk, type SkyInk } from '../features/scene/skyInk';
|
|
import { useResolvedSkyState, useSceneOn, useSkyState } from './useSkyState';
|
|
import { ScreenSize, useScreenSizeContext } from './useScreenSize';
|
|
|
|
export enum ThemeKind {
|
|
Light = 'light',
|
|
Dark = 'dark',
|
|
}
|
|
|
|
export type Theme = {
|
|
id: string;
|
|
kind: ThemeKind;
|
|
classNames: string[];
|
|
};
|
|
|
|
export const LightTheme: Theme = {
|
|
id: 'light-theme',
|
|
kind: ThemeKind.Light,
|
|
classNames: [lightTheme, onLightFontWeight, 'prism-light'],
|
|
};
|
|
|
|
export const SilverTheme: Theme = {
|
|
id: 'silver-theme',
|
|
kind: ThemeKind.Light,
|
|
classNames: ['silver-theme', silverTheme, onLightFontWeight, 'prism-light'],
|
|
};
|
|
export const DarkTheme: Theme = {
|
|
id: 'dark-theme',
|
|
kind: ThemeKind.Dark,
|
|
classNames: ['dark-theme', darkTheme, onDarkFontWeight, 'prism-dark'],
|
|
};
|
|
export const ButterTheme: Theme = {
|
|
id: 'butter-theme',
|
|
kind: ThemeKind.Dark,
|
|
classNames: ['butter-theme', butterTheme, onDarkFontWeight, 'prism-dark'],
|
|
};
|
|
|
|
export const MidnightSkyTheme: Theme = {
|
|
id: 'midnight-sky',
|
|
kind: ThemeKind.Dark,
|
|
classNames: ['gamecult-theme', 'midnight-sky', midnightSkyTheme, onDarkFontWeight, 'prism-dark'],
|
|
};
|
|
|
|
export const NoondaySkyTheme: Theme = {
|
|
id: 'noonday-sky',
|
|
kind: ThemeKind.Light,
|
|
classNames: ['gamecult-theme', 'noonday-sky', noondaySkyTheme, onLightFontWeight, 'prism-light'],
|
|
};
|
|
|
|
export const useThemes = (): Theme[] => {
|
|
const themes: Theme[] = useMemo(
|
|
() => [MidnightSkyTheme, NoondaySkyTheme, LightTheme, SilverTheme, DarkTheme, ButterTheme],
|
|
[]
|
|
);
|
|
|
|
return themes;
|
|
};
|
|
|
|
export const useThemeNames = (): Record<string, string> =>
|
|
useMemo(
|
|
() => ({
|
|
[MidnightSkyTheme.id]: 'Midnight Sky',
|
|
[NoondaySkyTheme.id]: 'Noonday Sky',
|
|
[LightTheme.id]: 'Light',
|
|
[SilverTheme.id]: 'Silver',
|
|
[DarkTheme.id]: 'Dark',
|
|
[ButterTheme.id]: 'Butter',
|
|
}),
|
|
[]
|
|
);
|
|
|
|
export const useSystemThemeKind = (): ThemeKind => {
|
|
const darkModeQueryList = useMemo(() => window.matchMedia('(prefers-color-scheme: dark)'), []);
|
|
const [themeKind, setThemeKind] = useState<ThemeKind>(
|
|
darkModeQueryList.matches ? ThemeKind.Dark : ThemeKind.Light
|
|
);
|
|
|
|
useEffect(() => {
|
|
const handleMediaQueryChange = () => {
|
|
setThemeKind(darkModeQueryList.matches ? ThemeKind.Dark : ThemeKind.Light);
|
|
};
|
|
|
|
darkModeQueryList.addEventListener('change', handleMediaQueryChange);
|
|
return () => {
|
|
darkModeQueryList.removeEventListener('change', handleMediaQueryChange);
|
|
};
|
|
}, [darkModeQueryList, setThemeKind]);
|
|
|
|
return themeKind;
|
|
};
|
|
|
|
const toTheme = (id: ReturnType<typeof sceneThemeId>): Theme | undefined =>
|
|
id === NoondaySkyTheme.id ? NoondaySkyTheme : id && MidnightSkyTheme;
|
|
|
|
/** A theme the person chose: the system scheme, or one other than the default. A saved Midnight Sky is the default. */
|
|
const chosenTheme = (useSystemTheme: boolean, themeId?: string) =>
|
|
useSystemTheme || (themeId !== undefined && themeId !== defaultThemeId);
|
|
|
|
/** The theme the sky asks for on a page with no theme of its own (signed out), or undefined for none. */
|
|
export const useSkyTheme = (): Theme | undefined =>
|
|
toTheme(sceneThemeId(useSkyState(), useResolvedSkyState(), useSceneOn(), false));
|
|
|
|
/** The sky theme the signed-in page wears, or undefined when the person's own theme wins. */
|
|
const useSceneSkyThemeId = (): ReturnType<typeof sceneThemeId> => {
|
|
const sky = useSkyState();
|
|
const resolved = useResolvedSkyState();
|
|
const sceneOn = useSceneOn();
|
|
const [systemTheme] = useSetting(settingsAtom, 'useSystemTheme');
|
|
const [themeId] = useSetting(settingsAtom, 'themeId');
|
|
// A person's own theme wins over 'auto'; a fixed sky state is itself a choice and wins over the theme.
|
|
return sceneThemeId(sky, resolved, sceneOn, chosenTheme(systemTheme, themeId));
|
|
};
|
|
|
|
/** The ink the page wears over the scene (`sceneInk`, skyInk.ts), or undefined when the theme's own ink stands. */
|
|
export const useSceneInk = (): SkyInk | undefined => {
|
|
const themeId = useSceneSkyThemeId();
|
|
const state = useResolvedSkyState();
|
|
const sceneOn = useSceneOn();
|
|
const phone = useScreenSizeContext() === ScreenSize.Mobile;
|
|
return useMemo(() => sceneInk(themeId, sceneOn, state, phone), [themeId, sceneOn, state, phone]);
|
|
};
|
|
|
|
const SkyInkContext = createContext<SkyInk | undefined>(undefined);
|
|
export const SkyInkContextProvider = SkyInkContext.Provider;
|
|
/** The ink for this sky state and viewport (provided once by the theme manager), or undefined for the theme's own. */
|
|
export const useSkyInk = (): SkyInk | undefined => useContext(SkyInkContext);
|
|
|
|
export const useActiveTheme = (): Theme => {
|
|
const systemThemeKind = useSystemThemeKind();
|
|
const themes = useThemes();
|
|
const [systemTheme] = useSetting(settingsAtom, 'useSystemTheme');
|
|
const [themeId] = useSetting(settingsAtom, 'themeId');
|
|
const [lightThemeId] = useSetting(settingsAtom, 'lightThemeId');
|
|
const [darkThemeId] = useSetting(settingsAtom, 'darkThemeId');
|
|
const skyTheme = toTheme(useSceneSkyThemeId());
|
|
|
|
if (skyTheme) return skyTheme;
|
|
|
|
if (!systemTheme) {
|
|
const selectedTheme = themes.find((theme) => theme.id === themeId) ?? MidnightSkyTheme;
|
|
|
|
return selectedTheme;
|
|
}
|
|
|
|
const selectedTheme =
|
|
systemThemeKind === ThemeKind.Dark
|
|
? themes.find((theme) => theme.id === darkThemeId) ?? MidnightSkyTheme
|
|
: themes.find((theme) => theme.id === lightThemeId) ?? NoondaySkyTheme;
|
|
|
|
return selectedTheme;
|
|
};
|
|
|
|
const ThemeContext = createContext<Theme | null>(null);
|
|
export const ThemeContextProvider = ThemeContext.Provider;
|
|
|
|
export const useTheme = (): Theme => {
|
|
const theme = useContext(ThemeContext);
|
|
if (!theme) {
|
|
throw new Error('No theme provided!');
|
|
}
|
|
|
|
return theme;
|
|
};
|