Open History to see the last commit
1import { resolveTextScaleVariables } from "./appearancePreferences";
2import {
3 resolveFontFamilyVariables,
4 type MobileFontId,
5 type MobileMonoFontId,
6} from "./fontFamilies";
7import { BUILT_IN_THEME_IDS, type BuiltInThemeId } from "@t3tools/shared/themePalettes";
8import {
9 DEFAULT_MOBILE_THEME_ID,
10 type MobileThemeAppearance,
11 type MobileThemeId,
12 type MobileThemeMode,
13} from "./mobileTheme";
14
15export type MobileUniwindThemeName =
16 | MobileThemeAppearance
17 | `${BuiltInThemeId}-${MobileThemeAppearance}`;
18
19export interface MobileThemeRuntimeState {
20 readonly baseFontSize: number;
21 readonly fontId: MobileFontId;
22 readonly monoFontId: MobileMonoFontId;
23 readonly themeAppearance: MobileThemeAppearance;
24 readonly themeMode: MobileThemeMode;
25}
26
27export type MobileThemeRuntimeOperation =
28 | {
29 readonly kind: "update-text-variables";
30 readonly themeName: "light" | "dark" | MobileUniwindThemeName;
31 readonly variables: Readonly<Record<string, string | number>>;
32 }
33 | {
34 readonly kind: "set-appearance-mode";
35 readonly appearance: MobileThemeAppearance;
36 readonly themeMode: MobileThemeMode;
37 };
38
39const UNIWIND_THEME_NAMES: ReadonlyArray<"light" | "dark" | MobileUniwindThemeName> = [
40 "light",
41 "dark",
42 ...BUILT_IN_THEME_IDS.flatMap((themeId) => [
43 `${themeId}-light` as const,
44 `${themeId}-dark` as const,
45 ]),
46];
47
48export function getMobileUniwindThemeName(
49 themeId: MobileThemeId,
50 appearance: MobileThemeAppearance,
51): MobileUniwindThemeName {
52 return themeId === DEFAULT_MOBILE_THEME_ID || themeId === "material-you"
53 ? appearance
54 : `${themeId}-${appearance}`;
55}
56
57export function createMobileThemeRuntimeOperations(
58 previous: MobileThemeRuntimeState | null,
59 next: MobileThemeRuntimeState,
60): ReadonlyArray<MobileThemeRuntimeOperation> {
61 const operations: MobileThemeRuntimeOperation[] = [];
62
63 if (
64 previous === null ||
65 previous.baseFontSize !== next.baseFontSize ||
66 previous.fontId !== next.fontId ||
67 previous.monoFontId !== next.monoFontId
68 ) {
69 const variables = {
70 ...resolveTextScaleVariables(next.baseFontSize),
71 ...resolveFontFamilyVariables(next.fontId, next.monoFontId),
72 };
73 for (const themeName of UNIWIND_THEME_NAMES) {
74 operations.push({ kind: "update-text-variables", themeName, variables });
75 }
76 }
77
78 if (previous === null || previous.themeMode !== next.themeMode) {
79 operations.push({
80 kind: "set-appearance-mode",
81 appearance: next.themeAppearance,
82 themeMode: next.themeMode,
83 });
84 }
85
86 return operations;
87}