Skip to content

Commit 29f20d3

Browse files
authored
font api updates (#42724)
<!-- Thanks for opening a PR! Your contribution is much appreciated. To make sure your PR is handled as smoothly as possible we request that you follow the checklist sections below. Choose the right checklist for the change that you're making: --> Update the next fonts API reference to match the latest changes from [beta.nextjs.org](https://beta.nextjs.org/docs/api-reference/components/font) ## Bug - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [ ] Errors have a helpful link attached, see `contributing.md` ## Feature - [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. - [ ] Related issues linked using `fixes #number` - [ ] Integration tests added - [x] Documentation added - [ ] Telemetry added. In case of a feature if it's used or not. - [ ] Errors have a helpful link attached, see `contributing.md` ## Documentation / Examples - [ ] Make sure the linting passes by running `pnpm build && pnpm lint` - [ ] The "examples guidelines" are followed from [our contributing doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
1 parent 6490252 commit 29f20d3

File tree

2 files changed

+137
-77
lines changed

2 files changed

+137
-77
lines changed

docs/api-reference/next/font.md

+92-77
Original file line numberDiff line numberDiff line change
@@ -13,157 +13,172 @@ description: Optimizing loading web fonts with the built-in `@next/font` loaders
1313

1414
</details>
1515

16-
This API reference will help you understand how to use [`@next/font/google`](#nextfontgoogle) and [`@next/font/local`](#nextfontlocal). For features and usage, please see the [Optimizing Fonts](/docs/basic-features/font-optimization.md) page.
17-
18-
## @next/font/google
16+
This API reference will help you understand how to use `@next/font/google` and `@next/font/local`. For features and usage, please see the [Optimizing Fonts](/docs/basic-features/font-optimization.md) page.
1917

2018
### Font function arguments
2119

22-
For usage, review [Google Fonts](/docs/basic-features/font-optimization.md#google-fonts).
20+
For usage, review [Google Fonts](/docs/basic-features/font-optimization.md#google-fonts) and [Local Fonts](/docs/basic-features/font-optimization#local-fonts).
21+
22+
| Key | `font/google` | `font/local` | Data type | Required |
23+
| ------------------------------------------- | ------------- | ------------ | -------------------------- | ----------------- |
24+
| [`src`](#src) ||| String or Array of Objects | Required |
25+
| [`weight`](#weight) ||| String or Array | Required/Optional |
26+
| [`style`](#style) ||| String or Array | Optional |
27+
| [`subsets`](#subsets) ||| Array of Strings | Optional |
28+
| [`axes`](#axes) ||| Array of Strings | Optional |
29+
| [`display`](#display) ||| String | Optional |
30+
| [`preload`](#preload) ||| Boolean | Optional |
31+
| [`fallback`](#fallback) ||| Array of Strings | Optional |
32+
| [`adjustFontFallback`](#adjustFontFallback) ||| Boolean or String | Optional |
33+
| [`variable`](#variable) ||| String | Optional |
34+
| [`declarations`](#declarations) ||| Array of Objects | Optional |
2335

24-
| Key | Example | Data type | Required |
25-
| ------------------------------------------- | ---------------------------------- | --------------------------------------------------------- | ------------------------------------------------ |
26-
| [`weight`](#weight) | `weight: '600'` | String | Required if font is not variable |
27-
| [`style`](#style) | `style: 'italic'` | String | Optional |
28-
| [`subsets`](#subsets) | `subsets: ['latin']` | Array of Strings | Optional |
29-
| [`axes`](#axes) | `axes: ['slnt']` | Array of Strings based on the available axes for the font | Optional for variable fonts that have extra axes |
30-
| [`display`](#display) | `display: 'swap'` | String | Optional |
31-
| [`preload`](#preload) | `preload: false` | Boolean | Optional |
32-
| [`fallback`](#fallback) | `fallback: ['system-ui', 'arial']` | Array of Strings | Optional |
33-
| [`adjustFontFallback`](#adjustFontFallback) | `adjustFontFallback: false` | Boolean | Optional |
34-
| [`variable`](#variable) | `variable: '--my-font'` | String | Optional |
36+
### `src`
3537

36-
### `weight`
38+
The path of the font file as a string or an array of objects (with type `Array<{path: string, weight?: string, style?: string}>`) relative to the directory where the font loader function is called.
3739

38-
The font [`weight`](https://fonts.google.com/knowledge/glossary/weight) represented as a string with possible values of the weights available for the specific font. For example, for the font [`Inter`](https://fonts.google.com/specimen/Inter?query=inter), the possible values are `'100'`, `'200'`, `'300'`, `'400'`, `'500'`, `'600'`, `'700'`, `'800'`, `'900'` or `'variable'` (`'variable'` is the default in this case).
40+
Used in `@next/font/local`
3941

40-
- Required if the font being used is **not** [variable](https://fonts.google.com/variablefonts)
41-
42-
### `style`
42+
- Required
4343

44-
The font [`style`](https://developer.mozilla.org/en-US/docs/Web/CSS/font-style) with possible string [values](https://developer.mozilla.org/en-US/docs/Web/CSS/font-style#values) of `'normal'` or `'italic'` with default value of `'normal'`.
44+
Examples:
4545

46-
- Optional
46+
- `src:'./fonts/my-font.woff2'` where `my-font.woff2` is placed in a directory named `fonts` inside the `app` directory
47+
- `src:[{path: './inter/Inter-Thin.ttf', weight: '100',},{path: './inter/Inter-Regular.ttf',weight: '400',},{path: './inter/Inter-Bold-Italic.ttf', weight: '700',style: 'italic',},]`
48+
- if the font loader function is called in `app/page.tsx` using `src:'../styles/fonts/my-font.ttf'`, then `my-font.ttf` is placed in `styles/fonts` at the root of the project
4749

48-
### `subsets`
50+
### `weight`
4951

50-
The font [`subsets`](https://fonts.google.com/knowledge/glossary/subsetting) defined by an Array of string values with the names of each subset you would like to be [preloaded](/docs/basic-features/font-optimization.md#specifying-a-subset). Fonts specified via `subsets` will have a link preload tag injected into the head when the [`preload`](#preload) option is true, which is the default.
52+
The font [`weight`](https://fonts.google.com/knowledge/glossary/weight) with the following possibilities:
5153

52-
- Optional
54+
- A string with possible values of the weights available for the specific font or a range of values if it's a [variable](https://fonts.google.com/variablefonts) font
55+
- An array of weight values if the font is not a [variable google font](https://fonts.google.com/variablefonts). It applies to `@next/font/google` only.
5356

54-
### `axes`
57+
Used in `@next/font/google` and `@next/font/local`
5558

56-
Some variable fonts have extra `axes` that can be included. By default, only the font weight is included to keep the file size down. The possible values of `axes` depend on the specific font. For example, the `Inter` variable font has `slnt` as additional `axes` as shown [here](https://fonts.google.com/variablefonts?vfquery=inter#font-families). You can find the possible `axes` values for your font by using the filter on the [Google variable fonts page](https://fonts.google.com/variablefonts#font-families) and looking for axes other than `wght`.
59+
- Required if the font being used is **not** [variable](https://fonts.google.com/variablefonts)
5760

58-
- Optional
61+
Examples:
5962

60-
### `display`
63+
- `weight: '400'`: A string for a single weight value - for the font [`Inter`](https://fonts.google.com/specimen/Inter?query=inter), the possible values are `'100'`, `'200'`, `'300'`, `'400'`, `'500'`, `'600'`, `'700'`, `'800'`, `'900'` or `'variable'` where `'variable'` is the default)
64+
- `weight: '100 900'`: A string for the range between `100` and `900` for a variable font
65+
- `weight: ['100','400','900']`: An array of 3 possible values for a non variable font
6166

62-
The font [`display`](https://developer.mozilla.org/en-US/docs/Web/CSS/@font-face/font-display) with possible string [values](https://developer.mozilla.org/en-US/docs/Web/CSS/@font-face/font-display#values) of `'auto'`, `'block'`, `'swap'`, `'fallback'` or `'optional'` with default value of `'optional'`.
67+
### `style`
6368

64-
- Optional
69+
The font [`style`](https://developer.mozilla.org/en-US/docs/Web/CSS/font-style) with the following possibilities:
6570

66-
### `preload`
71+
- A string [value](https://developer.mozilla.org/en-US/docs/Web/CSS/font-style#values) with default value of `'normal'`
72+
- An array of style values if the font is not a [variable google font](https://fonts.google.com/variablefonts). It applies to `@next/font/google` only.
6773

68-
A boolean value that specifies whether the font should be [preloaded](/docs/basic-features/font-optimization.md#preloading) or not. The default is `true`.
74+
Used in `@next/font/google` and `@next/font/local`
6975

7076
- Optional
7177

72-
### `fallback`
78+
Examples:
7379

74-
The fallback font to use if the font cannot be loaded. An array of strings of fallback fonts such as `['system-ui', 'arial']` with no default.
80+
- `style: 'italic'`: A string - it can be `normal` or `italic` for `@next/font/google`
81+
- `style: 'oblique'`: A string - it can take any value for `@next/font/local` but is expected to come from [standard font styles](https://developer.mozilla.org/en-US/docs/Web/CSS/font-style)
82+
- `style: ['italic','normal']`: An array of 2 values for `@next/font/google` - the values are from `normal` and `italic`
7583

76-
- Optional
84+
### `subsets`
7785

78-
### `adjustFontFallback`
86+
The font [`subsets`](https://fonts.google.com/knowledge/glossary/subsetting) defined by an array of string values with the names of each subset you would like to be [preloaded](/docs/optimizing/fonts.md#specifying-a-subset). Fonts specified via `subsets` will have a link preload tag injected into the head when the [`preload`](/docs/api-reference/components/font.md#preload) option is true, which is the default.
7987

80-
A boolean value that sets whether an automatic fallback font should be used to reduce [Cumulative Layout Shift](https://web.dev/cls/). The default is `true`.
88+
Used in `@next/font/google`
8189

8290
- Optional
8391

84-
### `variable`
92+
Examples:
8593

86-
A string value to define the CSS variable name to be used if the style is applied with the [CSS variable method](#css-variables).
94+
- `subsets: ['latin']`: An array with the subset `latin`
8795

88-
- Optional
96+
### `axes`
8997

90-
## @next/font/local
98+
Some variable fonts have extra `axes` that can be included. By default, only the font weight is included to keep the file size down. The possible values of `axes` depend on the specific font.
9199

92-
### Font function arguments
100+
Used in `@next/font/google`
93101

94-
For usage, review [Local Fonts](/docs/basic-features/font-optimization.md#local-fonts).
102+
- Optional
95103

96-
| Key | Example | Data type | Required |
97-
| ------------------------------------------- | ----------------------------------------------------------- | -------------------------------------- | -------- |
98-
| [`src`](#src) | `src: './my-font.woff2'` | String | Required |
99-
| [`weight`](#weight) | `weight: '600' or '100 900'` | String | Optional |
100-
| [`style`](#style) | `style: 'italic'` | String | Optional |
101-
| [`display`](#display) | `display: 'swap'` | String | Optional |
102-
| [`preload`](#preload) | `preload: false` | Boolean | Optional |
103-
| [`fallback`](#fallback) | `fallback: ['system-ui', 'arial']` | Array of Strings | Optional |
104-
| [`adjustFontFallback`](#adjustFontFallback) | `adjustFontFallback: false` | ['Arial', 'Times New Roman', false] | Optional |
105-
| [`variable`](#variable) | `variable: '--my-font'` | String | Optional |
106-
| [`declarations`](#declarations) | `declarations: [{ prop: 'ascent-override', value: '90%' }]` | Array<{ prop: string; value: string }> | Optional |
104+
Examples:
107105

108-
### `src`
106+
- `axes: ['slnt']`: An array with value `slnt` for the `Inter` variable font which has `slnt` as additional `axes` as shown [here](https://fonts.google.com/variablefonts?vfquery=inter#font-families). You can find the possible `axes` values for your font by using the filter on the [Google variable fonts page](https://fonts.google.com/variablefonts#font-families) and looking for axes other than `wght`
109107

110-
The path of the font file as a string relative to the directory where the font loader function is called or to the `pages` directory.
108+
### `display`
111109

112-
- Required
110+
The font [`display`](https://developer.mozilla.org/en-US/docs/Web/CSS/@font-face/font-display) with possible string [values](https://developer.mozilla.org/en-US/docs/Web/CSS/@font-face/font-display#values) of `'auto'`, `'block'`, `'swap'`, `'fallback'` or `'optional'` with default value of `'optional'`.
113111

114-
Examples are:
112+
Used in `@next/font/google` and `@next/font/local`
115113

116-
- `'./fonts/my-font.woff2'` where `my-font.woff2` is placed in a directory named `fonts` inside the `pages` directory
117-
- if the font loader function is called in `pages/index.js` using `'../styles/fonts/my-font.ttf'`, then `my-font.ttf` is placed in `styles/fonts` at the root of the project
114+
- Optional
118115

119-
### `weight`
116+
Examples:
120117

121-
The font [`weight`](https://developer.mozilla.org/en-US/docs/Web/CSS/font-weight) either as a specific weight string value such as `'400'` or a range of values if it's a variable font such as `'100 900'`.
118+
- `display: 'swap'`: A string assigned to the `swap` value
122119

123-
- Optional
120+
### `preload`
124121

125-
### `style`
122+
A boolean value that specifies whether the font should be [preloaded](/docs/optimizing/fonts#preloading) or not. The default is `true`.
126123

127-
The font [`style`](https://developer.mozilla.org/en-US/docs/Web/CSS/font-style) with possible string [values](https://developer.mozilla.org/en-US/docs/Web/CSS/font-style#values) of `'normal'`, `'italic'` or `'oblique'` with default value of `'normal'`.
124+
Used in `@next/font/google` and `@next/font/local`
128125

129126
- Optional
130127

131-
### `display`
128+
Examples:
132129

133-
The font [`display`](https://developer.mozilla.org/en-US/docs/Web/CSS/@font-face/font-display) with possible string [values](https://developer.mozilla.org/en-US/docs/Web/CSS/@font-face/font-display#values) of `'auto'`, `'block'`, `'swap'`, `'fallback'` or `'optional'` with default value of `'optional'`.
134-
135-
- Optional
130+
- `preload: false`
136131

137-
### `preload`
132+
### `fallback`
138133

139-
A boolean value that specifies whether the font should be [preloaded](/docs/basic-features/font-optimization.md#preloading) or not. The default is `true`.
134+
The fallback font to use if the font cannot be loaded. An array of strings of fallback fonts with no default.
140135

141136
- Optional
142137

143-
### `fallback`
138+
Used in `@next/font/google` and `@next/font/local`
144139

145-
The fallback font to use if the font cannot be loaded. An array of strings of fallback fonts such as `['system-ui', 'arial']` with no default.
140+
Examples:
146141

147-
- Optional
142+
- `fallback: ['system-ui', 'arial']`: An array setting the fallback fonts to `system-ui` or `arial`
148143

149144
### `adjustFontFallback`
150145

151-
A string or boolean `false` value that sets whether an automatic fallback font should be used to reduce [Cumulative Layout Shift](https://web.dev/cls/). The possible values are `'Arial'`, `'Times New Roman'` or `false`. The default is `'Arial'`.
146+
- For `@next/font/google`: A boolean value that sets whether an automatic fallback font should be used to reduce [Cumulative Layout Shift](https://web.dev/cls/). The default is `true`.
147+
- For `@next/font/local`: A string or boolean `false` value that sets whether an automatic fallback font should be used to reduce [Cumulative Layout Shift](https://web.dev/cls/). The possible values are `'Arial'`, `'Times New Roman'` or `false`. The default is `'Arial'`.
148+
149+
Used in `@next/font/google` and `@next/font/local`
152150

153151
- Optional
154152

153+
Examples:
154+
155+
- `adjustFontFallback: false`: for ``@next/font/google`
156+
- `adjustFontFallback: 'Times New Roman'`: for `@next/font/local`
157+
155158
### `variable`
156159

157160
A string value to define the CSS variable name to be used if the style is applied with the [CSS variable method](#css-variables).
158161

162+
Used in `@next/font/google` and `@next/font/local`
163+
159164
- Optional
160165

166+
Examples:
167+
168+
- `variable: '--my-font'`: The CSS variable `--my-font` is declared
169+
161170
### `declarations`
162171

163-
An array of font face [descriptor](https://developer.mozilla.org/en-US/docs/Web/CSS/@font-face#descriptors) key-value pairs that define the generated `@font-face` further such as `{ prop: 'ascent-override', value: '90%' }`.
172+
An array of font face [descriptor](https://developer.mozilla.org/en-US/docs/Web/CSS/@font-face#descriptors) key-value pairs that define the generated `@font-face` further.
173+
174+
Used in `@next/font/local`
164175

165176
- Optional
166177

178+
Examples:
179+
180+
- `declarations: [{ prop: 'ascent-override', value: '90%' }]`
181+
167182
## Applying Styles
168183

169184
You can apply the font styles in three ways:

docs/basic-features/font-optimization.md

+45
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,51 @@ export default function MyApp({ Component, pageProps }) {
158158

159159
View the [Font API Reference](/docs/api-reference/next/font.md#nextfontlocal) for more information.
160160

161+
## With Tailwind CSS
162+
163+
`@next/font` can be used with Tailwind CSS through a [CSS variable](/docs/api-reference/next/font#css-variables).
164+
165+
In the example below, we use the font `Inter` from `@next/font/google` (You can use any font from Google or Local Fonts). Load your font with the `variable` option to define your CSS variable name and assign it to `inter`. Then, use `inter.variable` to add the CSS variable to your HTML document.
166+
167+
```js
168+
// pages/_app.js
169+
import { Inter } from '@next/font/google'
170+
171+
const inter = Inter({
172+
variable: '--font-inter',
173+
})
174+
175+
export default function MyApp({ Component, pageProps }) {
176+
return (
177+
<main className={inter.variable}>
178+
<Component {...pageProps} />
179+
</main>
180+
)
181+
}
182+
```
183+
184+
Finally, add the CSS variable to your [Tailwind CSS config](https://github.com/vercel/next.js/tree/canary/examples/with-tailwindcss):
185+
186+
```js
187+
// tailwind.config.js
188+
const { fontFamily } = require('tailwindcss/defaultTheme')
189+
190+
/** @type {import('tailwindcss').Config} \*/
191+
module.exports = {
192+
content: ['./app/**/*.{js,ts,jsx,tsx}'],
193+
theme: {
194+
extend: {
195+
fontFamily: {
196+
sans: ['var(--font-inter)', ...fontFamily.sans],
197+
},
198+
},
199+
},
200+
plugins: [],
201+
}
202+
```
203+
204+
You can now use the `font-sans` utility class to apply the font to your elements.
205+
161206
## Preloading
162207

163208
When a font function is called on a page of your site, it is not globally available and preloaded on all routes. Rather, the font is only preloaded on the related route/s based on the type of file where it is used:

0 commit comments

Comments
 (0)