Unverified Commit 0a2ae766 authored by Hannes Bornö's avatar Hannes Bornö Committed by GitHub

docs: Add examples of using multiple weights and styles (#43031)

Add examples of using multiple weights and styles for google and local fonts. Although it's in the [API reference](https://nextjs.org/docs/api-reference/next/font) it's probably a good idea to have examples since it's a common use case.

Also added subets to google font examples to make them more 'copayable'.

cc @leerob 

## 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
- [ ] 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)
parent 397894b1
...@@ -33,7 +33,7 @@ To use the font in all your pages, add it to [`_app.js` file](https://nextjs.org ...@@ -33,7 +33,7 @@ To use the font in all your pages, add it to [`_app.js` file](https://nextjs.org
import { Inter } from '@next/font/google' import { Inter } from '@next/font/google'
// If loading a variable font, you don't need to specify the font weight // If loading a variable font, you don't need to specify the font weight
const inter = Inter() const inter = Inter({ subsets: ['latin'] })
export default function MyApp({ Component, pageProps }) { export default function MyApp({ Component, pageProps }) {
return ( return (
...@@ -52,6 +52,7 @@ import { Roboto } from '@next/font/google' ...@@ -52,6 +52,7 @@ import { Roboto } from '@next/font/google'
const roboto = Roboto({ const roboto = Roboto({
weight: '400', weight: '400',
subsets: ['latin'],
}) })
export default function MyApp({ Component, pageProps }) { export default function MyApp({ Component, pageProps }) {
...@@ -63,6 +64,16 @@ export default function MyApp({ Component, pageProps }) { ...@@ -63,6 +64,16 @@ export default function MyApp({ Component, pageProps }) {
} }
``` ```
You can specify multiple weights and/or styles by using an array:
```js
const roboto = Roboto({
weight: ['400', '700'],
style: ['normal', 'italic'],
subsets: ['latin'],
})
```
#### Apply the font in `<head>` #### Apply the font in `<head>`
You can also use the font without a wrapper and `className` by injecting it inside the `<head>` as follows: You can also use the font without a wrapper and `className` by injecting it inside the `<head>` as follows:
...@@ -71,7 +82,7 @@ You can also use the font without a wrapper and `className` by injecting it insi ...@@ -71,7 +82,7 @@ You can also use the font without a wrapper and `className` by injecting it insi
// pages/_app.js // pages/_app.js
import { Inter } from '@next/font/google' import { Inter } from '@next/font/google'
const inter = Inter() const inter = Inter({ subsets: ['latin'] })
export default function MyApp({ Component, pageProps }) { export default function MyApp({ Component, pageProps }) {
return ( return (
...@@ -95,7 +106,7 @@ To use the font on a single page, add it to the specific page as shown below: ...@@ -95,7 +106,7 @@ To use the font on a single page, add it to the specific page as shown below:
// pages/index.js // pages/index.js
import { Inter } from '@next/font/google' import { Inter } from '@next/font/google'
const inter = Inter() const inter = Inter({ subsets: ['latin'] })
export default function Home() { export default function Home() {
return ( return (
...@@ -156,6 +167,35 @@ export default function MyApp({ Component, pageProps }) { ...@@ -156,6 +167,35 @@ export default function MyApp({ Component, pageProps }) {
} }
``` ```
If you want to use multiple files for a single font family, `src` can be an array:
```js
const roboto = localFont({
src: [
{
path: './Roboto-Regular.woff2',
weight: '400',
style: 'normal',
},
{
path: './Roboto-Italic.woff2',
weight: '400',
style: 'italic',
},
{
path: './Roboto-Bold.woff2',
weight: '700',
style: 'normal',
},
{
path: './Roboto-BoldItalic.woff2',
weight: '700',
style: 'italic',
},
],
})
```
View the [Font API Reference](/docs/api-reference/next/font.md#nextfontlocal) for more information. View the [Font API Reference](/docs/api-reference/next/font.md#nextfontlocal) for more information.
## With Tailwind CSS ## With Tailwind CSS
...@@ -190,7 +230,10 @@ const { fontFamily } = require('tailwindcss/defaultTheme') ...@@ -190,7 +230,10 @@ const { fontFamily } = require('tailwindcss/defaultTheme')
/** @type {import('tailwindcss').Config} \*/ /** @type {import('tailwindcss').Config} \*/
module.exports = { module.exports = {
content: ['./app/**/*.{js,ts,jsx,tsx}'], content: [
'./pages/**/*.{js,ts,jsx,tsx}',
'./components/**/*.{js,ts,jsx,tsx}',
],
theme: { theme: {
extend: { extend: {
fontFamily: { fontFamily: {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment