Upgrading to Next.js 14: Stuck on Next/Font Two Buttons with Tailwind Different Class Names
In this article, we will discuss a common issue faced when upgrading to Next.js 14 - getting stuck on using Next/Font while trying to style two buttons with different Tailwind class names. We will explore key concepts, subtitles, and provide detailed explanations to resolve this problem.
Background
Next.js is a popular React framework, and upgrading to the latest version can sometimes prove challenging. One such challenge is integrating Next/Font to style buttons with Tailwind CSS, applying different class names to each button.
Stuck on Next/Font Integration
Using Next/Font when upgrading to Next.js 14, developers may encounter issues when trying to apply different Tailwind CSS class names to two buttons. This issue typically arises from improper configuration or conflicts between different class names.
Resolving the Issue
To resolve this issue, let's break it down into smaller parts, focusing on proper configuration and overcoming conflicts between class names.
Properly Importing Next/Font
The first step is to import Next/Font properly, making it available for your project's components.
import '../styles/globals.css'
import { NextFont } from 'next/font'
const inter = NextFont({
src: '../public/inter-var-condensed.woff2',
weight: '400',
style: 'normal',
})
Applying Font to Components
Now we can use the imported font in our components by wrapping our component with <Inter> (the name of the imported font).
Understanding Class Name Conflicts
Class name conflicts emerge when additional class names are added to the existing class name provided by the Next/Font wrapper.
The specific issue discussed here arises when developers try to apply different class names (colors, for example) to two buttons, both using the same Next/Font.
Solving Class Name Conflicts
To tackle conflicts, it's helpful to add the next/font class name explicitly for each button, defining the desired style separately. By doing so, we can apply different Tailwind CSS class names to the buttons.
- Import Next/Font properly to enable its usage throughout the project.
- Wrap the component with the imported Next/Font to ensure the proper styling.
- Resolve class name conflicts by explicitly adding the imported font class name for each element and applying the desired CSS class names separately.