Skip to content

Commit 53461a5

Browse files
brkunveraklinker1
andauthored
Add custom font example for react content ui (#20)
Co-authored-by: Aaron <[email protected]>
1 parent da6c72d commit 53461a5

File tree

15 files changed

+1585
-745
lines changed

15 files changed

+1585
-745
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
pnpm-debug.log*
8+
lerna-debug.log*
9+
10+
node_modules
11+
.output
12+
stats.html
13+
stats-*.json
14+
.wxt
15+
web-ext.config.ts
16+
17+
# Editor directories and files
18+
.vscode/*
19+
!.vscode/extensions.json
20+
.idea
21+
.DS_Store
22+
*.suo
23+
*.ntvs*
24+
*.njsproj
25+
*.sln
26+
*.sw?
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
---
2+
name: React Content Script UI Custom Font
3+
description: Basic example of using a custom ttf font in your react content ui.
4+
apis:
5+
- createShadowRootUi
6+
---
7+
8+
```sh
9+
pnpm i
10+
pnpm dev
11+
```
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export default function App() {
2+
// use font family in your components
3+
return (
4+
<div style={{ fontFamily: "JB Mono", position: "fixed", top: 0, left: 0, zIndex: 9999 }}>
5+
<h1>Hello Custom Font</h1>
6+
</div>
7+
)
8+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import ReactDOM from "react-dom/client"
2+
import App from "./App.tsx"
3+
4+
export default defineContentScript({
5+
matches: ["*://*.google.com/*"],
6+
cssInjectionMode: "ui",
7+
8+
async main(ctx) {
9+
const ui = await createShadowRootUi(ctx, {
10+
name: "custom-font",
11+
position: "inline",
12+
anchor: "body",
13+
append: "first",
14+
onMount: (container) => {
15+
// Load custom font. Don't forget add font to web accessible resources in wxt.config.ts
16+
const fontUrl = browser.runtime.getURL("/fonts/jbmono.ttf")
17+
18+
// create a style element
19+
const fontStyle = document.createElement("style")
20+
21+
// add font face
22+
fontStyle.textContent = `
23+
@font-face {
24+
font-family: 'JB Mono';
25+
src: url('${fontUrl}') format('truetype');
26+
font-weight: 400;
27+
font-style: normal;
28+
}
29+
`
30+
// append style element to head
31+
document.head.appendChild(fontStyle)
32+
33+
const wrapper = document.createElement("div")
34+
container.append(wrapper)
35+
36+
const root = ReactDOM.createRoot(wrapper)
37+
root.render(<App />)
38+
return { root, wrapper }
39+
},
40+
onRemove: (elements) => {
41+
elements?.root.unmount()
42+
elements?.wrapper.remove()
43+
},
44+
})
45+
ui.mount()
46+
},
47+
})
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"name": "react-content-ui-custom-font",
3+
"description": "Custom font example for react content ui",
4+
"private": false,
5+
"version": "0.0.0",
6+
"type": "module",
7+
"scripts": {
8+
"dev": "wxt",
9+
"dev:firefox": "wxt -b firefox",
10+
"build": "wxt build",
11+
"build:firefox": "wxt build -b firefox",
12+
"zip": "wxt zip",
13+
"zip:firefox": "wxt zip -b firefox",
14+
"compile": "tsc --noEmit",
15+
"postinstall": "wxt prepare"
16+
},
17+
"dependencies": {
18+
"react": "^19.1.0",
19+
"react-dom": "^19.1.0"
20+
},
21+
"devDependencies": {
22+
"@types/react": "^19.1.0",
23+
"@types/react-dom": "^19.1.2",
24+
"@wxt-dev/module-react": "^1.1.3",
25+
"typescript": "^5.8.3",
26+
"wxt": "^0.20.0"
27+
}
28+
}
Binary file not shown.
Loading
Loading
Loading
Loading
Loading
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"extends": "./.wxt/tsconfig.json",
3+
"compilerOptions": {
4+
"allowImportingTsExtensions": true,
5+
"jsx": "react-jsx"
6+
}
7+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { defineConfig } from "wxt"
2+
3+
// See https://wxt.dev/api/config.html
4+
export default defineConfig({
5+
modules: ["@wxt-dev/module-react"],
6+
7+
// add font to web accessible resources
8+
manifest: {
9+
web_accessible_resources: [{ resources: ["fonts/*"], matches: ["*://*.google.com/*"] }],
10+
},
11+
})

metadata.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,22 @@
248248
],
249249
"permissions": []
250250
},
251+
{
252+
"name": "React Content Script UI Custom Font",
253+
"description": "Basic example of using a custom ttf font in your react content ui.",
254+
"searchText": "React Content Script UI Custom Font|Basic example of using a custom ttf font in your react content ui.|react|react-dom|@wxt-dev/module-react|browser.runtime.getURL|createShadowRootUi",
255+
"url": "https://github.com/wxt-dev/examples/tree/main/examples/react-content-ui-custom-font",
256+
"apis": [
257+
"browser.runtime.getURL",
258+
"createShadowRootUi"
259+
],
260+
"packages": [
261+
"react",
262+
"react-dom",
263+
"@wxt-dev/module-react"
264+
],
265+
"permissions": []
266+
},
251267
{
252268
"name": "React Mantine",
253269
"description": "Add mantine to HTML pages and content scripts",

0 commit comments

Comments
 (0)