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

useScaledTextRole.ts

34 lines1.2 KB

1import { useMemo } from "react";
2​
3import {
4 DEFAULT_BASE_FONT_SIZE,
5 normalizeBaseFontSize,
6 scaledTypographyLineHeight,
7} from "../../../lib/appearancePreferences";
8import { MOBILE_TYPOGRAPHY } from "../../../lib/typography";
9import { useAppearancePreferences } from "./AppearancePreferencesProvider";
10​
11export interface ScaledTextRole {
12 readonly fontSize: number;
13 readonly lineHeight: number;
15​
16/**
17 * Mirrors the values injected into Uniwind for style-prop consumers that
18 * cannot use a `text-*` class. This deliberately does not subscribe to CSS
19 * variables, so palette-only setTheme calls remain native-only.
20 */
21export function useScaledTextRole(role: keyof typeof MOBILE_TYPOGRAPHY): ScaledTextRole {
22 const { appearance } = useAppearancePreferences();
23 return useMemo(() => {
24 const baseFontSize = normalizeBaseFontSize(appearance.baseFontSize);
25 const typography = MOBILE_TYPOGRAPHY[role];
26 return {
27 fontSize: Math.max(
28 8,
29 Math.round(typography.fontSize * (baseFontSize / DEFAULT_BASE_FONT_SIZE)),
30 ),
31 lineHeight: scaledTypographyLineHeight(typography, baseFontSize),
32 };
33 }, [appearance.baseFontSize, role]);