Skip to content

Commit

Permalink
Merge pull request #1177 from prezly/feature/dev-13354-support-new-fo…
Browse files Browse the repository at this point in the history
…nts-in-bea

[DEV-13354] Add support for more fonts
  • Loading branch information
kudlajz authored Aug 19, 2024
2 parents 50338a4 + 2dfd4af commit 67e2f05
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 5 deletions.
26 changes: 23 additions & 3 deletions modules/Head/components/BrandingSettings.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { withoutUndefined } from '@/utils';
import { DEFAULT_THEME_SETTINGS, getGoogleFontName, type ThemeSettings } from 'theme-settings';
import {
DEFAULT_THEME_SETTINGS,
getGoogleFontName,
getRelatedFont,
type ThemeSettings,
} from 'theme-settings';

import { getCssVariables } from './getCssVariables';
import { InjectCssVariables } from './InjectCssVariables';
Expand All @@ -14,12 +19,27 @@ export function BrandingSettings({ settings }: Props) {
...withoutUndefined(settings),
};

const googleFontName = getGoogleFontName(compiledSettings.font).replace(' ', '+');
const primaryGoogleFontName = getGoogleFontName(compiledSettings.font).replace(' ', '+');
const relatedFont = getRelatedFont(compiledSettings.font);

let families = [];
if (relatedFont) {
const relatedGoogleFontName = getGoogleFontName(relatedFont).replace(' ', '+');

families = [
`${primaryGoogleFontName}:wght@600`,
`${relatedGoogleFontName}:wght@400;500;600;700;900`,
];
} else {
families = [`${primaryGoogleFontName}:wght@400;500;600;700;900`];
}

return (
<>
<link
href={`https://fonts.googleapis.com/css2?family=${googleFontName}:wght@400;500;600;700;900&display=swap`}
href={`https://fonts.googleapis.com/css2?display=swap&${families
.map((family) => `family=${family}`)
.join('&')}`}
rel="stylesheet"
/>

Expand Down
12 changes: 11 additions & 1 deletion modules/Head/components/getCssVariables.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
/* eslint-disable @typescript-eslint/naming-convention */
import tinycolor from 'tinycolor2';

import { Font, FONT_FAMILY, type ThemeSettings } from 'theme-settings';
import { Font, FONT_FAMILY, getRelatedFont, type ThemeSettings } from 'theme-settings';

function getFontFamily(font: Font): string {
return FONT_FAMILY[font] || FONT_FAMILY[Font.INTER];
}

function getSecondaryFontFamily(font: Font): string {
switch (getRelatedFont(font)) {
case Font.ALEGREYA_SANS:
return FONT_FAMILY[Font.ALEGREYA_SANS];
default:
return getFontFamily(font);
}
}

export function getCssVariables(settings: ThemeSettings): Record<string, string> {
const {
accent_color,
Expand Down Expand Up @@ -42,6 +51,7 @@ export function getCssVariables(settings: ThemeSettings): Record<string, string>

return {
'--prezly-font-family': getFontFamily(font),
'--prezly-font-family-secondary': getSecondaryFontFamily(font),
'--prezly-border-color': borderColor.toHex8String(),
'--prezly-border-color-secondary': borderColorSecondary.toHex8String(),
'--prezly-text-color': text_color,
Expand Down
8 changes: 7 additions & 1 deletion styles/_typography.scss
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
body {
font-size: 16px;
font-family: var(--prezly-font-family);
font-family: var(--prezly-font-family-secondary);
color: var(--prezly-text-color);
word-break: break-word;
background: var(--prezly-background-color);
Expand All @@ -9,16 +9,22 @@ body {
h1,
.h1 {
@include heading-1;

font-family: var(--prezly-font-family);
}

h2,
.h2 {
@include heading-2;

font-family: var(--prezly-font-family);
}

h3,
.h3 {
@include heading-3;

font-family: var(--prezly-font-family);
}

p,
Expand Down
30 changes: 30 additions & 0 deletions theme-settings.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
export enum Font {
ALEGREYA = 'alegreya',
ALEGREYA_SANS = 'alegreya_sans',
INTER = 'inter',
MERRIWEATHER = 'merriweather',
MULISH = 'mulish',
NUNITO = 'nunito',
OPEN_SANS = 'open_sans',
PLAYFAIR_DISPLAY = 'playfair_display',
PT_SERIF = 'pt_serif',
ROBOTO = 'roboto',
SOURCE_CODE_PRO = 'source_code_pro',
Expand Down Expand Up @@ -48,20 +53,45 @@ export const DEFAULT_THEME_SETTINGS: ThemeSettings = {
};

export const FONT_FAMILY = {
[Font.ALEGREYA]: "'Alegreya', serif",
[Font.ALEGREYA_SANS]: "'Alegreya Sans', sans-serif",
[Font.INTER]: 'Inter, sans-serif',
[Font.MERRIWEATHER]: 'Merriweather, serif',
[Font.MULISH]: 'Mulish, sans-serif',
[Font.NUNITO]: "'Nunito', sans-serif",
[Font.OPEN_SANS]: "'Open Sans', sans-serif",
[Font.PLAYFAIR_DISPLAY]: "'Playfair Display', serif",
[Font.PT_SERIF]: "'PT Serif', serif",
[Font.ROBOTO]: 'Roboto, sans-serif',
[Font.SOURCE_CODE_PRO]: "'Source Code Pro', monospace",
};

export function getRelatedFont(font: Font): Font | null {
switch (font) {
case Font.ALEGREYA:
case Font.PLAYFAIR_DISPLAY:
return Font.ALEGREYA_SANS;
default:
return null;
}
}

export function getGoogleFontName(font: Font): string {
switch (font) {
case Font.ALEGREYA:
return 'Alegreya';
case Font.ALEGREYA_SANS:
return 'Alegreya Sans';
case Font.MERRIWEATHER:
return 'Merriweather';
case Font.MULISH:
return 'Mullish';
case Font.NUNITO:
return 'Nunito';
case Font.OPEN_SANS:
return 'Open Sans';
case Font.PLAYFAIR_DISPLAY:
return 'Playfair Display';
case Font.PT_SERIF:
return 'PT Serif';
case Font.ROBOTO:
Expand Down

0 comments on commit 67e2f05

Please sign in to comment.