Skip to content

Add custom font example for WXT 0.20.7+ #27

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions examples/custom-fonts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
---
name: Custom Fonts
description: Show how to use custom fonts throughout your extension.
---

> [!IMPORTANT]
> This example only works for WXT v0.20.7 and above. For older versions, see [React Content Script UI Custom Font](https://github.com/wxt-dev/examples/tree/main/examples/react-content-ui-custom-font) example

In this example, we'll add custom fonts from [Google Fonts](https://fonts.google.com/) to the popup and a ShadowRoot content script UI.

```sh
pnpm i
pnpm dev
```

Important steps:

1. Export the fonts you want to use from Google Fonts using <https://gwfh.mranftl.com/fonts>
- Set the folder prefix to `wxt-extension://__MSG_@@extension_id__/fonts/` and copy the CSS into `assets/fonts.css`
https://github.com/wxt-dev/examples/blob/84eb502bda5a1544f58919b69769af0bd3e617a0/examples/custom-fonts/assets/fonts.css#L1-L21
- Move the downloaded font files into the [`public/fonts` directory](public/fonts)
2. Create `assets/style.css`. This file will contain the extension's base styles that all entrypoints will use. Make sure to import the `fonts.css` file:
https://github.com/wxt-dev/examples/blob/84eb502bda5a1544f58919b69769af0bd3e617a0/examples/custom-fonts/assets/style.css#L1
3. In your HTML files, just link to the `style.css` file like normal:
https://github.com/wxt-dev/examples/blob/84eb502bda5a1544f58919b69769af0bd3e617a0/examples/custom-fonts/entrypoints/popup.html#L7
4. In your content script entrypoint, import the `style.css`. If you're using `createShadowRootUi`, remember to set `cssInjectionMode: "ui"`, just like the [normal setup](https://wxt.dev/guide/essentials/content-scripts.html#shadow-root). There's nothing special here.
https://github.com/wxt-dev/examples/blob/84eb502bda5a1544f58919b69769af0bd3e617a0/examples/custom-fonts/entrypoints/content.ts#L2
https://github.com/wxt-dev/examples/blob/84eb502bda5a1544f58919b69769af0bd3e617a0/examples/custom-fonts/entrypoints/content.ts#L6
https://github.com/wxt-dev/examples/blob/84eb502bda5a1544f58919b69769af0bd3e617a0/examples/custom-fonts/entrypoints/content.ts#L9-L10

And that's it! You can now use custom fonts in your extension, wherever you want.
21 changes: 21 additions & 0 deletions examples/custom-fonts/assets/fonts.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/* Generated with https://gwfh.mranftl.com/fonts */

/* overpass-regular - latin */
@font-face {
font-display: swap; /* Check https://developer.mozilla.org/en-US/docs/Web/CSS/@font-face/font-display for other options. */
font-family: "Overpass";
font-style: normal;
font-weight: 400;
src: url("wxt-extension://__MSG_@@extension_id__/fonts/overpass-v18-latin-regular.woff2")
format("woff2"); /* Chrome 36+, Opera 23+, Firefox 39+, Safari 12+, iOS 10+ */
}

/* poppins-regular - latin */
@font-face {
font-display: swap; /* Check https://developer.mozilla.org/en-US/docs/Web/CSS/@font-face/font-display for other options. */
font-family: "Poppins";
font-style: normal;
font-weight: 400;
src: url("wxt-extension://__MSG_@@extension_id__/fonts/poppins-v23-latin-regular.woff2")
format("woff2"); /* Chrome 36+, Opera 23+, Firefox 39+, Safari 12+, iOS 10+ */
}
9 changes: 9 additions & 0 deletions examples/custom-fonts/assets/style.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
@import url("./fonts.css");

.font-poppins {
font-family: "Poppins", sans-serif;
}

.font-overpass {
font-family: "Overpass", sans-serif;
}
39 changes: 39 additions & 0 deletions examples/custom-fonts/entrypoints/content.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { ContentScriptContext } from "#imports";
import "~/assets/style.css";

export default defineContentScript({
matches: ["*://*/*"],
cssInjectionMode: "ui",

async main(ctx) {
const ui = await createUi(ctx);
ui.autoMount();
},
});

function createUi(ctx: ContentScriptContext) {
return createShadowRootUi(ctx, {
name: "custom-font-example",
position: "overlay",
anchor: "body",
append: "first",
onMount(container) {
// Make UI more visible, not necessary for fonts to work
container.style.backgroundColor = "white";
container.style.position = "relative";
container.style.zIndex = "9999";

// Create 3 paragraphs with different fonts
container.appendChild(createP("Default"));
container.appendChild(createP("Poppins", "font-poppins"));
container.appendChild(createP("Overpass", "font-overpass"));
},
});
}

function createP(text: string, className?: string) {
const p = document.createElement("p");
p.textContent = text;
if (className) p.classList.add(className);
return p;
}
14 changes: 14 additions & 0 deletions examples/custom-fonts/entrypoints/popup.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Popup</title>
<link rel="stylesheet" href="~/assets/style.css" />
</head>
<body>
<p>Default</p>
<p class="font-poppins">Poppins</p>
<p class="font-overpass">Overpass</p>
</body>
</html>
20 changes: 20 additions & 0 deletions examples/custom-fonts/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "custom-fonts",
"private": true,
"version": "0.0.0",
"type": "module",
"scripts": {
"dev": "wxt",
"dev:firefox": "wxt -b firefox",
"build": "wxt build",
"build:firefox": "wxt build -b firefox",
"zip": "wxt zip",
"zip:firefox": "wxt zip -b firefox",
"compile": "tsc --noEmit",
"postinstall": "wxt prepare"
},
"devDependencies": {
"typescript": "^5.8.2",
"wxt": "^0.20.7"
}
}
Binary file not shown.
Binary file not shown.
3 changes: 3 additions & 0 deletions examples/custom-fonts/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "./.wxt/tsconfig.json"
}
16 changes: 16 additions & 0 deletions examples/custom-fonts/wxt.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { defineConfig } from "wxt";

// See https://wxt.dev/api/config.html
export default defineConfig({
manifest: {
web_accessible_resources: [
{
resources: ["fonts/*"],
matches: ["*://*/*"],
},
],
},
webExt: {
startUrls: ["https://wxt.dev"],
},
});
Loading