/** * Builds the --aureon-font-* / --aureon-link-* / --aureon-button-* / * --aureon-misc-* CSS variable list shown in the "View CSS Variables" modal. */ import { HEADING_LEVELS, HEADING_SCALE_RATIOS, scaleLength } from './constants'; import type { TypographySchema, TypographyFieldValues } from '../../types'; import type { CssVariable } from '../../controls/css-variables-callout'; const HEADING_LEVEL_KEYS = HEADING_LEVELS.map( ( level ) => level.key ); /** * Mirrors Dynamic_CSS::resolve_heading_sizes() on the PHP side: a non-empty * per-level override wins, otherwise the size is `base_size * scale ratio`. * @param values * @param level */ function resolveHeadingSize( values: TypographyFieldValues, level: string ): string { const override = values.overrides?.[ level ]?.trim(); if ( override ) { return override; } const ratios = HEADING_SCALE_RATIOS[ values.scale ?? 'modular' ] ?? HEADING_SCALE_RATIOS.modular; return scaleLength( values.base_size ?? '3rem', ratios[ level ] ?? 1 ); } interface BuildTypographyCssVariablesArgs { schema: TypographySchema; typography: Record< string, TypographyFieldValues >; familyLabel: ( role: string ) => string; } /** * Mirrors Dynamic_CSS::resolve_link_decoration() on the PHP side: * `underline-hover` isn't a real CSS value, it's "no underline at rest, * underline on hover" — split into the two declarations the frontend * actually consumes. * @param decoration */ function resolveLinkDecoration( decoration: string | undefined ): { base: string; hover: string; } { switch ( decoration ) { case 'underline': return { base: 'underline', hover: 'underline' }; case 'underline-hover': return { base: 'none', hover: 'underline' }; default: return { base: 'none', hover: 'none' }; } } export function buildTypographyCssVariables( { schema, typography, familyLabel, }: BuildTypographyCssVariablesArgs ): CssVariable[] { const cssVariables: CssVariable[] = []; Object.values( schema.sections ).forEach( ( section ) => { const values = typography[ section.key ] ?? {}; cssVariables.push( { name: `--aureon-font-${ section.family_role }`, value: familyLabel( section.family_role ) ?? '—', } ); if ( 'headings' === section.key ) { HEADING_LEVEL_KEYS.forEach( ( level ) => { cssVariables.push( { name: `--aureon-font-size-${ level }`, value: resolveHeadingSize( values, level ), } ); } ); } if ( 'body' === section.key || 'misc' === section.key ) { cssVariables.push( { name: `--aureon-font-size-${ section.key }`, value: values.size ?? '—', } ); } if ( 'headings' === section.key ) { cssVariables.push( { name: '--aureon-heading-scale', value: values.scale ?? '—', }, { name: '--aureon-heading-base-size', value: values.base_size ?? '—', } ); } if ( 'body' === section.key || 'headings' === section.key || 'misc' === section.key ) { cssVariables.push( { name: `--aureon-font-line-height-${ 'headings' === section.key ? 'heading' : section.key }`, value: values.line_height ?? '—', } ); } cssVariables.push( { name: `--aureon-font-weight-${ 'headings' === section.key ? 'heading' : section.key }`, value: values.weight ?? '—', } ); if ( 'body' === section.key || 'headings' === section.key ) { cssVariables.push( { name: `--aureon-font-letter-spacing-${ 'headings' === section.key ? 'heading' : section.key }`, value: values.letter_spacing ?? '—', } ); } if ( 'body' === section.key || 'links' === section.key || 'buttons' === section.key ) { cssVariables.push( { name: `--aureon-font-transform-${ section.key }`, value: values.transform ?? '—', } ); } if ( 'links' === section.key ) { const { base, hover } = resolveLinkDecoration( values.decoration ); cssVariables.push( { name: '--aureon-link-decoration', value: base }, { name: '--aureon-link-decoration-hover', value: hover }, { name: '--aureon-link-letter-spacing', value: values.letter_spacing ?? '—', } ); } if ( 'buttons' === section.key ) { cssVariables.push( { name: '--aureon-button-letter-spacing', value: values.letter_spacing ?? '—', } ); } if ( 'misc' === section.key ) { cssVariables.push( { name: '--aureon-misc-color', value: values.color ?? '—', } ); } } ); return cssVariables; }