File size: 1,725 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
---
title: Add custom fonts to a Vite project
description:
  Learn how to add a custom font when using Chakra UI in a Vite project
publishedAt: "2024-11-18"
collection: theming
---

## Loading the custom fonts

To add a custom font in your project, install the Google font you want to use
from [Fontsource](https://fontsource.org/). For this guide, we'll use the
["Bricolage Grotesque"](https://fontsource.org/fonts/bricolage-grotesque) font
as an example.

```bash
pnpm add @fontsource-variable/bricolage-grotesque
```

Next, import the font at the root of your project, referencing the `css` path:

```tsx title="main.tsx"
import "@fontsource-variable/bricolage-grotesque/index.css"
```

## Configure the custom font

Use the `createSystem` method to define the custom font in Chakra UI's theme
configuration:

```tsx title="components/ui/provider.tsx"
"use client"

import { createSystem, defaultConfig } from "@chakra-ui/react"

const system = createSystem(defaultConfig, {
  theme: {
    tokens: {
      fonts: {
        heading: { value: "Bricolage Grotesque Variable" },
        body: { value: "Bricolage Grotesque Variable" },
      },
    },
  },
})
```

:::info

You can customize which text elements use the font by specifying it for
`heading`, `body`, or both. In this case, we are setting both the body and
heading fonts to "Bricolage Grotesque".

:::

Finally, pass the `system` into the `ChakraProvider`

```tsx title="components/ui/provider.tsx" /value={system}/
export function Provider(props: ColorModeProviderProps) {
  return (
    <ChakraProvider value={system}>
      <ColorModeProvider {...props} />
    </ChakraProvider>
  )
}
```

This ensures that the custom font is applied across your entire app.