Skip to content

🎥 Make videos programmatically with React

Improve misleading Font Picker docs

Jonny Burger9293f9d5324d
diff --git a/packages/docs/docs/font-picker.md b/packages/docs/docs/font-picker.md
index 6d0bec38ad..66603501bd 100644
--- a/packages/docs/docs/font-picker.md
+++ b/packages/docs/docs/font-picker.md
@@ -21,34 +21,41 @@ The following snippet renders a dropdown with all Google Fonts, and loads one it
```tsx twoslash
import { getAvailableFonts } from "@remotion/google-fonts";
-import React from "react";
+import React, { useCallback } from "react";
export const FontPicker: React.FC = () => {
const newFonts = getAvailableFonts();
+ const onChange = useCallback(
+ async (e: React.ChangeEvent<HTMLSelectElement>) => {
+ const fonts = newFonts[e.target.selectedIndex];
+
+ // Load font information
+ const loaded = await fonts.load();
+
+ // Load the font itself
+ const { fontFamily, ...otherInfo } = loaded.loadFont();
+
+ // Or get metadata about the font
+ const info = loaded.getInfo();
+ const styles = Object.keys(info.fonts);
+ console.log("Font", info.fontFamily, " Styles", styles);
+ for (const style of styles) {
+ const weightObject = info.fonts[style as keyof typeof info.fonts];
+ const weights = Object.keys(weightObject);
+ console.log("- Style", style, "supports weights", weights);
+ for (const weight of weights) {
+ const scripts = Object.keys(weightObject[weight]);
+ console.log("-- Weight", weight, "supports scripts", scripts);
+ }
+ }
+ },
+ [newFonts],
+ );
+
return (
<div>
- <select
- onChange={(e) => {
- const fonts = newFonts[e.target.selectedIndex];
- const loaded = fonts.load();
-
- loaded.then((l) => {
- const info = l.getInfo();
- const styles = Object.keys(info.fonts);
- console.log("Font", info.fontFamily, " Styles", styles);
- for (const style of styles) {
- const weightObject = info.fonts[style as keyof typeof info.fonts];
- const weights = Object.keys(weightObject);
- console.log("- Style", style, "supports weights", weights);
- for (const weight of weights) {
- const scripts = Object.keys(weightObject[weight]);
- console.log("-- Weight", weight, "supports scripts", scripts);
- }
- }
- });
- }}
- >
+ <select onChange={onChange}>
{newFonts.map((f) => {
return (
<option key={f.importName} value={f.importName}>
diff --git a/packages/player-example/src/FontPicker.tsx b/packages/player-example/src/FontPicker.tsx
index d9aaa2a5bc..6fb6e1664b 100644
--- a/packages/player-example/src/FontPicker.tsx
+++ b/packages/player-example/src/FontPicker.tsx
@@ -1,32 +1,39 @@
-import React from 'react';
+import React, {useCallback} from 'react';
import {getAvailableFonts} from '@remotion/google-fonts';
export const FontPicker: React.FC = () => {
const newFonts = getAvailableFonts();
+ const onChange = useCallback(
+ async (e: React.ChangeEvent<HTMLSelectElement>) => {
+ const fonts = newFonts[e.target.selectedIndex];
+
+ // Load font information
+ const loaded = await fonts.load();
+
+ // Load the font itself
+ const {fontFamily, ...otherInfo} = loaded.loadFont();
+
+ // Or get metadata about the font
+ const info = loaded.getInfo();
+ const styles = Object.keys(info.fonts);
+ console.log('Font', info.fontFamily, ' Styles', styles);
+ for (const style of styles) {
+ const weightObject = info.fonts[style as keyof typeof info.fonts];
+ const weights = Object.keys(weightObject);
+ console.log('- Style', style, 'supports weights', weights);
+ for (const weight of weights) {
+ const scripts = Object.keys(weightObject[weight]);
+ console.log('-- Weight', weight, 'supports scripts', scripts);
+ }
+ }
+ },
+ [newFonts],
+ );
+
return (
<div>
- <select
- onChange={(e) => {
- const fonts = newFonts[e.target.selectedIndex];
- const loaded = fonts.load();
-
- loaded.then((l) => {
- const info = l.getInfo();
- const styles = Object.keys(info.fonts);
- console.log('Font', info.fontFamily, ' Styles', styles);
- for (const style of styles) {
- const weightObject = info.fonts[style as keyof typeof info.fonts];
- const weights = Object.keys(weightObject);
- console.log('- Style', style, 'supports weights', weights);
- for (const weight of weights) {
- const scripts = Object.keys(weightObject[weight]);
- console.log('-- Weight', weight, 'supports scripts', scripts);
- }
- }
- });
- }}
- >
+ <select onChange={onChange}>
{newFonts.map((f) => {
return (
<option key={f.importName} value={f.importName}>