Sky-state control in the estate title bar; the state picks the theme
Some checks failed
Build pull request / Build pull request (pull_request) Has been cancelled
Some checks failed
Build pull request / Build pull request (pull_request) Has been cancelled
One button beside Find a room opens Auto / Day / Dusk / Night (fits a 320 px bar). Day and Dusk take Noonday Sky's dark ink, Night Midnight Sky's light ink, Auto keeps the theme the page would have had; signed-out pages follow the state too. skyTheme.test.ts checks contrast from the baked tokens: day and night ink clear AA on bare sky, every container carries its text, links clear AA on the surface, and dusk text on bare sky does not, so it stays on surfaces. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
a129866236
commit
d3488f2348
7 changed files with 181 additions and 3 deletions
|
|
@ -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}
|
||||
</Text>
|
||||
</Box>
|
||||
<SkyStateControl />
|
||||
<IconButton
|
||||
variant="Background"
|
||||
aria-label="Find a room"
|
||||
|
|
|
|||
80
src/app/features/scene/SkyStateControl.tsx
Normal file
80
src/app/features/scene/SkyStateControl.tsx
Normal file
|
|
@ -0,0 +1,80 @@
|
|||
import React, { MouseEventHandler, useState } from 'react';
|
||||
import { Box, config, Icon, IconButton, Icons, Menu, MenuItem, PopOut, RectCords, Text } from 'folds';
|
||||
import FocusTrap from 'focus-trap-react';
|
||||
import { stopPropagation } from '../../utils/keyboard';
|
||||
import { useSkyState } from '../../hooks/useSkyState';
|
||||
import { SKY_STATES, setSkyState, type SkyState } from './skyState';
|
||||
|
||||
const LABEL: Record<SkyState, string> = {
|
||||
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<RectCords>();
|
||||
|
||||
const open: MouseEventHandler<HTMLButtonElement> = (evt) => {
|
||||
setCords(evt.currentTarget.getBoundingClientRect());
|
||||
};
|
||||
|
||||
return (
|
||||
<PopOut
|
||||
anchor={cords}
|
||||
position="Bottom"
|
||||
align="End"
|
||||
content={
|
||||
<FocusTrap
|
||||
focusTrapOptions={{
|
||||
initialFocus: false,
|
||||
onDeactivate: () => setCords(undefined),
|
||||
clickOutsideDeactivates: true,
|
||||
isKeyForward: (evt: KeyboardEvent) => evt.key === 'ArrowDown',
|
||||
isKeyBackward: (evt: KeyboardEvent) => evt.key === 'ArrowUp',
|
||||
escapeDeactivates: stopPropagation,
|
||||
}}
|
||||
>
|
||||
<Menu>
|
||||
<Box direction="Column" gap="100" style={{ padding: config.space.S100 }}>
|
||||
{SKY_STATES.map((s) => (
|
||||
<MenuItem
|
||||
key={s}
|
||||
size="300"
|
||||
variant="Surface"
|
||||
radii="300"
|
||||
aria-pressed={state === s}
|
||||
onClick={() => {
|
||||
setSkyState(s);
|
||||
setCords(undefined);
|
||||
}}
|
||||
after={state === s ? <Icon size="100" src={Icons.Check} /> : undefined}
|
||||
>
|
||||
<Box grow="Yes">
|
||||
<Text size="T300">{LABEL[s]}</Text>
|
||||
</Box>
|
||||
</MenuItem>
|
||||
))}
|
||||
</Box>
|
||||
</Menu>
|
||||
</FocusTrap>
|
||||
}
|
||||
>
|
||||
<IconButton
|
||||
variant="Background"
|
||||
aria-label={`Sky: ${LABEL[state]}`}
|
||||
title={`Sky: ${LABEL[state]}`}
|
||||
aria-haspopup="menu"
|
||||
aria-pressed={!!cords}
|
||||
onClick={open}
|
||||
>
|
||||
<Icon src={Icons.Bulb} size="200" />
|
||||
</IconButton>
|
||||
</PopOut>
|
||||
);
|
||||
}
|
||||
59
src/app/features/scene/skyTheme.test.ts
Normal file
59
src/app/features/scene/skyTheme.test.ts
Normal file
|
|
@ -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);
|
||||
});
|
||||
});
|
||||
18
src/app/features/scene/skyTheme.ts
Normal file
18
src/app/features/scene/skyTheme.ts
Normal file
|
|
@ -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<Exclude<SkyState, 'auto'>, SkyThemeId> = {
|
||||
day: 'noonday-sky',
|
||||
dusk: 'noonday-sky',
|
||||
night: 'midnight-sky',
|
||||
};
|
||||
|
||||
export const skyThemeId = (state: SkyState): SkyThemeId | undefined =>
|
||||
state === 'auto' ? undefined : BY_STATE[state];
|
||||
4
src/app/hooks/useSkyState.ts
Normal file
4
src/app/hooks/useSkyState.ts
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
import { useSyncExternalStore } from 'react';
|
||||
import { getSkyState, subscribe, type SkyState } from '../features/scene/skyState';
|
||||
|
||||
export const useSkyState = (): SkyState => useSyncExternalStore(subscribe, getSkyState);
|
||||
|
|
@ -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;
|
||||
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue