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

useScaledTextRole.ts

36 lines1.2 KB

1import { useCSSVariable } from "uniwind";
2​
3import { MOBILE_TYPOGRAPHY } from "../../../lib/typography";
4​
5const TEXT_ROLE_VARIABLES = {
6 micro: "--text-3xs",
7 caption: "--text-2xs",
8 label: "--text-xs",
9 footnote: "--text-sm",
10 body: "--text-base",
11 headline: "--text-lg",
12 title: "--text-xl",
13 largeTitle: "--text-2xl",
14 display: "--text-3xl",
15} as const satisfies Record<keyof typeof MOBILE_TYPOGRAPHY, string>;
16​
17export interface ScaledTextRole {
18 readonly fontSize: number;
19 readonly lineHeight: number;
21​
22/**
23 * Reads a typography role's current size from the Uniwind `--text-*` CSS
24 * variables (scaled at runtime with the base font size). Use for style-prop
25 * consumers that can't express their size as a `text-*` className. Reactive:
26 * re-renders when the appearance provider re-injects the variables.
27 */
28export function useScaledTextRole(role: keyof typeof MOBILE_TYPOGRAPHY): ScaledTextRole {
29 const variable = TEXT_ROLE_VARIABLES[role];
30 const [fontSize, lineHeight] = useCSSVariable([variable, `${variable}--line-height`]);
31​
32 return {
33 fontSize: typeof fontSize === "number" ? fontSize : MOBILE_TYPOGRAPHY[role].fontSize,
34 lineHeight: typeof lineHeight === "number" ? lineHeight : MOBILE_TYPOGRAPHY[role].lineHeight,
35 };