diff --git a/src/app/features/estate/EstateNavigator.tsx b/src/app/features/estate/EstateNavigator.tsx index 1a9ca8ee..2cbbfe4a 100644 --- a/src/app/features/estate/EstateNavigator.tsx +++ b/src/app/features/estate/EstateNavigator.tsx @@ -42,6 +42,7 @@ import { EstateRoom, EstateTier } from './buildEstate'; import { activateEstateRoom, awaitMyJoin } from './joinEstateRoom'; import { useEstate, useEstateRootId } from './useEstate'; import * as css from './EstateNavigator.css'; +import { SkyStateControl } from '../scene/SkyStateControl'; /** How long a tap waits for the client store to hold the room it just joined. */ const JOIN_ARRIVAL_TIMEOUT_MS = 20000; @@ -295,6 +296,7 @@ export function EstateNavigator() { {title} + = { + auto: 'Auto', + day: 'Day', + dusk: 'Dusk', + night: 'Night', +}; + +/** + * One title-bar button that opens the four sky states. A single button, not a row, so the bar + * still fits a 320 px phone beside the search button; the menu items carry the state names. + */ +export function SkyStateControl() { + const state = useSkyState(); + const [cords, setCords] = useState(); + + const open: MouseEventHandler = (evt) => { + setCords(evt.currentTarget.getBoundingClientRect()); + }; + + return ( + setCords(undefined), + clickOutsideDeactivates: true, + isKeyForward: (evt: KeyboardEvent) => evt.key === 'ArrowDown', + isKeyBackward: (evt: KeyboardEvent) => evt.key === 'ArrowUp', + escapeDeactivates: stopPropagation, + }} + > + + + {SKY_STATES.map((s) => ( + { + setSkyState(s); + setCords(undefined); + }} + after={state === s ? : undefined} + > + + {LABEL[s]} + + + ))} + + + + } + > + + + + + ); +} diff --git a/src/app/features/scene/skyTheme.test.ts b/src/app/features/scene/skyTheme.test.ts new file mode 100644 index 00000000..74b3280b --- /dev/null +++ b/src/app/features/scene/skyTheme.test.ts @@ -0,0 +1,59 @@ +import { describe, expect, it } from 'vitest'; +import { baked } from '../../../sky/baked'; +import { AA, contrast, luminance } from '../../../sky/skyTokens.js'; +import { SKY_STATES, skyBackdrop, type SkyState } from './skyState'; +import { skyThemeId } from './skyTheme'; + +const SKY = { 'midnight-sky': baked.midnight, 'noonday-sky': baked.noonday } as const; +const ratio = (a: number, b: number) => (Math.max(a, b) + 0.05) / (Math.min(a, b) + 0.05); + +describe('skyThemeId', () => { + it('leaves the theme alone in auto and picks one for every fixed state', () => { + expect(skyThemeId('auto')).toBeUndefined(); + SKY_STATES.filter((s) => s !== 'auto').forEach((s) => { + expect(skyThemeId(s)).toBeDefined(); + }); + }); + + it('lights day and dusk with dark ink and night with light ink', () => { + expect(skyThemeId('day')).toBe('noonday-sky'); + expect(skyThemeId('dusk')).toBe('noonday-sky'); + expect(skyThemeId('night')).toBe('midnight-sky'); + }); +}); + +describe('contrast of the theme a state picks', () => { + const picked = SKY_STATES.flatMap((s) => { + const id = skyThemeId(s); + return id ? [[s, SKY[id]] as const] : []; + }); + + it.each(picked)('%s: every container carries its text at AA', (_state, sky) => { + Object.values(sky.tokens).forEach((group) => { + expect(contrast(group.OnContainer, group.Container)).toBeGreaterThanOrEqual(AA); + }); + }); + + it.each(picked)('%s: links read at AA on the background surface', (_state, sky) => { + expect(contrast(sky.link, sky.tokens.Background.Container)).toBeGreaterThanOrEqual(AA); + }); + + // Bare sky has no surface under it. Text may sit there only where the ink clears AA against + // both the darkest and the lightest sky the state can show; anywhere else it needs a surface. + const onBareSky = (state: SkyState): number => { + const id = skyThemeId(state); + if (!id) return 0; + const ink = luminance(SKY[id].tokens.Background.OnContainer); + const { darkest, lightest } = skyBackdrop(state); + return Math.min(ratio(ink, darkest), ratio(ink, lightest)); + }; + + it('day and night text on bare sky clears AA', () => { + expect(onBareSky('day')).toBeGreaterThanOrEqual(AA); + expect(onBareSky('night')).toBeGreaterThanOrEqual(AA); + }); + + it('dusk text on bare sky does not, so dusk text stays on surfaces', () => { + expect(onBareSky('dusk')).toBeLessThan(AA); + }); +}); diff --git a/src/app/features/scene/skyTheme.ts b/src/app/features/scene/skyTheme.ts new file mode 100644 index 00000000..ebd4504f --- /dev/null +++ b/src/app/features/scene/skyTheme.ts @@ -0,0 +1,18 @@ +// Which theme a sky state lights the page with. 'auto' picks none: the page keeps the theme it +// would have had (the user's choice when signed in, Midnight Sky when signed out). +// +// Day and Dusk take the dark ink of Noonday Sky, Night the light ink of Midnight Sky. Dusk's +// gradient runs from near-black to bright orange, so no single ink reads on all of it; Dusk +// text therefore only ever sits on a theme surface, never on bare sky (skyTheme.test.ts). +import type { SkyState } from './skyState'; + +export type SkyThemeId = 'midnight-sky' | 'noonday-sky'; + +const BY_STATE: Record, SkyThemeId> = { + day: 'noonday-sky', + dusk: 'noonday-sky', + night: 'midnight-sky', +}; + +export const skyThemeId = (state: SkyState): SkyThemeId | undefined => + state === 'auto' ? undefined : BY_STATE[state]; diff --git a/src/app/hooks/useSkyState.ts b/src/app/hooks/useSkyState.ts new file mode 100644 index 00000000..985e2da2 --- /dev/null +++ b/src/app/hooks/useSkyState.ts @@ -0,0 +1,4 @@ +import { useSyncExternalStore } from 'react'; +import { getSkyState, subscribe, type SkyState } from '../features/scene/skyState'; + +export const useSkyState = (): SkyState => useSyncExternalStore(subscribe, getSkyState); diff --git a/src/app/hooks/useTheme.ts b/src/app/hooks/useTheme.ts index d3663283..bf556dd0 100644 --- a/src/app/hooks/useTheme.ts +++ b/src/app/hooks/useTheme.ts @@ -12,6 +12,8 @@ import '../../gamecult.css'; import '../../sky.css'; import { settingsAtom } from '../state/settings'; import { useSetting } from '../state/hooks/settings'; +import { skyThemeId } from '../features/scene/skyTheme'; +import { useSkyState } from './useSkyState'; export enum ThemeKind { Light = 'light', @@ -100,7 +102,14 @@ export const useSystemThemeKind = (): ThemeKind => { return themeKind; }; +/** The theme the sky state asks for, or undefined while it is 'auto'. */ +export const useSkyTheme = (): Theme | undefined => { + const id = skyThemeId(useSkyState()); + return id === NoondaySkyTheme.id ? NoondaySkyTheme : id && MidnightSkyTheme; +}; + export const useActiveTheme = (): Theme => { + const skyTheme = useSkyTheme(); const systemThemeKind = useSystemThemeKind(); const themes = useThemes(); const [systemTheme] = useSetting(settingsAtom, 'useSystemTheme'); @@ -108,6 +117,8 @@ export const useActiveTheme = (): Theme => { const [lightThemeId] = useSetting(settingsAtom, 'lightThemeId'); const [darkThemeId] = useSetting(settingsAtom, 'darkThemeId'); + if (skyTheme) return skyTheme; + if (!systemTheme) { const selectedTheme = themes.find((theme) => theme.id === themeId) ?? MidnightSkyTheme; diff --git a/src/app/pages/ThemeManager.tsx b/src/app/pages/ThemeManager.tsx index e730b00d..b7c50d93 100644 --- a/src/app/pages/ThemeManager.tsx +++ b/src/app/pages/ThemeManager.tsx @@ -4,17 +4,21 @@ import { MidnightSkyTheme, ThemeContextProvider, useActiveTheme, + useSkyTheme, } from '../hooks/useTheme'; import { useSetting } from '../state/hooks/settings'; import { settingsAtom } from '../state/settings'; export function UnAuthRouteThemeManager() { + // Signed-out pages are Midnight Sky unless the sky state picks otherwise; they ignore the + // system scheme. + const theme = useSkyTheme() ?? MidnightSkyTheme; + useEffect(() => { document.body.className = ''; document.body.classList.add(configClass, varsClass); - // Signed-out pages are always Midnight Sky and ignore the system scheme. - document.body.classList.add(...MidnightSkyTheme.classNames); - }, []); + document.body.classList.add(...theme.classNames); + }, [theme]); return null; }