font icon indicating copy to clipboard operation
font copied to clipboard

Usage with the Next JS app router

Open Lermatroid opened this issue 1 year ago • 5 comments

Hello,

I am attempting to use cal-sans inside of the Next.js App Router. What is the best way to go about doing this?

Currently I am importing it within my root layout, but it still seems to not be working. Thanks!

Lermatroid avatar Apr 30 '23 08:04 Lermatroid

You could check out how it's been done on the packages/web cal app

It was recently upgraded to leverage @next/font in this PR if that helps https://github.com/calcom/cal.com/pull/6346

agustif avatar Apr 30 '23 12:04 agustif

As far as I can tell though (from here), this was done by downloading the font file (not using the npm package) and putting it in the project? Is this the recommended way to do it?

Lermatroid avatar Apr 30 '23 14:04 Lermatroid

i'm curious too if this is the best practice or not :)

ceIia avatar Jun 22 '23 09:06 ceIia

I have no idea about if it is -best practice- or not but seems so going from the nextjs docs, you could also leverage tailwindcss to reuse the local font https://nextjs.org/docs/pages/building-your-application/optimizing/fonts#local-fonts

agustif avatar Jun 22 '23 16:06 agustif

My solution to implement the font inside a NextJs app

// src/layout.tsx
const fontCal = localFont({
  src: '../assets/fonts/CalSans-SemiBold.woff2',
  variable: '--font-cal',
});

export default async function RootLayout({ children }: { children: React.ReactNode }) {
  ...
  return (
    <html className="dark" style={{ colorScheme: 'dark' }}>
	<head />
	<body className={cn('min-h-screen bg-background antialiased', fontCal.variable)}>
          <main>{children}</main>
	</body>
    </html>
  );
}

Inside your tailwind.config.ts

import { fontFamily } from 'tailwindcss/defaultTheme';
export default {
  ...
  theme: {
    extend: {
      fontFamily: {
        ...
        heading: ['var(--font-cal)', ...fontFamily.sans],
      },
   }
}

Now you can use your font inside your className props

<p className="font-cal">
 My font heading
</p>

VGontier-cmd avatar Feb 04 '24 14:02 VGontier-cmd