| 1 |
const FONT_VAR_PREFIX = '--wp--preset--font-family'; |
| 2 |
const HEADING_FONT_VAR_KEY = '--wp--preset--font-family--heading'; |
| 3 |
const BODY_FONT_VAR_KEY = '--wp--preset--font-family--body'; |
| 4 |
|
| 5 |
// Generates font links and styles from custom variations to override fonts in `theme-processed.json` |
| 6 |
export const getFontOverrides = (variation) => { |
| 7 |
let customFontLinks = ''; |
| 8 |
let fontOverrides = ''; |
| 9 |
|
| 10 |
if (!variation?.title) { |
| 11 |
return { customFontLinks, fontOverrides }; |
| 12 |
} |
| 13 |
|
| 14 |
// These values look like `var(--wp--preset--font-family--${fontFamilySlug})` |
| 15 |
const headingFontVarValue = |
| 16 |
variation.styles?.elements?.heading?.typography?.fontFamily; |
| 17 |
const bodyFontVarValue = variation.styles?.typography?.fontFamily; |
| 18 |
const customFonts = |
| 19 |
variation.settings?.typography?.fontFamilies?.custom ?? []; |
| 20 |
|
| 21 |
if (headingFontVarValue) { |
| 22 |
fontOverrides += `:root { ${HEADING_FONT_VAR_KEY}: ${headingFontVarValue}; }`; |
| 23 |
fontOverrides += `h1, h2, h3, h4, h5, h6 { font-family: ${headingFontVarValue} !important; }`; |
| 24 |
} |
| 25 |
|
| 26 |
if (bodyFontVarValue) { |
| 27 |
fontOverrides += `:root { ${BODY_FONT_VAR_KEY}: ${bodyFontVarValue}; }`; |
| 28 |
fontOverrides += `body { font-family: ${bodyFontVarValue} !important; }`; |
| 29 |
} |
| 30 |
|
| 31 |
for (const font of customFonts) { |
| 32 |
const customFontLink = `<link id="ext-custom-font-${font.slug}" rel="stylesheet" href="${font.css}">`; |
| 33 |
if (!customFontLinks.includes(customFontLink)) { |
| 34 |
customFontLinks += customFontLink; |
| 35 |
} |
| 36 |
|
| 37 |
const fontOverride = `:root { ${FONT_VAR_PREFIX}--${font.slug}: "${font.fontFamily}"; }`; |
| 38 |
if (!fontOverrides.includes(fontOverride)) { |
| 39 |
fontOverrides += fontOverride; |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
return { customFontLinks, fontOverrides }; |
| 44 |
}; |
| 45 |
|