Skip to content

Commit

Permalink
feat(css): Reduce layout shift with fonts fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
mvsde committed Jun 28, 2024
1 parent 6a2bc73 commit 79e2598
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 2 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,14 @@ node --run dev
node --run build
```

## Fonts

Generate fonts fallback CSS file:

```sh
node generate-fonts-fallback.js
```

## Formatting

```sh
Expand Down
32 changes: 32 additions & 0 deletions generate-fonts-fallback.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import fs from "node:fs/promises";

import { createFontStack } from "@capsizecss/core";
import Arial from "@capsizecss/metrics/arial";
import NotoSans from "@capsizecss/metrics/notoSans";
import Roboto from "@capsizecss/metrics/roboto";
import Teko from "@capsizecss/metrics/teko";
import WorkSans from "@capsizecss/metrics/workSans";
import prettier from "prettier";
import stylelint from "stylelint";

const filepath = "./public/css/global/fonts-fallback.css";

const heading = createFontStack([Teko, NotoSans, Roboto, Arial]);
const text = createFontStack([WorkSans, NotoSans, Roboto, Arial]);

let code = `
${heading.fontFaces}
${text.fontFaces}
`;

// Run Stylelint autofixer
const linterResult = await stylelint.lint({ code, fix: true });
code = linterResult.code ?? "";

// Run Prettier formatter
const prettierOptions = await prettier.resolveConfig(filepath, {
editorconfig: true,
});
code = await prettier.format(code, { filepath, ...prettierOptions });

await fs.writeFile(filepath, code);
26 changes: 26 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
"@11ty/eleventy-plugin-rss": "^2.0.1",
"@11ty/eleventy-plugin-syntaxhighlight": "^5.0.0",
"@11ty/eleventy-plugin-webc": "^0.11.2",
"@capsizecss/core": "^4.1.2",
"@capsizecss/metrics": "^3.2.0",
"@eslint/js": "^9.5.0",
"eslint": "^9.5.0",
"eslint-plugin-simple-import-sort": "^12.1.0",
Expand Down
49 changes: 49 additions & 0 deletions public/css/global/fonts-fallback.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
@font-face {
ascent-override: 155.511%;
descent-override: 77.1062%;
font-family: "Teko Fallback: Noto Sans";
size-adjust: 61.6034%;
src: local("Noto Sans Regular"), local("NotoSans-Regular");
}

@font-face {
ascent-override: 145.9389%;
descent-override: 72.3601%;
font-family: "Teko Fallback: Roboto";
size-adjust: 65.6439%;
src: local("Roboto"), local("Roboto-Regular");
}

@font-face {
ascent-override: 146.2593%;
descent-override: 72.519%;
font-family: "Teko Fallback: Arial";
line-gap-override: 0%;
size-adjust: 65.5001%;
src: local("Arial"), local("ArialMT");
}

@font-face {
ascent-override: 88.3407%;
descent-override: 23.0826%;
font-family: "Work Sans Fallback: Noto Sans";
size-adjust: 105.2743%;
src: local("Noto Sans Regular"), local("NotoSans-Regular");
}

@font-face {
ascent-override: 82.9031%;
descent-override: 21.6618%;
font-family: "Work Sans Fallback: Roboto";
size-adjust: 112.1791%;
src: local("Roboto"), local("Roboto-Regular");
}

@font-face {
ascent-override: 83.0851%;
descent-override: 21.7093%;
font-family: "Work Sans Fallback: Arial";
line-gap-override: 0%;
size-adjust: 111.9334%;
src: local("Arial"), local("ArialMT");
}
1 change: 1 addition & 0 deletions public/css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
/* Global */

@import url("global/fonts.css") layer(global);
@import url("global/fonts-fallback.css") layer(global);
@import url("global/base.css") layer(global);

/* Typography */
Expand Down
6 changes: 4 additions & 2 deletions public/css/tokens/typo.css
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
:root {
/* Default */
--typo-Default-font-family: "Work Sans", sans-serif;
--typo-Default-font-family: "Work Sans", "Work Sans Fallback: Noto Sans",
"Work Sans Fallback: Roboto", "Work Sans Fallback: Arial", sans-serif;
--typo-Default-line-height: 1.4;

/* Heading */
--typo-Heading-font-family: "Teko", serif;
--typo-Heading-font-family: "Teko", "Teko Fallback: Noto Sans",
"Teko Fallback: Roboto", "Teko Fallback: Arial", sans-serif;
--typo-Heading-font-weight: 600;
--typo-Heading-line-height: 1;

Expand Down

0 comments on commit 79e2598

Please sign in to comment.