Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

mobileThemeRuntime.ts

75 lines2.3 KB

1import { resolveTextScaleVariables } from "./appearancePreferences";
2import { BUILT_IN_THEME_IDS, type BuiltInThemeId } from "@t3tools/shared/themePalettes";
3import {
4 DEFAULT_MOBILE_THEME_ID,
5 type MobileThemeAppearance,
6 type MobileThemeId,
7 type MobileThemeMode,
8} from "./mobileTheme";
9​
10export type MobileUniwindThemeName =
11 | MobileThemeAppearance
12 | `${BuiltInThemeId}-${MobileThemeAppearance}`;
13​
14export interface MobileThemeRuntimeState {
15 readonly baseFontSize: number;
16 readonly themeAppearance: MobileThemeAppearance;
17 readonly themeMode: MobileThemeMode;
19​
20export type MobileThemeRuntimeOperation =
21 | {
22 readonly kind: "update-text-variables";
23 readonly themeName: "light" | "dark" | MobileUniwindThemeName;
24 readonly variables: Readonly<Record<string, number>>;
25 }
26 | {
27 readonly kind: "set-appearance-mode";
28 readonly appearance: MobileThemeAppearance;
29 readonly themeMode: MobileThemeMode;
30 };
31​
32const UNIWIND_THEME_NAMES: ReadonlyArray<"light" | "dark" | MobileUniwindThemeName> = [
33 "light",
34 "dark",
35 ...BUILT_IN_THEME_IDS.flatMap((themeId) => [
36 `${themeId}-light` as const,
37 `${themeId}-dark` as const,
38 ]),
39];
40​
41export function getMobileUniwindThemeName(
42 themeId: MobileThemeId,
43 appearance: MobileThemeAppearance,
44): MobileUniwindThemeName {
45 return themeId === DEFAULT_MOBILE_THEME_ID ? appearance : `${themeId}-${appearance}`;
47​
48/**
49 * Plans imperative runtime work separately from theme selection. Palette
50 * changes are handled by one root ScopedTheme render; only typography and the
51 * native appearance override need imperative Uniwind/React Native updates.
52 */
53export function createMobileThemeRuntimeOperations(
54 previous: MobileThemeRuntimeState | null,
55 next: MobileThemeRuntimeState,
56): ReadonlyArray<MobileThemeRuntimeOperation> {
57 const operations: MobileThemeRuntimeOperation[] = [];
58​
59 if (previous === null || previous.baseFontSize !== next.baseFontSize) {
60 const variables = resolveTextScaleVariables(next.baseFontSize);
61 for (const themeName of UNIWIND_THEME_NAMES) {
62 operations.push({ kind: "update-text-variables", themeName, variables });
63 }
64 }
65​
66 if (previous === null || previous.themeMode !== next.themeMode) {
67 operations.push({
68 kind: "set-appearance-mode",
69 appearance: next.themeAppearance,
70 themeMode: next.themeMode,
71 });
72 }
73​
74 return operations;