Skip to content

🎥 Make videos programmatically with React

enahanced documentation

Jonny Burgere9a362ccc11a
diff --git a/packages/docs/components/TableOfContents/google-fonts.tsx b/packages/docs/components/TableOfContents/google-fonts.tsx
new file mode 100644
index 0000000000..994078fed9
--- /dev/null
+++ b/packages/docs/components/TableOfContents/google-fonts.tsx
@@ -0,0 +1,16 @@
+import React from "react";
+import { Grid } from "./Grid";
+import { TOCItem } from "./TOCItem";
+
+export const TableOfContents: React.FC = () => {
+ return (
+ <div>
+ <Grid>
+ <TOCItem link="/docs/google-fonts/load-font">
+ <strong>loadFont()</strong>
+ <div>Load a Google Font</div>
+ </TOCItem>
+ </Grid>
+ </div>
+ );
+};
diff --git a/packages/docs/docs/google-fonts/index.md b/packages/docs/docs/google-fonts/index.md
index 00e01cb18a..7c9b54bb52 100644
--- a/packages/docs/docs/google-fonts/index.md
+++ b/packages/docs/docs/google-fonts/index.md
@@ -6,6 +6,7 @@ title: "@remotion/google-fonts"
import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';
+import {TableOfContents} from '../../components/TableOfContents/google-fonts';
The `@remotion/google-fonts` package provides APIs for easily integrating [Google Fonts](https://fonts.google.com/) into Remotion.
@@ -44,67 +45,56 @@ pnpm i @remotion/google-fonts
</TabItem>
</Tabs>
-All Google Fonts is available under `"@remotion/google-fonts/<FontName>"`.
-
-So if we want to use **Montserrat** font then we can import it from `"@remotion/google-fonts/Montserrat"`.
-
## Usage
-Before using it, we need to import the fonts we want first, there are 2 ways to do this.
+To load a font, import the package `@remotion/google-fonts/<FontName>` and call [`loadFont()`](/docs/load-font).
-**Import one font**
-
-```tsx
-import { loadFont, fontFamily } from "@remotion/google-fonts/TitanOne";
+```tsx twoslash title="Load all font styles"
+import { loadFont } from "@remotion/google-fonts/TitanOne";
+const { fontFamily } = loadFont(); // "Titan One"
```
-**Import multiple fonts**
+If you want to import multiple fonts and want to avoid a variable name collision, you can import the fonts using an `import * as` statement.
-```tsx
-import * as TitanOne from "@remotion/google-fonts/TitanOne";
+```tsx twoslash title="Scope loadFont() variable"
import * as Montserrat from "@remotion/google-fonts/Montserrat";
+Montserrat.loadFont();
```
-After that call [loadFont()](./load-font.md) function with specific style, weights, and subsets or just omit the function arguments to load all variant available.
+Call [`loadFont()`](/docs/google-fonts/load-font) to start the loading process. By default, every style, weight and subset is loaded.
-```tsx
-import * as TitanOne from "@remotion/google-fonts/TitanOne";
-import * as Montserrat from "@remotion/google-fonts/Montserrat";
+You can pass a style (such as `normal`, `italic`) to only load that specific style. If you want multiple styles, call `loadFont()` multiple times.
-// Load Titan One font
-TitanOne.loadFont("normal", {
- weights: ["400"],
- subsets: ["latin"],
-});
+```tsx twoslash title="Load just one style"
+import { loadFont } from "@remotion/google-fonts/TitanOne";
+
+loadFont("normal");
+```
+
+Use the TypeScript autocomplete to see the available styles. To further narrow down what's being loaded, you can specify `weights` and `subsets`. These options are also typesafe.
+
+```tsx twoslash title="Load a specific style with limit weights and subsets"
+import * as Montserrat from "@remotion/google-fonts/Montserrat";
-// Load Montserrat using 2 font style
Montserrat.loadFont("normal", {
weights: ["400", "600", "800"],
subsets: ["latin", "latin-ext"],
});
-Montserrat.loadFont("italic", {
- weights: ["400", "500", "600", "700", "800"],
- subsets: ["latin", "latin-ext"],
-});
-
-// Load all Montserrat variant
-Montserrat.loadFont();
```
-Then use it anywhere in our code.
+`loadFonts()` returns an object with a `fontFamily` property. You can use the `style` attribute to render text in the font you loaded.
-```tsx
-import * as TitanOne from "@remotion/google-fonts/TitanOne";
+```tsx twoslash title="Use the fontFamily property"
+import { loadFont } from "@remotion/google-fonts/TitanOne";
+import { AbsoluteFill } from "remotion";
-TitanOne.loadFont();
+const { fontFamily } = loadFont();
export const GoogleFontsDemoComposition = () => {
return (
<AbsoluteFill
style={{
- justifyContent: "center",
- alignItems: "center",
- fontFamily: TitanOne.fontFamily,
+ fontFamily,
}}
>
<div>Hallo Google Fonts</div>
@@ -113,42 +103,47 @@ export const GoogleFontsDemoComposition = () => {
};
```
-## APIs
+To get information about a font, you can import the `info` property:
-Each font will export these variables:
+```tsx twoslash title="Get info about the font"
+import { info } from "@remotion/google-fonts/Montserrat";
+console.log(info);
+```
-### loadFont()
+```json title="Example value of info object"
+{
+ "fontFamily": "Titan One",
+ "importName": "TitanOne",
+ "version": "v13",
+ "url": "https://fonts.googleapis.com/css2?family=Titan+One:ital,wght@0,400",
+ "unicodeRanges": {
+ "latin-ext": "U+0100-024F, U+0259, U+1E00-1EFF, U+2020, U+20A0-20AB, U+20AD-20CF, U+2113, U+2C60-2C7F, U+A720-A7FF",
+ "latin": "U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD"
+ },
+ "fonts": {
+ "normal": {
+ "400": {
+ "latin-ext": "https://fonts.gstatic.com/s/titanone/v13/mFTzWbsGxbbS_J5cQcjCmjgm6Es.woff2",
+ "latin": "https://fonts.gstatic.com/s/titanone/v13/mFTzWbsGxbbS_J5cQcjClDgm.woff2"
+ }
+ }
+ }
+}
+```
-Load font with selected style, weights, and subsets. See [loadFont()](./load-font.md).
+## APIs
+<TableOfContents />
-### fontFamily
+## Credits
-The font family name, so we can pass this instead hardcode string.
+Implemented by [Hidayatullah](https://github.com/ayatkyo).
-### info
+## License
-Complete information about the current font.
+MIT
-**Example:**
+## See also
-```js
-{
- fontFamily: 'Titan One',
- importName: 'TitanOne',
- version: 'v13',
- url: 'https://fonts.googleapis.com/css2?family=Titan+One:ital,wght@0,400',
- unicodeRanges: {
- 'latin-ext': 'U+0100-024F, U+0259, U+1E00-1EFF, U+2020, U+20A0-20AB, U+20AD-20CF, U+2113, U+2C60-2C7F, U+A720-A7FF',
- 'latin': 'U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD',
- },
- fonts: {
- normal: {
- '400': {
- 'latin-ext': 'https://fonts.gstatic.com/s/titanone/v13/mFTzWbsGxbbS_J5cQcjCmjgm6Es.woff2',
- 'latin': 'https://fonts.gstatic.com/s/titanone/v13/mFTzWbsGxbbS_J5cQcjClDgm.woff2',
- },
- },
- },
-}
-```
\ No newline at end of file
+- [Fonts](/docs/fonts)
+- [`loadFont()`](/docs/google-fonts/load-font)
diff --git a/packages/docs/docs/google-fonts/load-font.md b/packages/docs/docs/google-fonts/load-font.md
index 6a69621ebc..1b2f6e30f1 100644
--- a/packages/docs/docs/google-fonts/load-font.md
+++ b/packages/docs/docs/google-fonts/load-font.md
@@ -2,35 +2,27 @@
title: loadFont()
---
-Load font with selected style, weights, and subsets.
-
-```tsx
-const loadFont: (
- style?: string,
- options?: {
- weights?: string[],
- subsets?: string[],
- }
-) => void;
-```
+Load a [Google Font](https://fonts.google.com) for use in Remotion.
-## Example
+- Automatically blocks the render until the font is ready
+- Offers a way to narrow down weights, styles and character subsets.
+- Each `loadFont()` call is typesafe and available options are exposed through autocompletion.
-### Load all variant
+## Usage
-```tsx
-import { loadFont, fontFamily } from "@remotion/google-fonts/Lobster";
+Without arguments, all styles, weights and character subsets of a font are being loaded.
-loadFont();
+```tsx twoslash title="Load all variants of the Lobster font"
+import { loadFont } from "@remotion/google-fonts/Lobster";
+import { AbsoluteFill } from "remotion";
+
+const { fontFamily } = loadFont();
export const GoogleFontsExample: React.FC = () => {
return (
<AbsoluteFill
style={{
- justifyContent: 'center',
- alignItems: 'center',
fontFamily: fontFamily,
- fontSize: 280,
}}
>
<h1>Google Fonts</h1>
@@ -39,10 +31,11 @@ export const GoogleFontsExample: React.FC = () => {
};
```
-### Load specific variant
+Pass a specific style (e.g. `normal`, `italic`) and optionally weights and subsets to narrow down what gets loaded.
+If you want to load multiple styles, use multiple `loadFont()` statements.
-```tsx
-import { loadFont, fontFamily } from "@remotion/google-fonts/Lobster";
+```tsx title="Load a specific style, weight and subset"
+import { fontFamily, loadFont } from "@remotion/google-fonts/Lobster";
loadFont("normal", {
weights: ["400"],
@@ -53,8 +46,8 @@ export const GoogleFontsExample: React.FC = () => {
return (
<AbsoluteFill
style={{
- justifyContent: 'center',
- alignItems: 'center',
+ justifyContent: "center",
+ alignItems: "center",
fontFamily: fontFamily,
fontSize: 280,
}}
@@ -65,68 +58,37 @@ export const GoogleFontsExample: React.FC = () => {
};
```
-### Load font and use returned information
+## Arguments
-```tsx
-import { loadFont } from "@remotion/google-fonts/Lobster";
-
-const { fontFamily } = loadFont();
-
-export const GoogleFontsExample: React.FC = () => {
- return (
- <AbsoluteFill
- style={{
- justifyContent: 'center',
- alignItems: 'center',
- fontFamily: fontFamily,
- fontSize: 280,
- }}
- >
- <h1>Google Fonts</h1>
- </AbsoluteFill>
- );
-};
-```
-
-## Argumets
-
-Arguments can be omited if we want to load all variant available.
+All arguments and options are optional. Use TypeScript autocompletion or `info` to discover available options. With any of them, all styles, weights and subsets are loaded.
### style
-_optional_
-
-The font style we want to load.
+The font style we want to load. While each font has a different set of styles, common options are: `normal`, `italic` etc.
### options
-_optional_
-
#### weights
-_optional_
-
-List of Font weights that we want to load.
+Array of weights that should be loaded. By default, all.
#### subsets
-_optional_
-
-List of Font subsets that we want to load.
+Array of font subsets that should be loaded. By default, all.
## Return value
+An object with the following properties:
+
### fontFamily
-The font family name.
+The font family name, be used as the `fontFamily` CSS property.
### fonts
-The font variant information.
+Variant information about the font.
-**Example:**
-
-```js
+```js title="Example value"
{
normal: {
'400': {
@@ -137,16 +99,18 @@ The font variant information.
}
```
-
### unicodeRanges
-The font unicode ranges information.
+Unicode range information about the font.
-**Example:**
-
-```js
+```js title="Example value"
{
'latin-ext': 'U+0100-024F, U+0259, U+1E00-1EFF, U+2020, U+20A0-20AB, U+20AD-20CF, U+2113, U+2C60-2C7F, U+A720-A7FF',
'latin': 'U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2122, U+2191, U+2193, U+2212, U+2215, U+FEFF, U+FFFD',
}
-```
\ No newline at end of file
+```
+
+## See also
+
+- [Fonts](/docs/fonts)
+- [`@remotion/google-fonts`](/docs/google-fonts)
diff --git a/packages/docs/package.json b/packages/docs/package.json
index 1c4345a21e..7c449a96b0 100644
--- a/packages/docs/package.json
+++ b/packages/docs/package.json
@@ -31,6 +31,7 @@
"@remotion/lottie": "3.2.39",
"@remotion/media-utils": "3.2.39",
"@remotion/motion-blur": "3.2.39",
+ "@remotion/google-fonts": "3.2.39",
"@remotion/noise": "3.2.39",
"@remotion/paths": "3.2.39",
"@remotion/player": "3.2.39",
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 9ccd9b5c69..6f80bb500c 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -268,6 +268,7 @@ importers:
'@remotion/babel-loader': 3.2.39
'@remotion/bundler': 3.2.39
'@remotion/gif': 3.2.39
+ '@remotion/google-fonts': 3.2.39
'@remotion/lambda': 3.2.39
'@remotion/lottie': 3.2.39
'@remotion/media-utils': 3.2.39
@@ -318,6 +319,7 @@ importers:
'@remotion/babel-loader': link:../babel-loader
'@remotion/bundler': link:../bundler
'@remotion/gif': link:../gif
+ '@remotion/google-fonts': link:../google-fonts
'@remotion/lambda': link:../lambda
'@remotion/lottie': link:../lottie
'@remotion/media-utils': link:../media-utils