Fonts
@rari/font self-hosts fonts. At build time it copies local font files, or downloads Google Fonts, into your build output, generates the @font-face rules, and adds a size-adjusted fallback so text doesn't shift while the font loads. Nothing is requested from Google at runtime.
Fonts are a separate package, so apps that don't use them don't install the font tooling.
Install
$ npm install @rari/fontNo configuration is needed: the rari Vite plugin detects @rari/font and registers its font handling.
Google Fonts
Import a family by name from @rari/font/google and call it with a static options object:
import type { LayoutProps } from 'rari'
import { Inter } from '@rari/font/google'
const inter = Inter({ subsets: ['latin'], weight: ['400', '700'] })
export default function RootLayout({ children }: LayoutProps) {
return (
<html lang="en" className={inter.className}>
<body>{children}</body>
</html>
)
}Options include weight, style, subsets, display, variable, preload, fallback, adjustFontFallback, and axes (for variable fonts).
Local fonts
Import localFont from @rari/font/local and point src at files inside your project:
import localFont from '@rari/font/local'
export const brand = localFont({
src: [
{ path: './fonts/Brand-Regular.woff2', weight: '400' },
{ path: './fonts/Brand-Bold.woff2', weight: '700' },
],
variable: '--font-brand',
})Using a font
Each call returns:
className: applies the font family.style: an object withfontFamily,fontWeight, andfontStyle(unless it'snormal), for inline styles.variable: when you passvariable, a class that defines that CSS custom property, so you can usevar(--font-brand)in your own CSS.
The options must be a static object literal, because they're read at build time.
Migrating from rari/font
Fonts used to ship inside rari as rari/font/local and rari/font/google. Those imports have been removed, and a build that still uses them fails with a message pointing here. To migrate:
- Install the package:
npm install @rari/font. - Replace
rari/font/with@rari/font/in your imports.
The options and return values haven't changed.