<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Jean Carlo Pelanda</title>
    <description>The latest articles on DEV Community by Jean Carlo Pelanda (@jeanpelanda).</description>
    <link>https://dev.to/jeanpelanda</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1075027%2Fa6e36eeb-2d89-43e9-a95d-aefe12e1cf8e.png</url>
      <title>DEV Community: Jean Carlo Pelanda</title>
      <link>https://dev.to/jeanpelanda</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/jeanpelanda"/>
    <language>en</language>
    <item>
      <title>Configuring your fonts in Next.js 13 with Stitches</title>
      <dc:creator>Jean Carlo Pelanda</dc:creator>
      <pubDate>Mon, 01 May 2023 18:26:38 +0000</pubDate>
      <link>https://dev.to/jeanpelanda/font-with-nextjs-and-stitches-2cfn</link>
      <guid>https://dev.to/jeanpelanda/font-with-nextjs-and-stitches-2cfn</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxqwu0pqxk4atprl1hq8n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxqwu0pqxk4atprl1hq8n.png" alt="Next.js + Stitches"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Next.js 13 made using &lt;a href="https://nextjs.org/docs/basic-features/font-optimization" rel="noopener noreferrer"&gt;fonts&lt;/a&gt; way easier. But, let's take a look at how configure the fonts with Stitches.&lt;/p&gt;

&lt;p&gt;To start things off, install Stitches in your project's terminal.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;npm install @stitches/react&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Create your &lt;code&gt;stitches.config.ts&lt;/code&gt; (or &lt;code&gt;.js&lt;/code&gt;) and import the createStitches function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// stitches.config.ts
import { createStitches } from '@stitches/react';
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Also import the fonts you will use. In this case, I'm going to use a Google font and a local one to exemplify.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// stitches.config.ts
import { createStitches } from '@stitches/react'
import { MuseoModerno } from 'next/font/google' // google font
import localFont from 'next/font/local' // local font
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, you can define the instances of your fonts. In this case, Google's font subset is for Latin characters only (this could be different if you have a different language). You can get to specify which weights you want too.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// stitches.config.ts
import { createStitches } from '@stitches/react'
import { MuseoModerno } from 'next/font/google' // google font
import localFont from 'next/font/local' // local font

const museo = MuseoModerno({ subsets: ['latin'], weight: '400' })
const montserrat = localFont({ src: '../assets/Montserrat-Regular.ttf', weight: '400' })
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;createStitches&lt;/code&gt; function receives a configuration object with many definition options. Where in &lt;code&gt;theme&lt;/code&gt; you will define your font tokens in &lt;code&gt;fonts&lt;/code&gt;. Name a token for your font, and then getting the &lt;code&gt;.style.fontFamily&lt;/code&gt; property of the fonts that you defined before.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// stitches.config.ts
import { createStitches } from '@stitches/react'
import { MuseoModerno } from 'next/font/google' // google font
import localFont from 'next/font/local' // local font

const museo = MuseoModerno({ subsets: ['latin'], weight: '400' })
const montserrat = localFont({ src: '../assets/Montserrat-Regular.ttf', weight: '400' })

export const {
    getCssText,
    globalCss,
    styled,
    theme
} = createStitches({
    theme: {
        fonts: {
            museo: `${museo.style.fontFamily}, sans-serif`,
            montserrat: `${montserrat.style.fontFamily}, sans-serif`
        }
    }
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After your font tokens are defined, you can use them to style elements, for example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { globalCss } from "./stitches.config";

export const globalStyles = globalCss({

    h1: {
        fontFamily: "$museo"
    },

    p: {
        fontFamily: "$montserrat"
    }
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Finally, don't forget to call &lt;code&gt;globalStyles&lt;/code&gt; in your app.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { globalStyles } from '@/styles/global'
import type { AppProps } from 'next/app'

globalStyles()

export default function App({ Component, pageProps }: AppProps) {
  return &amp;lt;Component {...pageProps} /&amp;gt;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And to make it better, you can configure server-side rendering too, importing the &lt;code&gt;&amp;lt;style id="stitches" dangerouslySetInnerHTML={{ __html: getCssText() }} /&amp;gt;&lt;/code&gt; tag in to the Head 😉.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import { getCssText } from '../styles/stitches.config'
import Head from 'next/head'

export default function Home() {
  return (
    &amp;lt;&amp;gt;
      &amp;lt;Head&amp;gt;
        &amp;lt;title&amp;gt;Fonts&amp;lt;/title&amp;gt;
        &amp;lt;style id="stitches" dangerouslySetInnerHTML={{ __html: getCssText() }} /&amp;gt;
      &amp;lt;/Head&amp;gt;
      &amp;lt;main&amp;gt;
        &amp;lt;h1&amp;gt;Hello world!&amp;lt;/h1&amp;gt;
      &amp;lt;/main&amp;gt;
    &amp;lt;/&amp;gt;
  )
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Check the &lt;a href="https://github.com/jeanpelanda/fonts-with-nextjs-and-stitches" rel="noopener noreferrer"&gt;Git repository&lt;/a&gt; with the complete example, and to see more about &lt;a href="https://nextjs.org/docs/basic-features/font-optimization" rel="noopener noreferrer"&gt;next/font&lt;/a&gt;, and Stitches: &lt;a href="https://stitches.dev/docs/installation" rel="noopener noreferrer"&gt;installation&lt;/a&gt;, &lt;a href="https://stitches.dev/docs/tokens" rel="noopener noreferrer"&gt;theme tokens&lt;/a&gt; and &lt;a href="https://stitches.dev/docs/server-side-rendering" rel="noopener noreferrer"&gt;server-side rendering&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>nextjs</category>
      <category>stitches</category>
      <category>font</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
