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

useHtmlRenderTheme.ts

49 lines1.8 KB

1import { HTML_RENDER_DEFAULT_FONTS, htmlRenderTheme } from "@t3tools/shared/htmlRender";
2import { useMemo, useSyncExternalStore } from "react";
3​
4import { appearanceFontStack } from "../appearanceFonts";
5import {
6 getStandardThemeColors,
7 getThemeColorsForMode,
8 getThemeDefinition,
9 resolveThemeHalf,
10 subscribeToCustomThemes,
11 type ThemeAppearance,
12 type ThemeHalves,
13 type ThemePreference,
14} from "../themePalette";
15import { useClientSettings } from "./useSettings";
16import { useTheme } from "./useTheme";
17​
18/** The palette `applyTheme` paints for a preference, or the stock look when no theme applies. */
19function resolveActiveThemeColors(
20 theme: ThemePreference,
21 halves: ThemeHalves | null,
22 appearance: ThemeAppearance,
23) {
24 const definition = getThemeDefinition(resolveThemeHalf(theme, halves, appearance));
25 return definition === null
26 ? getStandardThemeColors(appearance)
27 : (getThemeColorsForMode(definition, appearance) ?? definition.colors);
29​
30/** The app's active theme and fonts, as handed to agent HTML renders. Stable until one changes. */
31export function useHtmlRenderTheme() {
32 const { theme, resolvedTheme, themeHalves } = useTheme();
33 // Custom and published palettes can be edited in place, under an unchanged preference.
34 const colors = useSyncExternalStore(
35 subscribeToCustomThemes,
36 () => resolveActiveThemeColors(theme, themeHalves, resolvedTheme),
37 () => getStandardThemeColors(resolvedTheme),
38 );
39 const sans = useClientSettings((settings) => settings.fontFamilySans);
40 const mono = useClientSettings((settings) => settings.fontFamilyCode);
41 return useMemo(
42 () =>
43 htmlRenderTheme(colors, resolvedTheme, {
44 sans: appearanceFontStack(sans, HTML_RENDER_DEFAULT_FONTS.sans),
45 mono: appearanceFontStack(mono, HTML_RENDER_DEFAULT_FONTS.mono),
46 }),
47 [colors, resolvedTheme, sans, mono],
48 );